Skip to content

Commit dbcff58

Browse files
committed
Time.desiredFps has moved to a getter / setter.
Time.physicsElapsed and Time.physicsElapsedMS are no longer calculated every frame, but only when the desiredFps is changed. Time.update has been streamlined and the `updateSetTimeout` and `updateRAF` methods merged and duplicate code removed.
1 parent d7a4237 commit dbcff58

1 file changed

Lines changed: 60 additions & 83 deletions

File tree

src/time/Time.js

Lines changed: 60 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ Phaser.Time = function (game) {
104104
*
105105
* This is used is used to calculate the physic/logic multiplier and how to apply catch-up logic updates.
106106
*
107-
* @property {number} desiredFps
107+
* @property {number} _desiredFps
108+
* @private
108109
* @default
109110
*/
110-
this.desiredFps = 60;
111+
this._desiredFps = 60;
111112

112113
/**
113114
* The suggested frame rate for your game, based on an averaged real frame rate.
@@ -337,13 +338,35 @@ Phaser.Time.prototype = {
337338
*/
338339
update: function (time) {
339340

341+
// Set to the old Date.now value
342+
var previousDateNow = this.time;
343+
344+
// this.time always holds a Date.now value
345+
//
346+
// this.now may hold the RAF high resolution time value if RAF is available (otherwise it also holds Date.now)
347+
//
348+
// With SetTimeout the time argument is always the same as Date.now, so no need to get it again
349+
this.time = (this.game.raf._isSetTimeOut) ? time : Date.now();
350+
351+
// Adjust accordingly.
352+
this.elapsedMS = this.time - previousDateNow;
353+
354+
// 'now' is currently still holding the time of the last call, move it into prevTime
355+
this.prevTime = this.now;
356+
357+
// update 'now' to hold the current time
358+
this.now = time;
359+
360+
// elapsed time between previous call and now - this could be a high resolution value
361+
this.elapsed = this.now - this.prevTime;
362+
340363
if (this.game.raf._isSetTimeOut)
341364
{
342-
this.updateSetTimeout(time);
343-
}
344-
else
345-
{
346-
this.updateRAF(time);
365+
// time to call this function again in ms in case we're using timers instead of RequestAnimationFrame to update the game
366+
this.timeToCall = Math.floor(Math.max(0, (1000.0 / this._desiredFps) - (this.timeCallExpected - time)));
367+
368+
// time when the next call is expected if using timers
369+
this.timeCallExpected = time + this.timeToCall;
347370
}
348371

349372
if (this.advancedTiming)
@@ -365,82 +388,6 @@ Phaser.Time.prototype = {
365388

366389
},
367390

368-
/**
369-
* setTimeOut specific time update handler.
370-
* Called automatically by Time.update.
371-
*
372-
* @method Phaser.Time#updateSetTimeout
373-
* @private
374-
* @param {number} time - The current relative timestamp; see {@link Phaser.Time#now now}.
375-
*/
376-
updateSetTimeout: function (time) {
377-
378-
// Set to the old Date.now value
379-
var previousDateNow = this.time;
380-
381-
// With SetTimeout the time value is always the same as Date.now, so no need to get it again
382-
this.time = time;
383-
384-
// Adjust accordingly.
385-
this.elapsedMS = this.time - previousDateNow;
386-
387-
// 'now' is currently still holding the time of the last call, move it into prevTime
388-
this.prevTime = this.now;
389-
390-
// update 'now' to hold the current time
391-
this.now = time;
392-
393-
// elapsed time between previous call and now
394-
this.elapsed = this.now - this.prevTime;
395-
396-
// time to call this function again in ms in case we're using timers instead of RequestAnimationFrame to update the game
397-
this.timeToCall = Math.floor(Math.max(0, (1000.0 / this.desiredFps) - (this.timeCallExpected - time)));
398-
399-
// time when the next call is expected if using timers
400-
this.timeCallExpected = time + this.timeToCall;
401-
402-
// Set the physics elapsed time... this will always be 1 / this.desiredFps because we're using fixed time steps in game.update now
403-
this.physicsElapsed = 1 / this.desiredFps;
404-
405-
this.physicsElapsedMS = this.physicsElapsed * 1000;
406-
407-
},
408-
409-
/**
410-
* raf specific time update handler.
411-
* Called automatically by Time.update.
412-
*
413-
* @method Phaser.Time#updateRAF
414-
* @private
415-
* @param {number} time - The current relative timestamp; see {@link Phaser.Time#now now}.
416-
*/
417-
updateRAF: function (time) {
418-
419-
// Set to the old Date.now value
420-
var previousDateNow = this.time;
421-
422-
// this.time always holds Date.now, this.now may hold the RAF high resolution time value if RAF is available (otherwise it also holds Date.now)
423-
this.time = Date.now();
424-
425-
// Adjust accordingly.
426-
this.elapsedMS = this.time - previousDateNow;
427-
428-
// 'now' is currently still holding the time of the last call, move it into prevTime
429-
this.prevTime = this.now;
430-
431-
// update 'now' to hold the current time
432-
this.now = time;
433-
434-
// elapsed time between previous call and now
435-
this.elapsed = this.now - this.prevTime;
436-
437-
// Set the physics elapsed time... this will always be 1 / this.desiredFps because we're using fixed time steps in game.update now
438-
this.physicsElapsed = 1 / this.desiredFps;
439-
440-
this.physicsElapsedMS = this.physicsElapsed * 1000;
441-
442-
},
443-
444391
/**
445392
* Handles the updating of the Phaser.Timers (if any)
446393
* Called automatically by Time.update.
@@ -599,4 +546,34 @@ Phaser.Time.prototype = {
599546

600547
};
601548

549+
/**
550+
* The desired frame rate of the game.
551+
*
552+
* This is used is used to calculate the physic / logic multiplier and how to apply catch-up logic updates.
553+
*
554+
* @name Phaser.Time#desiredFps
555+
* @property {integer} desiredFps - The desired frame rate of the game. Defaults to 60.
556+
*/
557+
Object.defineProperty(Phaser.Time.prototype, "desiredFps", {
558+
559+
get: function () {
560+
561+
return this._desiredFps;
562+
563+
},
564+
565+
set: function (value) {
566+
567+
this._desiredFps = value;
568+
569+
// Set the physics elapsed time... this will always be 1 / this.desiredFps
570+
// because we're using fixed time steps in game.update
571+
this.physicsElapsed = 1 / value;
572+
573+
this.physicsElapsedMS = this.physicsElapsed * 1000;
574+
575+
}
576+
577+
});
578+
602579
Phaser.Time.prototype.constructor = Phaser.Time;

0 commit comments

Comments
 (0)