From 4a122d618df13da6294701d06111a12037550a1f Mon Sep 17 00:00:00 2001 From: cha0s Date: Tue, 21 May 2019 03:10:14 -0500 Subject: [PATCH] refactor: async room --- server/game.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/server/game.js b/server/game.js index d0837ac..4f3109e 100644 --- a/server/game.js +++ b/server/game.js @@ -4,21 +4,27 @@ import {performance} from 'perf_hooks'; // 3rd party. // 2nd party. import {InputPacket} from '@avocado/input'; +import {World} from '@avocado/physics/matter/world'; import {Synchronizer} from '@avocado/state'; import {Ticker} from '@avocado/timing'; +import {Room} from '@avocado/topdown'; // 1st party. import {SelfEntityPacket} from '../common/packets/self-entity.packet'; 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(); - this.room.world.stepTime = config.simulationInterval; + 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; + }); // World time. Start at 10 am for testing. this.worldTime = new WorldTime(); this.worldTime.hour = 10; @@ -26,7 +32,6 @@ export default class Game { this.informables = []; // State synchronization. this.synchronizer = new Synchronizer([ - this.room, this.worldTime, ]); this.informTicker = new Ticker(config.informInterval); @@ -40,7 +45,9 @@ export default class Game { destroy(fn) { clearInterval(this.mainLoopHandle); - this.room.destroy(); + if (this.room) { + this.room.destroy(); + } fn(); } @@ -56,7 +63,9 @@ export default class Game { } }); // Add entity to room. - this.room.addEntityToLayer(entity, 0); + if (this.room) { + this.room.addEntityToLayer(entity, 0); + } // Initial information. const packets = this.synchronizer.packetsForUpdate(true); packets.unshift(new SelfEntityPacket(entity.numericUid)); @@ -82,7 +91,9 @@ export default class Game { const elapsed = (now - lastTime) / 1000; lastTime = now; // Tick synchronized. - this.room.tick(elapsed); + if (this.room) { + this.room.tick(elapsed); + } this.worldTime.tick(elapsed); // Tick informer. this.informTicker.tick(elapsed);