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 {
constructor() {
this.hour = 0;
this._hour = 0;
this.ticker = new Ticker(1);
this._state = 0;
this.ticker.on('tick', () => {
this._state = this.hour;
this._state = this._hour;
});
}
hour() {
return this.hour;
get hour() {
return this._hour;
}
set hour(hour) {
this._hour = hour;
this._state = hour;
}
humanReadable() {
const rawHour = (this.hour + 12) % 12;
const rawHour = (this._hour + 12) % 12;
const hour = Math.floor(0 === Math.floor(rawHour) ? 12 : rawHour);
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`;
}
else {
@ -27,10 +32,11 @@ export class WorldTime {
}
}
patchState(patch) {
for (const {path, value} of patch) {
patchState(steps) {
for (const step of steps) {
const {path, value} = step;
if ('/' === path) {
this.hour = value;
this._hour = value;
}
}
}
@ -44,8 +50,8 @@ export class WorldTime {
}
tick(elapsed) {
this.hour += (elapsed / this.secondsPerHour());
this.hour = this.hour % 24;
this._hour += (elapsed / this.secondsPerHour());
this._hour = this._hour % 24;
this.ticker.tick(elapsed);
}