38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
|
import { EventBus } from '../event-bus';
|
||
|
import { RAFTimer } from './raf';
|
||
|
import { Timer } from './timer';
|
||
|
export class Scheduler extends EventBus {
|
||
|
constructor(logicPerSecond) {
|
||
|
super();
|
||
|
this.logicPerSecond = logicPerSecond;
|
||
|
this.init();
|
||
|
}
|
||
|
init() {
|
||
|
const interval = 1000 / this.logicPerSecond;
|
||
|
this.logicTimer = new Timer(interval, {
|
||
|
id: 0,
|
||
|
func: (dt) => {
|
||
|
this.emit('preupdate.logic');
|
||
|
this.emit('update.logic', dt);
|
||
|
this.emit('postupdate.logic');
|
||
|
}
|
||
|
});
|
||
|
this.drawTimer = new RAFTimer({
|
||
|
id: 1,
|
||
|
func: (dt) => {
|
||
|
this.emit('preupdate.draw');
|
||
|
this.emit('update.draw', dt);
|
||
|
this.emit('postupdate.draw');
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
start() {
|
||
|
this.logicTimer.start();
|
||
|
this.drawTimer.start();
|
||
|
}
|
||
|
stop() {
|
||
|
this.logicTimer.stop();
|
||
|
this.drawTimer.stop();
|
||
|
}
|
||
|
}
|