feat: less worldTime jank

This commit is contained in:
cha0s 2019-04-08 09:01:39 -04:00
parent fc9ee44764
commit 43813dd085

View File

@ -3,23 +3,28 @@ import {Ticker} from '@avocado/timing';
export class WorldTime { export class WorldTime {
constructor() { constructor() {
this.hour = 0; this._hour = 0;
this.ticker = new Ticker(1); this.ticker = new Ticker(1);
this._state = 0; this._state = 0;
this.ticker.on('tick', () => { this.ticker.on('tick', () => {
this._state = this.hour; this._state = this._hour;
}); });
} }
hour() { get hour() {
return this.hour; return this._hour;
}
set hour(hour) {
this._hour = hour;
this._state = hour;
} }
humanReadable() { humanReadable() {
const rawHour = (this.hour + 12) % 12; const rawHour = (this._hour + 12) % 12;
const hour = Math.floor(0 === Math.floor(rawHour) ? 12 : rawHour); const hour = Math.floor(0 === Math.floor(rawHour) ? 12 : rawHour);
const minutes = String(Math.floor((rawHour - Math.floor(rawHour)) * 60)).padStart(2, '0'); const minutes = String(Math.floor((rawHour - Math.floor(rawHour)) * 60)).padStart(2, '0');
if (this.hour >= 12) { if (this._hour >= 12) {
return `${hour}:${minutes} pm`; return `${hour}:${minutes} pm`;
} }
else { else {
@ -27,10 +32,11 @@ export class WorldTime {
} }
} }
patchState(patch) { patchState(steps) {
for (const {path, value} of patch) { for (const step of steps) {
const {path, value} = step;
if ('/' === path) { if ('/' === path) {
this.hour = value; this._hour = value;
} }
} }
} }
@ -44,8 +50,8 @@ export class WorldTime {
} }
tick(elapsed) { tick(elapsed) {
this.hour += (elapsed / this.secondsPerHour()); this._hour += (elapsed / this.secondsPerHour());
this.hour = this.hour % 24; this._hour = this._hour % 24;
this.ticker.tick(elapsed); this.ticker.tick(elapsed);
} }