Skip to content

Instantly share code, notes, and snippets.

View Srushtika's full-sized avatar
:octocat:

Srushtika Neelakantam Srushtika

:octocat:
View GitHub Profile
@Srushtika
Srushtika / gameRoomFull.html
Created June 11, 2020 12:32
Code snippet 30 - For multiplayer space invaders article
<!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"
@Srushtika
Srushtika / script.js
Created June 9, 2020 23:04
Code snippet 29 - For multiplayer space invaders article
publishMyDeathNews(bullet, avatar) {
if (amIalive) {
deadPlayerCh.publish("dead-notif", {
killerBulletId: bulletThatShotMe,
deadPlayerId: myClientId,
});
}
amIalive = false;
}
@Srushtika
Srushtika / explosion.js
Created June 9, 2020 22:59
Code snippet 28 - For multiplayer space invaders article
class Explosion extends Phaser.GameObjects.Sprite {
constructor(scene, x, y) {
super(scene, x, y, "explosion");
scene.add.existing(this);
this.play("explode");
}
}
@Srushtika
Srushtika / script.js
Created June 9, 2020 22:50
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";
@Srushtika
Srushtika / script.js
Created June 9, 2020 22:29
Code snippet 26 - For multiplayer space invaders article
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;
}
@Srushtika
Srushtika / script.js
Last active June 9, 2020 22:26
Code snippet 25 - For multiplayer space invaders article
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,
@Srushtika
Srushtika / script.js
Created June 9, 2020 21:04
Code snippet 24 - For multiplayer space invaders article
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);
@Srushtika
Srushtika / script.js
Created June 9, 2020 21:01
Code snippet 23 - For multiplayer space invaders article
let gameRoom;
let deadPlayerCh;
let myClientId;
let myChannel;
let gameOn = false;
let players = {};
let totalPlayers = 0;
let latestShipPosition;
let bulletThatShotMe;
let bulletThatShotSomeone;
@Srushtika
Srushtika / script.js
Last active June 9, 2020 20:51
Code snippet 4 - For multiplayer space invaders article
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(
@Srushtika
Srushtika / server.js
Created June 9, 2020 20:45
Code snippet 22 - For multiplayer space invaders article
function randomAvatarSelector() {
return Math.floor(Math.random() * 3);
}