2020-07-12 23:30:48 +08:00
|
|
|
|
/** 提供帧定时信息 */
|
2020-06-08 21:53:09 +08:00
|
|
|
|
class Time {
|
2020-07-12 23:30:48 +08:00
|
|
|
|
/** deltaTime的未缩放版本。不受时间尺度的影响 */
|
2020-06-08 21:53:09 +08:00
|
|
|
|
public static unscaledDeltaTime;
|
2020-07-12 23:30:48 +08:00
|
|
|
|
/** 前一帧到当前帧的时间增量,按时间刻度进行缩放 */
|
2020-06-11 00:03:26 +08:00
|
|
|
|
public static deltaTime: number = 0;
|
2020-07-12 23:30:48 +08:00
|
|
|
|
/** 时间刻度缩放 */
|
2020-06-08 21:53:09 +08:00
|
|
|
|
public static timeScale = 1;
|
2020-07-12 23:30:48 +08:00
|
|
|
|
/** 已传递的帧总数 */
|
|
|
|
|
|
public static frameCount = 0;
|
2020-06-08 21:53:09 +08:00
|
|
|
|
|
|
|
|
|
|
private static _lastTime = 0;
|
2020-07-12 23:30:48 +08:00
|
|
|
|
/** 自场景加载以来的总时间 */
|
|
|
|
|
|
public static _timeSinceSceneLoad;
|
2020-06-08 21:53:09 +08:00
|
|
|
|
|
|
|
|
|
|
public static update(currentTime: number){
|
|
|
|
|
|
let dt = (currentTime - this._lastTime) / 1000;
|
|
|
|
|
|
this.deltaTime = dt * this.timeScale;
|
|
|
|
|
|
this.unscaledDeltaTime = dt;
|
2020-07-12 23:30:48 +08:00
|
|
|
|
this._timeSinceSceneLoad += dt;
|
2020-06-11 00:03:26 +08:00
|
|
|
|
this.frameCount ++;
|
2020-06-08 21:53:09 +08:00
|
|
|
|
|
|
|
|
|
|
this._lastTime = currentTime;
|
|
|
|
|
|
}
|
2020-07-12 23:30:48 +08:00
|
|
|
|
|
|
|
|
|
|
public static sceneChanged(){
|
|
|
|
|
|
this._timeSinceSceneLoad = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 允许在间隔检查。只应该使用高于delta的间隔值,否则它将始终返回true。
|
|
|
|
|
|
* @param interval
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static checkEvery(interval: number){
|
|
|
|
|
|
// 我们减去了delta,因为timeSinceSceneLoad已经包含了这个update ticks delta
|
|
|
|
|
|
return (this._timeSinceSceneLoad / interval) > ((this._timeSinceSceneLoad - this.deltaTime) / interval);
|
|
|
|
|
|
}
|
2020-06-08 21:53:09 +08:00
|
|
|
|
}
|