refactor: inform guards

This commit is contained in:
cha0s 2019-04-22 14:06:57 -05:00
parent 4fe9c45ee8
commit d60247b751
2 changed files with 14 additions and 11 deletions

View File

@ -27,17 +27,8 @@ export default class Game {
room: this.room, room: this.room,
worldTime: this.worldTime, worldTime: this.worldTime,
}); });
this.informTicker = new Ticker(1 / 40); this.informTicker = new Ticker(config.informInterval);
this.informTicker.on('tick', () => { this.informTicker.on('tick', this.inform, this);
// Inform entities of the new state.
for (let i = 0; i < this.informables.length; ++i) {
const entity = this.informables[i];
if (!entity.socket) {
continue;
}
entity.inform(this.synchronizer.state);
}
});
// Simulation. // Simulation.
this.mainLoopHandle = setInterval( this.mainLoopHandle = setInterval(
this.createMainLoop(), this.createMainLoop(),
@ -102,8 +93,17 @@ export default class Game {
}; };
} }
inform() {
// Inform entities of the new state.
for (let i = 0; i < this.informables.length; ++i) {
const entity = this.informables[i];
entity.inform(this.synchronizer.state);
}
}
readConfig() { readConfig() {
return { return {
informInterval: 1 / 40,
simulationInterval: 1 / 80, simulationInterval: 1 / 80,
}; };
} }

View File

@ -211,6 +211,9 @@ export class Informed extends decorate(Trait) {
return { return {
inform: (state) => { inform: (state) => {
if (!this._socket) {
return;
}
// Reduce state. // Reduce state.
const reducedState = this.reduceState(state); const reducedState = this.reduceState(state);
// Take a pure JS diff. // Take a pure JS diff.