refactor: async room

This commit is contained in:
cha0s 2019-05-21 03:10:14 -05:00
parent 81c91f3b40
commit 4a122d618d

View File

@ -4,21 +4,27 @@ import {performance} from 'perf_hooks';
// 3rd party. // 3rd party.
// 2nd party. // 2nd party.
import {InputPacket} from '@avocado/input'; import {InputPacket} from '@avocado/input';
import {World} from '@avocado/physics/matter/world';
import {Synchronizer} from '@avocado/state'; import {Synchronizer} from '@avocado/state';
import {Ticker} from '@avocado/timing'; import {Ticker} from '@avocado/timing';
import {Room} from '@avocado/topdown';
// 1st party. // 1st party.
import {SelfEntityPacket} from '../common/packets/self-entity.packet'; import {SelfEntityPacket} from '../common/packets/self-entity.packet';
import {WorldTime} from '../common/world-time'; import {WorldTime} from '../common/world-time';
import {createEntityForConnection} from './create-entity-for-connection'; import {createEntityForConnection} from './create-entity-for-connection';
import {createRoom} from './create-server-room';
// Create game. // Create game.
export default class Game { export default class Game {
constructor() { constructor() {
const config = this.readConfig(); const config = this.readConfig();
// Room. // Room.
this.room = createRoom(); this.room = undefined;
this.room.world.stepTime = config.simulationInterval; Room.load('/kitty-fire.room.json').then((room) => {
room.world = new World();
room.world.stepTime = config.simulationInterval;
this.synchronizer.addChild(room);
this.room = room;
});
// World time. Start at 10 am for testing. // World time. Start at 10 am for testing.
this.worldTime = new WorldTime(); this.worldTime = new WorldTime();
this.worldTime.hour = 10; this.worldTime.hour = 10;
@ -26,7 +32,6 @@ export default class Game {
this.informables = []; this.informables = [];
// State synchronization. // State synchronization.
this.synchronizer = new Synchronizer([ this.synchronizer = new Synchronizer([
this.room,
this.worldTime, this.worldTime,
]); ]);
this.informTicker = new Ticker(config.informInterval); this.informTicker = new Ticker(config.informInterval);
@ -40,7 +45,9 @@ export default class Game {
destroy(fn) { destroy(fn) {
clearInterval(this.mainLoopHandle); clearInterval(this.mainLoopHandle);
if (this.room) {
this.room.destroy(); this.room.destroy();
}
fn(); fn();
} }
@ -56,7 +63,9 @@ export default class Game {
} }
}); });
// Add entity to room. // Add entity to room.
if (this.room) {
this.room.addEntityToLayer(entity, 0); this.room.addEntityToLayer(entity, 0);
}
// Initial information. // Initial information.
const packets = this.synchronizer.packetsForUpdate(true); const packets = this.synchronizer.packetsForUpdate(true);
packets.unshift(new SelfEntityPacket(entity.numericUid)); packets.unshift(new SelfEntityPacket(entity.numericUid));
@ -82,7 +91,9 @@ export default class Game {
const elapsed = (now - lastTime) / 1000; const elapsed = (now - lastTime) / 1000;
lastTime = now; lastTime = now;
// Tick synchronized. // Tick synchronized.
if (this.room) {
this.room.tick(elapsed); this.room.tick(elapsed);
}
this.worldTime.tick(elapsed); this.worldTime.tick(elapsed);
// Tick informer. // Tick informer.
this.informTicker.tick(elapsed); this.informTicker.tick(elapsed);