react-simple-game-engine
Version:
[WIP] not able to use in currently. <!-- Document cumming soon... -->
144 lines (143 loc) • 5.07 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import p5 from "p5";
var P5 = /** @class */ (function (_super) {
__extends(P5, _super);
function P5(sketch) {
var _this = _super.call(this, sketch) || this;
_this.running = true;
_this.sizeChangeListeners = [];
return _this;
}
Object.defineProperty(P5.prototype, "realMouseX", {
/**
* Use this instead of mouseX
* @number
*/
get: function () {
return (this.mouseX / this.scaler.value - this.width / 2 + this.simpleCamera.x);
},
enumerable: false,
configurable: true
});
Object.defineProperty(P5.prototype, "realMouseY", {
/**
* Use this instead of mouseY
* @number
*/
get: function () {
return (this.mouseY / this.scaler.value - this.height / 2 + this.simpleCamera.y);
},
enumerable: false,
configurable: true
});
Object.defineProperty(P5.prototype, "isForeground", {
get: function () {
return window.document.visibilityState === "visible";
},
enumerable: false,
configurable: true
});
Object.defineProperty(P5.prototype, "mouseScreen", {
get: function () {
var _a = this.scaler, viewportDelta = _a.viewportDelta, value = _a.value;
return {
x: this.mouseX - viewportDelta.x * value,
y: this.mouseY - viewportDelta.y * value,
};
},
enumerable: false,
configurable: true
});
P5.prototype.emitSizeChangeEvent = function () {
var listeners = this.sizeChangeListeners;
for (var _i = 0, listeners_1 = listeners; _i < listeners_1.length; _i++) {
var listener = listeners_1[_i];
listener(this.width, this.height);
}
};
P5.prototype.addSizeChangeListener = function (func) {
var listeners = this.sizeChangeListeners;
listeners.push(func);
return function () {
var index = listeners.indexOf(func);
if (index > -1) {
listeners.splice(index, 1);
}
};
};
P5.prototype.resizeCanvas = function (width, height) {
_super.prototype.resizeCanvas.call(this, width, height);
this.emitSizeChangeEvent();
};
P5.prototype.drawHandle = function (position, onDraw) {
var camera = this.simpleCamera;
this.push();
this.translate(this.width / 2 + position.x - camera.x, this.height / 2 + position.y - camera.y);
this.noStroke();
onDraw(this);
this.pop();
};
P5.prototype.constrainMax = function (value, max) {
if (value > max) {
return max;
}
else {
return value;
}
};
P5.prototype.constrainMin = function (value, min) {
if (value < min) {
return min;
}
else {
return value;
}
};
P5.prototype.choose = function (collection) {
var lastItem = collection[collection.length - 1];
var restItems = collection.slice(0, collection.length - 1);
var totalPercentsOfRestItems = restItems.reduce(function (t, item) {
return t + item[1];
}, 0);
if (totalPercentsOfRestItems > 1) {
var overMax = totalPercentsOfRestItems - 1;
var avg = 1 / restItems.length;
for (var _i = 0, restItems_1 = restItems; _i < restItems_1.length; _i++) {
var item = restItems_1[_i];
if (item[1] > avg) {
item[1] -= overMax;
}
}
totalPercentsOfRestItems = 1;
}
lastItem[1] = 1 - totalPercentsOfRestItems;
var items = [];
var _loop_1 = function (item, percent) {
Array.from({ length: Math.round(percent * 10) }).forEach(function () {
items.push(item);
});
};
for (var _a = 0, collection_1 = collection; _a < collection_1.length; _a++) {
var _b = collection_1[_a], item = _b[0], percent = _b[1];
_loop_1(item, percent);
}
this.shuffle(items, true);
return this.random(items);
};
return P5;
}(p5));
export { P5 };