-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcountdown.ts
More file actions
66 lines (53 loc) · 1.46 KB
/
countdown.ts
File metadata and controls
66 lines (53 loc) · 1.46 KB
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
class Countdown {
private running: boolean;
private loadTime: number;
private endTime: number;
constructor() {
this.running = false;
}
public load(seconds: number): void {
const wasRunning = this.running;
if (wasRunning)
this.stop();
this.loadTime = Math.round(seconds * 1000);
if (wasRunning)
this.start();
}
public add(seconds: number): void {
const wasRunning = this.running;
if (wasRunning)
this.stop();
this.loadTime += Math.round(seconds * 1000);
if (wasRunning)
this.start();
}
public isExpired(): boolean {
if (this.running)
return this.endTime < game.runtime();
else
return this.loadTime < 0;
}
public start(): void {
if (!this.running) {
this.endTime = game.runtime() + this.loadTime;
this.running = true;
}
}
public stop(): void {
if (this.running) {
this.loadTime = this.loadTime - game.runtime();
this.running = false;
}
}
public remainingTimeMs(): number {
let result: number;
if (this.running)
result = this.endTime - game.runtime();
else
result = this.loadTime;
return result < 0 ? 0 : result;
}
public remainingTime(): number {
return Math.idiv(this.remainingTimeMs(), 1000);
}
}