-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile.go
113 lines (93 loc) · 1.9 KB
/
tile.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package antwar
import (
"fmt"
"image/color"
)
type Tile struct {
ants AntSet
food int
team *Team
board *Board
pos Pos
antHill *AntHill
here, north, east, south, west *Tile
}
func (t *Tile) removeAnt(theAnt *Ant) {
t.ants.Remove(theAnt)
}
func (t *Tile) putAnt(a *Ant) {
if t.team != a.team {
t.killAnts()
t.destroyAntHill()
t.team = a.team
}
t.ants.Put(a)
a.tile = t
}
func (t *Tile) putFood(amount int) {
t.food += amount
}
func (t *Tile) removeFood(amount int) {
t.food -= amount
}
func (t *Tile) createAntHill(team *Team) *AntHill {
anthill := new(AntHill)
anthill.team = team
anthill.tile = t
t.team = team
t.antHill = anthill
fmt.Printf("Created anthill at %v for team %v\n", anthill.tile.pos, team.name)
return anthill
}
func (t *Tile) destroyAntHill() {
if t.antHill == nil {
return
}
fmt.Printf("Destroyed anthill for %v", t.antHill.team.name)
t.board.removeAntHill(t.antHill)
t.antHill = nil
}
func (t *Tile) killAnts() {
if 0 == t.ants.Len() {
return
}
t.ants.Do(func(a *Ant) {
t.board.ants.Remove(a)
t.ants.Remove(a)
a.team.ants.Remove(a)
})
}
func (t *Tile) color() color.Color {
if t.ants.Len() > 0 {
return t.team.color
}
if t.team != nil {
r, g, b, _ := t.team.color.RGBA()
return color.RGBA{uint8(int(r) << 7), uint8(int(g) << 7), uint8(int(b) << 7), 100}
}
return color.RGBA{255, 255, 255, uint8(t.food)}
}
func (t *Tile) update() {
t.board.Update(t)
}
func (t *Tile) AntCount() int {
return t.ants.Len()
}
func (t *Tile) FoodCount() int {
return t.food
}
func (t *Tile) Here() *Tile {
return t
}
func (t *Tile) North() *Tile {
return t.north
}
func (t *Tile) East() *Tile {
return t.east
}
func (t *Tile) South() *Tile {
return t.south
}
func (t *Tile) West() *Tile {
return t.west
}