Skip to content

Instantly share code, notes, and snippets.

@Srushtika
Created June 9, 2020 22:50
Show Gist options
  • Save Srushtika/0c758dc6d131c5f0f6db108227ed2e9d to your computer and use it in GitHub Desktop.
Save Srushtika/0c758dc6d131c5f0f6db108227ed2e9d to your computer and use it in GitHub Desktop.
Code snippet 27 - For multiplayer space invaders article
explodeAndKill(deadPlayerId) {
this.avatars[deadPlayerId].disableBody(true, true);
let explosion = new Explosion(
this,
this.avatars[deadPlayerId].x,
this.avatars[deadPlayerId].y
);
delete this.avatars[deadPlayerId];
document.getElementById("join-leave-updates").innerHTML =
players[deadPlayerId].nickname + " died";
setTimeout(() => {
document.getElementById("join-leave-updates").innerHTML = "";
}, 2000);
}
publishMyInput() {
if (Phaser.Input.Keyboard.JustDown(this.cursorKeys.left) && amIalive) {
myChannel.publish("pos", {
keyPressed: "left",
});
} else if (
Phaser.Input.Keyboard.JustDown(this.cursorKeys.right) &&
amIalive
) {
myChannel.publish("pos", {
keyPressed: "right",
});
}
}
createBullet(bulletObject) {
let bulletId = bulletObject.id;
this.visibleBullets[bulletId].bulletSprite = this.physics.add
.sprite(this.ship.x - 8, bulletObject.y, "bullet")
.setOrigin(0.5, 0.5);
//add an overlap callback if the current player is still alive
if (amIalive) {
if (
this.physics.add.overlap(
this.visibleBullets[bulletId].bulletSprite,
this.avatars[myClientId],
this.publishMyDeathNews,
null,
this
)
) {
bulletThatShotMe = bulletId;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment