-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.go
41 lines (35 loc) · 879 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
package main
import (
"fmt"
"time"
)
// TimerHandler holds/handles all timers in the game
type TimerHandler struct {
timers []Timer
numTimers int
}
// Timer is a timer that is X seconds long
type Timer struct {
timer *time.Timer
id int
fired bool
}
// Creates a new timer for X seconds
func (t *TimerHandler) createTimer(seconds int) {
t.timers = append(t.timers, Timer{time.NewTimer(time.Duration(seconds) * time.Second), t.numTimers, false})
t.numTimers++
}
// This runs the timer then removes it after it fires
func (t *TimerHandler) runTimer(id int) {
go func() {
<-t.timers[id].timer.C
t.timers[id].fired = true
fmt.Println("timer with ID ", id, " fired")
t.timers = t.remove(id)
t.numTimers--
}()
}
// Removes a timer from the timers slice
func (t *TimerHandler) remove(id int) []Timer {
return append(t.timers[:id], t.timers[id+1:]...)
}