2013-08-06 02:14:48 +00:00
|
|
|
/// <reference path="../Game.ts" />
|
2013-04-13 03:25:03 +00:00
|
|
|
|
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
|
|
|
|
2013-05-04 11:56:33 +00:00
|
|
|
/**
|
|
|
|
* Time constructor
|
|
|
|
* Create a new <code>Time</code>.
|
|
|
|
*
|
|
|
|
* @param game {Phaser.Game} Current game instance.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
constructor(game: Game) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-08-06 02:14:48 +00:00
|
|
|
this.game = game;
|
|
|
|
|
2013-05-14 02:37:38 +00:00
|
|
|
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
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-04 11:56:33 +00:00
|
|
|
/**
|
2013-08-06 02:14:48 +00:00
|
|
|
* Local reference to game.
|
2013-05-04 11:56:33 +00:00
|
|
|
*/
|
2013-08-06 02:14:48 +00:00
|
|
|
public game: Game;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-04 11:56:33 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2013-05-04 11:56:33 +00:00
|
|
|
/**
|
|
|
|
* Time scale factor.
|
|
|
|
* Set it to 0.5 for slow motion, to 2.0 makes game twice faster.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public timeScale: number = 1.0;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-04 11:56:33 +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
|
|
|
/**
|
2013-05-04 11:56:33 +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
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
2013-05-04 11:56:33 +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
|
|
|
/**
|
2013-05-04 11:56:33 +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
|
|
|
|
2013-05-04 11:56:33 +00:00
|
|
|
/**
|
|
|
|
* Frames per second.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public fps: number = 0;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-04 11:56:33 +00:00
|
|
|
/**
|
|
|
|
* Minimal fps.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public fpsMin: number = 1000;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-04 11:56:33 +00:00
|
|
|
/**
|
|
|
|
* Maximal fps.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public fpsMax: number = 0;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-04 11:56:33 +00:00
|
|
|
/**
|
|
|
|
* Mininal duration between 2 frames.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public msMin: number = 1000;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-04 11:56:33 +00:00
|
|
|
/**
|
|
|
|
* Maximal duration between 2 frames.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public msMax: number = 0;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-04 11:56:33 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2013-05-04 11:56:33 +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
|
|
|
/**
|
2013-05-14 02:37:38 +00:00
|
|
|
* Update clock and calculate the fps.
|
|
|
|
* This is called automatically by Game._raf
|
2013-05-04 11:56:33 +00:00
|
|
|
* @method update
|
2013-05-14 02:37:38 +00:00
|
|
|
* @param {number} raf The current timestamp, either performance.now or Date.now
|
2013-05-04 11:56:33 +00:00
|
|
|
*/
|
2013-05-14 02:37:38 +00:00
|
|
|
public update(raf: number) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-14 02:37:38 +00:00
|
|
|
this.now = raf; // mark
|
|
|
|
//this.now = Date.now(); // 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
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
2013-05-04 11:56:33 +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
|
|
|
/**
|
2013-05-04 11:56:33 +00:00
|
|
|
* How long has passed since give 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
|
|
|
/**
|
2013-05-04 11:56:33 +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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|