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
function startDownwardMovement(playerId) { | |
let interval = setInterval(() => { | |
if (players[playerId] && players[playerId].isAlive) { | |
players[playerId].y += PLAYER_VERTICAL_INCREMENT; | |
players[playerId].score += PLAYER_SCORE_INCREMENT; | |
if (players[playerId].y > SHIP_PLATFORM) { | |
finishGame(playerId); | |
clearInterval(interval); | |
} |
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
function subscribeToPlayerInput(channelInstance, playerId) { | |
channelInstance.subscribe("pos", (msg) => { | |
if (msg.data.keyPressed == "left") { | |
if (players[playerId].x - 20 < 20) { | |
players[playerId].x = 20; | |
} else { | |
players[playerId].x -= 20; | |
} | |
} else if (msg.data.keyPressed == "right") { | |
if (players[playerId].x + 20 > 1380) { |
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
function startGameDataTicker() { | |
let tickInterval = setInterval(() => { | |
if (!gameTickerOn) { | |
clearInterval(tickInterval); | |
} else { | |
bulletOrBlank = ""; | |
bulletTimer += GAME_TICKER_MS; | |
if (bulletTimer >= GAME_TICKER_MS * 5) { | |
bulletTimer = 0; | |
bulletOrBlank = { |
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
function startShipAndBullets() { | |
gameOn = true; | |
world = new p2.World({ | |
gravity: [0, -9.82], | |
}); | |
shipBody = new p2.Body({ | |
position: [shipX, shipY], | |
velocity: [calcRandomVelocity(), 0], | |
}); |
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
function startGameDataTicker() {} | |
function subscribeToPlayerInput(channelInstance, playerId) {} | |
function startDownwardMovement(playerId) {} | |
function finishGame(playerId) {} | |
function resetServerState() {} |
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
deadPlayerCh.subscribe("dead-notif", (msg) => { | |
players[msg.data.deadPlayerId].isAlive = false; | |
killerBulletId = msg.data.killerBulletId; | |
alivePlayers--; | |
if (alivePlayers == 0) { | |
setTimeout(() => { | |
finishGame(""); | |
}, 1000); | |
} | |
}); |
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("leave", (player) => { | |
let leavingPlayer = player.clientId; | |
alivePlayers--; | |
totalPlayers--; | |
delete players[leavingPlayer]; | |
if (totalPlayers <= 0) { | |
resetServerState(); | |
} | |
}); |
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(); | |
} |
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
realtime.connection.once("connected", () => { | |
gameRoom = realtime.channels.get("game-room"); | |
deadPlayerCh = realtime.channels.get("dead-player"); | |
gameRoom.presence.subscribe("enter", (player) => {}); | |
gameRoom.presence.subscribe("leave", (player) => {}); | |
deadPlayerCh.subscribe("dead-notif", (msg) => {}); | |
}); |
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
const realtime = Ably.Realtime({ | |
key: ABLY_API_KEY, | |
echoMessages: false, | |
}); | |
//create a uniqueId to assign to clients on auth | |
const uniqueId = function () { | |
return "id-" + totalPlayers + Math.random().toString(36).substr(2, 16); | |
}; |