orion-engine
Version:
A simple and lightweight web based game development library
77 lines (76 loc) • 1.92 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Time = void 0;
class Time {
_is_running = false;
_last_time = 0;
_pause_time = 0;
_delta_time = 0;
_time_scale = 1.0;
_elapsed_time = 0;
_current_time = 0;
constructor(start_time_on_creation = true) {
if (start_time_on_creation) {
this.start();
}
}
get delta_time() {
return this._delta_time;
}
get time_scale() {
return this._time_scale;
}
set time_scale(value) {
this._time_scale = value;
}
get elapsed_time() {
return this._elapsed_time;
}
get current_time() {
return this._current_time;
}
/**
* Starts the time system.
*/
start() {
this._is_running = true;
this._last_time = performance.now();
this._pause_time = 0;
this._delta_time = 0;
this._time_scale = 1.0;
this._elapsed_time = 0;
this._current_time = 0;
}
/**
* Pauses the time system.
*/
pause() {
if (this._is_running) {
this._is_running = false;
this._pause_time = performance.now();
}
}
/**
* Resumes the time system.
*/
resume() {
if (!this._is_running) {
this._is_running = true;
this._last_time += performance.now() - this._pause_time;
}
}
/**
* Updates the time system.
*/
update() {
if (!this._is_running) {
return;
}
const now = performance.now();
this._delta_time = ((now - this._last_time) / 1000) * this._time_scale;
this._elapsed_time += this._delta_time;
this._current_time = this._elapsed_time * 1000;
this._last_time = now;
}
}
exports.Time = Time;