-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·453 lines (387 loc) · 16.7 KB
/
index.html
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<html>
<head>
<title> The Game </title>
<link href="css/main.css" media="all" rel="stylesheet" type="text/css"/>
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script language="javascript" src="js/controls.js" type="text/javascript"></script>
<script language="javascript" src="js/jquery.hotkeys.js" type="text/javascript"></script>
<script language="javascript" src="js/util.js" type="text/javascript"></script>
<!--
<script language="javascript" src="javascripts/sprite.js" type="text/javascript"></script>
<script language="javascript" src="javascripts/sound.js" type="text/javascript"></script>
-->
</head>
<body>
<script type="text/javascript">
var DEBUG = 0;
var CANVAS_HEIGHT = 600;
var CANVAS_WIDTH = 800;
var FPS_FILTER = 40;
var FONT = "sans";
var KILLS_TO_NEXT_LEVEL = 20;
var fps = 0, frameCounter = 1, now, lastUpdate = new Date;
var COLOR_BLACK = "#000";
var player = new ClassPlayer();
var gameManager = new ClassGameManager();
var enemies = [];
var playerBullets = [];
var canvasElement = $("<canvas id='myCanvas' width='" + CANVAS_WIDTH + "' height='" + CANVAS_HEIGHT + "'></canvas>");
var canvas = canvasElement.get(0).getContext("2d");
canvasElement.appendTo('body');
var fpsPosX = CANVAS_WIDTH - 60;
var fpsPosY = 20;
var scorePosX = 10;
var scorePosY = 20;
function LOG(a) {
if (DEBUG) console.log(a);
}
function update() {
playerBullets.forEach(function(bullet) {
bullet.update();
});
playerBullets = playerBullets.filter(function(bullet) {
return bullet.active;
});
enemies.forEach(function(enemy) {
enemy.update();
});
enemies = enemies.filter(function(enemy) {
return enemy.active;
});
if (Math.random() < 0.1) {
enemies.push(createEnemy());
}
handleCollisions();
}
function draw() {
canvas.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
player.draw();
canvas.fillStyle = COLOR_BLACK;
canvas.font = "12pt " + FONT;
canvas.fillText(fps.toFixed(0) + " FPS", fpsPosX, fpsPosY);
canvas.fillText("Score: " + gameManager.score, scorePosX, scorePosY);
playerBullets.forEach(function(bullet) {
bullet.draw();
});
enemies.forEach(function(enemy) {
enemy.draw();
});
}
function handleCollisions() {
playerBullets.forEach(function(bullet) {
enemies.forEach(function(enemy) {
if (collides(bullet, enemy)) {
gameManager.score += (enemy.damage(bullet.power) / 10);
if (!enemy.health) {
gameManager.kills += 1;
gameManager.score += enemy.scoreRatio;
}
bullet.active = false;
}
});
});
enemies.forEach(function(enemy) {
if (collides(enemy, player)) {
//enemy.explode();
//player.explode();
player.damage(1);
}
});
}
function ClassGameManager() {
this.score = 0;
this.kills = 0;
// manageEnemyCreation(I) <-- ref to an enemy
// save some internal states to decide:
// 1) at which level we at
// 2) which enemy should be created
// 3) which movement pattern should be applied
// 4) which weapon should be added
// 5) should we increase an amount of HP
}
function ClassCharacter() {
this.color = "#A00";
this.x = CANVAS_WIDTH / 2;
this.y = CANVAS_HEIGHT / 2;
this.width = 32;
this.height = 32;
this.health = 100;
this.hpScale = this.height / this.health;
this.bulletSpeed = 20;
this.xVelocity = 0;
this.yVelocity = 0;
this.setInitialHealth = function(hp) {
if (hp > 0) {
this.health = hp
this.hpScale = this.height / this.health;
this.scoreRatio = this.health / 10;
}
}
this.midpoint = function() {
return {
x: this.x + this.width / 2,
y: this.y + this.height / 2
};
}
this.draw = function() {
LOG("Drawing character...");
canvas.strokeStyle = COLOR_BLACK;
canvas.strokeRect(this.x, this.y, this.width, this.height);
canvas.fillStyle = this.color;
heightScale = this.height - this.hpScale * this.health;
canvas.fillRect(this.x + 1, // additional pixel to make border visible
this.y + 1 + heightScale,
this.width - 2, // sub 2 pixels to make bottom-right borders visible
this.height - 2 - heightScale);
}
this.shoot = function(mPos) {
LOG("Pew pew!");
var midpoint = player.midpoint();
distX = mPos.x - midpoint.x;
distY = mPos.y - midpoint.y;
distance = Math.sqrt(distX * distX + distY * distY);
time = distance / this.bulletSpeed;
playerBullets.push(createBullet({
xVelocity: distX / time,
yVelocity: distY / time,
x: midpoint.x,
y: midpoint.y
}));
}
this.damage = function(damage) {
var loss;
if (damage >= this.health) {
loss = this.health;
this.health = 0;
this.explode();
} else {
this.health -= damage;
loss = damage;
}
return loss;
}
this.explode = function() {
// add explode graphics
}
}
function ClassPlayer() {
ClassCharacter.apply(this);
this.xVelocity = this.yVelocity = 5;
this.handleKeys = function() {
LOG("Handling player's keys...");
if (keydown.space) {
this.shoot({x:CANVAS_WIDTH / 2, y:0});
}
if (mousedown.pressed) {
c = mousemove.coords ? mousemove.coords : mousedown.coords
this.shoot(c);
}
if (keydown.a) { this.x -= this.xVelocity; }
if (keydown.d) { this.x += this.xVelocity; }
if (keydown.w) { this.y -= this.yVelocity; }
if (keydown.s) { this.y += this.yVelocity; }
this.x = this.x.keepInBounds(0, CANVAS_WIDTH - this.width);
this.y = this.y.keepInBounds(0, CANVAS_HEIGHT - this.height);
}
}
function ClassEnemy() {
ClassCharacter.apply(this);
this.active = true;
this.isInBounds = function() {
return this.x >= 0 && this.x <= CANVAS_WIDTH &&
this.y >= 0 && this.y <= CANVAS_HEIGHT;
}
this.explode = function() {
this.active = false;
// add explode graphics
}
}
function ClassEnemyRanger() {
ClassEnemy.apply(this);
// appearance
this.width = this.height = 16;
this.color = "#FF0" // yellow
// stats
this.setInitialHealth(50);
this.xVelocity = this.yVelocity = 5;
}
function ClassEnemySolder() {
ClassEnemy.apply(this);
// appearance
this.width = this.height = 32;
this.color = "#00F"; // blue
// stats
this.setInitialHealth(150);
this.xVelocity = this.yVelocity = 2;
}
function ClassEnemyHeavySolder() {
ClassEnemy.apply(this);
// appearance
this.width = this.height = 48;
this.color = "#A2F"; // purple
// stats
this.setInitialHealth(300);
this.xVelocity = this.yVelocity = 1;
}
function createEnemy(I) { // creates an enemy with circular motion
I = I || {};
// move this to GameManger - begin
if (gameManager.kills <= KILLS_TO_NEXT_LEVEL) {
ClassEnemySolder.apply(I);
addCircleMotion(I);
}
else if (gameManager.kills <= KILLS_TO_NEXT_LEVEL * 2) {
ClassEnemySolder.apply(I);
addFallingMotion(I);
}
else if (gameManager.kills <= KILLS_TO_NEXT_LEVEL * 3) {
ClassEnemyRanger.apply(I);
addCircleMotion(I);
}
else if (gameManager.kills <= KILLS_TO_NEXT_LEVEL * 4) {
ClassEnemyRanger.apply(I);
addFallingMotion(I);
}
else if (gameManager.kills <= KILLS_TO_NEXT_LEVEL * 5) {
ClassEnemyHeavySolder.apply(I);
addFallingMotion(I);
}
else {
ClassEnemyHeavySolder.apply(I);
addSurroundingMotion(I);
}
// move this to GameManger - end
return I;
}
function createBullet(I) {
I.active = true;
I.width = 3;
I.height = 3;
I.color = COLOR_BLACK;
I.power = 50; // damage given to an enemy
I.isInBounds = function() {
return I.x >= 0 && I.x <= CANVAS_WIDTH &&
I.y >= 0 && I.y <= CANVAS_HEIGHT;
}
I.draw = function() {
canvas.fillStyle = this.color;
canvas.fillRect(this.x, this.y, this.width, this.height);
}
I.explode = function() {
I.active = false;
// add explode graphics
}
I.update = function() {
I.x += I.xVelocity;
I.y += I.yVelocity;
I.active = I.active && I.isInBounds();
}
return I;
}
function addSurroundingMotion(I) {
variant = Math.floor((Math.random() * 4) + 1);
switch (variant) {
case 1:
I.x = Math.floor((Math.random() * (CANVAS_WIDTH - I.width)) + 1);
I.y = 0;
break;
case 2:
I.x = Math.floor((Math.random() * (CANVAS_WIDTH - I.width)) + 1);
I.y = CANVAS_HEIGHT - I.height;
break;
case 3:
I.x = 0;
I.y = Math.random() * (CANVAS_HEIGHT - I.height);
break;
case 4:
I.x = CANVAS_WIDTH - I.width;
I.y = Math.random() * (CANVAS_HEIGHT - I.height);
break;
}
I.speed = I.xVelocity;
I.recalc = function() {
target = player.midpoint();
distX = target.x - I.x;
distY = target.y - I.y;
distance = Math.sqrt(distX * distX + distY * distY);
time = distance / I.speed;
I.xVelocity = distX / time;
I.yVelocity = distY / time;
}
I.update = function() {
I.recalc();
I.x += I.xVelocity;
I.y += I.yVelocity;
}
}
function addFallingMotion(I) { // creates an enemy with falling-down-maneuvering
I.age = Math.floor(Math.random() * 128);
I.x = CANVAS_WIDTH / 4 + Math.random() * CANVAS_WIDTH / 2;
I.y = 0;
I.update = function() {
I.x += I.xVelocity;
I.y += I.yVelocity;
I.xVelocity = 3 * Math.sin(I.age * Math.PI / 64);
I.age++;
I.active = I.active && I.isInBounds();
}
}
function addCircleMotion(I) {
// I.angle = I.initialAngle;
// I.angleStep ??? smooth movement to player
// I.radiusStep ??? cooperate with angleStep
// I.initialAngle = randomize it!
I.initialAngle = -Math.PI / 2;
I.angle = I.initialAngle;
I.angleStep = Math.PI / 36; // become close to player every "angleStep" degrees;
I.stepIter = 1;
I.radius = 350; // need to think about it; half of the screen
I.recalc = function() {
I.x = Math.cos(I.angle) * I.radius + CANVAS_WIDTH / 2;
I.y = Math.sin(I.angle) * I.radius + CANVAS_HEIGHT / 2;
}
I.recalc();
I.update = function() {
I.recalc();
I.angle += 0.03; // angleStep???
if (I.angle >= I.initialAngle + I.angleStep * I.stepIter) { // step-angle + initial-angle
//I.angle = -Math.PI / 2; // set to initial-angle
I.stepIter++;
I.radius -= 2; // radiusStep
if (I.radius <= 20)
I.radius = 20;
}
if (I.angle >= I.initialAnlge + 2 * Math.PI) { // ATW completed
I.angle = I.initialAngle;
I.stepIter = 1;
}
}
}
/*
canvasElement.get(0).addEventListener("click", function(e) {
e.preventDefault();
player.shoot(getMousePos(canvasElement.get(0), e));
})
/*
$(canvasElement).click(function(e) {
e.preventDefault();
player.shoot(getMousePos(canvasElement.get(0), e));
})
*/
// Game loop
setInterval(function() {
player.handleKeys();
update();
draw();
elapsed = ((now = new Date) - lastUpdate);
if (elapsed > 1000) { // update FPS value once per second
fps = (frameCounter / elapsed * 1000);
frameCounter = 0;
lastUpdate = now;
}
++frameCounter;
}, 1000/FPS_FILTER);
//}, 1); // NO FPS LIMIT!
</script>
</body>
</html>