scratch-vm/src/io/clock.js

40 lines
902 B
JavaScript
Raw Normal View History

2017-04-17 15:10:04 -04:00
const Timer = require('../util/timer');
2017-04-17 19:42:48 -04:00
class Clock {
constructor (runtime) {
this._projectTimer = new Timer();
this._projectTimer.start();
this._pausedTime = null;
this._paused = false;
/**
* Reference to the owning Runtime.
* @type{!Runtime}
*/
this.runtime = runtime;
}
2017-04-17 19:42:48 -04:00
projectTimer () {
if (this._paused) {
return this._pausedTime / 1000;
}
return this._projectTimer.timeElapsed() / 1000;
}
2017-04-17 19:42:48 -04:00
pause () {
this._paused = true;
this._pausedTime = this._projectTimer.timeElapsed();
}
2017-04-17 19:42:48 -04:00
resume () {
this._paused = false;
const dt = this._projectTimer.timeElapsed() - this._pausedTime;
this._projectTimer.startTime += dt;
}
2017-04-17 19:42:48 -04:00
resetProjectTimer () {
this._projectTimer.start();
}
}
module.exports = Clock;