BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / collider.js

collider.js

optmisih
timfa Tim · pushed by timfa · 4 years ago
68c624b6 History
collider.js 5.6 KiB Download Raw
1class 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 p =
106 [
107 [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y - hh],
108 [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y - hh],
109 [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y + hh],
110 [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y + hh]
111 ];
112
113 for (var pi = 0; pi < p.length; pi++)
114 {
115 if (p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw)
116 {
117 if (p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh)
118 {
119 hit = true;
120 }
121 }
122 }
123
124 //CHECK FROM OTHER PERSPECTIVE
125 var p =
126 [
127 [o2.transform.position.x - ohw, o2.transform.position.y - ohh],
128 [o2.transform.position.x + ohw, o2.transform.position.y - ohh],
129 [o2.transform.position.x - ohw, o2.transform.position.y + ohh],
130 [o2.transform.position.x + ohw, o2.transform.position.y + ohh]
131 ];
132
133 for (var pi = 0; pi < p.length; pi++)
134 {
135 if (p[pi][0] > this.gameObject.transform.position.x - hw && p[pi][0] < this.gameObject.transform.position.x + hw)
136 {
137 if (p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh)
138 {
139 hit = true;
140 }
141 }
142 }
143
144 if (hit)
145 if (!this.gameObject.destroyed && typeof this.gameObject.onCollision == 'function') { this.gameObject.onCollision(o2); }
146 }
147 }
148 }
149 }
150}