BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / collider.js

collider.js

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