| 1 | class Collider |
| 2 | { |
| 3 | gameObject = null; |
| 4 | enabled = false; |
| 5 | |
| 6 | constructor(gameObject) |
| 7 | { |
| 8 | this.gameObject = gameObject; |
| 9 | } |
| 10 | |
| 11 | update(deltaTime) |
| 12 | { |
| 13 | if (GameInput.mousePressed()) |
| 14 | { |
| 15 | this.checkClick(true); |
| 16 | } |
| 17 | |
| 18 | if (GameInput.mouseHeld()) |
| 19 | { |
| 20 | this.checkClick(false); |
| 21 | } |
| 22 | |
| 23 | this.checkCollisions() |
| 24 | } |
| 25 | |
| 26 | checkClick(thisFrame) |
| 27 | { |
| 28 | var hit = false; |
| 29 | var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition); |
| 30 | |
| 31 | var hw = this.gameObject.transform.size.x / 2; |
| 32 | var hh = this.gameObject.transform.size.y / 2; |
| 33 | |
| 34 | if(clickPos.x > this.gameObject.transform.position.x - hw && clickPos.x < this.gameObject.transform.position.x + hw) |
| 35 | { |
| 36 | if(clickPos.y > this.gameObject.transform.position.y - hh && clickPos.y < this.gameObject.transform.position.y + hh) |
| 37 | { |
| 38 | hit = true; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | if (hit) |
| 43 | { |
| 44 | if (thisFrame) |
| 45 | { |
| 46 | this.onClick && this.onClick(); |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | this.onMouseHeld && this.onMouseHeld(); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | checkCollisions() |
| 56 | { |
| 57 | if(this.enabled && !this.gameObject.destroyed) |
| 58 | { |
| 59 | for(var i = 0; i < this.gameObject.scene.activeObjects.length; i++) |
| 60 | { |
| 61 | if(this.gameObject.scene.activeObjects[i] != this && this.gameObject.scene.activeObjects[i].ignoreCollisions) |
| 62 | { |
| 63 | var hit = false; |
| 64 | var o2 = this.gameObject.scene.activeObjects[i]; |
| 65 | |
| 66 | //hw = halfWidth, hh = halfHeight |
| 67 | var hw = this.gameObject.transform.size.x / 2; |
| 68 | var hh = this.gameObject.transform.size.y / 2; |
| 69 | |
| 70 | var ohw = o2.transform.size.x / 2; |
| 71 | var ohh = o2.transform.size.y / 2; |
| 72 | |
| 73 | //CHECK FROM OBJ PERSPECTIVE |
| 74 | var p = |
| 75 | [ |
| 76 | [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y - hh], |
| 77 | [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y - hh], |
| 78 | [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y + hh], |
| 79 | [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y + hh] |
| 80 | ]; |
| 81 | |
| 82 | for(var pi = 0; pi < p.length; pi++) |
| 83 | { |
| 84 | if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw) |
| 85 | { |
| 86 | if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh) |
| 87 | { |
| 88 | hit = true; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | //CHECK FROM OTHER PERSPECTIVE |
| 94 | var p = |
| 95 | [ |
| 96 | [o2.transform.position.x - ohw, o2.transform.position.y - ohh], |
| 97 | [o2.transform.position.x + ohw, o2.transform.position.y - ohh], |
| 98 | [o2.transform.position.x - ohw, o2.transform.position.y + ohh], |
| 99 | [o2.transform.position.x + ohw, o2.transform.position.y + ohh] |
| 100 | ]; |
| 101 | |
| 102 | for(var pi = 0; pi < p.length; pi++) |
| 103 | { |
| 104 | if(p[pi][0] > this.gameObject.transform.position.x - hw && p[pi][0] < this.gameObject.transform.position.x + hw) |
| 105 | { |
| 106 | if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh) |
| 107 | { |
| 108 | hit = true; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if(hit) |
| 114 | if (!this.gameObject.destroyed && typeof this.gameObject.onCollision == 'function') { this.gameObject.onCollision(o2); } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |