-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
executable file
·420 lines (336 loc) · 13.2 KB
/
game.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
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
// The point and size class used in this program
function Point(x, y) {
this.x = (x)? parseFloat(x) : 0.0;
this.y = (y)? parseFloat(y) : 0.0;
}
function Size(w, h) {
this.w = (w)? parseFloat(w) : 0.0;
this.h = (h)? parseFloat(h) : 0.0;
}
// Helper function for checking intersection between two rectangles
function intersect(pos1, size1, pos2, size2) {
return (pos1.x < pos2.x + size2.w && pos1.x + size1.w > pos2.x &&
pos1.y < pos2.y + size2.h && pos1.y + size1.h > pos2.y);
}
function createMonster(x, y) {
var monster = svgdoc.createElementNS("http://www.w3.org/2000/svg", "use");
svgdoc.getElementById("monsters").appendChild(monster);
monster.setAttribute("x", x);
monster.setAttribute("y", y);
monster.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", "#monster");
}
function shootBullet() {
// Disable shooting for a short period of time
canShoot = false;
var timer = setTimeout("canShoot = true", SHOOT_INTERVAL);
// Create the bullet by createing a use node
var bullet = svgdoc.createElementNS("http://www.w3.org/2000/svg", "use");
svgdoc.getElementById("bullets").appendChild(bullet);
// Calculate and set the position of the bullet
bullet.setAttribute("x", (player.position.x + (PLAYER_SIZE.w / 2)));
bullet.setAttribute("y", (player.position.y + (PLAYER_SIZE.h / 2)));
// Set the href of the use node to the bullet defined in the defs node
bullet.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", "#bullet");
// Append the bullet to the bullet group
}
function moveBullets() {
var bullets = svgdoc.getElementById("bullets");
bulletCount = bullets.childNodes.length;
for (var i = 0; i < bulletCount; i++) {
var node = bullets.childNodes.item(i);
var x = parseInt(node.getAttribute("x"));
// Update the position of the bullet
node.setAttribute("x", x + BULLET_SPEED);
// If the bullet is not inside the screen delete it from the group
if (x > SCREEN_SIZE.w) {
svgdoc.getElementById("bullets").removeChild(node);
}
}
}
function collisionDetection() {
// Check whether the player collides with a monster
var monsters = svgdoc.getElementById("monsters");
for (var i = 0; i < monsters.childNodes.length; i++) {
var monster = monsters.childNodes.item(i);
// For each monster check if it overlaps with the player
// if yes, stop the game
var monsterPos = new Point(monster.getAttribute("x"), monster.getAttribute("y"));
if (intersect(monsterPos, MONSTER_SIZE, player.position, PLAYER_SIZE)) {
alert("You are killed!");
gameOver();
}
}
// Check whether a bullet hits a monster
var bullets = svgdoc.getElementById("bullets");
for (var i = 0; i < bullets.childNodes.length; i++) {
var bullet = bullets.childNodes.item(i);
// For each bullet check if it overlaps with any monster
// if yes, remove both the monster and the bullet
for (var i = 0; i < monsters.childNodes.length; i++) {
var monster = monsters.childNodes.item(i);
var monsterPos = new Point(monster.getAttribute("x"), monster.getAttribute("y"));
var bulletPos = new Point(bullet.getAttribute("x"), bullet.getAttribute("y"));
if (intersect(monsterPos, MONSTER_SIZE, bulletPos, BULLET_SIZE)) {
score++;
bullets.removeChild(bullet);
monsters.removeChild(monster);
}
}
}
}
function Input(){
playerName = prompt("Enter your name ");
if(playerName.length == 0)
playerName = "Anonymous";
}
function gameOver() {
clearInterval(gameInterval);
var table = getHighScoreTable();
var record = new ScoreRecord(playerName, score);//playerName, score);
for(var i = 0 ; i < 10 ;++i){
if(table.length == 0 || i == table.length || table[i].score < record.score ){
table.splice(i, 0, record);
break;
}
}
setHighScoreTable(table);
showHighScoreTable(table);
}
// The player class used in this program
function Player() {
this.node = svgdoc.getElementById("player");
this.position = PLAYER_INIT_POS;
this.motion = motionType.NONE;
this.verticalSpeed = 0;
}
Player.prototype.isOnPlatform = function() {
var platforms = svgdoc.getElementById("platforms");
for (var i = 0; i < platforms.childNodes.length; i++) {
var node = platforms.childNodes.item(i);
if (node.nodeName != "rect") continue;
var x = parseFloat(node.getAttribute("x"));
var y = parseFloat(node.getAttribute("y"));
var w = parseFloat(node.getAttribute("width"));
var h = parseFloat(node.getAttribute("height"));
if (((this.position.x + PLAYER_SIZE.w > x && this.position.x < x + w) ||
((this.position.x + PLAYER_SIZE.w) == x && this.motion == motionType.RIGHT) ||
(this.position.x == (x + w) && this.motion == motionType.LEFT)) &&
this.position.y + PLAYER_SIZE.h == y) return true;
}
if (this.position.y + PLAYER_SIZE.h == SCREEN_SIZE.h) return true;
return false;
}
Player.prototype.collidePlatform = function(position) {
var platforms = svgdoc.getElementById("platforms");
for (var i = 0; i < platforms.childNodes.length; i++) {
var node = platforms.childNodes.item(i);
if (node.nodeName != "rect") continue;
var x = parseFloat(node.getAttribute("x"));
var y = parseFloat(node.getAttribute("y"));
var w = parseFloat(node.getAttribute("width"));
var h = parseFloat(node.getAttribute("height"));
var pos = new Point(x, y);
var size = new Size(w, h);
if (intersect(position, PLAYER_SIZE, pos, size)) {
position.x = this.position.x;
if (intersect(position, PLAYER_SIZE, pos, size)) {
if (this.position.y >= y + h)
position.y = y + h;
else
position.y = y - PLAYER_SIZE.h;
this.verticalSpeed = 0;
}
}
}
}
Player.prototype.collideScreen = function(position) {
if (position.x < 0) position.x = 0;
if (position.x + PLAYER_SIZE.w > SCREEN_SIZE.w) position.x = SCREEN_SIZE.w - PLAYER_SIZE.w;
if (position.y < 0) {
position.y = 0;
this.verticalSpeed = 0;
}
if (position.y + PLAYER_SIZE.h > SCREEN_SIZE.h) {
position.y = SCREEN_SIZE.h - PLAYER_SIZE.h;
this.verticalSpeed = 0;
}
}
//
// Below are constants used in the game
//
var PLAYER_SIZE = new Size(40, 40); // The size of the player
var MONSTER_SIZE = new Size(40, 40)
var SCREEN_SIZE = new Size(600, 560); // The size of the game screen
var PLAYER_INIT_POS = new Point(0, 0); // The initial position of the player
var MOVE_DISPLACEMENT = 5; // The speed of the player in motion
var JUMP_SPEED = 15; // The speed of the player jumping
var VERTICAL_DISPLACEMENT = 1; // The displacement of vertical speed
var GAME_INTERVAL = 25; // The time interval of running the game
var BULLET_SIZE = new Size(10, 10); // The size of a bullet
var BULLET_SPEED = 10.0; // The speed of a bullet
// = pixels it moves each game loop
var SHOOT_INTERVAL = 200.0; // The period when shooting is disabled
var MAX_BULLETS_ON_SCREEN = 3;
//
// Variables in the game
//
var motionType = {NONE:0, LEFT:1, RIGHT:2}; // Motion enum
var svgdoc = null; // SVG root document node
var player = null; // The player object
var gameInterval = null; // The interval
var zoom = 1.0; // The zoom level of the screen
var canShoot = true; // A flag indicating whether the player can shoot a bullet
var bulletCount = 0;
var playerName = ""; // The name of player
var score = 0; // The score of game
//
// The load function for the SVG document
//
function load(evt) {
// Set the root node to the global variable
svgdoc = evt.target.ownerDocument;
// Attach keyboard events
svgdoc.documentElement.addEventListener("keydown", keydown, false);
svgdoc.documentElement.addEventListener("keyup", keyup, false);
// Remove text nodes in the 'platforms' group
cleanUpGroup("platforms", true);
// Create the player
player = new Player();
createMonster(100, 200);
createMonster(200, 200);
createMonster(0, 200);
createMonster(100, 0);
createMonster(0, 300);
Input();
// Start the game interval
gameInterval = setInterval("gamePlay()", GAME_INTERVAL);
}
//
// This function removes all/certain nodes under a group
//
function cleanUpGroup(id, textOnly) {
var node, next;
var group = svgdoc.getElementById(id);
node = group.firstChild;
while (node != null) {
next = node.nextSibling;
if (!textOnly || node.nodeType == 3) // A text node
group.removeChild(node);
node = next;
}
}
//
// This is the keydown handling function for the SVG document
//
function keydown(evt) {
var keyCode = (evt.keyCode)? evt.keyCode : evt.getKeyCode();
switch (keyCode) {
case "N".charCodeAt(0):
player.motion = motionType.LEFT;
break;
case "M".charCodeAt(0):
player.motion = motionType.RIGHT;
break;
// Add your code here
case "Z".charCodeAt(0):
if (player.isOnPlatform()) {
player.verticalSpeed = JUMP_SPEED;
}
break;
case 32: // space bar
if (canShoot == true && bulletCount < MAX_BULLETS_ON_SCREEN) {
shootBullet();
}
break;
}
}
//
// This is the keyup handling function for the SVG document
//
function keyup(evt) {
// Get the key code
var keyCode = (evt.keyCode)? evt.keyCode : evt.getKeyCode();
switch (keyCode) {
case "N".charCodeAt(0):
if (player.motion == motionType.LEFT) player.motion = motionType.NONE;
break;
case "M".charCodeAt(0):
if (player.motion == motionType.RIGHT) player.motion = motionType.NONE;
break;
}
}
//
// This function updates the position and motion of the player in the system
//
function gamePlay() {
// Check whether the player is on a platform
var isOnPlatform = player.isOnPlatform();
// Update player position
var displacement = new Point();
// Move left or right
if (player.motion == motionType.LEFT)
displacement.x = -MOVE_DISPLACEMENT;
if (player.motion == motionType.RIGHT)
displacement.x = MOVE_DISPLACEMENT;
// Fall
if (!isOnPlatform && player.verticalSpeed <= 0) {
displacement.y = -player.verticalSpeed;
player.verticalSpeed -= VERTICAL_DISPLACEMENT;
}
// Jump
if (player.verticalSpeed > 0) {
displacement.y = -player.verticalSpeed;
player.verticalSpeed -= VERTICAL_DISPLACEMENT;
if (player.verticalSpeed <= 0)
player.verticalSpeed = 0;
}
// Get the new position of the player
var position = new Point();
position.x = player.position.x + displacement.x;
position.y = player.position.y + displacement.y;
// Check collision with platforms and screen
player.collidePlatform(position);
player.collideScreen(position);
// Set the location back to the player object (before update the screen)
player.position = position;
updateScreen();
}
//
// This function updates the position of the player's SVG object and
// set the appropriate translation of the game screen relative to the
// the position of the player
//
function updateScreen() {
// Transform the player
player.node.setAttribute("transform", "translate(" + player.position.x + "," + player.position.y + ")");
// Calculate the scaling and translation factors
// Add your code here
var scale = new Point(zoom, zoom);
var translate = new Point();
translate.x = SCREEN_SIZE.w / 2.0 - (player.position.x - PLAYER_SIZE.w / 2) * scale.x;
if (translate.x > 0) {
translate.x = 0;
} else if (translate.x < SCREEN_SIZE.w - SCREEN_SIZE.w * scale.x) {
translate.x = SCREEN_SIZE.w - SCREEN_SIZE.w * scale.x;
}
translate.y = SCREEN_SIZE.h / 2.0 - (player.position.y - PLAYER_SIZE.h / 2) * scale.y;
if (translate.y > 0) {
translate.y = 0;
} else if (translate.y < SCREEN_SIZE.h - SCREEN_SIZE.h * scale.y) {
translate.y = SCREEN_SIZE.h - SCREEN_SIZE.h * scale.y;
}
// Update the bullet count
bulletCount = svgdoc.getElementById("bullets").childNodes.length;
/* console.log(bulletCount); */
svgdoc.getElementById("gamearea").setAttribute("transform", "translate(" + translate.x + ", " + translate.y + ") scale(" + scale.x + "," + scale.y + ")");
moveBullets();
collisionDetection();
svgdoc.getElementById("score").firstChild.data = score; // Update score in the SVG
}
function setZoom() {
if (zoom == 1.0) {
zoom++;
} else {
zoom--;
}
}