11var NOOP = require ( '../utils/NOOP' ) ;
2+ var GetValue = require ( '../utils/object/GetValue' ) ;
23var RequestAnimationFrame = require ( '../dom/RequestAnimationFrame' ) ;
34
4- var VariableTimeStep = function ( game , framerate )
5+ // Frame Rate config
6+ // fps: {
7+ // min: 10,
8+ // target: 60,
9+ // max: 120
10+ // forceSetTimeOut: false,
11+ // deltaHistory: 10
12+ // }
13+
14+ var VariableTimeStep = function ( game , config )
515{
616 this . game = game ;
717
818 this . raf = new RequestAnimationFrame ( ) ;
919
1020 this . started = false ;
1121 this . running = false ;
22+
23+ this . minFps = GetValue ( config , 'min' , 5 ) ;
24+ this . maxFps = GetValue ( config , 'max' , 120 ) ;
25+ this . targetFps = GetValue ( config , 'target' , 60 ) ;
26+
27+ this . _min = 1000 / this . minFps ; // 200ms between frames (i.e. super slow!)
28+ this . _max = 1000 / this . maxFps ; // 8.333ms between frames (i.e. super fast, 120Hz displays)
29+ this . _target = 1000 / this . targetFps ; // 16.666ms between frames (i.e. normal)
1230
13- // For fixed-step physics
14- this . fps = framerate ;
31+ // 200 / 1000 = 0.2 (5fps)
32+ // 8.333 / 1000 = 0.008333 (120fps)
33+ // 16.666 / 1000 = 0.01666 (60fps)
1534
1635 this . callback = NOOP ;
1736
18- this . useRAF = true ;
37+ this . forceSetTimeOut = GetValue ( config , 'forceSetTimeOut' , false ) ;
1938
2039 this . time = 0 ;
2140 this . startTime = 0 ;
@@ -24,14 +43,14 @@ var VariableTimeStep = function (game, framerate)
2443 this . delta = 0 ;
2544 this . deltaIndex = 0 ;
2645 this . deltaHistory = [ ] ;
27- this . deltaSmoothingMax = 10 ;
46+ this . deltaSmoothingMax = GetValue ( config , 'deltaHistory' , 10 ) ;
2847} ;
2948
3049VariableTimeStep . prototype . constructor = VariableTimeStep ;
3150
3251VariableTimeStep . prototype = {
3352
34- start : function ( useRAF , callback )
53+ start : function ( callback )
3554 {
3655 if ( this . started )
3756 {
@@ -54,40 +73,41 @@ VariableTimeStep.prototype = {
5473
5574 for ( var i = 0 ; i < this . deltaSmoothingMax ; i ++ )
5675 {
57- history [ i ] = 0.0166 ;
76+ history [ i ] = this . _target ;
5877 }
5978
6079 this . delta = 0 ;
6180 this . deltaIndex = 0 ;
6281 this . deltaHistory = history ;
6382
64- this . useRAF = useRAF ;
6583 this . callback = callback ;
6684
67- this . raf . start ( this . step . bind ( this ) , useRAF ) ;
85+ this . raf . start ( this . step . bind ( this ) , this . forceSetTimeOut ) ;
6886 } ,
6987
88+ // time comes from requestAnimationFrame and is either a high res time value,
89+ // or Date.now if using setTimeout
7090 step : function ( time )
7191 {
7292 var idx = this . deltaIndex ;
7393 var history = this . deltaHistory ;
7494 var max = this . deltaSmoothingMax ;
7595
76- // delta time
77- var dt = ( time - this . lastTime ) / 1000 ;
96+ // delta time (time is in ms)
97+ var dt = ( time - this . lastTime ) ;
7898
79- // min / max range
80- if ( dt < 0.0001 || dt > 0.5 )
99+ // min / max range (yes, the < and > should be this way around)
100+ if ( dt > this . _min || dt < this . _max )
81101 {
82- // Probably super bad start time or browser tab inactivity / context loss
102+ // Probably super bad start time or browser tab context loss,
83103 // so use the last 'sane' dt value
84104
85105 console . log ( 'dt sync' , dt , 'ms over' , history [ idx ] ) ;
86106
87107 dt = history [ idx ] ;
88108
89- // clamp delta to 0.0001 to 0.5 range
90- dt = Math . max ( Math . min ( dt , 0.5 ) , 0.0001 ) ;
109+ // Clamp delta to min max range (in case history has become corrupted somehow)
110+ dt = Math . max ( Math . min ( dt , this . _max ) , this . _min ) ;
91111 }
92112
93113 // Smooth out the delta over the previous X frames
@@ -117,7 +137,7 @@ VariableTimeStep.prototype = {
117137 // Real-world timer advance
118138 this . time += avg ;
119139
120- this . callback ( this . time , avg * 1000 ) ;
140+ this . callback ( this . time , avg ) ;
121141
122142 // Shift time value over
123143 this . lastTime = time ;
0 commit comments