Skip to content

Instantly share code, notes, and snippets.

@Srushtika
Created June 9, 2020 17:02
Show Gist options
  • Save Srushtika/0186f8baa9495bd1b37a8e4f5ac998b9 to your computer and use it in GitHub Desktop.
Save Srushtika/0186f8baa9495bd1b37a8e4f5ac998b9 to your computer and use it in GitHub Desktop.
Code snippet 14 - For multiplayer space invaders article
function startGameDataTicker() {
let tickInterval = setInterval(() => {
if (!gameTickerOn) {
clearInterval(tickInterval);
} else {
bulletOrBlank = "";
bulletTimer += GAME_TICKER_MS;
if (bulletTimer >= GAME_TICKER_MS * 5) {
bulletTimer = 0;
bulletOrBlank = {
y: SHIP_PLATFORM,
id:
"bulletId-" + Math.floor((Math.random() * 2000 + 50) * 1000) / 1000,
};
}
if (shipBody) {
copyOfShipBody = shipBody;
}
gameRoom.publish("game-state", {
players: players,
playerCount: totalPlayers,
shipBody: copyOfShipBody.position,
bulletOrBlank: bulletOrBlank,
gameOn: gameOn,
killerBullet: killerBulletId,
});
}
}, GAME_TICKER_MS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment