-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.go
60 lines (50 loc) · 1.05 KB
/
control.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
package galaxy
import (
"time"
)
const animationSpeed = 50 * time.Millisecond
type GameInterface interface {
Player() (Player, error)
Run(cmdQueue <-chan Command, draw chan<- Game) error
Probe(x, y int) *Planet
}
func (game *Game) Player() (Player, error) {
return Player(1), nil // Local player
}
func (game *Game) Run(cmdQueue <-chan Command, draw chan<- Game) error {
defer close(draw)
fallingTimer := time.NewTicker(animationSpeed)
draw <- *game
var cmds []Command
for {
select {
case cmd := <-cmdQueue:
if cmd.CommandType == CommandQuit {
break
}
cmds = append(cmds, cmd)
case <-fallingTimer.C:
game.Tick(cmds)
cmds = nil
// Sole player on
if len(game.CountPlanetsByPlayer()) == 1 {
return nil
}
draw <- *game
}
}
}
func (g *Game) Probe(x int, y int) *Planet {
mouse := Vec2{float32(x), float32(y)}
var min *Planet
minDist := 100000.0
for label, planet := range g.Planets {
d := dist(mouse, planet.Center)
if d < minDist {
minDist = d
min = planet
min.Id = label // XXX
}
}
return min
}