-
Notifications
You must be signed in to change notification settings - Fork 3
/
timer.go
62 lines (51 loc) · 1.26 KB
/
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
package timer
import "math/rand"
// Timer measures a elapsed time, its very simple but effective
type Timer struct {
Progress, Period float64
}
// Period returns timer with given period
func Period(period float64) Timer {
return Timer{Period: period}
}
// Progress returns timer with given progress and period
func Progress(progress float64, period float64) Timer {
return Timer{progress, period}
}
func Random(period float64) Timer {
return Timer{period * rand.Float64(), period}
}
// Tick increases progress by delta
func (t *Timer) Tick(delta float64) {
t.Progress += delta
}
// Done returns whether progress exceeded period
func (t Timer) Done() bool {
return t.Progress >= t.Period
}
// Reset sets progres to 0
func (t *Timer) Reset() {
t.Progress = 0
}
// DoneReset returns true and resets the timer
func (t *Timer) DoneReset() bool {
if t.Progress >= t.Period {
t.Progress = 0
return true
}
return false
}
// TickDone does Tick and Done in one step
func (t *Timer) TickDone(delta float64) bool {
t.Progress += delta
return t.Progress >= t.Period
}
// TickDoneReset does tick and if Done that it resets
func (t *Timer) TickDoneReset(delta float64) bool {
t.Progress += delta
if t.Progress > t.Period {
t.Progress = 0
return true
}
return false
}