-
Notifications
You must be signed in to change notification settings - Fork 2
/
Game.js
345 lines (293 loc) · 10.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
BasicGame.Game = function (game) {
// When a State is added to Phaser it automatically has the following properties set on it, even if they already exist:
this.game; // a reference to the currently running game (Phaser.Game)
this.add; // used to add sprites, text, groups, etc (Phaser.GameObjectFactory)
this.camera; // a reference to the game camera (Phaser.Camera)
this.cache; // the game cache (Phaser.Cache)
this.input; // the global input manager. You can access this.input.keyboard, this.input.mouse, as well from it. (Phaser.Input)
this.load; // for preloading assets (Phaser.Loader)
this.math; // lots of useful common math operations (Phaser.Math)
this.sound; // the sound manager - add a sound, play one, set-up markers, etc (Phaser.SoundManager)
this.stage; // the game stage (Phaser.Stage)
this.time; // the clock (Phaser.Time)
this.tweens; // the tween manager (Phaser.TweenManager)
this.state; // the state manager (Phaser.StateManager)
this.world; // the game world (Phaser.World)
this.particles; // the particle manager (Phaser.Particles)
this.physics; // the physics manager (Phaser.Physics)
this.rnd; // the repeatable random number generator (Phaser.RandomDataGenerator)
// You can use any of these from any function within this State.
// But do consider them as being 'reserved words', i.e. don't create a property for your own game called "world" or you'll over-write the world reference.
this.map;
this.tileset;
this.layer;
this.player;
this.p1facing = 'idle';
this.p2facing = 'idle';
this.p1jumpTimer = 0;
this.p2jumpTimer = 0;
this.cursors;
this.bg;
this.itens;
this.score = 0;
//Minhas var
this.isMP;
this.player2;
this.medianX;
this.medianY;
this.worldScale = 1;
this.textoTempo;
this.somColeta;
this.music = null;
this.isAudioOn;
this.isMusicOn;
this.medianX = null;
this.medianY = null;
this.boost = null;
};
BasicGame.Game.prototype = {
init: function(isMultiPlayer, isAudioOn, isMusicOn, dificuldade) {
this.isMP = isMultiPlayer;
this.isAudioOn = isAudioOn;
this.isMusicOn = isMusicOn;
this.dif = dificuldade;
},
create: function () {
this.physics.startSystem(Phaser.Physics.ARCADE);
this.stage.backgroundColor = '#000000';
bg = this.add.tileSprite(-100, 0, 900, 600, 'background');
//bg = this.add.tileSprite(0, 0, 800, 600, 'background');
bg.fixedToCamera = true;
bg.tilePosition.x = -(this.camera.x * 0.7);
map = this.add.tilemap('level1');
map.addTilesetImage('tiles-1');
map.setCollisionByExclusion([ 13, 14, 15, 16, 46, 47, 48, 49, 50, 51 ]);
layer = map.createLayer('Tile Layer 1');
// Un-comment this on to see the collision tiles
//layer.debug = true;
layer.resizeWorld();
this.physics.arcade.gravity.y = 450;
player = this.add.sprite(32, 130, 'dude');
this.physics.enable(player, Phaser.Physics.ARCADE);
player.body.bounce.y = 0.2;
player.body.collideWorldBounds = true;
player.body.setSize(20, 32, 5, 16);
player.animations.add('left', [0, 1, 2, 3], 10, true);
player.animations.add('idle', [4], 20, true);
player.animations.add('right', [5, 6, 7, 8], 10, true);
player.pontuaçao = 0;
player.frame = 4;
player.data = {nmr: 0};
// -- Organiza itens ja coletados
player.coletados = [];
music = this.add.audio('game-Song', 0.3, true);
if (this.isMusicOn) {
music.play( '', 0, 0.3, true);
music.onLoop.add(this.playMusic, this);
}
if (!this.isMP) {
this.camera.follow(player);
}
else {
player2 = this.add.sprite(32, 32, 'dudeInv');
this.physics.enable(player2, Phaser.Physics.ARCADE);
player2.body.bounce.y = 0.2;
player2.body.collideWorldBounds = true;
player2.body.setSize(20, 32, 5, 16);
player2.animations.add('left', [0, 1, 2, 3], 10, true);
player2.animations.add('idle', [4], 20, true);
player2.animations.add('right', [5, 6, 7, 8], 10, true);
player2.frame = 4;
// Define teclas de comando pro p2
upButton = this.game.input.keyboard.addKey(Phaser.Keyboard.W);
downButton = this.game.input.keyboard.addKey(Phaser.Keyboard.S);
leftButton = this.game.input.keyboard.addKey(Phaser.Keyboard.A);
rightButton = this.game.input.keyboard.addKey(Phaser.Keyboard.D);
player2.pontuaçao = 0;
player2.coletados = [];
player2.data = {nmr: 1};
}
cursors = this.input.keyboard.createCursorKeys();
// Finally some itens to collect
itens = this.add.group();
// We will enable physics for any star that is created in this group
itens.enableBody = true;
// Cria itens para coletar
var f=0;
for (var i = 0; i < 12; i++, f++) {
if(f==10)
f=0;
var _item = itens.create( i * 70, 0, 'items', f);
_item.data = {item: f};
_item.body.gravity.y = 300;
_item.body.collideWorldBounds = true;
_item.body.bounce.y = 0.8 + Math.random() * 0.2;
}
// Indicador de tempo
textoTempo = this.game.add.text(320, 15);
textoTempo.font = 'Arial Black';
textoTempo.fontSize = 50;
textoTempo.fontWeight = 'bold';
textoTempo.stroke = '#000000';
textoTempo.strokeThickness = 6;
textoTempo.fill = '#43d637';
textoTempo.fixedToCamera = true;
// HUD pra mostrar itens ja coletados
var hud;
if (this.isMP) {
hud = this.add.sprite(0, 0, 'hudMP');
console.log('hud mp');
}
else {
console.log('hud sp');
hud = this.add.sprite(0, 0, 'hudSP');
}
hud.fixedToCamera = true;
this.somColeta = this.game.add.audio('coletou');
boost = this.add.group();
boost.enableBody = true;
this.boost = boost.create(665, 265,'pula');
//gamby
this.boost.body.gravity = 0;
// variavel inicio ajuda a contar tempo
this.inicio = this.time.now;
},
update: function () {
// Movimento de camera para multiplayer
if (this.isMP) {
var posicaoP1X = player.body.x,
posicaoP1Y = player.body.y,
posicaoP2X = player2.body.x,
posicaoP2Y = player2.body.y,
distanciaX = (posicaoP1X > posicaoP2X ? (posicaoP1X - posicaoP2X) : (posicaoP2X - posicaoP1X) ),
distanciaY = (posicaoP1Y > posicaoP2Y ? (posicaoP1Y - posicaoP2Y) : (posicaoP2Y - posicaoP1Y) );
this.medianX = (player.body.x + player2.body.x) / 2;
this.medianY = (player.body.y + player2.body.y) / 2;
this.game.camera.focusOnXY(this.medianX, this.medianY);
this.physics.arcade.overlap(player2, this.boost, this.boostEstrela, null, this);
if (distanciaX >= 790) {
player.body.velocity.x = 0;
player2.body.velocity.x = 0;
}
if (distanciaY >= 590) {
player.body.velocity.y = 0;
player2.body.velocity.y = 0;
}
}
this.physics.arcade.overlap(player, this.boost, this.boostEstrela, null, this);
textoTempo.text = this.time.elapsedSecondsSince(this.inicio).toFixed(3) ;
this.physics.arcade.collide(itens, layer);
this.physics.arcade.collide(player, layer);
if (this.isMP) {
this.physics.arcade.collide(player2, layer);
player2.body.velocity.x = 0;
this.physics.arcade.overlap(player2, itens, this.collectStar, null, this);
}
//Limita movimento ao controle do usuario
player.body.velocity.x = 0;
this.physics.arcade.overlap(player, itens, this.collectStar, null, this);
if (cursors.left.isDown) {
player.body.velocity.x = -150;
if (this.facing != 'left') {
player.animations.play('left');
this.facing = 'left';
}
}
else if (cursors.right.isDown) {
player.body.velocity.x = 150;
if (this.facing != 'right') {
player.animations.play('right');
this.facing = 'right';
}
}
else {
if (this.facing != 'idle') {
player.animations.stop();
if (this.facing == 'left') {
player.frame = 0;
}
else {
player.frame = 5;
}
player.frame = 4;
this.facing = 'idle';
}
}
if (cursors.up.isDown && player.body.onFloor() && this.time.now > this.p1jumpTimer) {
player.body.velocity.y = -250;
this.jumpTimer = this.time.now + 750;
}
// Comandos caso seja MP
if (this.isMP) {
if (leftButton.isDown) {
player2.body.velocity.x = -150;
if (this.facing != 'left') {
player2.animations.play('left');
this.p2facing = 'left';
}
}
else if (rightButton.isDown) {
player2.body.velocity.x = 150;
if (this.p2facing != 'right') {
player2.animations.play('right');
this.p2facing = 'right';
}
}
else {
if (this.p2facing != 'idle') {
player2.animations.stop();
if (this.p2facing == 'left') {
player2.frame = 0;
}
else {
player2.frame = 5;
}
player2.frame = 4;
this.p2facing = 'idle';
}
}
if (upButton.isDown && player2.body.onFloor() && this.time.now > this.p2jumpTimer) {
player2.body.velocity.y = -250;
this.p2jumpTimer = this.time.now + 750;
}
}
// Fim comandos MP
},
quitGame: function (pointer) {
// Here you should destroy anything you no longer need.
// Stop music, delete sprites, purge caches, free resources, all that good stuff.
let tempoFinal = this.time.elapsedSecondsSince(this.inicio).toFixed(3);
if (this.isMusicOn) {
music.stop();
}
// Then let's go back to the main menu.
this.state.start('endState', true, false, tempoFinal, this.isAudioOn, this.isMusicOn);
},
playMusic: function() {
music.play('', 0, 0.4, true);
},
collectStar: function(player, star) {
let indexItem = star.data['item'];
if (player.coletados.indexOf(indexItem) === -1) {
player.coletados.push(indexItem);
star.kill();
if (this.isAudioOn) {
this.somColeta.play();
}
this.atualizaHud(player, star, indexItem);
if (++player.pontuaçao === 10) {
this.quitGame();
}
}
},
atualizaHud: function(player, star, index) {
var topo = 18,
esquerda = player.data['nmr'] === 0 ? 6 : 475;
var img = this.add.sprite(esquerda + (index*32), topo, star.key, index);
img.tint = 0x00ff00;
img.fixedToCamera = true;
},
boostEstrela: function(player) {
player.body.velocity.y -= 160;
}
};