-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility functions.js
219 lines (207 loc) · 6.75 KB
/
utility functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
function getRandomColor()
{
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++)
color += letters[Math.floor(Math.random() * 16)];
return color;
}
//------------------------------------------------------------------------
var lastTime= 0, fps, delta;
function calculate_framerate(timestamp)
{
fps= Math.round(1000 / (timestamp - lastTime));
lastTime= timestamp;
document.getElementById("framerate").innerHTML= "FPS: " + fps;
return fps;
}
//------------------------------------------------------------------------
function stopwatch()
{
var start= Date.now();
function get_duration()
{
var d= Date.now() - start;
return d;
}
return get_duration;
}
//------------------------------------------------------------------------
var mouse_x, mouse_y;
function get_co_ords(event)
{
mouse_x= event.offsetX;
mouse_y= event.offsetY;
}
//------------------------------------------------------------------------
var up= false,
down= false,
left= false,
right= false;
jump= false;
var keycode_up, key_up;
function keyup_get_key(event)
{
keycode_up= event.keyCode;
key_up= event.key;
switch(keycode_up)
{
case 38 : {up= false; break;}
case 40 : {down= false; break;}
case 37 : {left= false; break;}
case 39 : {right= false; break;}
case 32 : {jump= false; break;}
}
}
var keycode_down, key_down;
function keydown_get_key(event)
{
keycode_down= event.keyCode;
key_down= event.key;
switch(keycode_down)
{
case 38 : {up= true; break;}
case 40 : {down= true; break;}
case 37 : {left= true; break;}
case 39 : {right= true; break;}
case 32 : {jump= true; break;}
}
}
//------------------------------------------------------------------------
function draw_vector(pos, vel= pos, color= "red")
{
context.save();
context.beginPath();
context.strokeStyle= color;
context.translate(pos.dx, pos.dy);
context.rotate(vel.get_angle());
context.moveTo(0, 0);
context.lineTo(30, 0);
context.lineTo(23, -5);
context.moveTo(30, 0);
context.lineTo(23, 5);
context.stroke();
context.restore();
}
//------------------------------------------------------------------------
function clone_particle(original)
{
var clone= new particle(0, 0, 0, 0);
clone.position.dx= original.position.dx + 0;
clone.position.dy= original.position.dy + 0;
clone.velocity.set_mag(original.velocity.get_mag() + 0);
clone.velocity.set_angle(original.velocity.get_angle() + 0);
clone.collider_type= original.collider_type;
clone.radius= original.radius;
clone.width= original.width;
clone.height= original.height;
clone.color= original.color;
return clone;
}
//------------------------------------------------------------------------
function check_b2b_collision(b1, b2)
{
var dx= Math.abs(b1.position.dx - b2.position.dx);
var dy= Math.abs(b1.position.dy - b2.position.dy);
return (((dx * dx) + (dy * dy)) <= ((b1.radius + b2.radius) * (b1.radius + b2.radius)));
}
//------------------------------------------------------------------------
function check_x2x_collision(x1, x2)
{
return (x1.position.dx >= x2.position.dx - x2.width && x1.position.dx <= x2.position.dx + x2.width && x1.position.dy >= x2.position.dy - x2.height && x1.position.dy <= x2.position.dy + x2.height) ;
}
//------------------------------------------------------------------------
function check_wall_collision(ref)
{
let w= ref.collider_type == "ball" ? ref.radius : (ref.type == "box" ? ref.width : 0);
let h= ref.collider_type == "ball" ? ref.radius : (ref.type == "box" ? ref.height : 0);
return ((ref.position.dx >= width / 2 - w || ref.position.dx <= -width / 2 + w) || (ref.position.dy >= height / 2 - h || ref.position.dy <= -height / 2 + h));
}
function p2wall_collision(ref, inprison= false, stick= false)
{
let d, flag= false;
let position= new vector(ref.position.dx, ref.position.dy);
let w= ref.collider_type == "ball" ? ref.radius : (ref.type == "box" ? ref.width : 0);
let h= ref.collider_type == "ball" ? ref.radius : (ref.type == "box" ? ref.height : 0);
if(ref.position.dx >= width / 2 - w || ref.position.dx <= -width / 2 + w)
{
position.dx= Math.sign(ref.position.dx) * (width / 2) + (Math.sign(ref.position.dx) * -1 * (w + 0));
flag= true;
}
if(ref.position.dy >= height / 2 - h || ref.position.dy <= -height / 2 + h)
{
position.dy= Math.sign(ref.position.dy) * (height / 2) + (Math.sign(ref.position.dy) * -1 * (h + 0));
flag= true;
}
if(flag === true)
{
if(inprison === true)
ref.position.set_vect(position);
if(stick === true)
ref.velocity.set_mag(0);
return position;
}
else
return false;
}
//------------------------------------------------------------------------
function uncolide(pt1, pt2)
{
var nudge= new vector(0, 0);
nudge.set_mag(1);
var tta= Math.atan2(pt1.position.dy - pt2.position.dy, pt1.position.dx - pt2.position.dx);
//nudge.set_angle(tta);
//pt1.position.add(nudge);
nudge.set_angle(tta + Math.PI);
pt2.position.add(nudge);
}
//------------------------------------------------------------------------
function unstack_particles(array, type)
{
for(var e1= 0; e1 < array.length; e1++)
for(var e2= e1; e2 < array.length ; e2++)
{
if(e1 == e2) continue;
if(type == "ball")
while(check_b2b_collision(array[e1], array[e2])){uncolide(array[e1], array[e2]);}
else if(type == "box")
while(check_x2x_collision(array[e1], array[e2])){uncolide(array[e1], array[e2]);}
}
}
//------------------------------------------------------------------------
function wrapp_around(ref)
{
let d, flag= false;
let w= ref.collider_type == "ball" ? ref.radius : (ref.type == "box" ? ref.width : 0);
let h= ref.collider_type == "ball" ? ref.radius : (ref.type == "box" ? ref.height : 0);
if(ref.position.dx >= width / 2 - w || ref.position.dx <= -width / 2 + w)
{
ref.position.dx= Math.sign(ref.position.dx) * (-width / 2) + (Math.sign(ref.position.dx) * 1 * (w + 0));
}
if(ref.position.dy >= height / 2 - h || ref.position.dy <= -height / 2 + h)
{
ref.position.dy= Math.sign(ref.position.dy) * (-height / 2) + (Math.sign(ref.position.dy) * 1 * (h + 0));
}
}
//------------------------------------------------------------------------
function draw_object(pos, ang= 0, image, w, h)
{
if(w === undefined && h === undefined)
{
w= 50;
h= w * (image.height / image.width);
}
else
{
if(w === undefined)
w= h * (image.width / image.height);
if(h === undefined)
h= w * (image.height / image.width);
}
context.save();
context.translate(pos.dx, pos.dy);
context.rotate(ang);
context.drawImage(image, -w / 2, -h / 2, w, h);
context.restore();
}
//------------------------------------------------------------------------