/
timer.go
68 lines (52 loc) · 1002 Bytes
/
timer.go
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
package sk
var nextTimerId ID = 0
type Timer struct {
DeltaTime float64
id ID
scale float64
paused bool
attached map[ID]*Timer
}
func NewTimer() *Timer {
t := &Timer{
DeltaTime: 0,
id: nextTimerId,
scale: 1,
paused: false,
attached: make(map[ID]*Timer),
}
nextTimerId++
return t
}
// Delta time in seconds
func (t *Timer) Update(dt float64) {
if t.paused {
t.DeltaTime = 0
return
}
t.DeltaTime = dt * t.scale
for _, other := range t.attached {
other.Update(dt)
}
}
func (t *Timer) IsPaused() bool {
return t.paused
}
func (t *Timer) Pause() {
t.paused = true
}
func (t *Timer) Resume() {
t.paused = false
}
func (t *Timer) GetScale() float64 {
return t.scale
}
func (t *Timer) SetScale(scale float64) {
t.scale = scale
}
func (t *Timer) Attach(other *Timer) {
t.attached[other.id] = other
}
func (t *Timer) Detach(other *Timer) {
delete(t.attached, other.id)
}