-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlevel.js
90 lines (86 loc) · 2.9 KB
/
level.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
Level = function(game) {
this.game = game;
this.bgtile = null;
this.ground = null;
this.pipes = null;
this.pipeTimer = null;
this.scoreObj = null;
};
Level.prototype = {
preload: function() {
this.game.load.image('bg', 'assets/bg.png');
//this.game.load.image('ground', 'assets/ground.png');
this.game.load.image('pipe', 'assets/pipe_sml.png');
},
create: function() {
this.bgtile = this.game.add.sprite(0, 0, 'bg');
//this.ground = this.game.add.tileSprite(0, game.world.height - 96, game.world.width, 96, 'ground');
this.pipes = this.game.add.group();
this.scoreObj = this.game.add.group();
this.pipeTimer = game.time.create(game);
this.pipeTimer.loop(1500, this.spawnPipes, this);
this.pipeTimer.start();
},
update: function() {
//scroll background
//this.ground.tilePosition.x -= game.time.physicsElapsed * SPEED / 2;
//this.bgtile.tilePosition.x -= 1;
// Remove offscreen pipes
this.pipes.forEachAlive(function(pipe) {
if (pipe.x + pipe.width < this.game.world.bounds.left) {
pipe.kill();
}
});
this.scoreObj.forEachAlive(function(obj) {
if (obj.x + obj.width < this.game.world.bounds.left) {
obj.kill();
}
});
},
updateScore: function(obj) {
this.scoreObj.remove(obj);
},
gameOver: function() {
// Stop all pipes
this.pipes.forEachAlive(function(pipe) {
pipe.body.velocity.x = 0;
});
this.scoreObj.forEachAlive(function(obj) {
obj.body.velocity.x = 0;
});
// Stop spawning pipes
this.pipeTimer.stop();
// Stop scrolling bg
//this.bgtile.tilePosition.x = 0;
},
spawnPipe: function(pipeY, flipped) {
var pipe = this.pipes.create(
this.game.width,
pipeY + (flipped ? -this.o() : this.o()) / 2,
'pipe'
);
pipe.body.allowGravity = false;
// Flip pipe
pipe.scale.setTo(1, flipped ? -1 : 1);
pipe.body.offset.y = flipped ? -pipe.body.height * 2 : 0;
// Move to the left
pipe.body.velocity.x = -SPEED;
return pipe;
},
spawnPipes: function() {
var pipeY = ((this.game.height - 16 - this.o() / 2) / 2) + (Math.random() > 0.5 ? -1 : 1) * Math.random() * this.game.height / 6;
// Bottom pipe
var botpipe = this.spawnPipe(pipeY);
// Top pipe (flipped)
var toppipe = this.spawnPipe(pipeY, true);
// Invisible object for score keeping
var obj = this.scoreObj.create(toppipe.x + toppipe.width, 0);
obj.width = 2;
obj.height = this.game.world.height;
obj.body.allowGravity = false;
obj.body.velocity.x = -SPEED;
},
o: function() {
return OPENING + 60;
}
};