react-simple-game-engine
Version:
[WIP] not able to use in currently. <!-- Document cumming soon... -->
118 lines (117 loc) • 4.48 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 __());
};
})();
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import p5 from "p5";
import { copyProperties } from "../utils";
var Particle = /** @class */ (function (_super) {
__extends(Particle, _super);
function Particle() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.vec = Renderer.createVector(); //velocity
_this.acc = Renderer.createVector(); //acceleration
_this.angle = 0;
_this.color = [255, 255, 255];
_this.size = 5;
_this.lifetimeRemain = _this.lifetime; // in seconds
_this.forceSpriteSize = false;
_this._lifetime = 2; // in seconds
return _this;
}
Object.defineProperty(Particle.prototype, "lifetime", {
get: function () {
return this._lifetime;
},
set: function (lifetime) {
this._lifetime = lifetime;
this.lifetimeRemain = lifetime;
},
enumerable: false,
configurable: true
});
Particle.prototype.initial = function (_a) {
var vec = _a.vec, params = __rest(_a, ["vec"]);
if (vec) {
this.vec.set(vec.x, vec.y);
}
copyProperties(this, params);
};
Particle.prototype.applyForce = function (force) {
this.acc.add(force);
};
Particle.prototype.isDead = function () {
return this.lifetimeRemain <= 0;
};
Particle.prototype.update = function () {
this.vec.add(this.acc);
this.add(this.vec);
this.acc.set(0);
this.lifetimeRemain -= Renderer.deltaTime / 1000;
};
Particle.prototype.draw = function () {
Renderer.push();
Renderer.translate(Renderer.width / 2 + this.x - this.simpleCamera.x, Renderer.height / 2 + this.y - this.simpleCamera.y);
Renderer.rotate(this.angle);
Renderer.noStroke();
var color = __spreadArray([], this.color, true);
var alpha = Renderer.map(this.lifetimeRemain, 0, this.lifetime, 0, 255);
color[3] = alpha;
if (this.sprite) {
var size = this.forceSpriteSize
? [this.size, this.size]
: [this.sprite.width, this.sprite.height];
Renderer.tint.apply(Renderer, color);
Renderer.image.apply(Renderer, __spreadArray(__spreadArray([this.sprite,
// position on canvas
0,
0], size, false), [
//crop on source image
0,
0,
this.sprite.width,
this.sprite.height], false));
}
else {
Renderer.fill.apply(Renderer, color);
this.onDraw(color);
}
//
Renderer.pop();
};
Particle.prototype.onDraw = function (_) {
Renderer.circle(0, 0, this.size);
};
return Particle;
}(p5.Vector));
export { Particle };