Files
esengine/src/Utils/Time.ts

92 lines
2.2 KiB
TypeScript
Raw Normal View History

/**
*
*
*/
export class Time {
/**
*
*/
public static deltaTime: number = 0;
/**
*
*/
public static unscaledDeltaTime: number = 0;
/**
*
*/
public static totalTime: number = 0;
/**
*
*/
public static unscaledTotalTime: number = 0;
/**
*
*/
public static timeScale: number = 1;
/**
*
*/
public static frameCount: number = 0;
/**
*
*/
private static _lastTime: number = 0;
/**
*
*/
private static _isFirstUpdate: boolean = true;
/**
*
* @param currentTime
*/
public static update(currentTime: number = -1): void {
if (currentTime === -1) {
currentTime = Date.now();
}
if (this._isFirstUpdate) {
this._lastTime = currentTime;
this._isFirstUpdate = false;
return;
}
// 计算帧时间间隔(转换为秒)
this.unscaledDeltaTime = (currentTime - this._lastTime) / 1000;
this.deltaTime = this.unscaledDeltaTime * this.timeScale;
// 更新总时间
this.unscaledTotalTime += this.unscaledDeltaTime;
this.totalTime += this.deltaTime;
// 更新帧数
this.frameCount++;
// 记录当前时间
this._lastTime = currentTime;
}
/**
*
*/
public static sceneChanged(): void {
this._isFirstUpdate = true;
}
/**
*
* @param interval
* @param lastTime
* @returns
*/
public static checkEvery(interval: number, lastTime: number): boolean {
return this.totalTime - lastTime >= interval;
}
}