-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
364 lines (347 loc) · 9.1 KB
/
main.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//main game code
/*
author : Vighnesh SK;
*/
var canvas = document.getElementById("main");
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
var context= canvas.getContext("2d");
var t0 = new Date().getTime();
var keyStates = [];
var enemiesArray = [];
var explosionsArray = [];
var globalBulletArray = [];
var rocket = new Rocket();
var turret = new gun(rocket);
function addEnemy(count) {
for (i=1; i<count+1; i++) {
p = new Pirate();
p.x_pos = 100;
p.y_pos = canvas.height/2;
g = new gun(p);
profile = {
body : p,
gun : g
};
enemiesArray.push(profile);
}
}
rocket.x_pos = canvas.width/2;
rocket.y_pos = canvas.height/2;
//events
window.addEventListener("keydown", function (e) {
e.preventDefault();
var key = e.keyCode;
if (key===38 || key===40 && keyStates.length<2) {
console.log(keyStates);
keyStates.push(key);
}
if (key==39) {
rocket.angle += 10;
}
if (key==37) {
rocket.angle -= 10;
}
}, false);
window.addEventListener("keyup", function (e) {
keyStates = [];
}, false);
//mouse event under dev
window.addEventListener("mousemove", function (e) {
var x_dist = rocket.x_pos - e.clientX;
var y_dist = rocket.y_pos - e.clientY;
rocket.angle = (Math.atan2(y_dist, x_dist)*180/Math.PI)+180;
}, false);
//firing using mouse
window.addEventListener("mousedown", function (e) {
if (e.which===1) {
keyStates.push(40);
}
if (e.which===2) {
keyStates.push(38);
}
}, false);
window.addEventListener("mouseup", function (e) {
if (e.which===1) {
keyStates.splice(keyStates.indexOf(40), 1);
}
if (e.which===2) {
keyStates.splice(keyStates.indexOf(38), 1);
}
})
//initializing
window.onload = loop;
//rocket object
function Rocket() {
this.x_pos = 0;
this.y_pos = 0;
this.angle = 0;
this.x_vel = 0;
this.y_vel = 0;
this.x_acc = 0;
this.y_acc = 0;
this.mass = 20;
this.life = 200;
this.max_vel = 3.2;
this.pivot = [0, 0]
this.draw = function draw() {
vertices = [10, 0, -10, 10, -5, 0, -10, -10];
context.strokeStyle = "#FFF";
//rotation
var _x = Math.cos(this.angle*(Math.PI/180));
var _y = Math.sin(this.angle*(Math.PI/180));
context.beginPath();
for (i=0; i<=vertices.length-2; i+=2) {
//tilting the axis as for rotation
var new_x = (vertices[i])*_x - (vertices[i+1])*_y;
var new_y = (vertices[i+1])*_x + (vertices[i])*_y;
if (i==0) {
context.moveTo(new_x+this.x_pos, new_y+this.y_pos);
//the pivot point <=== injected code for gun false damage
this.pivot[0] = this.x_pos+new_x;
this.pivot[1] = this.y_pos+new_y;
}
else {
context.lineTo(new_x+this.x_pos, new_y+this.y_pos);
}
}
context.closePath();
context.stroke();
}
}
//enemies
function Pirate() {
this.x_pos = 0;
this.y_pos = 0;
this.x_vel = 0;
this.y_vel = 0;
this.a_vel = 0;
this.angle = 0;
this.max_vel = 1.2;
this.range = 0;
this.life = 200;
this.pivot = [0, 0];
this.draw = function draw() {
vertices = [10, 0, -10, 10, -5, 0, -10, -10];
context.strokeStyle = "#F00";
//rotation
var _x = Math.cos(this.angle*(Math.PI/180));
var _y = Math.sin(this.angle*(Math.PI/180));
context.beginPath();
for (i=0; i<=vertices.length-2; i+=2) {
//tilting the axis as for rotation
var new_x = (vertices[i])*_x - (vertices[i+1])*_y;
var new_y = (vertices[i+1])*_x + (vertices[i])*_y;
if (i==0) {
context.moveTo(new_x+this.x_pos, new_y+this.y_pos);
//the pivot point <=== injected code for gun false damage
this.pivot[0] = this.x_pos+new_x;
this.pivot[1] = this.y_pos+new_y;
}
else {
context.lineTo(new_x+this.x_pos, new_y+this.y_pos);
}
}
context.closePath();
context.stroke();
}
// a small AI for aiming and moving
this.ai = function ai(aimingObject) {
var x_dist = aimingObject.x_pos - this.x_pos;
var y_dist = aimingObject.y_pos - this.y_pos;
this.range = Math.hypot(y_dist, x_dist);
if (this.range<500) {
this.angle = Math.atan2(y_dist, x_dist)*180/Math.PI;
this.x_vel = this.max_vel*Math.cos(this.angle*Math.PI/180);
this.y_vel = this.max_vel*Math.sin(this.angle*Math.PI/180);
}
if (this.range<50) {
this.x_vel = 0;
this.y_vel = 0;
}
}
}
//gun object
function gun(rocket) {
this.fire = function turret() {
bullet = {
bull_x : rocket.pivot[0],
bull_y : rocket.pivot[1],
heading : rocket.angle,
lifeTime : 40
};
globalBulletArray.push(bullet);
}
}
//fancy bullet graphic previous a part of gun object dammit man i must use git .. maybe next it >_0
function BulletGraphics() {
context.strokeStyle = "#FFF";
for (i=0; i<globalBulletArray.length; i++) {
var bullet = globalBulletArray[i];
//i dont know why?... but i have to do this to get it work.. wtf?
if (bullet===undefined) {
continue;
}
context.beginPath();
context.arc(bullet["bull_x"], bullet["bull_y"], 1, 0, 2*Math.PI, false);
context.closePath()
context.stroke();
//movement part
globalBulletArray[i]["bull_x"] += 20*Math.cos(bullet["heading"]*Math.PI/180);
globalBulletArray[i]["bull_y"] += 20*Math.sin(bullet["heading"]*Math.PI/180);
if (bullet["lifeTime"]<=0) {
delete globalBulletArray[i];
}
else {
globalBulletArray[i]["lifeTime"] -= 1;
}
}
this.forceClear = function clearBullets() {
globalBulletArray = [];
console.log("Bullets were cleared due to drop in FPS");
}
}
//collsion and explosion callback
function damage(object, globalBulletArray) {
for (var i=0; i<globalBulletArray.length; i++) {
var b = globalBulletArray[i];
//please someone help me with this... forever alone!
if (b===undefined) {
continue;
}
var x_dist = object.x_pos - b["bull_x"];
var y_dist = object.y_pos - b["bull_y"];
var t_dist = Math.hypot(x_dist, y_dist);
if (t_dist<7) {
object.life -= 1;
}
}
}
//Main game looop
function loop() {
//events <= still under production
for (x=0; x<keyStates.length; x++) {
var key = keyStates[x];
if (key==38) {
rocket.y_acc = 0.2*Math.sin(rocket.angle*(Math.PI/180));
rocket.x_acc = 0.2*Math.cos(rocket.angle*(Math.PI/180));
}
if (key==40) {
turret.fire();
}
}
//add enemies if not present
if (enemiesArray.length<1) {
addEnemy(1);
}
//time correction
var t1 = new Date().getTime();
var dt = t1 - t0;
t0 = t1;
//clearing the canvas for next draw
context.clearRect(0, 0, canvas.width, canvas.height);
rocket.draw();
//enemies update code
for (i=0; i<enemiesArray.length; i++) {
var pirate = enemiesArray[i];
if (pirate===undefined) {
continue;
}
pirateBody = pirate["body"];
pirateBody.draw();
//movement
pirateBody.x_pos += pirateBody.x_vel;
pirateBody.y_pos += pirateBody.y_vel;
//AI
pirateBody.ai(rocket);
pirateGun = pirate["gun"];
if (pirateBody.range<200) {
pirateGun.fire();
}
if (dt>1020) {
globalBulletArray = [];
}
//keeping the pirate on the screen
if (pirateBody.x_pos>canvas.width) pirateBody.x_pos = 0;
if (pirateBody.x_pos<0) pirateBody.x_pos = canvas.width;
if (pirateBody.y_pos>canvas.height) pirateBody.y_pos = 0;
if (pirateBody.y_pos<0) pirateBody.y_pos = canvas.height;
//damage
var d = new damage(pirateBody, globalBulletArray);
if (pirateBody.life<=0) {
var e = new Explode(pirateBody.x_pos, pirateBody.y_pos);
// delete enemiesArray[i];
enemiesArray = [];
explosionsArray.push(e);
}
if (pirateBody.life<30) {
context.fillStyle = "#F00";
}
else {
context.fillStyle = "#FFF"
}
context.font = "12px Arial";
context.fillText("Health : "+pirateBody.life.toString(), pirateBody.x_pos-20, pirateBody.y_pos-20);
}
//keep the ship on the screen
if (rocket.x_pos>canvas.width) rocket.x_pos = 0;
if (rocket.x_pos<0) rocket.x_pos = canvas.width;
if (rocket.y_pos>canvas.height) rocket.y_pos = 0;
if (rocket.y_pos<0) rocket.y_pos = canvas.height;
//angle correction
if (Math.abs(rocket.angle)>360) rocket.angle -= 360;
//velocity
if (Math.hypot(rocket.x_vel, rocket.y_vel)>rocket.max_vel) {
rocket.x_vel = (rocket.max_vel-0.5)*Math.cos(rocket.angle*(Math.PI/180));
rocket.y_vel = (rocket.max_vel-0.5)*Math.sin(rocket.angle*(Math.PI/180));
}
else {
rocket.x_vel += rocket.x_acc;
rocket.y_vel += rocket.y_acc;
}
rocket.y_pos += rocket.y_vel;
rocket.x_pos += rocket.x_vel;
//reset accel
rocket.x_acc = 0;
rocket.y_acc = 0;
//gun properties
BulletGraphics();
//that explosion cuz games with strong enemies are cool
var d1 = new damage(rocket, globalBulletArray);
if (rocket.life<=0) {
var e = new Explode(rocket.x_pos, rocket.y_pos);
explosionsArray.push(e);
rocket.x_pos = canvas.width/2;
rocket.y_pos = canvas.height/2;
rocket.life = 200;
rocket.x_vel = rocket.max_vel;
rocket.y_vel = 0;
rocket.angle = 0;
}
//explosion effect
for (i=0; i<explosionsArray.length; i++) {
var explosion = explosionsArray[i];
if (explosion===undefined) {
continue;
}
explosion.update();
delete explosionsArray[i];
}
//data display
if (rocket.life<30) {
context.fillStyle = "#F00";
}
else {
context.fillStyle = "#FFF"
}
context.font = "12px Arial";
context.fillText("Health: " + rocket.life, 10, 40);
context.fillStyle = "#FFF";
context.fillText("FPS : " + Math.floor(1000/dt).toString(), 10, 20);
//lag lag lag
if (dt>70) {
globalBulletArray = [];
}
//iterate
window.requestAnimationFrame(loop, 1000/60);
}