Created
June 9, 2020 14:06
-
-
Save Srushtika/516a89a19ce95a0d3b40daaa2e5b6ef1 to your computer and use it in GitHub Desktop.
Code snippet 10 - For multiplayer space invaders article
This file contains hidden or 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
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