refactor: async room
This commit is contained in:
parent
81c91f3b40
commit
4a122d618d
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user