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