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