-
Notifications
You must be signed in to change notification settings - Fork 0
/
timemanager.js
101 lines (85 loc) · 2.58 KB
/
timemanager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* GDevelop JS Platform
* Copyright 2013-2016 Florian Rival ([email protected]). All rights reserved.
* This project is released under the MIT License.
*/
/**
* Manage the timers and times elapsed during last
* frame, since the beginning of the scene and other time related values.
*
* @class TimeManager
* @memberof gdjs
*/
gdjs.TimeManager = function()
{
this.reset();
}
gdjs.TimeManager.prototype.reset = function() {
this._elapsedTime = 0;
this._timeScale = 1;
this._timeFromStart = 0;
this._firstFrame = true;
this._timers = new Hashtable();
}
gdjs.TimeManager.prototype.update = function(elapsedTime, minimumFPS) {
if (this._firstUpdateDone) this._firstFrame = false;
this._firstUpdateDone = true;
//Compute the elapsed time since last frame
this._elapsedTime = Math.min(elapsedTime, 1000/minimumFPS);
this._elapsedTime *= this._timeScale;
//Update timers and others members
for(var name in this._timers.items) {
if (this._timers.items.hasOwnProperty(name)) {
this._timers.items[name].updateTime(this._elapsedTime);
}
}
this._timeFromStart += this._elapsedTime;
};
/**
* Set the time scale: time will be slower if time scale is < 1,
* faster if > 1.
* @param {number} timeScale The new time scale (must be positive).
*/
gdjs.TimeManager.prototype.setTimeScale = function(timeScale) {
if ( timeScale >= 0 ) this._timeScale = timeScale;
};
/**
* Get the time scale.
* @return {number} The time scale (positive, 1 is normal speed).
*/
gdjs.TimeManager.prototype.getTimeScale = function() {
return this._timeScale;
};
/**
* Get the time since the instanciation of the manager (i.e: since
* the beginning of the scene most of the time), in milliseconds.
*/
gdjs.TimeManager.prototype.getTimeFromStart = function() {
return this._timeFromStart;
};
/**
* Return true if update was called only once (i.e: if the scene
* is rendering its first frame).
*/
gdjs.TimeManager.prototype.isFirstFrame = function() {
return this._firstFrame;
};
/**
* Return the time elapsed since the last call to update
* (i.e: the last frame), in milliseconds.
*/
gdjs.TimeManager.prototype.getElapsedTime = function() {
return this._elapsedTime;
};
gdjs.TimeManager.prototype.addTimer = function(name) {
this._timers.put(name, new gdjs.Timer(name));
};
gdjs.TimeManager.prototype.hasTimer = function(name) {
return this._timers.containsKey(name);
};
gdjs.TimeManager.prototype.getTimer = function(name) {
return this._timers.get(name);
};
gdjs.TimeManager.prototype.removeTimer = function(name) {
if (this._timers.containsKey(name)) this._timers.remove(name);
};