phaser/Phaser/time/TimeManager.ts

214 lines
5 KiB
TypeScript
Raw Normal View History

/// <reference path="../_definitions.ts" />
2013-04-18 13:16:18 +00:00
/**
2013-08-06 02:14:48 +00:00
* Phaser - TimeManager
2013-04-18 15:49:08 +00:00
*
* This is the game clock and it manages elapsed time and calculation of delta values, used for game object motion.
2013-04-18 13:16:18 +00:00
*/
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
module Phaser {
2013-04-12 16:19:56 +00:00
2013-08-06 02:14:48 +00:00
export class TimeManager {
2013-04-12 16:19:56 +00:00
/**
* Time constructor
* Create a new <code>Time</code>.
*
* @param game {Phaser.Game} Current game instance.
*/
constructor(game: Phaser.Game) {
2013-04-12 16:19:56 +00:00
2013-08-06 02:14:48 +00:00
this.game = game;
this._started = 0;
2013-04-18 13:16:18 +00:00
this._timeLastSecond = this._started;
this.time = this._started;
2013-04-12 16:19:56 +00:00
this.game.onPause.add(this.gamePaused, this);
this.game.onResume.add(this.gameResumed, this);
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
2013-08-06 02:14:48 +00:00
* Local reference to game.
*/
public game: Phaser.Game;
/**
* Time when this object created.
* @param {number}
*/
2013-04-18 13:16:18 +00:00
private _started: number;
2013-04-12 16:19:56 +00:00
/**
* Elapsed since last frame.
* @type {number}
*/
2013-04-18 13:16:18 +00:00
public elapsed: number = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
* Game time counter.
* @property time
* @type {number}
*/
2013-04-18 13:16:18 +00:00
public time: number = 0;
2013-04-12 16:19:56 +00:00
/**
* How long the game has been paused for. Gets reset each time the game pauses.
* @property pausedTime
* @type {number}
*/
public pausedTime: number = 0;
2013-04-18 13:16:18 +00:00
/**
* Time of current frame.
* @property now
* @type {number}
*/
2013-04-18 13:16:18 +00:00
public now: number = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
* Elapsed time since last frame.
* @property delta
* @type {number}
*/
2013-04-18 13:16:18 +00:00
public delta: number = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
*
* @method totalElapsedSeconds
* @return {Number}
*/
public get totalElapsedSeconds(): number {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return (this.now - this._started) * 0.001;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* Frames per second.
* @type {number}
*/
2013-04-18 13:16:18 +00:00
public fps: number = 0;
/**
* Minimal fps.
* @type {number}
*/
2013-04-18 13:16:18 +00:00
public fpsMin: number = 1000;
/**
* Maximal fps.
* @type {number}
*/
2013-04-18 13:16:18 +00:00
public fpsMax: number = 0;
/**
* Minimum duration between 2 frames.
* @type {number}
*/
2013-04-18 13:16:18 +00:00
public msMin: number = 1000;
/**
* Maximum duration between 2 frames.
* @type {number}
*/
2013-04-18 13:16:18 +00:00
public msMax: number = 0;
/**
* How many frames in last second.
* @type {number}
*/
2013-04-18 13:16:18 +00:00
public frames: number = 0;
2013-04-12 16:19:56 +00:00
/**
* Time of last second.
* @type {number}
*/
2013-04-18 13:16:18 +00:00
private _timeLastSecond: number = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
* Update clock and calculate the fps.
* This is called automatically by Game._raf
* @method update
* @param {number} raf The current timestamp, either performance.now or Date.now
*/
public update(raf: number) {
2013-04-12 16:19:56 +00:00
this.now = raf; // mark
2013-04-18 13:16:18 +00:00
this.delta = this.now - this.time; // elapsedMS
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.msMin = Math.min(this.msMin, this.delta);
this.msMax = Math.max(this.msMax, this.delta);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.frames++;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this.now > this._timeLastSecond + 1000)
{
this.fps = Math.round((this.frames * 1000) / (this.now - this._timeLastSecond));
this.fpsMin = Math.min(this.fpsMin, this.fps);
this.fpsMax = Math.max(this.fpsMax, this.fps);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._timeLastSecond = this.now;
this.frames = 0;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.time = this.now; // _total
2013-04-12 16:19:56 +00:00
// Paused?
if (this.game.paused)
{
this.pausedTime = this.now - this._pauseStarted;
}
}
private gamePaused() {
this._pauseStarted = this.now;
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
private gameResumed() {
// Level out the delta timer to avoid spikes
this.pauseDuration = this.pausedTime;
}
public pauseDuration: number = 0;
private _pauseStarted: number = 0;
2013-04-18 13:16:18 +00:00
/**
* How long has passed since given time.
* @method elapsedSince
* @param {number} since The time you want to measure.
* @return {number} Duration between given time and now.
*/
2013-04-18 13:16:18 +00:00
public elapsedSince(since: number): number {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return this.now - since;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
* How long has passed since the given time (in seconds).
* @method elapsedSecondsSince
* @param {number} since The time you want to measure (in seconds).
* @return {number} Duration between given time and now (in seconds).
*/
2013-04-18 13:16:18 +00:00
public elapsedSecondsSince(since: number): number {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return (this.now - since) * 0.001;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
* Set the start time to now.
* @method reset
*/
2013-04-18 13:16:18 +00:00
public reset() {
this._started = this.now;
}
2013-04-12 16:19:56 +00:00
}
}