Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/libs/Editor_files/Timer.js
4627 views
1
/*!
2
* @class bkcore.Timer
3
*
4
* new Date().getTime() wrapper to use as timers.
5
*
6
* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>
7
*/
8
9
/**
10
* RAF shim
11
*/
12
window.requestAnimFrame = (function(){
13
return window.requestAnimationFrame ||
14
window.webkitRequestAnimationFrame ||
15
window.mozRequestAnimationFrame ||
16
window.oRequestAnimationFrame ||
17
window.msRequestAnimationFrame ||
18
function( callback ){
19
window.setTimeout(callback, 1000 / 60);
20
};
21
})();
22
23
/*!
24
* @package bkcore
25
*/
26
var bkcore = bkcore || {};
27
28
/*!
29
* Creates a new timer, inactive by default.
30
* Call Timer.start() to activate.
31
*/
32
bkcore.Timer = function()
33
{
34
this.time = {
35
start: 0,
36
current: 0,
37
previous: 0,
38
elapsed: 0,
39
delta: 0
40
}
41
42
this.active = false;
43
}
44
45
/*!
46
* Starts/restarts the timer.
47
*/
48
bkcore.Timer.prototype.start = function()
49
{
50
var now = new Date().getTime();
51
52
this.time.start = now;
53
this.time.current = now;
54
this.time.previous = now;
55
this.time.elapsed = 0;
56
this.time.delta = 0;
57
58
this.active = true;
59
}
60
61
/*!
62
* Restarts timer, returning last ms tick
63
*/
64
bkcore.Timer.prototype.restart = function()
65
{
66
var now = new Date().getTime();
67
var e = now - this.time.start;
68
69
this.time.start = now;
70
this.time.current = now;
71
this.time.previous = now;
72
this.time.elapsed = 0;
73
this.time.delta = 0;
74
75
this.active = true;
76
77
return e;
78
}
79
80
/*!
81
* Pauses(true)/Unpauses(false) the timer.
82
*
83
* @param bool Do pause
84
*/
85
bkcore.Timer.prototype.pause = function(bool)
86
{
87
this.active = !bool;
88
}
89
90
/*!
91
* Update method to be called inside a RAF loop
92
*/
93
bkcore.Timer.prototype.update = function()
94
{
95
if(!this.active) return;
96
97
var now = new Date().getTime();
98
99
this.time.current = now;
100
this.time.elapsed = this.time.current - this.time.start;
101
this.time.delta = now - this.time.previous;
102
this.time.previous = now;
103
104
return this.time.elapsed;
105
}
106
107
/*!
108
* Returns elapsed milliseconds
109
*/
110
bkcore.Timer.prototype.getElapsed = function()
111
{
112
return this.time.elapsed;
113
}
114
115
/*!
116
* Returns a formatted version of the current elapsed time using msToTime().
117
*
118
*
119
*/
120
bkcore.Timer.prototype.getElapsedTime = function()
121
{
122
return bkcore.Timer.msToTime(this.time.elapsed);
123
}
124
125
/*!
126
* Formats a millisecond integer into a h/m/s/ms object
127
*
128
* @param x int In milliseconds
129
* @return Object{h,m,s,ms}
130
*/
131
bkcore.Timer.msToTime = function(t)
132
{
133
var ms, s, m, h;
134
135
ms = t%1000;
136
137
s = Math.floor((t/1000)%60);
138
139
m = Math.floor((t/60000)%60);
140
h = Math.floor((t/3600000));
141
142
return {h:h, m:m, s:s, ms:ms};
143
}
144
145
/*!
146
* Formats a millisecond integer into a h/m/s/ms object with prefix zeros
147
*
148
* @param x int In milliseconds
149
* @return Object<string>{h,m,s,ms}
150
*/
151
bkcore.Timer.msToTimeString = function(t)
152
{
153
var ms, s, m, h;
154
155
ms = t%1000;
156
if(ms < 10) ms = "00"+ms;
157
else if(ms < 100) ms = "0"+ms;
158
159
s = Math.floor((t/1000)%60);
160
if(s < 10) s = "0"+s;
161
162
m = Math.floor((t/60000)%60);
163
h = Math.floor((t/3600000));
164
165
return {h:h, m:m, s:s, ms:ms};
166
}
167