1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- class GameObject
- {
- constructor(name)
- {
- this.id = 0;
- this.name = name;
- this.children = [];
- this.parented = false;
- this.parent = null;
- this.isOnTree = false;
- GameHandler.instanceGameObject(this);
- }
- // Getters
- 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;
- }
- // Methods
- addChild(child)
- {
- child.parent = this;
- child.parented = true;
- this.children.push(child);
- if (this.isOnTree) child.setup();
- }
- 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)
- {
- this._draw(delta);
- for (let i = 0; i < this.children.length; i++)
- this.children[i].draw(delta);
- }
- // Callbacks
- _setup()
- {
- }
- _update(delta)
- {
- }
- _draw(delta)
- {
- }
- }
|