-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
179 lines (164 loc) · 3.9 KB
/
grid.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"errors"
"github.com/veandco/go-sdl2/sdl"
)
// special value used for the "From" of the start node
var NOWHERE = Position{-1, -1}
// deltas = neighbor x, y offsets
//
// Y ^
// |
// | -1, 1 0, 1 1, 1
// |
// | -1, 0 [cur] 1, 0
// |
// | -1, -1 0, -1 1, -1
// |
// |
// --------------------------------->
// X
var deltas = [][2]int{
[2]int{-1, 1},
[2]int{0, 1},
[2]int{1, 1},
[2]int{-1, 0},
[2]int{1, 0},
[2]int{-1, -1},
[2]int{0, -1},
[2]int{1, -1},
}
type Grid struct {
W int
H int
Cells [][]int
start *Position
end *Position
path []PositionPair
r *sdl.Renderer
st *sdl.Texture
}
// Construct a new grid, along with its SDL texture, generating random terrain
func NewGrid(r *sdl.Renderer) *Grid {
st, err := r.CreateTexture(
sdl.PIXELFORMAT_RGBA8888,
sdl.TEXTUREACCESS_TARGET,
WINDOW_WIDTH,
WINDOW_HEIGHT)
if err != nil {
panic(err)
}
g := Grid{
W: GRID_CELL_DIMENSION,
H: GRID_CELL_DIMENSION,
Cells: MakeTerrain(GRID_CELL_DIMENSION, GRID_CELL_DIMENSION),
r: r,
st: st,
}
g.UpdateTexture()
return &g
}
// delete the saved path, start, and end data
func (g *Grid) Clear() {
g.path = g.path[:0]
if g.start != nil {
g.Cells[g.start.X][g.start.Y] = EMPTY
g.start = nil
}
if g.end != nil {
g.Cells[g.end.X][g.end.Y] = EMPTY
g.end = nil
}
}
// tests if a position is in the grid bounds
func (g *Grid) InGrid(p Position) bool {
return p.X >= 0 && p.X < GRID_CELL_DIMENSION &&
p.Y >= 0 && p.Y < GRID_CELL_DIMENSION
}
// tests if a position contains an obstacle
func (g *Grid) IsObstacle(p Position) bool {
return g.Cells[p.X][p.Y] == OBSTACLE
}
// returns the neighbor position given an offset 'delta' or error if not a valid
// neighbor (returns error on cross-corners)
func (g *Grid) NbrOf(cur Position, delta [2]int) (
pos Position, dist int, err error) {
nbr := Position{
cur.X + delta[0],
cur.Y + delta[1],
}
if !g.InGrid(nbr) ||
g.IsObstacle(nbr) ||
// excludes cells which cross an obstacle on the corner
(delta[0]*delta[1] != 0 &&
g.Cells[cur.X][nbr.Y] == OBSTACLE ||
g.Cells[nbr.X][cur.Y] == OBSTACLE) {
return NOWHERE, -1, errors.New("invalid neighbor")
} else {
// dist is an integer expression of the distance from
// cur to the neighbor cell we're looking at here.
// if either x or y offset is 0, we're moving straight,
// so put 10. Otherwise we're moving diagonal, so put 14
// (these are 1 and sqrt(2), but made into integers for speed)
if delta[0]*delta[1] == 0 {
dist = 10
} else {
dist = 14
}
return nbr, dist, nil
}
}
func (g *Grid) SetStart(start Position) {
g.start = &start
g.Cells[start.X][start.Y] = START
}
func (g *Grid) SetEnd(end Position) {
g.end = &end
g.Cells[end.X][end.Y] = END
}
// redraw the texture according to current state
func (g *Grid) UpdateTexture() {
g.r.SetRenderTarget(g.st)
defer g.r.SetRenderTarget(nil)
g.r.SetDrawColor(0, 0, 0, 0)
g.r.Clear()
g.DrawGrid()
g.DrawPath()
}
// draw the grid cells (EMPTY, OBSTACLE, START, END) to `st`
func (g *Grid) DrawGrid() {
for x := 0; x < GRID_CELL_DIMENSION; x++ {
for y := 0; y < GRID_CELL_DIMENSION; y++ {
var c sdl.Color
kind := g.Cells[x][y]
switch kind {
case EMPTY:
c = sdl.Color{R: 0, G: 0, B: 0}
case OBSTACLE:
c = sdl.Color{R: 255, G: 0, B: 0}
case START:
c = sdl.Color{R: 0, G: 255, B: 0}
case END:
c = sdl.Color{R: 0, G: 255, B: 255}
}
drawRect(g.r,
Rect2D{
float64(x * GRIDCELL_WORLD_W),
float64(y * GRIDCELL_WORLD_H),
GRIDCELL_WORLD_W,
GRIDCELL_WORLD_H},
c)
}
}
}
// draw the path to `st`
func (g *Grid) DrawPath() {
for _, pp := range g.path {
p1 := GridCellSpaceToGridWorldSpace(pp.p1)
p2 := GridCellSpaceToGridWorldSpace(pp.p2)
drawVector(g.r,
p1,
p2.Sub(p1),
sdl.Color{R: 255, G: 255, B: 255})
}
}