-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
59 lines (48 loc) · 1.2 KB
/
controller.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
// Package maze, controlling the moving objects
package maze
import (
"fmt"
"time"
)
// Controller manages the actors on the level, responds to keyboard events and
// renders the maze.
type Controller struct {
frame int
level *Level
render Renderer
}
func NewController(level *Level, render Renderer) *Controller {
c := Controller{level: level, render: render}
return &c
}
func (c *Controller) Start() {
for _, actor := range c.level.Actors {
actor.PathNav.Initialize(c.level, actor)
}
}
// RunLoop is called in a loop to update the state of the moving objects,
// render the maze and collect keyboard events.
func (c *Controller) RunLoop() bool {
Render(*c.level, fmt.Sprintf("render #%d", c.frame), c.render)
isDone := false
for _, actor := range c.level.Actors {
actor.PathNav.NextPosition()
if actor.HasFinished() {
isDone = true
}
}
select {
case k := <-c.render.GetKeyboardEvent():
if k == KBEventCancel {
isDone = true
}
default:
}
time.Sleep(100 * time.Millisecond)
c.frame++
return !isDone
}
func (c *Controller) Done() {
Render(*c.level, fmt.Sprintf("Woohoo! Done after %d iterations. Press any key to exit...", c.frame), c.render)
<-c.render.GetKeyboardEvent()
}