-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTime.js
110 lines (85 loc) · 2.66 KB
/
Time.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
102
103
104
105
106
107
108
109
110
import EventDispatcher from 'foam-event/EventDispatcher';
import TimeEvent from './TimeEvent';
export const State = Object.freeze({
PAUSED : 'paused',
STOPPED : 'stopped',
RUNNING : 'running'
});
class Time extends EventDispatcher {
/*----------------------------------------------------------------------------------------------------------------*/
// Constructor
/*----------------------------------------------------------------------------------------------------------------*/
constructor(){
super();
this._start = 0;
this._now = 0;
this._previous = 0;
this._elapsed = 0;
this._frame = 0;
this._delta = 0;
this._deltaSeconds = 0;
this._framesElapsed = 0;
this._secondsElapsed = 0;
this._reset();
this._state = State.STOPPED;
}
getState(){
return this._state;
}
_reset(){
this._start = 0;
this._now = 0;
this._previous = 0;
this._elapsed = 0;
this._frame = 0;
this._delta = 0;
this._deltaSeconds = 0;
}
/*----------------------------------------------------------------------------------------------------------------*/
// State check
/*----------------------------------------------------------------------------------------------------------------*/
isStopped(){
return this._state === State.STOPPED;
}
isPaused(){
return this._state === State.PAUSED;
}
/*----------------------------------------------------------------------------------------------------------------*/
// Getter
/*----------------------------------------------------------------------------------------------------------------*/
getStart(){
return this._start;
}
getNow(){
return this._now;
}
getSecondsElapsed(){
return this._secondsElapsed;
}
getDelta(){
return this._delta;
}
toString(){
return `
start: ${this._start}
now: ${this._now}
prev: ${this._prev}
frame: ${this._frame}
delta: ${this._delta}
elapsed: ${this._secondsElapsed}`;
}
/*----------------------------------------------------------------------------------------------------------------*/
// Shared
/*----------------------------------------------------------------------------------------------------------------*/
makeShared(){
Time.__sharedTime = this;
}
static sharedTime(){
if(!Time.__sharedTime){
throw new Error('Time not initialized.');
}
return Time.__sharedTime;
}
}
Time.__sharedTime = null;
export default Time;