humus-old/server/game.js

110 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-03-20 15:28:18 -05:00
// Node.
2019-04-13 18:42:53 -05:00
import msgpack from 'msgpack-lite';
2019-03-20 15:28:18 -05:00
import {performance} from 'perf_hooks';
// 3rd party.
// 2nd party.
2019-04-15 10:03:14 -05:00
import {InputPacket} from '@avocado/input';
2019-04-07 20:04:34 -05:00
import {Synchronizer} from '@avocado/state';
2019-04-09 09:55:25 -05:00
import {Ticker} from '@avocado/timing';
2019-03-27 15:51:58 -05:00
// 1st party.
2019-04-04 07:31:21 -05:00
import {WorldTime} from '../common/world-time';
2019-03-27 15:51:58 -05:00
import {createEntityForConnection} from './create-entity-for-connection';
import {createRoom} from './create-server-room';
2019-03-20 15:28:18 -05:00
// Create game.
2019-04-20 14:16:06 -05:00
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(1 / 40);
this.informTicker.on('tick', () => {
// 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);
}
});
// Simulation.
this.mainLoopHandle = setInterval(
this.createMainLoop(),
1000 * config.simulationInterval
);
}
acceptConnection(socket) {
2019-03-20 15:28:18 -05:00
// Create and track a new entity for the connection.
const entity = createEntityForConnection(socket);
2019-03-27 15:51:58 -05:00
// Track informables.
2019-04-20 14:16:06 -05:00
this.informables.push(entity);
2019-03-27 15:51:58 -05:00
entity.on('destroyed', () => {
2019-04-20 14:16:06 -05:00
const index = this.informables.indexOf(entity);
2019-03-27 15:51:58 -05:00
if (-1 !== index) {
2019-04-20 14:16:06 -05:00
this.informables.splice(index, 1);
2019-03-27 15:51:58 -05:00
}
});
// Add entity to room.
2019-04-20 14:16:06 -05:00
this.room.addEntityToLayer(entity, 'everything');
2019-04-08 08:01:17 -05:00
// Initial information.
2019-04-20 14:16:06 -05:00
entity.inform(this.synchronizer.state);
2019-03-20 15:28:18 -05:00
// Listen for events.
2019-04-20 14:16:06 -05:00
socket.on('packet', this.createPacketListener(socket));
socket.on('disconnect', this.createDisconnectionListener(socket));
2019-03-20 15:28:18 -05:00
}
2019-04-20 14:16:06 -05:00
destroy() {
clearInterval(this.mainLoopHandle);
for (let i = 0; i < this.informables.length; ++i) {
const entity = this.informables[i];
entity.socket.disconnect();
2019-03-20 15:28:18 -05:00
}
2019-04-20 14:16:06 -05:00
this.room.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);
}
}
createDisconnectionListener(socket) {
const {entity} = socket;
return () => {
entity.destroy();
};
}
createPacketListener(socket) {
const {entity} = socket;
return (packet) => {
if (packet instanceof InputPacket) {
entity.inputState = packet.toState();
}
};
2019-04-09 09:55:25 -05:00
}
2019-04-20 14:16:06 -05:00
readConfig() {
return {
simulationInterval: 1 / 80,
};
2019-03-20 15:28:18 -05:00
}
2019-04-20 14:16:06 -05:00
2019-03-20 15:28:18 -05:00
}