phaser/Phaser/Time.ts

189 lines
4.4 KiB
TypeScript
Raw Normal View History

/// <reference path="Game.ts" />
2013-04-18 13:16:18 +00:00
/**
2013-04-18 15:49:08 +00:00
* Phaser - Time
*
* 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-04-18 13:16:18 +00:00
export class Time {
2013-04-12 16:19:56 +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-04-18 13:16:18 +00:00
this._started = Date.now();
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
/**
* Local private reference to game.
*/
2013-04-18 13:16:18 +00:00
private _game: 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
/**
* 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;
/**
* 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
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;
/**
* Mininal duration between 2 frames.
* @type {number}
*/
2013-04-18 13:16:18 +00:00
public msMin: number = 1000;
/**
* Maximal 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 calc fps.
* @method update
*/
2013-04-18 13:16:18 +00:00
public update() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
// Can we use performance.now() ?
this.now = Date.now(); // mark
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
//// Lock the delta at 0.1 to minimise fps tunneling
//if (this.delta > 0.1)
//{
// this.delta = 0.1;
//}
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 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 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
/**
* 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
}
}