-
Notifications
You must be signed in to change notification settings - Fork 42
/
space.go
executable file
·292 lines (202 loc) · 7.19 KB
/
space.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package resolv
import (
"math"
)
// Space represents a collision space. Internally, each Space contains a 2D array of Cells, with each Cell being the same size. Cells contain information on which
// Objects occupy those spaces.
type Space struct {
Cells [][]*Cell
objects []*Object
CellWidth, CellHeight int // Width and Height of each Cell in "world-space" / pixels / whatever
}
// NewSpace creates a new Space. spaceWidth and spaceHeight is the width and height of the Space (usually in pixels), which is then populated with cells of size
// cellWidth by cellHeight. Generally, you want cells to be the size of the smallest collide-able objects in your game, and you want to move Objects at a maximum
// speed of one cell size per collision check to avoid missing any possible collisions.
func NewSpace(spaceWidth, spaceHeight, cellWidth, cellHeight int) *Space {
sp := &Space{
CellWidth: cellWidth,
CellHeight: cellHeight,
}
sp.Resize(spaceWidth/cellWidth, spaceHeight/cellHeight)
// sp.Resize(int(math.Ceil(float64(spaceWidth)/float64(cellWidth))),
// int(math.Ceil(float64(spaceHeight)/float64(cellHeight))))
return sp
}
// Add adds the specified Objects to the Space, updating the Space's cells to refer to the Object.
func (sp *Space) Add(objects ...*Object) {
for _, obj := range objects {
obj.Space = sp
// We call Update() once to make sure the object gets its cells added.
obj.Update()
}
sp.objects = append(sp.objects, objects...)
}
// Remove removes the specified Objects from being associated with the Space. This should be done whenever an Object is removed from the
// game.
func (sp *Space) Remove(objects ...*Object) {
for _, obj := range objects {
for _, cell := range obj.TouchingCells {
cell.unregister(obj)
}
obj.TouchingCells = []*Cell{}
obj.Space = nil
for i, o := range sp.objects {
if o == obj {
sp.objects[i] = nil
sp.objects = append(sp.objects[:i], sp.objects[i+1:]...)
break
}
}
}
}
func (sp *Space) removeFromCells(objects ...*Object) {
for _, obj := range objects {
for _, cell := range obj.TouchingCells {
cell.unregister(obj)
}
obj.TouchingCells = []*Cell{}
}
}
// Objects returns a new slice of the objects in the Space.
func (s *Space) Objects() []*Object {
return append(make([]*Object, 0, len(s.objects)), s.objects...)
}
// ForEachObject iterates through each Object in the Space and runs the provided function on them, passing the object, its index in the
// objects slice, and the maximum number of objects in the space.
func (s *Space) ForEachObject(forEach func(object *Object, index, maxCount int)) {
for i, o := range s.objects {
forEach(o, i, len(s.objects))
}
}
// Resize resizes the internal Cells array.
func (sp *Space) Resize(width, height int) {
sp.Cells = [][]*Cell{}
for y := 0; y < height; y++ {
sp.Cells = append(sp.Cells, []*Cell{})
for x := 0; x < width; x++ {
sp.Cells[y] = append(sp.Cells[y], newCell(x, y))
}
}
}
// Cell returns the Cell at the given cellular / spatial (not world) X and Y position in the Space. If the X and Y position are
// out of bounds, Cell() will return nil.
func (sp *Space) Cell(x, y int) *Cell {
if y >= 0 && y < len(sp.Cells) && x >= 0 && x < len(sp.Cells[y]) {
return sp.Cells[y][x]
}
return nil
}
// CheckCells checks a set of cells (from x,y to x + w, y + h in cellular coordinates) and returns
// a slice of the objects found within those Cells.
// The objects must have any of the tags provided (if any are provided).
func (sp *Space) CheckCells(x, y, w, h int, tags ...string) []*Object {
res := []*Object{}
for ix := x; ix < x+w; ix++ {
for iy := y; iy < y+h; iy++ {
cell := sp.Cell(ix, iy)
if cell != nil {
if len(tags) > 0 {
if cell.ContainsTags(tags...) {
for _, obj := range cell.Objects {
if obj.HasTags(tags...) {
res = append(res, obj)
}
}
}
} else if cell.Occupied() {
res = append(res, cell.Objects...)
}
}
}
}
return res
}
// CheckWorld checks the cells of the Grid with the given world coordinates.
// Internally, this is just syntactic sugar for calling Space.WorldToSpace() on the
// position and size given.
func (sp *Space) CheckWorld(x, y, w, h float64, tags ...string) []*Object {
sx, sy := sp.WorldToSpace(x, y)
cw, ch := sp.WorldToSpace(w, h)
return sp.CheckCells(sx, sy, cw, ch, tags...)
}
// CheckWorldVec checks the cells of the Grid with the given world coordinates.
// This function takes vectors for the position and size of the checked area.
// Internally, this is just syntactic sugar for calling Space.WorldToSpace() on the
// position and size given.
func (sp *Space) CheckWorldVec(pos, size Vector, tags ...string) []*Object {
sx, sy := sp.WorldToSpace(pos.X, pos.Y)
cw, ch := sp.WorldToSpace(size.X, size.Y)
return sp.CheckCells(sx, sy, cw, ch, tags...)
}
// UnregisterAllObjects unregisters all Objects registered to Cells in the Space.
func (sp *Space) UnregisterAllObjects() {
for y := 0; y < len(sp.Cells); y++ {
for x := 0; x < len(sp.Cells[y]); x++ {
cell := sp.Cells[y][x]
sp.Remove(cell.Objects...)
}
}
}
// WorldToSpace converts from a world position (x, y) to a position in the Space (a grid-based position).
func (sp *Space) WorldToSpace(x, y float64) (int, int) {
fx := int(math.Floor(x / float64(sp.CellWidth)))
fy := int(math.Floor(y / float64(sp.CellHeight)))
return fx, fy
}
// WorldToSpaceVec converts from a world position Vector to a position in the Space (a grid-based position).
func (sp *Space) WorldToSpaceVec(position Vector) (int, int) {
return sp.WorldToSpace(position.X, position.Y)
}
// SpaceToWorld converts from a position in the Space (on a grid) to a world-based position, given the size of the Space when first created.
func (sp *Space) SpaceToWorld(x, y int) (float64, float64) {
fx := float64(x * sp.CellWidth)
fy := float64(y * sp.CellHeight)
return fx, fy
}
func (sp *Space) SpaceToWorldVec(x, y int) Vector {
outX, outY := sp.SpaceToWorld(x, y)
return Vector{outX, outY}
}
// Height returns the height of the Space grid in Cells (so a 320x240 Space with 16x16 cells would have a height of 15).
func (sp *Space) Height() int {
return len(sp.Cells)
}
// Width returns the width of the Space grid in Cells (so a 320x240 Space with 16x16 cells would have a width of 20).
func (sp *Space) Width() int {
if len(sp.Cells) > 0 {
return len(sp.Cells[0])
}
return 0
}
func (sp *Space) CellsInLine(startX, startY, endX, endY int) []*Cell {
cells := []*Cell{}
cell := sp.Cell(startX, startY)
endCell := sp.Cell(endX, endY)
if cell != nil && endCell != nil {
dv := Vector{float64(endX - startX), float64(endY - startY)}.Unit()
dv.X *= float64(sp.CellWidth / 2)
dv.Y *= float64(sp.CellHeight / 2)
pX, pY := sp.SpaceToWorld(startX, startY)
p := Vector{pX + float64(sp.CellWidth/2), pY + float64(sp.CellHeight/2)}
alternate := false
for cell != nil {
if cell == endCell {
cells = append(cells, cell)
break
}
cells = append(cells, cell)
if alternate {
p.Y += dv.Y
} else {
p.X += dv.X
}
cx, cy := sp.WorldToSpace(p.X, p.Y)
c := sp.Cell(cx, cy)
if c != cell {
cell = c
}
alternate = !alternate
}
}
return cells
}