-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphys.js
269 lines (189 loc) · 6.7 KB
/
phys.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
function phys_player(){
// If player dies
if(player.health.state < 1){
player_dead();
}
// If an axis is coliding
var xHit, yHit;
/* BULLET COLLISION */
for(var i = 0; i < projectile.length; i++){
// Check if it's their own projectile
if(projectile[i].id === player.id){
continue; // The the next projectile
}
// Sum boools
xHit = false;
yHit = false;
// Collision check
if(player.x <= projectile[i].x){
if(player.x + player.width >= projectile[i].x){
xHit = true;
}
}
if(player.y <= projectile[i].y){
if(player.y + player.height >= projectile[i].y){
yHit = true;
}
}
// Check if both axis hit
if(xHit && yHit){
// Deal dmg
player.health.state -= projectile[i].damage;
// Remove the bullet
projectile.splice(i, 1);
i--; // Go back an index since this index got spliced
}
}
/* MOVEMENT */
var incX = true, incY = true;
// Calc the player velocity based on input
if(client.key.left && player.velocity.x > -player.velocity.max) player.velocity.x -= player.velocity.inc;
if(client.key.right && player.velocity.x < player.velocity.max) player.velocity.x += player.velocity.inc;
if(client.key.up && player.velocity.y > -player.velocity.max) player.velocity.y -= player.velocity.inc;
if(client.key.down && player.velocity.y < player.velocity.max) player.velocity.y += player.velocity.inc;
// Calculate player velocity based on friction
if(player.velocity.x >= 0) player.velocity.x -= player.velocity.friction;
else player.velocity.x += player.velocity.friction;
if(player.velocity.y >= 0) player.velocity.y -= player.velocity.friction;
else player.velocity.y += player.velocity.friction;
// Make the player stand still if velocity too low
if(Math.abs(player.velocity.x) < player.velocity.min) incX = false;
if(Math.abs(player.velocity.y) < player.velocity.min) incY = false;
// Apply the player velocity to the player position
if(incX) player.x += player.velocity.x;
if(incY) player.y += player.velocity.y;
/*-- - Set the offset for the screen! - --*/
client.offset.x = player.x - (client.width / 2) + (player.width / 2);
client.offset.y = player.y - (client.height / 2) + (player.height / 2);
}
function phys_enemy(){
// every enemy
for(var i = 0; i < enemy.length; i++){
// If enemy dies
if(enemy[i].health.state < 1){
// Got a kill
client.stat.currentKills++;
enemy.splice(i, 1);
continue;
}
// If an axis is coliding
var xHit, yHit;
/* BULLET COLLISION */
for(var b = 0; b < projectile.length; b++){
// Check if it's their own projectile
if(projectile[b].id === enemy[i].id){
continue; // The the next projectile
}
// Sum boools
xHit = false;
yHit = false;
// Collision check
if(enemy[i].x <= projectile[b].x){
if(enemy[i].x + enemy[i].width >= projectile[b].x){
xHit = true;
}
}
if(enemy[i].y <= projectile[b].y){
if(enemy[i].y + enemy[i].height >= projectile[b].y){
yHit = true;
}
}
// Check if both axis hit
if(xHit && yHit){
// Deal dmg
enemy[i].health.state -= projectile[b].damage;
// Remove the bullet
projectile.splice(b, 1);
continue;
}
}
/* Shoot the projectile */
// calculate the distance between the player and itself
var d = dist(enemy[i].x, enemy[i].y, player.x, player.y);
// Try to shoot a shoot towards the player
if(d <= enemy[i].instruction.attackDist){
create_enemy_projectile(i);
}
/* Movement */
const MIN_DIST = 2;
var dx = enemy[i].x - player.x;
var dy = enemy[i].y - player.y;
var distance = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
if(distance > MIN_DIST){
// Accelerate the enemy towards player
if(Math.abs(enemy[i].velocity.x) < enemy[i].velocity.max){
// Accelerate
if(dx < 0){
enemy[i].velocity.x += enemy[i].velocity.inc;
}
else{
enemy[i].velocity.x -= enemy[i].velocity.inc;
}
}
if(Math.abs(enemy[i].velocity.y) < enemy[i].velocity.max){
// Accelerate
if(dy < 0){
enemy[i].velocity.y += enemy[i].velocity.inc;
}
else{
enemy[i].velocity.y -= enemy[i].velocity.inc;
}
}
}
else{
// Accelerate the enemy slowly away from the player
}
// Apply the friction
if(enemy[i].velocity.x >= 0) enemy[i].velocity.x -= enemy[i].velocity.friction;
else enemy[i].velocity.x += enemy[i].velocity.friction;
if(enemy[i].velocity.y >= 0) enemy[i].velocity.y -= enemy[i].velocity.friction;
else enemy[i].velocity.y += enemy[i].velocity.friction;
// Make the enemy stand still if velocity too low
var incX = true, incY = true;
if(Math.abs(enemy[i].velocity.x) < enemy[i].velocity.min) incX = false;
if(Math.abs(enemy[i].velocity.y) < enemy[i].velocity.min) incY = false;
// Apply the enemy velocity to the enemy position
if(incX) enemy[i].x += enemy[i].velocity.x;
if(incY) enemy[i].y += enemy[i].velocity.y;
// Apply the acceleration to the movement
enemy[i].x += enemy[i].velocity.x;
enemy[i].y += enemy[i].velocity.y;
}
}
function phys_projectile(){
// Get the time
var d = new Date();
var t = d.getTime();
// Remove indexes
var removeIndex = []
// Loop through all the projectiles
for(var i = 0; i < projectile.length; i++){
// Check if it should be removed now
if(projectile[i].ending <= t){
// It's past the removal time
removeIndex.push(i); // Add the index to an array
continue; // Next obj
}
}
// Loop through the removeIndex and remove all the unwanted projectiles
for(var i = 0; i < removeIndex.length; i++){
// Remove the projectile
projectile.splice(removeIndex[i] - i, 1);
}
/* Move the projectile acording to it's coordinate */
for(var i = 0; i < projectile.length; i++){
// Increment the bullet position
projectile[i].x += projectile[i].xInc;
projectile[i].y += projectile[i].yInc;
}
}
function phys(){
// The players physics
phys_player();
// Physics for the emeys (bot)
phys_enemy();
// Physics for the projectiles
phys_projectile();
// Check if it should create projectile
if(client.mouse.down){ create_player_projectile() }
}