-
Notifications
You must be signed in to change notification settings - Fork 9
/
Timer.js
48 lines (38 loc) · 1.21 KB
/
Timer.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
var Timer = pc.createScript('timer');
Timer.prototype.initialize = function() {
//values
this.originalValue = this.cloneNumber(this.entity.element.text);
this.currentValue = this.cloneNumber(this.originalValue);
//global events
this.app.on('Timer:' + this.entity.name, this.setTimer, this);
this.on('state', this.onStateChange, this);
this.on('destroy', this.onDestroy, this);
//for initalizing
this.onStateChange(true);
};
Timer.prototype.onDestroy = function() {
this.app.off('Timer:' + this.entity.name, this.setTimer, this);
};
Timer.prototype.setTimer = function() {
this.currentValue = this.cloneNumber(this.originalValue);
};
Timer.prototype.onStateChange = function(state) {
if(state === true){
clearInterval(this.timer);
this.timer = setInterval(function(self){
self.setNextValue();
}, 1000, this);
}else{
clearInterval(this.timer);
}
};
Timer.prototype.setNextValue = function() {
if(this.currentValue <= 0){
return false;
}
this.currentValue--;
this.entity.element.text = this.currentValue + '';
};
Timer.prototype.cloneNumber = function(value) {
return parseInt(value + '');
};