| 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 | initQuadtree (quadTree) |
| 21 | { |
| 22 | this.quadTree = quadTree; |
| 23 | } |
| 24 | |
| 25 | checkQuadtree () |
| 26 | { |
| 27 | if (!this.quadTree) |
| 28 | return; |
| 29 | |
| 30 | if (this.node) |
| 31 | { |
| 32 | if (this.transform.position.x < this.node.x1 |
| 33 | || this.transform.position.x > this.node.x2 |
| 34 | || this.transform.position.y < this.node.y1 |
| 35 | || this.transform.position.y > this.node.y2) |
| 36 | { |
| 37 | this.quadTree.remove(this); |
| 38 | this.quadTree.add(this); |
| 39 | } |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | this.quadTree.add(this); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | update (deltaTime) |
| 48 | { |
| 49 | this.transform.update(deltaTime); |
| 50 | this.collider.update(deltaTime); |
| 51 | this.renderer.update(deltaTime); |
| 52 | |
| 53 | if (!this.destroyed) |
| 54 | this.checkQuadtree(); |
| 55 | } |
| 56 | |
| 57 | destroy () |
| 58 | { |
| 59 | if (!this.destroyed) |
| 60 | { |
| 61 | this.onDestroy && this.onDestroy(); |
| 62 | this.quadTree && this.quadTree.remove(this); |
| 63 | this.scene.removeObject(this); |
| 64 | this.destroyed = true; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | draw () |
| 69 | { |
| 70 | this.renderer.draw(); |
| 71 | } |
| 72 | } |