Skip to content

Commit 3f2ae0f

Browse files
author
Gabriel de Oliveira Rohden
committed
Explosion, adjacent explosion and precision fix on player get pos
1 parent b48b216 commit 3f2ae0f

File tree

6 files changed

+121
-30
lines changed

6 files changed

+121
-30
lines changed

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [{
7+
"type": "node",
8+
"request": "launch",
9+
"name": "Launch server",
10+
"program": "${workspaceFolder}\\server\\src\\server.ts",
11+
"outFiles": [
12+
"${workspaceFolder}/**/*.js"
13+
]
14+
}]
15+
}

client/src/assets.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
export const MAIN_TILES = 'MAIN_TILES'
22

3+
export const BOMB_TIME = 2000;
4+
35
export const ASSETS = {
46
PLAYER: 'dude',
5-
BOMB: 'bomb'
7+
BOMB: 'bomb',
8+
EXPLOSION: 'explosion'
69
}
710

811
export const MAPS = {

client/src/game.ts

Lines changed: 94 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { BackendState, GameDimensions, PlayerDirections, SimpleCoordinates, SocketEvents } from "commons"
22
import Phaser from "phaser"
3-
import { ASSETS, MAIN_TILES, MAPS } from "./assets"
3+
import { ASSETS, MAIN_TILES, MAPS, BOMB_TIME } from "./assets"
44
import Socket = SocketIOClient.Socket;
55

6+
const debug = true;
7+
68
interface Directions {
79
left: boolean;
810
right: boolean;
@@ -29,8 +31,13 @@ export class BombGame {
2931
private backgroundMap: SceneMap;
3032
private breakableMap: SceneMap;
3133
private wallsMap: SceneMap;
34+
private spawnedBombCount = 0
35+
private playerMaxBombSpawn = 2
3236
private bombMap: {
33-
[xy: string]: Phaser.GameObjects.Sprite;
37+
[xy: string]: {
38+
sprite: Phaser.GameObjects.Sprite,
39+
range: number
40+
}
3441
} = {};
3542

3643
private playerRegistry: {
@@ -79,6 +86,11 @@ export class BombGame {
7986
frameWidth: GameDimensions.playerWidth,
8087
frameHeight: GameDimensions.playerHeight
8188
})
89+
90+
scene.load.spritesheet(ASSETS.EXPLOSION, "assets/explosion.png", {
91+
frameWidth: GameDimensions.playerWidth,
92+
frameHeight: GameDimensions.playerHeight
93+
})
8294
}
8395

8496
private static applyPhysicsAndAnimations(
@@ -143,7 +155,7 @@ export class BombGame {
143155
default: "arcade",
144156
arcade: {
145157
gravity: {},
146-
debug: true
158+
debug
147159
}
148160
},
149161
scene: {
@@ -174,16 +186,7 @@ export class BombGame {
174186
player.setBounce(1.2);
175187
player.setCollideWorldBounds(true);
176188

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);
187190

188191
scene.physics.add.collider(player, this.wallsMap.layer);
189192

@@ -288,7 +291,51 @@ export class BombGame {
288291
})
289292
}
290293

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+
292339
const { tileWidth, tileHeight } = GameDimensions;
293340
const newBomb = scene.add.sprite(
294341
x * tileWidth + tileWidth / 2,
@@ -297,25 +344,49 @@ export class BombGame {
297344
);
298345

299346
const key = `${x}-${y}`
300-
this.bombMap[key] = newBomb
347+
this.bombMap[key] = {
348+
sprite: newBomb,
349+
range: 3
350+
}
301351

302352
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);
306367
}
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+
}
308378
}
309379

310-
private hasABombAt(x: number, y: number): boolean {
380+
private hasBombAt(x: number, y: number): boolean {
311381
return `${x}-${y}` in this.bombMap
312382
}
313383

314384
private findPlayerMapPosition(coords: SimpleCoordinates): SimpleCoordinates {
315385
const { tileWidth, tileHeight } = GameDimensions;
316386
return {
317387
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)
319390
}
320391
}
321392

@@ -337,8 +408,8 @@ export class BombGame {
337408

338409
if (cursors.space!.isDown) {
339410
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)
342413
}
343414
}
344415
} else {

server/build/static/assets/bomb.png

10.4 KB
Loading
12.7 KB
Loading

server/src/server.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const state: BackendState = {
3333
}
3434

3535
io.on('connection', function (socket) {
36+
const playerId = socket.id
3637
const newPlayer = {
3738
directions: {
3839
down: false,
@@ -43,24 +44,25 @@ io.on('connection', function (socket) {
4344
y: 0
4445
}
4546
}
46-
state.playerRegistry[socket.id] = newPlayer
47+
48+
state.playerRegistry[playerId] = newPlayer
4749

4850
socket.emit(SocketEvents.InitWithState, state)
49-
socket.broadcast.emit(SocketEvents.NewPlayer, {...newPlayer, id: socket.id})
51+
socket.broadcast.emit(SocketEvents.NewPlayer, { ...newPlayer, id: playerId })
5052

5153
socket.on(SocketEvents.Movement, (directions: PlayerDirections) => {
52-
const player = state.playerRegistry[socket.id]
54+
const player = state.playerRegistry[playerId]
5355
if (player) {
5456
player.directions = directions
5557
}
5658
})
5759

5860
socket.on(SocketEvents.Disconnect, () => {
59-
if (socket.id in state.playerRegistry) {
60-
delete state.playerRegistry[socket.id]
61+
if (playerId in state.playerRegistry) {
62+
delete state.playerRegistry[playerId]
6163
}
6264

63-
io.sockets.emit(SocketEvents.PlayerDisconnect, socket.id)
65+
io.sockets.emit(SocketEvents.PlayerDisconnect, playerId)
6466
})
6567

6668
socket.on(SocketEvents.WallDestroyed, (coordinates: SimpleCoordinates) => {

0 commit comments

Comments
 (0)