2012-06-21 10:13:21 +02:00
|
|
|
ig.module(
|
|
|
|
'impact.timer'
|
|
|
|
)
|
2012-06-22 17:26:53 +02:00
|
|
|
.defines(function(){ "use strict";
|
2012-06-21 10:13:21 +02:00
|
|
|
|
|
|
|
ig.Timer = ig.Class.extend({
|
|
|
|
target: 0,
|
|
|
|
base: 0,
|
|
|
|
last: 0,
|
2012-06-22 17:26:53 +02:00
|
|
|
pausedAt: 0,
|
2012-06-21 10:13:21 +02:00
|
|
|
|
|
|
|
init: function( seconds ) {
|
|
|
|
this.base = ig.Timer.time;
|
|
|
|
this.last = ig.Timer.time;
|
|
|
|
|
|
|
|
this.target = seconds || 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
set: function( seconds ) {
|
|
|
|
this.target = seconds || 0;
|
|
|
|
this.base = ig.Timer.time;
|
2012-06-22 17:26:53 +02:00
|
|
|
this.pausedAt = 0;
|
2012-06-21 10:13:21 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
reset: function() {
|
|
|
|
this.base = ig.Timer.time;
|
2012-06-22 17:26:53 +02:00
|
|
|
this.pausedAt = 0;
|
2012-06-21 10:13:21 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
tick: function() {
|
|
|
|
var delta = ig.Timer.time - this.last;
|
|
|
|
this.last = ig.Timer.time;
|
2012-06-22 17:26:53 +02:00
|
|
|
return (this.pausedAt ? 0 : delta);
|
2012-06-21 10:13:21 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
delta: function() {
|
2012-06-22 17:26:53 +02:00
|
|
|
return (this.pausedAt || ig.Timer.time) - this.base - this.target;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
pause: function() {
|
|
|
|
if( !this.pausedAt ) {
|
|
|
|
this.pausedAt = ig.Timer.time;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
unpause: function() {
|
|
|
|
if( this.pausedAt ) {
|
|
|
|
this.base += ig.Timer.time - this.pausedAt;
|
|
|
|
this.pausedAt = 0;
|
|
|
|
}
|
2012-06-21 10:13:21 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ig.Timer._last = 0;
|
2013-04-03 21:02:06 +02:00
|
|
|
ig.Timer.time = Number.MIN_VALUE;
|
2012-06-21 10:13:21 +02:00
|
|
|
ig.Timer.timeScale = 1;
|
|
|
|
ig.Timer.maxStep = 0.05;
|
|
|
|
|
|
|
|
ig.Timer.step = function() {
|
|
|
|
var current = Date.now();
|
|
|
|
var delta = (current - ig.Timer._last) / 1000;
|
|
|
|
ig.Timer.time += Math.min(delta, ig.Timer.maxStep) * ig.Timer.timeScale;
|
|
|
|
ig.Timer._last = current;
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|