-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainGameScene.js
228 lines (195 loc) · 7.71 KB
/
MainGameScene.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
var player;
var candles;
var fires;
var platforms;
var cursors;
var score = 0;
var scoreText;
var birthday;
var emitter;
var particles;
var candleOverlap;
var collectcandle;
var hitfire;
var MainGameScene = Phaser.Class ({
Extends: Phaser.Scene,
initialize: function() {
Phaser.Scene.call(this, {"key": "MainGameScene"});
},
init: function () {},
preload: function ()
{
this.load.image('sky', 'assets/sky.png');
this.load.image('ground', 'assets/platform.png');
this.load.image('candle', 'assets/candle.png');
this.load.image('fire', 'assets/fire.png');
this.load.spritesheet('dude', 'assets/dude.png', { frameWidth: 63, frameHeight: 96 });
this.load.image('red', 'assets/red.png');
this.load.image('birthday', 'assets/birthday.png');
},
create: function ()
{
// Top left is 0,0... because why not.
// Add sky (we need sky)
this.add.image(400, 300, 'sky');
// Add a group to contain ground ledges... they need some containing.
platforms = this.physics.add.staticGroup();
// Create the ground. Be godlike.
// It needs to fit tho.... so make it the width of Rlandia.
platforms.create(400, 568, 'ground').setScale(2).refreshBody();
// Create some sweeeeet ledges.
platforms.create(600, 400, 'ground');
platforms.create(-30, 150, 'ground');
platforms.create(750, 220, 'ground');
platforms.create(30, 300, 'ground');
// What's Rlandia without a R? Add the little dude.
player = this.physics.add.sprite(100, 400, 'dude');
// Give the little dude a slight bounce, you know, being his birthday and all.
player.setBounce(0.2);
player.setCollideWorldBounds(true);
// Make him able to, you know, move. Boring otherwise.
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'turn',
frames: [ { key: 'dude', frame: 4 } ],
frameRate: 20
});
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
frameRate: 10,
repeat: -1
});
// Where are the keeeeeys?
cursors = this.input.keyboard.createCursorKeys();
// Finally, the birthday candles! Nice and evenly spaced at 70 pixels apart. Artwerk.
candles = this.physics.add.group({
key: 'candle',
repeat: 9,
setXY: { x: 12, y: 0, stepX: 70 },
setRotation: { value: 45, step: 30}
});
candles.children.iterate(function (child) {
// Give each candle a slightly different bounce - make them funlike.
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
child.allowGravity=false;
});
fires = this.physics.add.group();
// The candle score - not the musical variety, sadly.
scoreText = this.add.text(16, 16, 'Candles collected: 0', { fontSize: '32px', fill: '#000' });
// Time to collide all the things !
this.physics.add.collider(player, platforms);
this.physics.add.collider(candles, platforms);
this.physics.add.collider(fires, platforms);
// Check to see if the R dude bumps any candles, if he does, stash them in the collectcandle function.
candleOverlap = this.physics.add.overlap(player, candles, this.collectcandle, null, this);
// But if R dude bumps into FIRE! It ouchies.
fireCollider = this.physics.add.collider(player, fires, this.hitfire, null, this);
// Prepare the particles!
particles = this.add.particles('red');
particles.visible = false;
// Particles need to be emitted from somewhere.
emitter = particles.createEmitter({
speed: 100,
scale: { start: 1, end: 0 },
blendMode: 'ADD'
});
emitter.visible = false;
// Shhhhh, it's the cake
birthday = this.physics.add.image(400, 100, 'birthday');
birthday.disableBody(true, true)
birthday.visible = false;
},
// Spies
update: function ()
{
if (cursors.left.isDown)
{
player.setVelocityX(-160);
player.anims.play('left', true);
}
else if (cursors.right.isDown)
{
player.setVelocityX(160);
player.anims.play('right', true);
}
else
{
player.setVelocityX(0);
player.anims.play('turn');
}
if (cursors.up.isDown && player.body.touching.down)
{
player.setVelocityY(-330);
}
},
// Yay R is doing the things!
collectcandle: function (player, candle)
{
// Turn off candle now that it's caught
candle.disableBody(true, true);
// Add and update the score, because duh.
score += 1;
scoreText.setText('Candles collected ' + score);
if ( score >= 0 && score < 38 ) {
if (candles.countActive(true) === 0)
{
// But wait! Moarrr candles! Coz we not the youth.
candles.children.iterate(function (child) {
child.enableBody(true, child.x, 0, true, true);
});
var x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
// Throw in some fire to spice it up - R doesn't like being burnt by fire
if (fires.countActive(true) < 2){
var fire = fires.create(x, 2, 'fire');
fire.setBounce(1);
fire.setCollideWorldBounds(true);
fire.setVelocity(Phaser.Math.Between(-200, 200), 30);
fire.allowGravity = false;
}
}
}
else if (score = 38) // Most mature (ಠ_ృ)
// Cake time oh yeah~!
{
this.gameWon();
this.physics.world.removeCollider(candleOverlap);
this.physics.world.removeCollider(fireCollider);
fires.setVisible(false);
candles.setVisible(false);
}
},
gameWon: function ()
{
// Eat da cake, forget the diabetus.
scoreText.setText('Happy Birthday R!');
birthday.enableBody(true,true)
birthday.visible = true;
birthday.setVelocity(100, 200);
birthday.setBounce(1, 1);
birthday.setCollideWorldBounds(true);
emitter.startFollow(birthday);
particles.visible = true;
emitter.visible = true;
},
hitfire: function ()
{
// Burnt R is.... burnt. Ouchies.
score -=3; // harsh, but necessary.
scoreText.setText('Candles collected: ' + score);
if (score < 0) {
// Ruhoh.... Got too much burny :'(
scoreText.setText('oH No! yOu CaNt CeLeBrAtE wItHoUt CANDLES (✖╭╮✖)');
scoreText.setFontSize('24px');
scoreText.setColor('red');
player.setTint(0xff0000);
player.anims.play('turn');
this.physics.pause();
}
}
});