humus-old/server/game.js

129 lines
3.4 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-05-21 03:10:14 -05:00
import {World} from '@avocado/physics/matter/world';
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-05-21 03:10:14 -05:00
import {Room} from '@avocado/topdown';
2019-03-27 15:51:58 -05:00
// 1st party.
2019-05-13 21:07:57 -05:00
import {SelfEntityPacket} from '../common/packets/self-entity.packet';
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';
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.
2019-05-21 03:10:14 -05:00
this.room = undefined;
Room.load('/kitty-fire.room.json').then((room) => {
room.world = new World();
room.world.stepTime = config.simulationInterval;
this.synchronizer.addChild(room);
this.room = room;
});
2019-04-20 14:16:06 -05:00
// World time. Start at 10 am for testing.
this.worldTime = new WorldTime();
this.worldTime.hour = 10;
// Entity tracking.
this.informables = [];
// State synchronization.
2019-05-13 21:07:57 -05:00
this.synchronizer = new Synchronizer([
this.worldTime,
]);
2019-04-22 14:06:57 -05:00
this.informTicker = new Ticker(config.informInterval);
this.informTicker.on('tick', this.inform, this);
2019-04-20 14:16:06 -05:00
// Simulation.
this.mainLoopHandle = setInterval(
this.createMainLoop(),
1000 * config.simulationInterval
);
}
2019-04-22 14:17:54 -05:00
destroy(fn) {
clearInterval(this.mainLoopHandle);
2019-05-21 03:10:14 -05:00
if (this.room) {
this.room.destroy();
}
2019-04-22 14:17:54 -05:00
fn();
}
2019-04-20 14:16:06 -05:00
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-05-21 03:10:14 -05:00
if (this.room) {
this.room.addEntityToLayer(entity, 0);
}
2019-04-08 08:01:17 -05:00
// Initial information.
2019-05-13 21:07:57 -05:00
const packets = this.synchronizer.packetsForUpdate(true);
2019-05-16 18:20:54 -05:00
packets.unshift(new SelfEntityPacket(entity.numericUid));
2019-05-13 21:07:57 -05:00
entity.inform(packets);
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
2019-04-22 14:17:54 -05:00
createDisconnectionListener(socket) {
const {entity} = socket;
return () => {
if (entity.is('existent')) {
entity.destroy();
}
};
2019-04-20 14:16:06 -05:00
}
createMainLoop() {
let lastTime = performance.now();
return () => {
const now = performance.now();
const elapsed = (now - lastTime) / 1000;
lastTime = now;
// Tick synchronized.
2019-05-21 03:10:14 -05:00
if (this.room) {
this.room.tick(elapsed);
}
2019-05-13 21:07:57 -05:00
this.worldTime.tick(elapsed);
2019-04-20 14:16:06 -05:00
// Tick informer.
this.informTicker.tick(elapsed);
}
}
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
2019-04-22 14:06:57 -05:00
inform() {
2019-05-13 21:07:57 -05:00
const packets = this.synchronizer.packetsForUpdate();
2019-04-22 14:06:57 -05:00
// Inform entities of the new state.
for (let i = 0; i < this.informables.length; ++i) {
const entity = this.informables[i];
2019-05-13 21:07:57 -05:00
entity.inform(packets);
2019-04-22 14:06:57 -05:00
}
}
2019-04-20 14:16:06 -05:00
readConfig() {
return {
2019-04-23 16:59:17 -05:00
informInterval: 1 / 60,
simulationInterval: 1 / 60,
2019-04-20 14:16:06 -05:00
};
2019-03-20 15:28:18 -05:00
}
2019-04-20 14:16:06 -05:00
2019-03-20 15:28:18 -05:00
}