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 / 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 / server.js
Created June 9, 2020 20:45
Code snippet 22 - For multiplayer space invaders article
function randomAvatarSelector() {
return Math.floor(Math.random() * 3);
}
@Srushtika
Srushtika / server.js
Created June 9, 2020 20:44
Code snippet 21 - For multiplayer space invaders article
function calcRandomVelocity() {
let randomShipXVelocity = Math.floor(Math.random() * 200) + 20;
randomShipXVelocity *= Math.floor(Math.random() * 2) == 1 ? 1 : -1;
return randomShipXVelocity;
}
@Srushtika
Srushtika / server.js
Created June 9, 2020 20:38
Code snippet 20 - For multiplayer space invaders article
function startMovingPhysicsWorld() {
let p2WorldInterval = setInterval(function () {
if (!gameOn) {
clearInterval(p2WorldInterval);
} else {
// updates velocity every 5 seconds
if (++shipVelocityTimer >= 80) {
shipVelocityTimer = 0;
shipBody.velocity[0] = calcRandomVelocity();
}
@Srushtika
Srushtika / server.js
Created June 9, 2020 20:30
Code snippet 18 - For multiplayer space invaders article
function resetServerState() {
peopleAccessingTheWebsite = 0;
gameOn = false;
gameTickerOn = false;
totalPlayers = 0;
alivePlayers = 0;
for (let item in playerChannels) {
playerChannels[item].unsubscribe();
}
}
@Srushtika
Srushtika / server.js
Created June 9, 2020 20:20
Code snippet 17 - For multiplayer space invaders article
function finishGame(playerId) {
let firstRunnerUpName = "";
let secondRunnerUpName = "";
let winnerName = "Nobody";
let leftoverPlayers = new Array();
for (let item in players) {
leftoverPlayers.push({
nickname: players[item].nickname,
score: players[item].score,
});