BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / collider.js

collider.js

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