gibbon.js
Version:
Actor/Component system for use with pixi.js.
108 lines • 2.73 kB
JavaScript
import { quickSplice } from './utils/array-utils';
import { Ticker } from 'pixi.js';
export class Engine {
/**
* @property {Container} objectLayer
*/
_objectLayer;
get objectLayer() { return this._objectLayer; }
set objectLayer(v) {
this._objectLayer = v;
}
/**
* @property {Actor[]} objects
*/
objects;
/**
* @property {IUpdater[]} updaters - Updaters are for systems or objects with update
* functions that don't require complex Actors.
*/
updaters;
ticker;
constructor(ticker) {
this.objects = [];
this.updaters = [];
this.ticker = ticker ?? new Ticker();
this.ticker.add(this.update, this);
}
update() {
const sec = this.ticker.deltaMS / 1000;
const updaters = this.updaters;
for (let i = updaters.length - 1; i >= 0; i--) {
updaters[i].update(sec);
}
const objs = this.objects;
for (let i = objs.length - 1; i >= 0; i--) {
const obj = objs[i];
if (obj.isDestroyed === true) {
obj._destroy();
quickSplice(objs, i);
}
else if (obj.active)
obj.update(sec);
}
}
start() {
this.ticker.start();
}
stop() {
this.ticker.stop();
}
/**
* Add Actor to the engine.
* @param {Actor} obj
*/
add(obj) {
if (obj.isAdded) {
console.warn(`error: Actor already added to engine.`);
}
if (obj.clip != null && obj.clip.parent == null) {
this._objectLayer.addChild(obj.clip);
}
obj._added();
this.objects.push(obj);
}
/**
*
* @param {IUpdater} sys
*/
addUpdater(sys) {
if (this.updaters.indexOf(sys) < 0) {
this.updaters.push(sys);
}
}
/**
*
* @param {IUpdater} sys
*/
removeUpdater(sys) {
const ind = this.updaters.indexOf(sys);
if (ind >= 0) {
this.updaters.splice(ind, 1);
}
}
/**
* Remove a Actor from the Engine.
* @param {Actor} obj
* @returns {boolean} true if object was removed.
*/
remove(obj) {
const ind = this.objects.indexOf(obj);
if (ind < 0)
return false;
this.objects.splice(ind, 0);
//this._objects[ind] = this._objects[ this._objects.length-1];
//this._objects.pop();
return true;
}
/**
* Destroy a game object.
* @param {Actor} obj
*/
destroy(obj) {
if (obj.isDestroyed !== true) {
obj.destroy();
}
}
}
//# sourceMappingURL=engine.js.map