class GameObject { transform = null; renderer = null; collider = null; scene = null; _layer = 0; tags = []; destroyed = false; constructor (x, y, sprite, layer = 0) { this.transform = new Transform(this, x, y, 1, 1, 0); this.renderer = new Renderer(this, sprite); this.collider = new Collider(this); this._layer = layer; } get layer () { return this._layer; } set layer (value) { if (this._layer === value) { return; } this._layer = value; if (this.scene) { this.scene.objectLayerChanged(this); } } initQuadtree (quadTree) { this.quadTree = quadTree; } checkQuadtree () { if (!this.quadTree) { return; } const x = this.transform.position.x; const y = this.transform.position.y; // Skip reinsert when the object has not moved since the last check. if (this.node && this._qtX === x && this._qtY === y) { return; } this._qtX = x; this._qtY = y; if (this.node) { if (x < this.node.x1 || x > this.node.x2 || y < this.node.y1 || y > this.node.y2) { this.quadTree.remove(this); this.quadTree.add(this); } } else { this.quadTree.add(this); } } update (deltaTime) { if (this.destroyed) { return; } this.transform.update(deltaTime); this.collider.update(deltaTime); this.renderer.update(deltaTime); this.checkQuadtree(); } destroy () { if (!this.destroyed) { this.onDestroy && this.onDestroy(); this.quadTree && this.quadTree.remove(this); this.scene && this.scene.removeObject(this); this.destroyed = true; } } draw () { if (this.destroyed) { return; } this.renderer.draw(); } }