BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / collider.js

collider.js

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