Skip to content

Instantly share code, notes, and snippets.

@Srushtika
Created June 9, 2020 14:06
Show Gist options
  • Save Srushtika/516a89a19ce95a0d3b40daaa2e5b6ef1 to your computer and use it in GitHub Desktop.
Save Srushtika/516a89a19ce95a0d3b40daaa2e5b6ef1 to your computer and use it in GitHub Desktop.
Code snippet 10 - For multiplayer space invaders article
gameRoom.presence.subscribe("enter", (player) => {
let newPlayerId;
let newPlayerData;
alivePlayers++;
totalPlayers++;
if (totalPlayers === 1) {
gameTickerOn = true;
startGameDataTicker();
}
newPlayerId = player.clientId;
playerChannels[newPlayerId] = realtime.channels.get(
"clientChannel-" + player.clientId
);
newPlayerObject = {
id: newPlayerId,
x: Math.floor((Math.random() * 1370 + 30) * 1000) / 1000,
y: 20,
invaderAvatarType: avatarTypes[randomAvatarSelector()],
invaderAvatarColor: avatarColors[randomAvatarSelector()],
score: 0,
nickname: player.data,
isAlive: true,
};
players[newPlayerId] = newPlayerObject;
if (totalPlayers === MIN_PLAYERS_TO_START_GAME) {
startShipAndBullets();
}
subscribeToPlayerInput(playerChannels[newPlayerId], newPlayerId);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment