-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
440 lines (355 loc) · 13.7 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
438
439
440
var JUMP = 390; // jump height
var ASSET_VERSION = (new Date()).getTime();
var BASE_PATH = '';
var VELOCITY = 330; // speed left-right
var DISTANCE = 150;
WebFontConfig = {
//active: function() { game.time.events.add(Phaser.Timer.SECOND, createText, this); },
google: {
families: ['Revalia']
}
};
var state = {
preload: function() {
this.game.load.script('webfont', 'https://ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js');
this.load.image('redpixel', BASE_PATH + 'assets/redpixel.png?' + ASSET_VERSION, 800, 8);
this.load.image('santa-idle', 'assets/santa-idle.png');
this.load.image('gameover', 'assets/GameOverBild02.png');
this.load.image('background', 'assets/background03.png');
this.load.image('santa-jump1', 'assets/santa-jump01.png');
this.load.image('santa-jump2', 'assets/santa-jump02.png');
this.load.image('santa-jump3', 'assets/santa-jump03.png');
this.load.image('platform1', 'assets/present01.png');
this.load.image('platform2', 'assets/present02.png');
this.load.image('platform3', 'assets/present03.png');
this.load.image('platform4', 'assets/present04.png');
this.load.image('powerup', 'assets/squirrel.png');
this.load.image('ground', 'assets/ground02.png');
},
start: function() {
this.isGameOver = false;
this.isPlaying = true;
this.count = 0;
this.countPowerUp = 0;
this.score = 0;
this.currentJump = 0;
this.nextPowerUpScore = 500;
},
addBackground: function() {
var background = this.backgrounds.create(0, - 800 * this.backgroundCount , 'background');
background.alpha = 0.5;
this.backgroundCount++;
},
create: function() {
this.backgroundCount = 0;
this.backgrounds = this.add.group();
this.addBackground();
this.isGameOver = false;
this.isPlaying = false;
// interface
this.addScoreText();
// Stage
this.stage.backgroundColor = '#404389';
// Scaling
this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
this.scale.maxWidth = this.game.width;
this.scale.maxHeight = this.game.height;
this.scale.pageAlignHorizontally = true;
this.scale.pageAlignVertically = true;
//this.scale.setScreenSize( true );
this.game.physics.startSystem(Phaser.Physics.ARCADE);
// camera and platform tracking vars
this.cameraYMin = 99999;
this.platformYMin = 99999;
this.floor = this.add.sprite(0, this.world.height - 10, 'redpixel');
this.game.physics.enable(this.floor);
this.floor.body.immovable = true;
// Create all platforms
this.powerUps = this.add.group();
this.platformsCreate();
this.createPlayer();
this.cursor = this.input.keyboard.createCursorKeys();
},
addScoreText: function() {
this.scoreText = this.add.text(
5,
5,
"",
{
//fill: '#ffdd00',
}
);
this.scoreText.font = 'Revalia';
this.scoreText.anchor.setTo(0, 0);
this.scoreText.fontSize = 24;
this.scoreText.fixedToCamera = true;
grd = this.scoreText.context.createLinearGradient(0, 0, 0, this.scoreText.canvas.height);
grd.addColorStop(0, '#ffdd00');
grd.addColorStop(1, '#ff2211');
this.scoreText.fill = grd;
this.scoreText.setShadow(-3, 3, 'rgba(0,0,0,0.5)', 0);
this.scoreText.fixedToCamera = true;
},
calculateScore: function() {
this.score = Math.round(this.player.yChange);
},
onCollide: function(platform, player) {
// only jump when falling.
if (this.player.body.velocity.y > -1 && player.y > platform.y + 10) {
this.playerJump();
}
},
launchPowerUp: function() {
this.createPowerUp();
},
checkPowerUp: function() {
if (this.score >= this.nextPowerUpScore) {
this.launchPowerUp();
this.nextPowerUpScore = this.rnd.integerInRange(this.score + 600, this.score + 1400);
}
},
displayJumpImage: function() {
this.currentJump++;
if (this.currentJump > 3) {
this.currentJump = 1;
}
this.player.loadTexture('santa-jump'+this.currentJump);
},
onPowerUpCollide: function(a, b) {
velocity = this.player.body.velocity.y - 800;
this.player.body.velocity.y = Math.min(2000, this.player.body.velocity.y - 800);
this.game.camera.shake(0.02, 500);
b.kill();
},
renderBackground: function() {
var maxY = 0;
this.backgrounds.forEachAlive(function( elem ) {
if (elem.y < maxY) {
maxY = elem.y;
}
if( elem.y > this.camera.y + this.game.height + 10) {
elem.kill();
}
}, this );
if (maxY + 500 > this.score * -1 ) {
this.addBackground();
}
},
update: function() {
this.calculateScore();
this.renderBackground();
// check if a new powerup is launched
this.checkPowerUp();
//this.background.cameraOffset = this.player.yChange;
//this.background.offsetY = this.player.yChange;
//this.background.tilePosition.y = this.player.yChange;
this.scoreText.setText("SCORE: "+this.score);
this.physics.arcade.collide(this.player, this.platforms, this.onCollide.bind(this));
this.physics.arcade.collide(this.player, this.powerUps, this.onPowerUpCollide.bind(this));
if (!this.isPlaying) {
if(this.isGameOver) {
this.animateGameOver();
}
if(this.game.input.keyboard.isDown(Phaser.Keyboard.UP)) {
if (this.isGameOver) {
this.shutdown();
this.create();
}
this.start();
}
return;
}
// this is where the main magic happens
// the y offset and the height of the world are adjusted
// to match the highest point the hero has reached
this.world.setBounds( 0, -this.player.yChange, this.world.width, this.game.height + this.player.yChange );
// the built in camera follow methods won't work for our needs
// this is a custom follow style that will not ever move down, it only moves up
this.cameraYMin = Math.min( this.cameraYMin, this.player.y - this.game.height + 300 );
this.camera.y = this.cameraYMin;
// hero collisions and movement
this.playerMove();
this.movePlatforms();
// for each plat form, find out which is the highest
// if one goes below the camera view, then create a new one at a distance from the highest one
// these are pooled so they are very performant
this.platforms.forEachAlive( function( elem ) {
this.platformYMin = Math.min( this.platformYMin, elem.y );
if( elem.y > this.camera.y + this.game.height ) {
elem.kill();
// at least 1, max 6
var isMovingPlatform = this.rnd.integerInRange(1,10) > Math.min(6, Math.max(1, 30000 / this.score));
this.createPlatform( this.rnd.integerInRange( 0, this.world.width - 50 ), this.platformYMin - DISTANCE, isMovingPlatform);
}
}, this );
this.rotatePowerUps();
},
movePlatforms: function() {
this.platforms.forEachAlive(function(elem) {
if(elem.isMoving) {
// Speed is at least 2, max 10
var speed = Math.min(Math.max(2, (this.score / 1000) -1), 10);
// speed variation +-1
speed += this.rnd.integerInRange(-1,1);
if (elem.moveDirection == 'left') {
speed *= -1;
}
elem.x += speed;
if (elem.moveDirection == 'left' && elem.x < 5) {
elem.moveDirection = 'right';
} else if (elem.moveDirection == 'right' && elem.x > this.world.width - 55) {
elem.moveDirection = 'left';
}
}
}.bind(this));
},
rotatePowerUps: function() {
this.powerUps.forEachAlive(function(elem){
if( elem.y > this.camera.y + this.game.height ) {
elem.kill;
}
elem.angle += 2;
}.bind(this));
},
shutdown: function() {
// reset everything, or the world will be messed up
this.world.setBounds( 0, 0, this.game.width, this.game.height );
this.cursor = null;
this.player.destroy();
this.player = null;
this.platforms.destroy();
this.platforms = null;
this.powerUps.destroy();
this.powerUps = null;
this.scoreText.destroy();
this.gameOverScreen.destroy();
},
getRandX() {
return this.rnd.integerInRange( 0, this.world.width - 50);
},
platformsCreate: function() {
this.platforms = this.add.group();
this.platforms.enableBody = true;
this.platforms.createMultiple(10, 'redpixel');
// Floor
this.platformsCreateOne( -16, this.world.height - 16, this.world.width + 16 );
var width = this.world.width;
var ratio = 4.70;
var height = width / ratio;
var ground = this.add.sprite(0, this.world.height - height, 'ground');
ground.width = width;
ground.height = height;
// create a batch of platforms that start to move up the level
for( var i = 0; i < 9; i++ ) {
this.createPlatform( this.rnd.integerInRange( 0, this.world.width - 50 ), this.world.height - DISTANCE - DISTANCE * i, false);
}
},
platformsCreateOne: function( x, y, width) {
var platform = this.platforms.getFirstDead();
platform.reset( x, y );
platform.scale.x = width;
platform.scale.y = 16;
platform.body.immovable = true;
this.game.debug.body(platform);
return platform;
},
createPlatform: function(x, y, isMoving) {
var platform = this.platforms.create(
x, y, 'platform' + this.rnd.integerInRange(1, 4)
);
platform.scale.x = 0.1
platform.scale.y = 0.1;
platform.body.immovable = true;
// Moveparameter.
platform.isMoving = isMoving;
var direction = this.rnd.integerInRange(0,1);
platform.moveDirection = direction == 1 ? 'left' : 'right';
return platform;
},
createPowerUp: function() {
var powerUp = this.powerUps.create(this.getRandX(), this.camera.y, 'powerup');
powerUp.width = 40;
powerUp.height = 40;
//powerUp.anchor.set( 0.5 );
this.physics.arcade.enable( powerUp );
powerUp.body.gravity.y = 400;
this.player.body.checkCollision.right = false;
},
createPlayer: function() {
this.player = game.add.sprite(this.world.centerX, this.world.height - 66, 'santa-idle');
this.player.anchor.set( 0.5 );
// track distance
this.player.yOrig = this.player.y;
this.player.yChange = 0;
this.physics.arcade.enable( this.player );
this.player.body.gravity.y = 500;
this.player.body.checkCollision.up = false;
this.player.body.checkCollision.left = false;
this.player.body.checkCollision.right = false;
this.player.width = 100;
this.player.height = 90;
},
playerJump: function() {
if (this.isPlaying) {
this.player.body.velocity.y = -JUMP;
this.isJumping = true;
this.displayJumpImage();
}
},
animateGameOver: function() {
},
displayGameOverScreen: function() {
this.gameOverScreen = this.add.group();
var text = this.add.text(
20,
100,
"GAME\nOVER!\nHoHoHo",
{
//fill: '#ffdd00',
},
this.gameOverScreen
);
text.font = 'Revalia';
text.anchor.setTo(0, 0);
text.fontSize = 90;
text.fixedToCamera = true;
grd = text.context.createLinearGradient(0, 0, 0, text.canvas.height);
grd.addColorStop(0, '#ff0033');
grd.addColorStop(1, '#ffdd00');
text.fill = grd;
text.setShadow(-5, 5, 'rgba(0,0,0,0.5)', 0);
text.fixedToCamera = true;
this.gameOverText = text;
var gameover = this.gameOverScreen.create(this.world.centerX, this.camera.y + 800, 'gameover');
gameover.anchor.set( 0.5 );
this.physics.arcade.enable( gameover );
gameover.body.gravity.y = 500;
},
playerMove: function() {
// handle the left and right movement of the hero
if( this.cursor.left.isDown ) {
this.player.body.velocity.x = -VELOCITY;
} else if( this.cursor.right.isDown ) {
this.player.body.velocity.x = VELOCITY;
} else {
this.player.body.velocity.x = 0;
}
// wrap world coordinated so that you can warp from left to right and right to left
this.world.wrap( this.player, this.player.width / 2, false );
// track the maximum amount that the hero has travelled
this.player.yChange = Math.max( this.player.yChange, Math.abs( this.player.y - this.player.yOrig ) );
// game over
if( this.player.y > this.cameraYMin + this.game.height && this.player.alive ) {
this.isGameOver = true;
this.isPlaying = false;
this.displayGameOverScreen();
}
}
};
var game = new Phaser.Game(
500,
800,
Phaser.CANVAS,
'',//document.querySelector('#screen'),
state
);