phaser/Phaser/Time.ts

140 lines
2.9 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
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
2013-04-18 13:16:18 +00:00
private _game: Game;
private _started: number;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public timeScale: number = 1.0;
public elapsed: number = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
*
* @property time
* @type Number
*/
public time: number = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
*
* @property now
* @type Number
*/
public now: number = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
*
* @property delta
* @type Number
*/
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-04-18 13:16:18 +00:00
public fps: number = 0;
public fpsMin: number = 1000;
public fpsMax: number = 0;
public msMin: number = 1000;
public msMax: number = 0;
public frames: number = 0;
2013-04-12 16:19:56 +00:00
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
/**
*
* @method update
*/
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
/**
*
* @method elapsedSince
* @param {Number} since
* @return {Number}
*/
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
/**
*
* @method elapsedSecondsSince
* @param {Number} since
* @return {Number}
*/
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
/**
*
* @method reset
*/
public reset() {
this._started = this.now;
}
2013-04-12 16:19:56 +00:00
}
}