2020-07-23 11:00:46 +08:00
|
|
|
|
module es {
|
|
|
|
|
|
/** 提供帧定时信息 */
|
|
|
|
|
|
export class Time {
|
2020-11-25 11:37:24 +08:00
|
|
|
|
/** 游戏运行的总时间 */
|
2020-12-15 11:46:33 +08:00
|
|
|
|
public static totalTime: number = 0;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/** deltaTime的未缩放版本。不受时间尺度的影响 */
|
2020-12-15 11:46:33 +08:00
|
|
|
|
public static unscaledDeltaTime: number = 0;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/** 前一帧到当前帧的时间增量,按时间刻度进行缩放 */
|
|
|
|
|
|
public static deltaTime: number = 0;
|
|
|
|
|
|
/** 时间刻度缩放 */
|
|
|
|
|
|
public static timeScale = 1;
|
2021-03-26 12:49:00 +08:00
|
|
|
|
/** DeltaTime可以为的最大值 */
|
|
|
|
|
|
public static maxDeltaTime = Number.MAX_VALUE;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/** 已传递的帧总数 */
|
|
|
|
|
|
public static frameCount = 0;
|
|
|
|
|
|
/** 自场景加载以来的总时间 */
|
2020-12-15 11:46:33 +08:00
|
|
|
|
public static timeSinceSceneLoad: number = 0;
|
2021-01-20 15:35:59 +08:00
|
|
|
|
private static _lastTime = -1;
|
2020-06-08 21:53:09 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public static update(currentTime: number) {
|
2021-01-20 15:35:59 +08:00
|
|
|
|
if (currentTime == -1)
|
|
|
|
|
|
currentTime = Date.now();
|
|
|
|
|
|
if (this._lastTime == -1)
|
|
|
|
|
|
this._lastTime = currentTime;
|
|
|
|
|
|
|
2021-04-30 20:35:10 +08:00
|
|
|
|
let dt = (currentTime - this._lastTime) / 1000;
|
2021-03-26 12:49:00 +08:00
|
|
|
|
if (dt > this.maxDeltaTime)
|
|
|
|
|
|
dt = this.maxDeltaTime;
|
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
|
|
|
|
}
|