Created
June 9, 2020 17:02
Code snippet 14 - For multiplayer space invaders article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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