Last active
June 9, 2020 22:26
-
-
Save Srushtika/243ca48379cadf920f906b81cf907e4a to your computer and use it in GitHub Desktop.
Code snippet 25 - 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
create() { | |
this.avatars = {}; | |
this.visibleBullets = {}; | |
this.ship = {}; | |
this.cursorKeys = this.input.keyboard.createCursorKeys(); | |
this.anims.create({ | |
key: "explode", | |
frames: this.anims.generateFrameNumbers("explosion"), | |
frameRate: 20, | |
repeat: 0, | |
hideOnComplete: true, | |
}); | |
gameRoom.subscribe("game-state", (msg) => { | |
if (msg.data.gameOn) { | |
gameOn = true; | |
if (msg.data.shipBody["0"]) { | |
latestShipPosition = msg.data.shipBody["0"]; | |
} | |
if (msg.data.bulletOrBlank != "") { | |
let bulletId = msg.data.bulletOrBlank.id; | |
this.visibleBullets[bulletId] = { | |
id: bulletId, | |
y: msg.data.bulletOrBlank.y, | |
toLaunch: true, | |
bulletSprite: "", | |
}; | |
} | |
if (msg.data.killerBulletId) { | |
bulletThatShotSomeone = msg.data.killerBulletId; | |
} | |
} | |
players = msg.data.players; | |
totalPlayers = msg.data.playerCount; | |
}); | |
gameRoom.subscribe("game-over", (msg) => { | |
gameOn = false; | |
localStorage.setItem("totalPlayers", msg.data.totalPlayers); | |
localStorage.setItem("winner", msg.data.winner); | |
localStorage.setItem("firstRunnerUp", msg.data.firstRunnerUp); | |
localStorage.setItem("secondRunnerUp", msg.data.secondRunnerUp); | |
gameRoom.unsubscribe(); | |
deadPlayerCh.unsubscribe(); | |
myChannel.unsubscribe(); | |
if (msg.data.winner == "Nobody") { | |
window.location.replace(BASE_SERVER_URL + "/gameover"); | |
} else { | |
window.location.replace(BASE_SERVER_URL + "/winner"); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment