1
1
import { BackendState , GameDimensions , PlayerDirections , SimpleCoordinates , SocketEvents } from "commons"
2
2
import Phaser from "phaser"
3
- import { ASSETS , MAIN_TILES , MAPS } from "./assets"
3
+ import { ASSETS , MAIN_TILES , MAPS , BOMB_TIME } from "./assets"
4
4
import Socket = SocketIOClient . Socket ;
5
5
6
+ const debug = true ;
7
+
6
8
interface Directions {
7
9
left : boolean ;
8
10
right : boolean ;
@@ -29,8 +31,13 @@ export class BombGame {
29
31
private backgroundMap : SceneMap ;
30
32
private breakableMap : SceneMap ;
31
33
private wallsMap : SceneMap ;
34
+ private spawnedBombCount = 0
35
+ private playerMaxBombSpawn = 2
32
36
private bombMap : {
33
- [ xy : string ] : Phaser . GameObjects . Sprite ;
37
+ [ xy : string ] : {
38
+ sprite : Phaser . GameObjects . Sprite ,
39
+ range : number
40
+ }
34
41
} = { } ;
35
42
36
43
private playerRegistry : {
@@ -79,6 +86,11 @@ export class BombGame {
79
86
frameWidth : GameDimensions . playerWidth ,
80
87
frameHeight : GameDimensions . playerHeight
81
88
} )
89
+
90
+ scene . load . spritesheet ( ASSETS . EXPLOSION , "assets/explosion.png" , {
91
+ frameWidth : GameDimensions . playerWidth ,
92
+ frameHeight : GameDimensions . playerHeight
93
+ } )
82
94
}
83
95
84
96
private static applyPhysicsAndAnimations (
@@ -143,7 +155,7 @@ export class BombGame {
143
155
default : "arcade" ,
144
156
arcade : {
145
157
gravity : { } ,
146
- debug : true
158
+ debug
147
159
}
148
160
} ,
149
161
scene : {
@@ -174,16 +186,7 @@ export class BombGame {
174
186
player . setBounce ( 1.2 ) ;
175
187
player . setCollideWorldBounds ( true ) ;
176
188
177
- scene . physics . add . collider (
178
- player ,
179
- this . breakableMap . layer ,
180
- ( _ , tile : unknown ) => {
181
- const { x, y } = tile as SimpleCoordinates ;
182
-
183
- this . breakableMap . map . removeTileAt ( x , y ) ;
184
- this . socket . emit ( SocketEvents . WallDestroyed , { x, y } )
185
- }
186
- ) ;
189
+ scene . physics . add . collider ( player , this . breakableMap . layer ) ;
187
190
188
191
scene . physics . add . collider ( player , this . wallsMap . layer ) ;
189
192
@@ -288,7 +291,51 @@ export class BombGame {
288
291
} )
289
292
}
290
293
291
- private fabricBombAt ( scene : Phaser . Scene , x : number , y : number ) {
294
+ private putExplosionAt ( scene : Phaser . Scene , x : number , y : number , range : number ) {
295
+ const { tileWidth, tileHeight } = GameDimensions ;
296
+ const offset = Math . floor ( range / 2 ) ;
297
+ const explosions : Array < Phaser . GameObjects . Sprite > = [ ]
298
+ const putAndExplodeAdjacent = ( sx : number , sy : number ) => {
299
+ const sprite = scene . add . sprite ( sx , sy , ASSETS . EXPLOSION )
300
+
301
+ explosions . push ( sprite )
302
+ }
303
+
304
+ for ( let i = 0 ; i < range ; i ++ ) {
305
+ const nX = ( ( x + i ) - offset )
306
+ const nY = ( ( y + i ) - offset )
307
+ putAndExplodeAdjacent (
308
+ nX * tileWidth + tileWidth / 2 ,
309
+ y * tileHeight + tileHeight / 2
310
+ ) ;
311
+
312
+ putAndExplodeAdjacent (
313
+ x * tileWidth + tileWidth / 2 ,
314
+ nY * tileHeight + tileHeight / 2 ,
315
+ ) ;
316
+
317
+ this . explodeBombAt ( scene , nX , y )
318
+ this . explodeBombAt ( scene , x , nY )
319
+ }
320
+
321
+ setTimeout ( ( ) => {
322
+ explosions . forEach ( e => e . destroy ( true ) )
323
+ } , 1000 )
324
+ }
325
+
326
+ private destroyWallAt ( x : number , y : number ) {
327
+ this . breakableMap . map . removeTileAt ( x , y ) ;
328
+ this . socket . emit ( SocketEvents . WallDestroyed , { x, y } )
329
+ }
330
+
331
+ private tryToSetupBombAt ( scene : Phaser . Scene , x : number , y : number ) {
332
+ if ( this . spawnedBombCount >= this . playerMaxBombSpawn ) {
333
+ return
334
+ } else {
335
+ this . spawnedBombCount ++
336
+ }
337
+
338
+
292
339
const { tileWidth, tileHeight } = GameDimensions ;
293
340
const newBomb = scene . add . sprite (
294
341
x * tileWidth + tileWidth / 2 ,
@@ -297,25 +344,49 @@ export class BombGame {
297
344
) ;
298
345
299
346
const key = `${ x } -${ y } `
300
- this . bombMap [ key ] = newBomb
347
+ this . bombMap [ key ] = {
348
+ sprite : newBomb ,
349
+ range : 3
350
+ }
301
351
302
352
setTimeout ( ( ) => {
303
- if ( this . hasABombAt ( x , y ) ) {
304
- this . bombMap [ key ] . destroy ( true ) ;
305
- delete this . bombMap [ key ] ;
353
+ this . explodeBombAt ( scene , x , y )
354
+ this . spawnedBombCount --
355
+ } , BOMB_TIME ) ;
356
+ }
357
+
358
+ private explodeBombAt ( scene : Phaser . Scene , x : number , y : number ) {
359
+ const key = `${ x } -${ y } `
360
+
361
+ if ( this . hasBombAt ( x , y ) ) {
362
+ const bomb = this . bombMap [ key ]
363
+ const offset = Math . floor ( bomb . range / 2 ) ;
364
+ for ( let i = 0 ; i < 3 ; i ++ ) {
365
+ this . destroyWallAt ( ( i + x ) - offset , y ) ;
366
+ this . destroyWallAt ( x , ( i + y ) - offset ) ;
306
367
}
307
- } , 1000 ) ;
368
+
369
+ bomb . sprite . destroy ( true ) ;
370
+ delete this . bombMap [ key ] ;
371
+
372
+ this . putExplosionAt ( scene , x , y , bomb . range )
373
+
374
+ debug && console . debug ( `Bomb exploded at x: ${ x } y:${ y } ` )
375
+ } else {
376
+ debug && console . debug ( `Bomb not found at x: ${ x } y:${ y } ` )
377
+ }
308
378
}
309
379
310
- private hasABombAt ( x : number , y : number ) : boolean {
380
+ private hasBombAt ( x : number , y : number ) : boolean {
311
381
return `${ x } -${ y } ` in this . bombMap
312
382
}
313
383
314
384
private findPlayerMapPosition ( coords : SimpleCoordinates ) : SimpleCoordinates {
315
385
const { tileWidth, tileHeight } = GameDimensions ;
316
386
return {
317
387
x : Math . floor ( coords . x / tileWidth ) ,
318
- y : Math . floor ( coords . y / tileHeight )
388
+ // +(tileHeight / 2) is a precision fix :D
389
+ y : Math . floor ( ( coords . y + ( tileHeight / 2 ) ) / tileHeight )
319
390
}
320
391
}
321
392
@@ -337,8 +408,8 @@ export class BombGame {
337
408
338
409
if ( cursors . space ! . isDown ) {
339
410
const { x, y } = this . findPlayerMapPosition ( player ) ;
340
- if ( ! this . hasABombAt ( x , y ) ) {
341
- this . fabricBombAt ( scene , x , y )
411
+ if ( ! this . hasBombAt ( x , y ) ) {
412
+ this . tryToSetupBombAt ( scene , x , y )
342
413
}
343
414
}
344
415
} else {
0 commit comments