-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat.html
399 lines (335 loc) · 11.8 KB
/
cat.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Cat Adventure</title>
<script type="text/javascript" src="js/phaser.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>
<style type="text/css">
body {
margin: 0;
width:825px;
margin-left:auto;
margin-right:auto;
}
.Login{
position: absolute;
top:10px;
right:25px;
margin: auto;
}
.AfterLogIn{
position: absolute;
top:10px;
right:45px;
margin: auto;
}
.Instruction{
position: absolute;
top: 10px;
left: 5px;
margin: auto;
}
</style>
</head>
<body>
<div class="Login" id="Login">
<label for="username">Username</label>
<input type="text" name="username" id="username"/> <br><br>
<label for="password">Password</label>
<input type="password" name="password" id="password"/> <br><br>
<button id="login_btn">Log In</button> <br> <br>
<button id="register_btn">Register</button> <br>
</div>
<div class="AfterLogIn" id="AfterLogIn">
<p id="ShowUser"> <br></p>
<button id="logout_btn" hidden="hidden">Log Out</button> <br>
<label id="scorePrompt" hidden="hidden">Your highest score: </label>
<div id="score" hidden="hidden"> </div>
<div id="gameOver" hidden="hidden"> </div>
<button id="sync_btn" hidden="hidden">Sync my score</button> <br>
</div>
<div class="Instruction" id="Instruction">
<ul>
<li>Use ← and → to control the cat's direction and use ↑ <br>
to jump. </li><br>
<li>You can only jump if you touch a block.</li><br>
<li>If the cat exits the screen from the border <br>
on the left, right or bottom, you will lose <br>
a life. When you don't have any life left, <br>
the game is over.</li><br>
<li>If the cat eats a star, you will get 10 points. </li><br>
<li>If the cat eats a diamond, you will gain <br>
an extra life.</li><br>
<li>If the cat reaches the bubble, you will go to <br>
the next level.</li><br>
<li>You can register or log in. After you log in, <br>
you will see you highest score. If you are a <br>
new user, your current highest score will <br>
automatically be 0 when the game starts.</li><br>
<li>After the game is over, click "sync my score"<br>
to synchronize your score to the database. <br>
If you click this button when the game is not<br>
over, your score will not be synchronized.</li><br>
</ul>
</div>
<script type="text/javascript" src="user/Logout.js"></script>
<script type="text/javascript" src="user/cat_user.js"></script>
<script type="text/javascript"> //To be separated from the main html file
// GameItem class extending Phaser.Sprite
GameItem = function (x, y, itemType) {
Phaser.Sprite.call(this, game, x, y, itemType);
this.itemType = itemType;
items.add(this);
game.physics.arcade.enable(this);
this.body.gravity.y = 300;
this.body.bounce.y = 0.7 + Math.random() * 0.2;
};
GameItem.prototype = Object.create(Phaser.Sprite.prototype);
GameItem.prototype.constructor = GameItem;
// Variables needed for preload.
var game = new Phaser.Game(825, 600, Phaser.AUTO, '', { preload: preload, create: load, update: update });
var totalLevels;
// First stage in Phaser, load resources from server.
function preload() {
game.load.json('leveldata', 'assets/leveldata.json');
game.load.image('bg', 'assets/bg.png');
game.load.image('block', 'assets/block.png');
game.load.image('star', 'assets/star.png');
game.load.image('diamond', 'assets/diamond.png');
game.load.image('bubble', 'assets/bubble.png');
game.load.spritesheet('dude', 'assets/cats.png', 25, 25);
//game.load.image('dude', 'assets/cat.png', 28, 28);
}
// Display objects
var player;
var layer;
var bg;
var items;
var door;
var map;
// Input means
var cursors;
// Game status
var doorReached = false;
var doorDelay = 0;
// Game process
var currentLevel = 1;
var score = 0;
var scoreText;
var lifeText;
// Data
var phaserJSON;
var gameOver = false;
// Second stage in phaser, load more map files according to leveldata.JSON
function load(){
// Read file names from the main JSON file containing level names.
phaserJSON = game.cache.getJSON('leveldata');
var data = phaserJSON.leveldata;
totalLevels = data.length;
// Load the corresponding files for maps.
for (var i = 1; i <= totalLevels; i++){
// e. g. Load 'assets/map-hearts.json' for "name": hearts in leveldata.JSON
var file = 'assets/map-' + phaserJSON.leveldata[i-1].name + '.json';
game.load.tilemap('level' + i, file, null, Phaser.Tilemap.TILED_JSON);
}
// Manually start loading after preload.
game.load.start();
// Turn on Physics.
game.physics.startSystem(Phaser.Physics.ARCADE);
game.physics.arcade.isPaused = true;
// Here is when update() starts to loop because create() is completed.
// Therefore we use bool variable isPaused to control whether update() should do its thing.
// When finished loading all maps, call create().
game.load.onLoadComplete.add(create, this);
}
// Load the rest of stuffs for the game.
function create() {
// Background image.
bg = game.add.sprite(0, 0, 'bg');
// Add map.
loadMap ();
// Add player and door.
player = game.add.sprite(phaserJSON.leveldata[currentLevel-1].player_x, phaserJSON.leveldata[currentLevel-1].player_y, 'dude');
door = game.add.sprite(phaserJSON.leveldata[currentLevel-1].door_x, phaserJSON.leveldata[currentLevel-1].door_y, 'bubble');
// Set player's animations for walking.
player.scale.setTo(0.82, 0.82);
player.animations.add('left', [0, 1], 10, true);
player.animations.add('right', [3, 4], 10, true);
// Add Texts.
player.health = 9;
scoreText = game.add.text(game.width - 30 - 100, 40, 'Score: 0', { fontSize: '24px', fill: '#000' });
lifeText = game.add.text(game.width - 30 - 100, 10, 'Life: 9', { fontSize: '24px', fill: '#000' });
// Set input controls.
cursors = game.input.keyboard.createCursorKeys();
// Set physics properties for items.
game.physics.arcade.enable(player);
game.physics.arcade.enable(door);
player.body.bounce.y = 0.25;
player.body.gravity.y = 300;
// Start the game.
game.physics.arcade.isPaused = false;
}
// Third stage in Phaser, update positions of objects and render properly on screen.
function update() {
// Update nothing if game has not yet started.
if (game.physics == null) return;
if (game.physics.arcade.isPaused) return;
// Collision checks.
var hitLayer;
game.physics.arcade.collide(items, layer);
game.physics.arcade.overlap(player, items, collectItem, null, this);
hitLayer = game.physics.arcade.collide(player, layer);
// Add drag force to the player.
player.body.velocity.x = 0;
if (cursors.left.isDown) // Move to the left
{
player.body.velocity.x = -117;
player.animations.play('left');
}
else if (cursors.right.isDown) // Move to the right
{
player.body.velocity.x = 117;
player.animations.play('right');
}
else // Stand still
{
player.animations.stop();
player.frame = 2;
}
// Allow the player to jump if they are touching the blocks (layer) or the platform.
if (cursors.up.isDown && hitLayer)
{
player.body.velocity.y = -200;
}
// Player dies while falling out of screen area.
if (player.y > game.world.height && player.health >= 0) {
playerDies();
}
// Items disappears while falling out of screen area.
for (var i = 0; i < items.children.length; i++)
{
var item = items.children[i];
if (item.y > game.world.height){
item.destroy();
}
}
// Stop walking if in air
if (!hitLayer){
player.animations.stop();
}
// Check if the player reaches the door
game.physics.arcade.overlap(player, door, reachDoor, radiusCheck, this);
// We use bool variable doorReached to delay several loops for rendering.
// Simply calling nextLevel() will result in display of player still far from door.
if (doorReached) {
doorDelay ++;
if (doorDelay>30){
doorReached = false;
doorDelay = 0;
nextLevel();
} else {
doorDelay ++;
}
}
updateScore();
if (player.health < 0){
gameOver = true;
game.physics.arcade.isPaused = true;
document.getElementById("gameOver").innerHTML = gameOver;
console.log(score);
document.getElementById("score").innerHTML = score;
}
}
// Based on currentLevel, load blocks from the coresponding map.
function loadMap () {
// Add blocks as a tile sprite.
map = game.add.tilemap('level' + currentLevel);
map.addTilesetImage('block');
map.setCollisionByExclusion([ 13, 14, 15, 16, 46, 47, 48, 49, 50, 51 ]);
layer = map.createLayer('Tile Layer 1');
//layer.fixedToCamera = false;
//layer.x=30;
// Load additional items.
items = game.add.group();
for (var i = 0; i < 12; i++)
{
new GameItem(i * 60 + 30, 0, 'star');
}
// Load more items based on leveldata.
if (!(phaserJSON.leveldata[currentLevel-1].items==null)){
for (i=0; i < phaserJSON.leveldata[currentLevel-1].items.length; i++){
var x = phaserJSON.leveldata[currentLevel-1].items[i].item_x;
var y = phaserJSON.leveldata[currentLevel-1].items[i].item_y;
new GameItem(x, y, phaserJSON.leveldata[currentLevel-1].items[i].type);
}
}
}
// Called to collect an item when player hits it.
function collectItem (player, item) {
if(item.itemType == 'star'){
score += 10;
scoreText.text = 'Score: ' + score;
} else if (item.itemType == 'diamond'){
player.health ++;
lifeText.text = 'Life: ' + player.health;
}
item.destroy();
}
// Called when player touches the door.
function reachDoor (player, door){
doorReached = true;
}
// Clean the old map and load a new one.
function nextLevel () {
alert("Good job! ");
// If it's the last level, don't load.
if (currentLevel >= totalLevels){
alert("Completed all levels! ");
game.physics.arcade.isPaused = true;
gameOver = true;
document.getElementById("gameOver").innerHTML = gameOver;
return;
}
// Clean up the last level.
layer.destroy();
items.removeAll(); //soft destroy
// Load next level.
currentLevel +=1;
loadMap ();
player.reset(phaserJSON.leveldata[currentLevel-1].player_x, phaserJSON.leveldata[currentLevel-1].player_y, player.health);
door.reset(phaserJSON.leveldata[currentLevel-1].door_x, phaserJSON.leveldata[currentLevel-1].door_y, player.health, door.health);
// Start the game.
game.physics.arcade.isPaused = false;
}
// Called when the player dies.
function playerDies(){
// Put player back in the original place and recalculate player's health.
player.reset(phaserJSON.leveldata[currentLevel-1].player_x, phaserJSON.leveldata[currentLevel-1].player_y, player.health-1);
// Update player's health.
lifeText.text = 'Life: ' + player.health;
}
// Model two objects as circles and return true if they overlap.
// Modified from http://www.wittern.net/articles/gameplay_implementation_collision.html
function radiusCheck(body1, body2){
var radius1 = body1.width*0.45;
var radius2 = body2.width*0.45;
if(Math.abs(body1.x - body2.x) < radius1 + radius2 &&
Math.abs(body1.y - body2.y) < radius1 + radius2){
var distance = game.physics.arcade.distanceBetween(body1, body2);
if (distance < (radius1 + radius2) * (radius1 + radius2)){
return true;
}
}
return false;
}
// Update the highest score of the user.
function updateScore(){
var currentHighest = document.getElementById("score").innerHTML;
if (score > currentHighest){
document.getElementById("score").innerHTML = score;
}
}
</script>
</body>
</html>