-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phase.js
45 lines (44 loc) · 798 Bytes
/
Phase.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
// Phase v1.0
class Phase {
constructor(init, update, draw, end){
this.init = init || this.pass;
this.update = update || this.pass;
this.draw = draw || this.pass;
this.end = end || this.pass;
this.waiting = false;
this.interval = null;
}
pass(){}
begin(framerate){
this.init(this);
this.resume(framerate);
}
stop(){
this.pause();
this.end(this);
}
switchTo(that, framerate){
this.stop();
that.begin(framerate);
}
wait(time){
this.waiting = true;
setTimeout(()=>{
this.waiting = false
}, time);
}
pause(){
clearInterval(this.interval);
this.interval = null;
}
resume(framerate){
if(!this.interval){
this.interval = setInterval(() => {
if(!this.waiting){
this.update(this);
this.draw(this);
}
}, 1000/framerate);
}
}
}