-
Notifications
You must be signed in to change notification settings - Fork 5
/
animation.html
400 lines (373 loc) · 12.4 KB
/
animation.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
<!DOCTYPE html>
<html>
<head>
<center>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>
</center>
</head>
<body style="background-color: black">
<script>
//var Swidth = screen.width;
// var Sheight = screen.height;
var Sheight = window.screen.availHeight * 0.85;
var Swidth = window.screen.availWidth * 1;
var config = {
type: Phaser.AUTO,
width: Swidth,
height: Sheight,
// game.scale.pageAlignHorizontally = true;
// game.scale.pageAlignVertically = true;
//game.scale.refresh();
physics: {
default: "arcade",
},
scene: {
preload: preload,
create: create,
update: update,
},
};
var game = new Phaser.Game(config);
var paused = 0;
//Game Objects
var platforms;
var map;
var player;
//Keyboard controls
var cursors;
var keys;
var space;
var graphics;
var acceleration = 2000;
var maxSpeed = 750;
var lastPlatformY = 800;
var highestPlayerY = Sheight;
var dead = false;
var allPlatforms = new Array();
var jumpsound;
var fallsound;
var deadmusic;
var mainMusic;
var bg;
var bg2;
var gameoverbg;
var gameovermusic;
var offset = 0;
var offset2 = 0;
var gameOverSpritesheet;
var anim;
function preload() {
//this.load.image('sky', 'https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/thumbnails%2Fbackground.png?1658781131738');
this.load.image(
"sky",
"https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/thumbnails%2FSpace%20Background(2).png?1658936337182"
);
this.load.image(
"spring",
"https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/thumbnails%2Fspring.jpg?1658953969923"
);
this.load.image(
"platform",
"https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/Platform1.png?1658934898080"
);
this.load.image(
"deathscreen",
"https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/GameOverScreen.png?v=1659033219745",
37, 45, 18
);
this.load.atlas(
"player",
"https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/mcSpriteIJump00.png?1658951644445",
"spriteJump00.json"
);
this.load.audio("mainbg", [
"https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/bgmusic.mp3?v=1658935436816",
]);
this.load.audio("jumpfx", [
"https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/jump.mp3?v=1658948463746",
]);
this.load.audio("deathbg1", [
"https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/deadmusic.mp3?v=1658868400882",
]);
}
function create() {
var bgmusic = this.sound.add("mainbg");
mainMusic = bgmusic;
mainMusic.setVolume(0.15);
mainMusic.play();
var jumpfx = this.sound.add("jumpfx");
jumpsound = jumpfx;
jumpsound.setVolume(1);
var deathbg = this.sound.add("deathbg1");
gameovermusic = deathbg;
gameovermusic.setVolume(0.3);
//Set the background origin to be at (0, 0) or top left corner of the image rather than the center of the image asset
var background = this.add
.tileSprite(
0,
-1000,
game.scale.width * 2,
game.scale.height * 2,
"sky"
)
.setOrigin(0, 0);
background.tileScaleX = 4;
background.tileScaleY = 4;
bg = background;
// var background2 = this.add
// .tileSprite(
// 0,
// -1000 - background.displayHeight,
// game.scale.width * 2,
// game.scale.height * 2,
// "sky"
// )
// .setOrigin(0, 0);
// background2.tileScaleX = 4;
// background2.tileScaleY = 4;
// bg2 = background2;
var gameover1 = this.add.sprite(0, 0, "deathscreen");
gameover1.visible = false;
gameoverbg = gameover1;
gameoverbg.setDisplaySize(Swidth, Sheight);
//create all animations
gameOverSpritesheet = this.add.sprite(200, 360, 'deathscreen', 5);
gameOverSpritesheet.smoothed = false;
anim = this.anims.create({
key: "bganimation",
frames: this.anims.generateFrameNames("deathscreen", {
prefix: "GameOverScreen",
suffix: ".png",
end: 19,
zeropad: 2,
}),
repeat: -1,
frameRate: 2,
})
anim.play(10, true);
let playerAnimations = {};
var idle = this.anims.create({
key: "player_idle",
frames: this.anims.generateFrameNames("player", {
prefix: "spriteJump",
end: 1,
zeroPad: 2,
}),
repeat: -1,
frameRate: 2,
});
playerAnimations["idle"] = idle;
var left = this.anims.create({
key: "player_left",
frames: this.anims.generateFrameNames("player", {
prefix: "spriteJumpLeft",
end: 1,
zeroPad: 2,
}),
repeat: -1,
frameRate: 2,
});
playerAnimations["left"] = left;
var right = this.anims.create({
key: "player_right",
frames: this.anims.generateFrameNames("player", {
prefix: "spriteJump",
end: 1,
zeroPad: 2,
}),
repeat: -1,
frameRate: 2,
});
playerAnimations["right"] = right;
var maxSpeed;
//Create the platforms and the player character set to collide with the platforms
startingPlatforms(this);
player = new Player(this, 400, 400, "player", playerAnimations);
this.physics.add.collider(player, platforms);
player.play(player.animations["idle"]);
//Set up user input
cursors = this.input.keyboard.createCursorKeys();
keys = this.input.keyboard.addKeys("A, D");
space = this.input.keyboard.addKey(
Phaser.Input.Keyboard.KeyCodes.SPACE
);
space.on("down", pause);
graphics = this.add.graphics();
}
function startingPlatforms(scene) {
platforms = scene.physics.add.staticGroup();
//basePlatform is the floor of the game
let basePlatform = platforms.create(
game.scale.width / 2,
game.scale.height - 30,
"platform"
);
basePlatform.setScale(8, 1).refreshBody();
while (lastPlatformY > 200) {
lastPlatformY -= 200;
let platform = platforms.create(
Math.random() * 1000,
lastPlatformY,
"platform"
);
platform.body.checkCollision.down = false;
platform.body.checkCollision.right = false;
platform.body.checkCollision.left = false;
platform.body.checkCollision.up = true;
platform.setScale(0.3, 0.3).refreshBody();
allPlatforms.push(platform);
}
}
function generatePlatform(scene) {
platforms = scene.physics.add.staticGroup();
lastPlatformY -= 200;
let platform = platforms.create(
Math.random() * Swidth,
lastPlatformY,
"platform"
);
platform.body.checkCollision.down = false;
platform.body.checkCollision.right = false;
platform.body.checkCollision.left = false;
platform.body.checkCollision.up = true;
platform.setScale(0.3, 0.3).refreshBody();
allPlatforms.push(platform);
}
function gameOver() {
dead = true;
console.log("Game Over!!!! :(");
gameoverbg.setPosition(Swidth / 2, highestPlayerY + Sheight / 2);
gameoverbg.visible = true;
console.log("playing dead music");
mainMusic.stop();
gameovermusic.play();
}
function update() {
/*if (bg.displayOriginY > player.body.position.y + Sheight / 2) {
offset += game.scale.height * 2;
console.log(offset);
}
if (bg2.displayOriginY > player.body.position.y + Sheight / 2) {
offset2 += game.scale.height * 2;
}*/
console.log(bg.originY);
if (allPlatforms.length > 10) {
while (allPlatforms.length > 10) {
allPlatforms[0].destroy();
allPlatforms.shift();
}
}
if (player.body.position.y < highestPlayerY) {
highestPlayerY = player.body.position.y;
}
if (player.body.position.y < highestPlayerY + Sheight / 2) {
bg.displayOriginY = -player.body.position.y - 400;
bg.tilePositionY = player.body.position.y * 0.1;
this.cameras.main.centerOnY(player.body.position.y);
}
if (
player.body.position.y > highestPlayerY + Sheight / 2 + 500 &&
dead != true
) {
for (let i = 0; i < allPlatforms.length; i++) {
allPlatforms[i].destroy();
}
gameOver();
player.disableBody();
}
//make platforms
if (player.body.position.y < lastPlatformY + Sheight + 200) {
while (player.body.position.y < lastPlatformY + Sheight + 200) {
generatePlatform(this);
this.cameras.main.y;
this.physics.add.collider(player, platforms);
}
}
this.physics.add.collider(player, platforms);
// console.log(paused);
//console.log(player.x);
//Player will not move in the x-axis unless a movement key is being pressed
//player.setVelocityX(0);
//Player has "drag" on the x-axis meaning they slide a bit after an input
player.setDragX(1000);
//Handle player movements
if (cursors.left.isDown || keys.A.isDown) {
if (player.body.velocity.x > -maxSpeed) {
player.setAccelerationX(-acceleration);
if (player.angle >= -90) {
player.angle -= 1;
}
} else {
player.setVelocityX(-maxSpeed);
}
player.play(player.animations["left"], true);
} else if (cursors.right.isDown || keys.D.isDown) {
player.setAccelerationX(acceleration);
if (player.body.velocity.x > maxSpeed) {
player.body.velocity.x = maxSpeed;
}
if (player.angle <= 90) {
player.angle++;
}
player.play(player.animations["right"], true);
} else {
if (player.angle < 0) {
player.angle += 4;
}
if (player.angle > 0) {
player.angle -= 4;
}
player.setAccelerationX(0);
player.play(player.animations["idle"], true);
}
if (player.x < 0) {
player.setPosition(Swidth, player.y);
}
if (player.x > Swidth) {
player.setPosition(0, player.y);
}
//let bounds = player.getBounds();
//graphics.clear();
//graphics.lineStyle(1, 0xff0000);
//graphics.strokeRectShape(bounds);
if (player.body.touching.down) {
jump();
}
}
function pause() {
paused = paused + 1;
if (paused == 1) {
player.disableBody();
}
if (paused == 2) {
paused = 0;
player.enableBody();
}
//let pauseX = player.x;
//let pauseY = player.y;
//player.setAccelerationX(0);
//player.setPosition(pauseX,pauseY);
//player.setVelocityY(0);
//console.log(pauseX)
}
function jump(event) {
if (player.body.touching.down) {
jumpsound.play();
player.setVelocityY(-1150);
}
}
class Player extends Phaser.Physics.Arcade.Sprite {
animations;
constructor(scene, x, y, spritesheet, animations) {
super(scene, x, y, spritesheet);
this.animations = animations;
scene.add.existing(this);
scene.physics.add.existing(this);
this.setScale(2);
this.setCollideWorldBounds(false);
this.setGravityY(1000);
}
}
</script>
</body>
</html>