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