| 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 | const pressed = GameInput.mousePressed(); |
| 24 | const held = GameInput.mouseHeld(); |
| 25 | |
| 26 | if (pressed.left || pressed.middle || pressed.right) |
| 27 | { |
| 28 | this.checkClick(true, pressed); |
| 29 | } |
| 30 | |
| 31 | if (held.left || held.middle || held.right) |
| 32 | { |
| 33 | this.checkClick(false, held); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | checkClick (thisFrame, btns) |
| 38 | { |
| 39 | if (!GameInput.mousePosition || !this.gameObject.scene) |
| 40 | { return; } |
| 41 | |
| 42 | var hit = false; |
| 43 | var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition); |
| 44 | |
| 45 | var hw = this.gameObject.transform.size.x / 2; |
| 46 | var hh = this.gameObject.transform.size.y / 2; |
| 47 | |
| 48 | if (clickPos.x > this.gameObject.transform.position.x - hw && clickPos.x < this.gameObject.transform.position.x + hw) |
| 49 | { |
| 50 | if (clickPos.y > this.gameObject.transform.position.y - hh && clickPos.y < this.gameObject.transform.position.y + hh) |
| 51 | { |
| 52 | hit = true; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if (hit) |
| 57 | { |
| 58 | if (thisFrame) |
| 59 | { |
| 60 | this.gameObject.onClick && this.gameObject.onClick(btns); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | this.gameObject.onMouseHeld && this.gameObject.onMouseHeld(btns); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | isInBounds (point) |
| 70 | { |
| 71 | var hw = this.gameObject.transform.size.x / 2; |
| 72 | var hh = this.gameObject.transform.size.y / 2; |
| 73 | |
| 74 | if (point.x > this.gameObject.transform.position.x - hw && point.x < this.gameObject.transform.position.x + hw) |
| 75 | { |
| 76 | if (point.y > this.gameObject.transform.position.y - hh && point.y < this.gameObject.transform.position.y + hh) |
| 77 | { |
| 78 | return true; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | checkCollisions () |
| 86 | { |
| 87 | if (this.enabled && !this.gameObject.destroyed) |
| 88 | { |
| 89 | const activeObjects = this.gameObject.scene.activeObjects; |
| 90 | const offset = activeObjects.indexOf(this.gameObject) + 1; |
| 91 | |
| 92 | for (var i = offset; i < activeObjects.length; i++) |
| 93 | { |
| 94 | var o2 = activeObjects[i]; |
| 95 | if (o2 === this.gameObject || !o2.collider.enabled || o2.destroyed) |
| 96 | { continue; } |
| 97 | |
| 98 | if (this.widestDiagonal + o2.collider.widestDiagonal < this.gameObject.transform.position.distanceTo(o2.transform.position)) |
| 99 | { continue; } |
| 100 | |
| 101 | var hit = false; |
| 102 | |
| 103 | //hw = halfWidth, hh = halfHeight |
| 104 | var hw = this.gameObject.transform.size.x / 2; |
| 105 | var hh = this.gameObject.transform.size.y / 2; |
| 106 | |
| 107 | var ohw = o2.transform.size.x / 2; |
| 108 | var ohh = o2.transform.size.y / 2; |
| 109 | |
| 110 | //CHECK FROM OBJ PERSPECTIVE |
| 111 | var points = |
| 112 | [ |
| 113 | this.gameObject.transform.position.x - hw, |
| 114 | this.gameObject.transform.position.x + hw, |
| 115 | this.gameObject.transform.position.y - hh, |
| 116 | this.gameObject.transform.position.y + hh |
| 117 | ]; |
| 118 | |
| 119 | if (points[1] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw) |
| 120 | { |
| 121 | if (points[3] >= o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh) |
| 122 | { |
| 123 | hit = true; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (hit) |
| 128 | { |
| 129 | if (!this.gameObject.destroyed && typeof this.gameObject.onCollision == 'function') |
| 130 | { this.gameObject.onCollision(o2); } |
| 131 | |
| 132 | if (!o2.destroyed && typeof o2.onCollision == 'function') |
| 133 | { o2.onCollision(this.gameObject); } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |