Fix sound on non-host players
This commit is contained in:
parent
7b2357b915
commit
c2025e0ca2
59
src/game.js
59
src/game.js
@ -127,6 +127,7 @@ const network = {
|
|||||||
lastInputSentAt: 0,
|
lastInputSentAt: 0,
|
||||||
activeRoster: [],
|
activeRoster: [],
|
||||||
lastPingAt: 0,
|
lastPingAt: 0,
|
||||||
|
hasReceivedSnapshot: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let audio = null;
|
let audio = null;
|
||||||
@ -711,6 +712,7 @@ function startMultiplayerMatch(roster) {
|
|||||||
network.inLobby = false;
|
network.inLobby = false;
|
||||||
network.lastSnapshotAt = now();
|
network.lastSnapshotAt = now();
|
||||||
network.lastInputSentAt = 0;
|
network.lastInputSentAt = 0;
|
||||||
|
network.hasReceivedSnapshot = network.isHost;
|
||||||
state.mode = "multiplayer";
|
state.mode = "multiplayer";
|
||||||
state.screen = "game";
|
state.screen = "game";
|
||||||
resetRoundMultiplayer(network.activeRoster);
|
resetRoundMultiplayer(network.activeRoster);
|
||||||
@ -749,6 +751,7 @@ function leaveLobbyInternals() {
|
|||||||
network.activeRoster = [];
|
network.activeRoster = [];
|
||||||
network.inputStateAtClient = { dir: null, bomb: false };
|
network.inputStateAtClient = { dir: null, bomb: false };
|
||||||
network.lastInputSentAt = 0;
|
network.lastInputSentAt = 0;
|
||||||
|
network.hasReceivedSnapshot = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function leaveMultiplayerToMainMenu() {
|
function leaveMultiplayerToMainMenu() {
|
||||||
@ -817,10 +820,65 @@ function serializeGameState() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function playSnapshotSfx(snapshot) {
|
||||||
|
const localEntityIds = new Set(
|
||||||
|
snapshot.players.filter((player) => player.ownerId === network.clientId).map((player) => player.id),
|
||||||
|
);
|
||||||
|
|
||||||
|
const previousBombIds = new Set(state.bombs.map((bomb) => bomb.id));
|
||||||
|
let placedByOther = false;
|
||||||
|
for (const bomb of snapshot.bombs) {
|
||||||
|
if (!previousBombIds.has(bomb.id) && !localEntityIds.has(bomb.ownerId)) {
|
||||||
|
placedByOther = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (placedByOther) {
|
||||||
|
audio.playPlaceBomb();
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextBombIds = new Set(snapshot.bombs.map((bomb) => bomb.id));
|
||||||
|
let bombDetonated = false;
|
||||||
|
for (const bomb of state.bombs) {
|
||||||
|
if (!nextBombIds.has(bomb.id)) {
|
||||||
|
bombDetonated = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bombDetonated && snapshot.explosions.length > 0) {
|
||||||
|
audio.playExplosion();
|
||||||
|
}
|
||||||
|
|
||||||
|
let powerupCollected = false;
|
||||||
|
for (let y = 0; y < state.map.length; y += 1) {
|
||||||
|
for (let x = 0; x < state.map[y].length; x += 1) {
|
||||||
|
const prevTile = state.map[y][x];
|
||||||
|
const nextTile = snapshot.map[y]?.[x];
|
||||||
|
if (!prevTile || !nextTile) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (prevTile.type === "floor" && prevTile.powerup && nextTile.type === "floor" && !nextTile.powerup) {
|
||||||
|
powerupCollected = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (powerupCollected) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (powerupCollected) {
|
||||||
|
audio.playPowerup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function applySnapshot(snapshot) {
|
function applySnapshot(snapshot) {
|
||||||
const previousPlayers = new Map(
|
const previousPlayers = new Map(
|
||||||
state.players.map((player) => [player.ownerId || `id:${player.id}`, { x: player.renderX, y: player.renderY }]),
|
state.players.map((player) => [player.ownerId || `id:${player.id}`, { x: player.renderX, y: player.renderY }]),
|
||||||
);
|
);
|
||||||
|
if (network.hasReceivedSnapshot) {
|
||||||
|
playSnapshotSfx(snapshot);
|
||||||
|
}
|
||||||
state.status = snapshot.status;
|
state.status = snapshot.status;
|
||||||
state.message = snapshot.message;
|
state.message = snapshot.message;
|
||||||
state.time = snapshot.time;
|
state.time = snapshot.time;
|
||||||
@ -857,6 +915,7 @@ function applySnapshot(snapshot) {
|
|||||||
state.nextBombId = snapshot.nextBombId;
|
state.nextBombId = snapshot.nextBombId;
|
||||||
state.outcomePlayed = snapshot.outcomePlayed;
|
state.outcomePlayed = snapshot.outcomePlayed;
|
||||||
state.menu.open = snapshot.menuOpen;
|
state.menu.open = snapshot.menuOpen;
|
||||||
|
network.hasReceivedSnapshot = true;
|
||||||
rebuildFireLookup();
|
rebuildFireLookup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user