-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
executable file
·437 lines (361 loc) · 10.6 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//Aliases
var Container = PIXI.Container,
autoDetectRenderer = PIXI.autoDetectRenderer,
loader = PIXI.loader,
resources = PIXI.loader.resources,
TextureCache = PIXI.utils.TextureCache,
Texture = PIXI.Texture,
Sprite = PIXI.Sprite,
Text = PIXI.Text,
Graphics = PIXI.Graphics;
//Create a Pixi stage and renderer and add the
//renderer.view to the DOM
var stage = new Container(),
renderer = autoDetectRenderer(512, 512);
var b=new Bump(PIXI);
var c = new Charm(PIXI);
document.body.appendChild(renderer.view);
/*
Working with sound files
=================================
*/
//Load the sounds
sounds.load([
"sounds/overworld_L.ogg"
]);
///
loader
.add("images/indyAdventure.json")
.load(setup);
//Define variables that might be used in more
//than one function
var state, explorer, treasure, blobs, chimes, exit, dungeon,
door, healthBar, message, gameScene, gameOverScene, id, hasTreasure = false;;
function setup() {
//Make the game scene and add it to the stage
gameScene = new Container();
stage.addChild(gameScene);
sounds.whenLoaded=setupSound;
//Make the sprites and add them to the `gameScene`
//Create an alias for the texture atlas frame ids
id = resources["images/indyAdventure.json"].textures;
//Dungeon
dungeon = new Sprite(id["dungeon.png"]);
gameScene.addChild(dungeon);
//Door
door = new Sprite(id["door.png"]);
door.position.set(32, 0);
gameScene.addChild(door);
//Explorer
explorer = new Sprite(id["explorerFin.png"]);
explorer.x = 68;
explorer.y = gameScene.height / 2 - explorer.height / 2;
explorer.vx = 0;
explorer.vy = 0;
gameScene.addChild(explorer);
//Treasure
treasure = new Sprite(id["treasure.png"]);
treasure.x = gameScene.width - treasure.width - 48;
treasure.y = gameScene.height / 2 - treasure.height / 2;
gameScene.addChild(treasure);
//Make the blobs
var numberOfBlobs = 7,
spacing = 48,
xOffset = 150,
speed = 2,
direction = 1;
//An array to store all the blob monsters
blobs = [];
//Make as many blobs as there are `numberOfBlobs`
for (var i = 0; i < numberOfBlobs; i++) {
//Make a blob
var blob = new Sprite(id["blob.png"]);
//Space each blob horizontally according to the `spacing` value.
//`xOffset` determines the point from the left of the screen
//at which the first blob should be added
var x = spacing * i + xOffset;
//Give the blob a random y position
var y = randomInt(0, stage.height - blob.height);
//Set the blob's position
blob.x = x;
blob.y = y;
//Set the blob's vertical velocity. `direction` will be either `1` or
//`-1`. `1` means the enemy will move down and `-1` means the blob will
//move up. Multiplying `direction` by `speed` determines the blob's
//vertical direction
blob.vy = speed * direction;
//Reverse the direction for the next blob
direction *= -1;
//Push the blob into the `blobs` array
blobs.push(blob);
//Add the blob to the `gameScene`
gameScene.addChild(blob);
}
//Create the health bar
healthBar = new Container();
healthBar.position.set(stage.width - 170, 6)
gameScene.addChild(healthBar);
//Create the black background rectangle
//drawRoundedRect(x, y, width, height, cornerRadius)
//The last argument, cornerRadius is a number in pixels that determines by how much the corners should be rounded.
var innerBar = new Graphics();
innerBar.beginFill(0x000000);
innerBar.drawRect(0, 0, 128, 8);
innerBar.endFill();
healthBar.addChild(innerBar);
//Create the front red rectangle
var outerBar = new Graphics();
outerBar.beginFill(0xFF2000);
outerBar.drawRect(0, 0, 128, 8);
outerBar.endFill();
healthBar.addChild(outerBar);
healthBar.outer = outerBar;
//Create the `gameOver` scene
gameOverScene = new Container();
//slide trans
gameOverScene.x=renderer.width;
stage.addChild(gameOverScene);
//Make the `gameOver` scene invisible when the game first starts
gameOverScene.visible = false;
//Create the text sprite and add it to the `gameOver` scene
message = new Text(
"The End!",
{font: "64px Futura", fill: "white"}
);
message.x = 120;
message.y = stage.height / 2 - 32;
gameOverScene.addChild(message);
//Capture the keyboard arrow keys
var left = keyboard(37),
up = keyboard(38),
right = keyboard(39),
down = keyboard(40);
//Left arrow key `press` method
left.press = function() {
//Change the explorer's velocity when the key is pressed
explorer.vx = -5;
explorer.vy = 0;
};
//Left arrow key `release` method
left.release = function() {
//If the left arrow has been released, and the right arrow isn't down,
//and the explorer isn't moving vertically:
//Stop the explorer
if (!right.isDown && explorer.vy === 0) {
explorer.vx = 0;
}
};
//Up
up.press = function() {
explorer.vy = -5;
explorer.vx = 0;
};
up.release = function() {
if (!down.isDown && explorer.vx === 0) {
explorer.vy = 0;
}
};
//Right
right.press = function() {
explorer.vx = 5;
explorer.vy = 0;
};
right.release = function() {
if (!left.isDown && explorer.vy === 0) {
explorer.vx = 0;
}
};
//Down
down.press = function() {
explorer.vy = 5;
explorer.vx = 0;
};
down.release = function() {
if (!up.isDown && explorer.vx === 0) {
explorer.vy = 0;
}
};
//Set the game state
state = play;
//Start the game loop
gameLoop();
}
function gameLoop(){
//Loop this function 60 times per second
requestAnimationFrame(gameLoop);
//Update the current game state
state();
c.update();
//Render the stage
renderer.render(stage);
}
function play() {
//use the explorer's velocity to make it move
explorer.x += explorer.vx;
explorer.y += explorer.vy;
//Contain the explorer inside the area of the dungeon
b.contain(explorer, {x: 28, y: 10, width: 488, height: 480});
//contain(explorer, stage);
//Set `explorerHit` to `false` before checking for a collision
var explorerHit = false;
//Loop through all the sprites in the `enemies` array
blobs.forEach(function(blob) {
//Move the blob
blob.y += blob.vy;
//Check the blob's screen boundaries
/*The difference is scoping-LET-. var is scoped to the nearest function block
and let is scoped to the nearest enclosing block (both are global if outside
any block), which can be smaller than a function block. */
let blobHitsWall = b.contain(blob, {x: 28, y: 10, width: 488, height: 480});
//If the blob hits the top or bottom of the stage, reverse
//its direction
if(blobHitsWall) {
if (blobHitsWall.has("top") || blobHitsWall.has("bottom")) {
blob.vy *= -1;
}
}
//Test for a collision. If any of the enemies are touching
//the explorer, set `explorerHit` to `true`
if(b.hitTestRectangle(explorer, blob)) {
explorerHit = true;
}
});
//If the explorer is hit...
if(explorerHit) {
//Make the explorer semi-transparent
explorer.alpha = 0.5;
//console.log(hasTreasure);
//Reduce the width of the health bar's inner rectangle by 1 pixel
healthBar.outer.width -= 1;
if(hasTreasure){
//console.log("here");
treasure.x= explorer.x+38;
treasure.y= explorer.y+10;
b.contain(treasure, {x: 28, y: 10, width: 500, height: 480});
hasTreasure=false;
}
explosionSound();
} else {
//Make the explorer fully opaque (non-transparent) if it hasn't been hit
explorer.alpha = 1;
}
//Check for a collision between the explorer and the treasure
if (b.hitTestRectangle(explorer, treasure)) {
hasTreasure=true;
//If the treasure is touching the explorer, center it over the explorer
treasure.x = explorer.x + 8;
treasure.y = explorer.y + 20;
}
//Does the explorer have enough health? If the width of the `innerBar`
//is less than zero, end the game and display "You lost!"
if (healthBar.outer.width < 0) {
state = end;
message.text = "You lost!";
}
//If the explorer has brought the treasure to the exit,
//end the game and display "You won!"
if (b.hitTestRectangle(treasure, door)) {
state = end;
bonusSound();
message.text = "You won!";
}
}
function end() {
gameOverScene.visible = true;
c.slide(gameOverScene, 0, 0);
c.slide(gameScene, -renderer.width, 0);
gameScene.visible = false;
}
/* Helper functions */
//The `randomInt` helper function
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
//The `keyboard` helper function
function keyboard(keyCode) {
var key = {};
key.code = keyCode;
key.isDown = false;
key.isUp = true;
key.press = undefined;
key.release = undefined;
//The `downHandler`
key.downHandler = function(event) {
if (event.keyCode === key.code) {
if (key.isUp && key.press) key.press();
key.isDown = true;
key.isUp = false;
}
event.preventDefault();
};
//The `upHandler`
key.upHandler = function(event) {
if (event.keyCode === key.code) {
if (key.isDown && key.release) key.release();
key.isDown = false;
key.isUp = true;
}
event.preventDefault();
};
//Attach event listeners
window.addEventListener(
"keydown", key.downHandler.bind(key), false
);
window.addEventListener(
"keyup", key.upHandler.bind(key), false
);
return key;
}
//The sound effect functions
function setupSound() {
//Create the sounds
var music = sounds["sounds/overworld_L.ogg"];
//Make the music loop
music.loop = true;
//Set the pan
music.pan = 0;
//Set the music volume
music.volume = 0.7;
//Capture the keyboard events
music.play();
var p = keyboard(80),
m = keyboard(77);
//Control the sounds based on which keys are pressed
//Play the loaded music sound
p.press = function() {
if (!music.playing) {
music.play();
}
}
//Pause the music
m.press = function() {
music.pause();
};
}
//The explosion sound
function explosionSound() {
soundEffect(
16, //frequency
0, //attack
1, //decay
"sawtooth", //waveform
.5, //volume
0, //pan
0, //wait before playing
0, //pitch bend amount
false, //reverse
0, //random pitch range
50, //dissonance
undefined, //echo: [delay, feedback, filter]
undefined //reverb: [duration, decay, reverse?]
);
}
//The bonus points sound
function bonusSound() {
//D
soundEffect(587.33, 0, 0.2, "square", 1, 0, 0);
//A
soundEffect(880, 0, 0.2, "square", 1, 0, 0.1);
//High D
soundEffect(1174.66, 0, 0.3, "square", 1, 0, 0.2);
}