BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / collider.js

collider.js

collider fies
timfa Tim · pushed by timfa · 4 years ago
6ae3b3c3 History
collider.js 5.0 KiB Download Raw
1class Collider
2{
3 gameObject = null;
4 enabled = false;
5
6 constructor(gameObject)
7 {
8 this.gameObject = gameObject;
9 }
10
11 update(deltaTime)
12 {
13 let pressed = GameInput.mousePressed();
14 let held = GameInput.mouseHeld();
15 if (pressed.left || pressed.middle || pressed.right)
16 {
17 this.checkClick(true, pressed);
18 }
19
20 if (held.left || held.middle || held.right)
21 {
22 this.checkClick(false, held);
23 }
24
25 this.checkCollisions()
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 {
38 if(clickPos.y > this.gameObject.transform.position.y - hh && clickPos.y < this.gameObject.transform.position.y + hh)
39 {
40 hit = true;
41 }
42 }
43
44 if (hit)
45 {
46 if (thisFrame)
47 {
48 this.gameObject.onClick && this.gameObject.onClick(btns);
49 }
50 else
51 {
52 this.gameObject.onMouseHeld && this.gameObject.onMouseHeld(btns);
53 }
54 }
55 }
56
57 isInBounds(point)
58 {
59 var hw = this.gameObject.transform.size.x / 2;
60 var hh = this.gameObject.transform.size.y / 2;
61
62 if(point.x > this.transform.position.x - hw && point.x < this.transform.position.x + hw)
63 {
64 if(point.y > this.transform.position.y - hh && point.y < this.transform.position.y + hh)
65 {
66 return true;
67 }
68 }
69
70 return false;
71 }
72
73 checkCollisions()
74 {
75 if(this.enabled && !this.gameObject.destroyed)
76 {
77 for(var i = 0; i < this.gameObject.scene.activeObjects.length; i++)
78 {
79 if(this.gameObject.scene.activeObjects[i] != this && this.gameObject.scene.activeObjects[i].collider.enabled)
80 {
81 var hit = false;
82 var o2 = this.gameObject.scene.activeObjects[i];
83
84 //hw = halfWidth, hh = halfHeight
85 var hw = this.gameObject.transform.size.x / 2;
86 var hh = this.gameObject.transform.size.y / 2;
87
88 var ohw = o2.transform.size.x / 2;
89 var ohh = o2.transform.size.y / 2;
90
91 //CHECK FROM OBJ PERSPECTIVE
92 var p =
93 [
94 [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y - hh],
95 [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y - hh],
96 [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y + hh],
97 [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y + hh]
98 ];
99
100 for(var pi = 0; pi < p.length; pi++)
101 {
102 if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw)
103 {
104 if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh)
105 {
106 hit = true;
107 }
108 }
109 }
110
111 //CHECK FROM OTHER PERSPECTIVE
112 var p =
113 [
114 [o2.transform.position.x - ohw, o2.transform.position.y - ohh],
115 [o2.transform.position.x + ohw, o2.transform.position.y - ohh],
116 [o2.transform.position.x - ohw, o2.transform.position.y + ohh],
117 [o2.transform.position.x + ohw, o2.transform.position.y + ohh]
118 ];
119
120 for(var pi = 0; pi < p.length; pi++)
121 {
122 if(p[pi][0] > this.gameObject.transform.position.x - hw && p[pi][0] < this.gameObject.transform.position.x + hw)
123 {
124 if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh)
125 {
126 hit = true;
127 }
128 }
129 }
130
131 if(hit)
132 if (!this.gameObject.destroyed && typeof this.gameObject.onCollision == 'function') { this.gameObject.onCollision(o2); }
133 }
134 }
135 }
136 }
137}