gibbon.js
Version:
Actor/Component system for use with pixi.js.
163 lines • 4.73 kB
JavaScript
export default class Component {
/**
* @property {Game} game
*/
get game() { return this.gameObject.game; }
/**
* @property {Engine} engine
*/
get engine() { return this.game.engine; }
/**
* @property {number} flags - Convenience accessor for GameObject.flags.
*/
get flags() { return this.gameObject.flags; }
set flags(v) { this.gameObject.flags = v; }
/**
* Group controlling the component's GameObject, if any.
*/
get group() { return this.gameObject?.group; }
/**
* @property {boolean} enabled - Whether the component is enabled.
*/
get enabled() { return this._enabled; }
set enabled(v) {
this._enabled = v;
if (v === true) {
this.onEnable?.();
}
else {
this.onDisable?.();
}
}
/**
* @property {number} x
*/
get x() { return this.clip?.x ?? 0; }
set x(v) {
if (this.clip != null) {
this.clip.x = v;
}
}
/**
* @property {number} y
*/
get y() { return this.clip?.y ?? 0; }
set y(v) { if (this.clip != null)
this.clip.y = v; }
/**
* @property {number} rotation - underlying clip rotation in radians.
*/
get rotation() { return this.clip?.rotation ?? 0; }
set rotation(v) {
/// TODO: modulus.
if (v > Math.PI)
v -= 2 * Math.PI;
else if (v < -Math.PI)
v += 2 * Math.PI;
if (this.clip)
this.clip.rotation = v;
}
/**
* @property {PIXI.Point} position - only usable after init()
*/
get position() { return this.gameObject.position; }
set position(v) { this.gameObject.position = v; }
/**
* Indicates the component has been marked for disposal and should no longer
* be referenced.
* @property {Boolean} isDestroyed
*/
get isDestroyed() { return this._destroyed; }
get sleep() { return this._sleep; }
set sleep(v) { this._sleep = v; }
/**
* @property {GameObject} - Game object containing this component.
*/
gameObject;
_enabled = false;
_destroyed = false;
/**
* True if component should sleep.
*/
_sleep = false;
/**
* @property {DisplayObject} clip - Convenience copy of GameObject clip.
*/
clip;
/**
* Constructor intentionally empty so components can be
* instantiated and added to GameObjects without
* knowledge of the underlying game system.
* @note component properties such as gameObject, clip, and game,
* are not available in component constructor.
*/
constructor() { }
/**
* Private initializer calls subclassed init()
* @param {GameObject} gameObject
*/
_init(gameObject) {
this.gameObject = gameObject;
this.clip = this.gameObject.clip;
this._enabled = true;
this.init?.();
}
/**
*
* @param {class} cls - class to add to the component's game object.
* @returns {Component}
*/
add(cls) {
return this.gameObject.add(cls);
}
/**
* Add a component already instantiated. Wraps gameObject.addExisting()
* @param {Component} comp
* @returns {Component} The added component instance.
*/
addInstance(comp, cls) {
return this.gameObject.addInstance(comp, cls);
}
/**
* Add a component already instantiated. Wraps gameObject.addExisting()
* @deprecated Use addInstance()
* @param {Component} comp
* @returns {Component} The added component instance.
*/
addExisting(comp, cls) {
return this.gameObject.addInstance(comp, cls);
}
/**
*
* @param {class} cls - wrapper for gameObject get()
* @returns {Component|null}
*/
get(cls) {
return this.gameObject.get(cls);
}
/**
* Wraps GameObject require().
* @param {*} cls
* @returns {Component}
*/
require(cls) { return this.gameObject.require(cls); }
/**
* Use to destroy a Component.
* override onDestroy() to clean up your components.
* Do not call _destroy() or destroy() directly.
*/
destroy() { this._destroy(); }
/**
* calls destroy() and cleans up any variables.
*/
_destroy() {
if (this.onDestroy) {
this.onDestroy();
}
this._enabled = false;
this._destroyed = true;
this.clip = null;
this.gameObject = undefined;
}
}
//# sourceMappingURL=component.js.map