123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- class GameObject
- {
-
- constructor(name)
- {
- this.id = 0;
- this.name = name;
- this.children = [];
- this.parented = false;
- this.parent = null;
- this.isOnTree = false;
- this.isRoot = false;
- this.signals = [];
- this.initSignals();
- GameHandler.instanceGameObject(this);
- }
-
- getChildren()
- {
- return this.children;
- }
-
- getChildByIndex(idx)
- {
- if (idx >= 0 && idx < this.children.length)
- return this.children[idx];
- return null;
- }
-
- getChildById(id)
- {
- for (let i = 0; i < this.children.length; i++)
- {
- if (this.children[i].id == id)
- return this.children[i];
- }
- return null;
- }
-
- getChildByName(name)
- {
- for (let i = 0; i < this.children.length; i++)
- if (this.children[i].name == name) return this.children[i];
- return null;
- }
-
- getParent()
- {
- if (!this.parented) return null;
- return this.parent;
- }
-
- addSignal(name)
- {
- this.signals.push(new Signal(name));
- }
-
- connect(signalName, target, callback)
- {
- for (let i = 0; i < this.signals.length; i++)
- {
- if (this.signals[i].name == signalName)
- {
- this.signals[i].targets.push(target);
- this.signals[i].callbacks.push(callback);
- return;
- }
- }
- }
-
- emitSignal(signalName, ...params)
- {
- for (let i = 0; i < this.signals.length; i++)
- {
- if (this.signals[i].name == signalName)
- {
- for (let j = 0; j < this.signals[i].callbacks.length; j++)
- this.signals[i].targets[j][this.signals[i].callbacks[j]](...params);
- return;
- }
- }
- }
-
- addChild(child)
- {
- child.parent = this;
- child.parented = true;
- this.children.push(child);
- if (this.isOnTree) child.setup();
- }
-
- removeChildByIndex(idx)
- {
- if (idx >= 0 && idx < this.children.length)
- this.children.splice(idx, 1);
- }
-
- removeChildById(id)
- {
- for (let i = 0; i < this.children.length; i++)
- {
- if (this.children[i].id == id)
- {
- this.children.splice(i, 1);
- return;
- }
- }
- }
-
- removeChildByName(name)
- {
- for (let i = 0; i < this.children.length; i++)
- {
- if (this.children[i].name == name)
- {
- this.children.splice(i, 1);
- return;
- }
- }
- }
-
- free()
- {
- if (this.parented)
- this.getParent().removeChildById(this.id);
- else if (this.isRoot)
- GameHandler.removeRootObjectById(this.id);
- this.destroy();
- }
-
- destroy()
- {
- for (let i = 0; i < this.children.length; i++)
- this.children[i].destroy();
- for (var prop in this)
- this[prop] = null;
- }
-
- initSignals()
- {
- this._initSignals();
- }
-
- setup()
- {
- this.isOnTree = true;
- this._setup();
- for (let i = 0; i < this.children.length; i++)
- {
- this.children[i].setup();
- }
- }
-
- updateChildren(delta)
- {
- this._update(delta);
- for (let i = 0; i < this.children.length; i++)
- {
- this.children[i].update(delta);
- }
- }
-
- update(delta)
- {
- this.updateChildren(delta);
- }
-
- drawChildren(delta, db)
- {
- this._draw(delta, db);
- for (let i = 0; i < this.children.length; i++)
- this.children[i].draw(delta, db);
- }
-
- draw(delta, db)
- {
- this.drawChildren(delta, db);
- }
-
- _initSignals()
- {
- }
-
- _setup()
- {
- }
-
- _update(delta)
- {
- }
-
- _draw(delta, db)
- {
- }
- }
|