| 1 | class Collider |
| 2 | { |
| 3 | gameObject = null; |
| 4 | enabled = false; |
| 5 | |
| 6 | constructor (gameObject) |
| 7 | { |
| 8 | this.gameObject = gameObject; |
| 9 | } |
| 10 | |
| 11 | get widestDiagonal () |
| 12 | { |
| 13 | return Math.sqrt((this.gameObject.transform.size.x * this.gameObject.transform.size.x) + (this.gameObject.transform.size.y * this.gameObject.transform.size.y)); |
| 14 | } |
| 15 | |
| 16 | update (deltaTime) |
| 17 | { |
| 18 | if (this.enabled) |
| 19 | { |
| 20 | this.checkCollisions(); |
| 21 | } |
| 22 | |
| 23 | // Bullets/FX have no click handlers — skip per-object mouse work entirely. |
| 24 | const obj = this.gameObject; |
| 25 | if (!obj.onClick && !obj.onMouseHeld) |
| 26 | { return; } |
| 27 | |
| 28 | const pressed = GameInput.mousePressed(); |
| 29 | const held = GameInput.mouseHeld(); |
| 30 | |
| 31 | if (pressed.left || pressed.middle || pressed.right) |
| 32 | { |
| 33 | this.checkClick(true, pressed); |
| 34 | } |
| 35 | |
| 36 | if (held.left || held.middle || held.right) |
| 37 | { |
| 38 | this.checkClick(false, held); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | checkClick (thisFrame, btns) |
| 43 | { |
| 44 | if (!GameInput.mousePosition || !this.gameObject.scene) |
| 45 | { return; } |
| 46 | |
| 47 | var hit = false; |
| 48 | var clickPos = GameInput.mousePosition |
| 49 | .subtract(this.gameObject.scene.real_position) |
| 50 | .subtract(this.gameObject.scene.cameraPosition); |
| 51 | |
| 52 | var hw = this.gameObject.transform.size.x / 2; |
| 53 | var hh = this.gameObject.transform.size.y / 2; |
| 54 | |
| 55 | if (clickPos.x > this.gameObject.transform.position.x - hw && clickPos.x < this.gameObject.transform.position.x + hw) |
| 56 | { |
| 57 | if (clickPos.y > this.gameObject.transform.position.y - hh && clickPos.y < this.gameObject.transform.position.y + hh) |
| 58 | { |
| 59 | hit = true; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (hit) |
| 64 | { |
| 65 | if (thisFrame) |
| 66 | { |
| 67 | this.gameObject.onClick && this.gameObject.onClick(btns); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | this.gameObject.onMouseHeld && this.gameObject.onMouseHeld(btns); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | isInBounds (point) |
| 77 | { |
| 78 | var hw = this.gameObject.transform.size.x / 2; |
| 79 | var hh = this.gameObject.transform.size.y / 2; |
| 80 | |
| 81 | if (point.x > this.gameObject.transform.position.x - hw && point.x < this.gameObject.transform.position.x + hw) |
| 82 | { |
| 83 | if (point.y > this.gameObject.transform.position.y - hh && point.y < this.gameObject.transform.position.y + hh) |
| 84 | { |
| 85 | return true; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | checkCollisions () |
| 93 | { |
| 94 | if (this.enabled && !this.gameObject.destroyed) |
| 95 | { |
| 96 | const activeObjects = this.gameObject.scene.activeObjects; |
| 97 | const offset = activeObjects.indexOf(this.gameObject) + 1; |
| 98 | |
| 99 | for (var i = offset; i < activeObjects.length; i++) |
| 100 | { |
| 101 | var o2 = activeObjects[i]; |
| 102 | if (o2 === this.gameObject || !o2.collider.enabled || o2.destroyed) |
| 103 | { continue; } |
| 104 | |
| 105 | if (this.widestDiagonal + o2.collider.widestDiagonal < this.gameObject.transform.position.distanceTo(o2.transform.position)) |
| 106 | { continue; } |
| 107 | |
| 108 | var hit = false; |
| 109 | |
| 110 | //hw = halfWidth, hh = halfHeight |
| 111 | var hw = this.gameObject.transform.size.x / 2; |
| 112 | var hh = this.gameObject.transform.size.y / 2; |
| 113 | |
| 114 | var ohw = o2.transform.size.x / 2; |
| 115 | var ohh = o2.transform.size.y / 2; |
| 116 | |
| 117 | //CHECK FROM OBJ PERSPECTIVE |
| 118 | var points = |
| 119 | [ |
| 120 | this.gameObject.transform.position.x - hw, |
| 121 | this.gameObject.transform.position.x + hw, |
| 122 | this.gameObject.transform.position.y - hh, |
| 123 | this.gameObject.transform.position.y + hh |
| 124 | ]; |
| 125 | |
| 126 | if (points[1] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw) |
| 127 | { |
| 128 | if (points[3] >= o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh) |
| 129 | { |
| 130 | hit = true; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (hit) |
| 135 | { |
| 136 | if (!this.gameObject.destroyed && typeof this.gameObject.onCollision == 'function') |
| 137 | { this.gameObject.onCollision(o2); } |
| 138 | |
| 139 | if (!o2.destroyed && typeof o2.onCollision == 'function') |
| 140 | { o2.onCollision(this.gameObject); } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |