@natewilcox/zelda-server
Version:
Server application for zelda multiplayer game
84 lines (83 loc) • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GameRoom = void 0;
const core_1 = require("@colyseus/core");
const GameRoomState_1 = require("./schema/GameRoomState");
const SimulationScene_1 = require("../scenes/SimulationScene");
const JoinCommand_1 = require("../commands/JoinCommand");
const LeaveCommand_1 = require("../commands/LeaveCommand");
const command_1 = require("@colyseus/command");
const colyseus_nathan_1 = require("@natewilcox/colyseus-nathan");
const SimulationEvents_1 = require("../utils/SimulationEvents");
class GameRoom extends core_1.Room {
constructor() {
super(...arguments);
this.dispatcher = new command_1.Dispatcher(this);
this.maxClients = 4;
this.PATCH = 20;
this.FPS = 20;
}
onCreate() {
console.info("Room created");
this.CLIENT = new colyseus_nathan_1.ClientService(this);
this.configureRoom();
//create a new simulation
this.simulation = this.createSimulation();
//start the simulation
console.log(`starting simulation...`);
this.simulation.scene.add('simulation', SimulationScene_1.SimulationScene, true, {
room: this,
dispatcher: this.dispatcher,
CLIENT: this.CLIENT
});
console.log(`simulation started`);
}
onJoin(client, options) {
console.log(client.sessionId, "joined!");
//join with random x and y between 0 and 800
this.dispatcher.dispatch(new JoinCommand_1.JoinCommand(), {
client,
x: Math.floor(Math.random() * 800),
y: Math.floor(Math.random() * 600)
});
}
onLeave(client, consented) {
console.log(client.sessionId, "left!");
this.dispatcher.dispatch(new LeaveCommand_1.LeaveCommand(), {
client
});
}
onDispose() {
console.log("room", this.roomId, "disposing...");
SimulationEvents_1.SimulationEventEmitter.removeAllListeners();
//stop the simulation and destroy it
this.simulation.destroy(false);
this.simulation = null;
console.log(`simulation destroyed`);
}
configureRoom() {
this.setPatchRate(1000 / this.PATCH);
this.setState(new GameRoomState_1.GameRoomState());
}
createSimulation() {
global.phaserOnNodeFPS = this.FPS;
const config = {
type: Phaser.HEADLESS,
width: 800,
height: 600,
fps: {
target: this.FPS,
forceSetTimeOut: true
},
physics: {
default: 'arcade',
arcade: {
gravity: { y: 0, x: 0 }
}
}
};
const simulation = new Phaser.Game(config);
return simulation;
}
}
exports.GameRoom = GameRoom;