This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Space Invaders</title> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<meta name="description" content="Multiplayer space invaders" /> | |
<link | |
id="favicon" |
This file contains 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
publishMyDeathNews(bullet, avatar) { | |
if (amIalive) { | |
deadPlayerCh.publish("dead-notif", { | |
killerBulletId: bulletThatShotMe, | |
deadPlayerId: myClientId, | |
}); | |
} | |
amIalive = false; | |
} |
This file contains 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
class Explosion extends Phaser.GameObjects.Sprite { | |
constructor(scene, x, y) { | |
super(scene, x, y, "explosion"); | |
scene.add.existing(this); | |
this.play("explode"); | |
} | |
} |
This file contains 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
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"; |
This file contains 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
update() { | |
if (gameOn) { | |
if (this.ship.x) { | |
this.ship.x = latestShipPosition; | |
} else { | |
this.ship = this.physics.add | |
.sprite(latestShipPosition, config.height - 32, "ship") | |
.setOrigin(0.5, 0.5); | |
this.ship.x = latestShipPosition; | |
} |
This file contains 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, |
This file contains 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({ | |
authUrl: BASE_SERVER_URL + "/auth", | |
}); | |
realtime.connection.once("connected", () => { | |
myClientId = realtime.auth.clientId; | |
gameRoom = realtime.channels.get("game-room"); | |
deadPlayerCh = realtime.channels.get("dead-player"); | |
myChannel = realtime.channels.get("clientChannel-" + myClientId); | |
gameRoom.presence.enter(myNickname); |
This file contains 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
let gameRoom; | |
let deadPlayerCh; | |
let myClientId; | |
let myChannel; | |
let gameOn = false; | |
let players = {}; | |
let totalPlayers = 0; | |
let latestShipPosition; | |
let bulletThatShotMe; | |
let bulletThatShotSomeone; |
This file contains 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
preload() { | |
this.load.spritesheet( | |
"avatarA", | |
"https://cdn.glitch.com/f66772e3-bbf6-4f6d-b5d5-94559e3c1c6f%2FInvaderA_00%402x.png?v=1589228669385", | |
{ | |
frameWidth: 48, | |
frameHeight: 32 | |
} | |
); | |
this.load.spritesheet( |
This file contains 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 randomAvatarSelector() { | |
return Math.floor(Math.random() * 3); | |
} |