123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- class GameObject
- {
- constructor(name)
- {
- this.id = 0;
- this.name = name;
- this.children = [];
- this.parented = false;
- this.parent = null;
- this.isOnTree = 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;
- }
- 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();
- }
- initSignals()
- {
- this._initSignals();
- }
- setup()
- {
- this.isOnTree = true;
- this._setup();
- for (let i = 0; i < this.children.length; i++)
- {
- this.children[i].setup();
- }
- }
- update(delta)
- {
- this._update(delta);
- for (let i = 0; i < this.children.length; i++)
- this.children[i].update(delta);
- }
- draw(delta, db)
- {
- this._draw(delta, db);
- for (let i = 0; i < this.children.length; i++)
- this.children[i].draw(delta, db);
- }
-
- _initSignals()
- {
- }
- _setup()
- {
- }
- _update(delta)
- {
- }
- _draw(delta, db)
- {
- }
- }
|