humus-old/server/game.js
2019-04-22 14:17:54 -05:00

112 lines
2.8 KiB
JavaScript

// Node.
import msgpack from 'msgpack-lite';
import {performance} from 'perf_hooks';
// 3rd party.
// 2nd party.
import {InputPacket} from '@avocado/input';
import {Synchronizer} from '@avocado/state';
import {Ticker} from '@avocado/timing';
// 1st party.
import {WorldTime} from '../common/world-time';
import {createEntityForConnection} from './create-entity-for-connection';
import {createRoom} from './create-server-room';
// Create game.
export default class Game {
constructor() {
const config = this.readConfig();
// Room.
this.room = createRoom();
// World time. Start at 10 am for testing.
this.worldTime = new WorldTime();
this.worldTime.hour = 10;
// Entity tracking.
this.informables = [];
// State synchronization.
this.synchronizer = new Synchronizer({
room: this.room,
worldTime: this.worldTime,
});
this.informTicker = new Ticker(config.informInterval);
this.informTicker.on('tick', this.inform, this);
// Simulation.
this.mainLoopHandle = setInterval(
this.createMainLoop(),
1000 * config.simulationInterval
);
}
destroy(fn) {
clearInterval(this.mainLoopHandle);
this.room.destroy();
fn();
}
acceptConnection(socket) {
// Create and track a new entity for the connection.
const entity = createEntityForConnection(socket);
// Track informables.
this.informables.push(entity);
entity.on('destroyed', () => {
const index = this.informables.indexOf(entity);
if (-1 !== index) {
this.informables.splice(index, 1);
}
});
// Add entity to room.
this.room.addEntityToLayer(entity, 'everything');
// Initial information.
entity.inform(this.synchronizer.state);
// Listen for events.
socket.on('packet', this.createPacketListener(socket));
socket.on('disconnect', this.createDisconnectionListener(socket));
}
createDisconnectionListener(socket) {
const {entity} = socket;
return () => {
if (entity.is('existent')) {
entity.destroy();
}
};
}
createMainLoop() {
let lastTime = performance.now();
return () => {
const now = performance.now();
const elapsed = (now - lastTime) / 1000;
lastTime = now;
// Tick synchronized.
this.synchronizer.tick(elapsed);
// Tick informer.
this.informTicker.tick(elapsed);
}
}
createPacketListener(socket) {
const {entity} = socket;
return (packet) => {
if (packet instanceof InputPacket) {
entity.inputState = packet.toState();
}
};
}
inform() {
// Inform entities of the new state.
for (let i = 0; i < this.informables.length; ++i) {
const entity = this.informables[i];
entity.inform(this.synchronizer.state);
}
}
readConfig() {
return {
informInterval: 1 / 40,
simulationInterval: 1 / 80,
};
}
}