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