Files
esengine/source/src/ECS/Utils/Time.ts

43 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-07-23 11:00:46 +08:00
module es {
/** 提供帧定时信息 */
export class Time {
2020-11-25 11:37:24 +08:00
/** 游戏运行的总时间 */
public static totalTime: number;
2020-07-23 11:00:46 +08:00
/** deltaTime的未缩放版本。不受时间尺度的影响 */
public static unscaledDeltaTime;
/** 前一帧到当前帧的时间增量,按时间刻度进行缩放 */
public static deltaTime: number = 0;
/** 时间刻度缩放 */
public static timeScale = 1;
/** 已传递的帧总数 */
public static frameCount = 0;
/** 自场景加载以来的总时间 */
2020-11-25 11:37:24 +08:00
public static timeSinceSceneLoad;
2020-07-28 16:25:20 +08:00
private static _lastTime = 0;
2020-07-28 16:25:20 +08:00
public static update(currentTime: number) {
2020-07-23 11:00:46 +08:00
let dt = (currentTime - this._lastTime) / 1000;
2020-11-25 11:37:24 +08:00
this.totalTime += dt;
2020-07-23 11:00:46 +08:00
this.deltaTime = dt * this.timeScale;
this.unscaledDeltaTime = dt;
2020-11-25 11:37:24 +08:00
this.timeSinceSceneLoad += dt;
2020-07-28 16:25:20 +08:00
this.frameCount++;
2020-07-12 23:30:48 +08:00
2020-07-23 11:00:46 +08:00
this._lastTime = currentTime;
}
2020-07-28 16:25:20 +08:00
public static sceneChanged() {
2020-11-25 11:37:24 +08:00
this.timeSinceSceneLoad = 0;
2020-07-23 11:00:46 +08:00
}
2020-07-12 23:30:48 +08:00
2020-07-23 11:00:46 +08:00
/**
* 使delta的间隔值true
* @param interval
*/
2020-07-28 16:25:20 +08:00
public static checkEvery(interval: number) {
2020-07-23 11:00:46 +08:00
// 我们减去了delta因为timeSinceSceneLoad已经包含了这个update ticks delta
2020-11-25 11:37:24 +08:00
return this.timeSinceSceneLoad / interval > (this.timeSinceSceneLoad - this.deltaTime) / interval;
2020-07-23 11:00:46 +08:00
}
2020-07-12 23:30:48 +08:00
}
2020-07-23 11:00:46 +08:00
}