@natewilcox/zelda-server
Version:
Server application for zelda multiplayer game
77 lines (76 loc) • 3.45 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimulationScene = void 0;
require("@geckos.io/phaser-on-nodejs");
const zelda_shared_1 = require("@natewilcox/zelda-shared");
const phaser_1 = __importDefault(require("phaser"));
const SimulationEvents_1 = require("../utils/SimulationEvents");
const nathan_core_1 = require("@natewilcox/nathan-core");
const Player_1 = require("../objects/Player");
const PatchStateComponent_1 = require("../components/PatchStateComponent");
class SimulationScene extends phaser_1.default.Scene {
constructor() {
super("simulation");
this.playerMap = new Map();
this.sceneComponents = new nathan_core_1.ComponentService();
this.onPlayerPatch = (client, patch) => {
const player = this.playerMap.get(client.id);
if (player) {
//if position is patched, move the player
if (patch.x && patch.y) {
player.moveTowards(patch.x, patch.y);
}
else {
player.stopMoving();
}
}
};
this.addPlayer = (playerState) => {
const { clientId, id } = playerState;
console.log(`adding player to simulation: ${clientId}-${id}`);
//get a new player added to the group
const player = this.players.get(playerState.x, playerState.y, 'link');
player.id = id;
player.playerState = playerState;
this.playerMap.set(clientId, player);
console.log(`player added to group: ${player.id} at ${player.x}, ${player.y}`);
this.sceneComponents.addComponent(player, new PatchStateComponent_1.PatchStateComponent(this));
console.log(`player ${id} was added to the simulation`);
};
this.removePlayer = (playerState) => {
const { id, clientId } = playerState;
console.log(`removing player from simulation: ${clientId}-${id}`);
const player = this.playerMap.get(clientId);
if (player) {
console.log(`destroying player object for ${clientId}-${id}`);
player.destroy();
this.playerMap.delete(clientId);
this.sceneComponents.destroyComponents(player);
console.log(`player ${clientId}-${id} was removed from the simulation`);
}
};
}
preload() {
}
create(config) {
this.players = this.physics.add.group({
classType: Player_1.Player
});
this.room = config.room;
this.dispatcher = config.dispatcher;
this.CLIENT = config.CLIENT;
//listen for client messages
console.log("Listening for client messages.");
this.CLIENT.on(zelda_shared_1.ClientMessages.PatchPlayerState, this.onPlayerPatch);
SimulationEvents_1.SimulationEventEmitter.on(SimulationEvents_1.SimulationEvents.PlayerJoined, this.addPlayer);
SimulationEvents_1.SimulationEventEmitter.on(SimulationEvents_1.SimulationEvents.PlayerLeft, this.removePlayer);
}
update(t, dt) {
//this.playersList.forEach(player => player.update(dt));
this.sceneComponents.update(dt, t);
}
}
exports.SimulationScene = SimulationScene;