-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.go
76 lines (61 loc) · 1.32 KB
/
clock.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
69
70
71
72
73
74
75
76
package eugor
import (
"container/heap"
"fmt"
)
type Action struct {
actor interface{}
happensAt int
}
type ActionQueue []*Action
func (aq ActionQueue) Peek() *Action {
if len(aq) <= 0 {
return nil
}
return aq[0]
}
func (aq ActionQueue) Len() int { return len(aq) }
func (aq ActionQueue) Less(i, j int) bool {
return aq[i].happensAt < aq[j].happensAt
}
func (aq ActionQueue) Swap(i, j int) {
aq[i], aq[j] = aq[j], aq[i]
}
func (aq *ActionQueue) Push(x interface{}) {
action := x.(*Action)
*aq = append(*aq, action)
}
func (aq *ActionQueue) Pop() interface{} {
old := *aq
n := len(old)
action := old[n-1]
*aq = old[0 : n-1]
return action
}
func (aq ActionQueue) String() string {
result := "["
for _, item := range aq {
result = result + fmt.Sprintf("%d, ", item.happensAt)
}
result = result + "]"
return result
}
type Clock struct {
currentTime int
actions ActionQueue
}
func MakeClock() *Clock {
return &Clock{currentTime: 0, actions: make(ActionQueue, 0)}
}
func (c *Clock) ActsIn(actor interface{}, ticks int) {
action := &Action{actor: actor, happensAt: c.currentTime + ticks}
heap.Push(&c.actions, action)
}
func (c *Clock) Time() int {
return c.currentTime
}
func (c *Clock) Tick() interface{} {
action := heap.Pop(&c.actions).(*Action)
c.currentTime = action.happensAt
return action.actor
}