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-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-10-11 02:17:07 -05:00
|
|
|
import {actionIds} from '../common/action-ids';
|
2019-05-13 21:07:57 -05:00
|
|
|
import {SelfEntityPacket} from '../common/packets/self-entity.packet';
|
2019-09-22 18:45:52 -05:00
|
|
|
import {WorldTime} from '../common/world-time.synchronized';
|
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.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-04-22 14:06:57 -05:00
|
|
|
this.informTicker = new Ticker(config.informInterval);
|
|
|
|
this.informTicker.on('tick', this.inform, this);
|
2019-10-11 02:17:07 -05:00
|
|
|
InputPacket.setActionIds(actionIds());
|
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.
|
2019-03-21 00:59:00 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|
2019-09-29 13:20:08 -05:00
|
|
|
Promise.resolve(entity.hydrate()).then(() => {
|
2019-10-01 22:43:20 -05:00
|
|
|
// Flush AABB for visibility.
|
|
|
|
entity.tick(0);
|
2019-09-29 13:20:08 -05:00
|
|
|
// Sync world time.
|
|
|
|
entity.addSynchronized(this.worldTime);
|
|
|
|
// Add entity to room.
|
|
|
|
if (this.room) {
|
|
|
|
this.room.addEntityToLayer(entity, 0);
|
|
|
|
entity.addSynchronized(this.room);
|
|
|
|
}
|
2019-10-01 22:43:20 -05:00
|
|
|
entity.queuePacket(new SelfEntityPacket(entity.numericUid));
|
|
|
|
entity.inform();
|
2019-09-29 13:20:08 -05:00
|
|
|
});
|
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) {
|
2019-10-11 02:17:07 -05:00
|
|
|
entity.inputStream = packet.data;
|
2019-04-20 14:16:06 -05:00
|
|
|
}
|
|
|
|
};
|
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() {
|
|
|
|
// Inform entities of the new state.
|
|
|
|
for (let i = 0; i < this.informables.length; ++i) {
|
2019-09-22 18:45:52 -05:00
|
|
|
this.informables[i].inform();
|
2019-04-22 14:06:57 -05:00
|
|
|
}
|
2019-09-22 18:45:52 -05:00
|
|
|
// Clean packets.
|
2019-09-29 13:20:08 -05:00
|
|
|
if (this.room) {
|
|
|
|
this.room.cleanPackets();
|
|
|
|
}
|
2019-09-22 18:45:52 -05:00
|
|
|
this.worldTime.cleanPackets();
|
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
|
|
|
}
|