| 1 | class GameObject |
| 2 | { |
| 3 | transform = null; |
| 4 | renderer = null; |
| 5 | collider = null; |
| 6 | scene = null; |
| 7 | _layer = 0; |
| 8 | tags = []; |
| 9 | |
| 10 | destroyed = false; |
| 11 | |
| 12 | constructor (x, y, sprite, layer = 0) |
| 13 | { |
| 14 | this.transform = new Transform(this, x, y, 1, 1, 0); |
| 15 | this.renderer = new Renderer(this, sprite); |
| 16 | this.collider = new Collider(this); |
| 17 | this._layer = layer; |
| 18 | } |
| 19 | |
| 20 | get layer () |
| 21 | { |
| 22 | return this._layer; |
| 23 | } |
| 24 | |
| 25 | set layer (value) |
| 26 | { |
| 27 | if (this._layer === value) |
| 28 | { return; } |
| 29 | this._layer = value; |
| 30 | if (this.scene) |
| 31 | { this.scene.objectLayerChanged(this); } |
| 32 | } |
| 33 | |
| 34 | initQuadtree (quadTree) |
| 35 | { |
| 36 | this.quadTree = quadTree; |
| 37 | } |
| 38 | |
| 39 | checkQuadtree () |
| 40 | { |
| 41 | if (!this.quadTree) |
| 42 | { return; } |
| 43 | |
| 44 | if (this.node) |
| 45 | { |
| 46 | if (this.transform.position.x < this.node.x1 |
| 47 | || this.transform.position.x > this.node.x2 |
| 48 | || this.transform.position.y < this.node.y1 |
| 49 | || this.transform.position.y > this.node.y2) |
| 50 | { |
| 51 | this.quadTree.remove(this); |
| 52 | this.quadTree.add(this); |
| 53 | } |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | this.quadTree.add(this); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | update (deltaTime) |
| 62 | { |
| 63 | if (this.destroyed) |
| 64 | { return; } |
| 65 | |
| 66 | this.transform.update(deltaTime); |
| 67 | this.collider.update(deltaTime); |
| 68 | this.renderer.update(deltaTime); |
| 69 | |
| 70 | this.checkQuadtree(); |
| 71 | } |
| 72 | |
| 73 | destroy () |
| 74 | { |
| 75 | if (!this.destroyed) |
| 76 | { |
| 77 | this.onDestroy && this.onDestroy(); |
| 78 | this.quadTree && this.quadTree.remove(this); |
| 79 | this.scene && this.scene.removeObject(this); |
| 80 | this.destroyed = true; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | draw () |
| 85 | { |
| 86 | if (this.destroyed) |
| 87 | { return; } |
| 88 | |
| 89 | this.renderer.draw(); |
| 90 | } |
| 91 | } |