forked from SolarLune/resolv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
space.go
executable file
·265 lines (179 loc) · 6.14 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
package resolv
import (
"math"
"github.com/quartercastle/vector"
)
// 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
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) {
if sp == nil {
panic("ERROR: space is nil")
}
for _, obj := range objects {
obj.Space = sp
// We call Update() once to make sure the object gets its cells added.
obj.Update()
}
}
// 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) {
if sp == nil {
panic("ERROR: space is nil")
}
for _, obj := range objects {
for _, cell := range obj.TouchingCells {
cell.unregister(obj)
}
obj.TouchingCells = []*Cell{}
obj.Space = nil
}
}
// Objects loops through all Cells in the Space (from top to bottom, and from left to right) to return all Objects
// that exist in the Space. Of course, each Object is counted only once.
func (sp *Space) Objects() []*Object {
objectsAdded := map[*Object]bool{}
objects := []*Object{}
for cy := range sp.Cells {
for cx := range sp.Cells[cy] {
for _, o := range sp.Cells[cy][cx].Objects {
if _, added := objectsAdded[o]; !added {
objects = append(objects, o)
objectsAdded[o] = true
}
}
}
}
return 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 return the first object within those Cells that contains any of the tags given.
// If no tags are given, then CheckCells will return the first Object found in any Cell.
func (sp *Space) CheckCells(x, y, w, h int, tags ...string) *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...) {
return obj
}
}
}
} else if cell.Occupied() {
return cell.Objects[0]
}
}
}
}
return nil
}
// CheckCellsWorld 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) CheckCellsWorld(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...)
}
// 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
}
// 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
}
// 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.Vector{float64(endX - startX), float64(endY - startY)}.Unit()
dv[0] *= float64(sp.CellWidth / 2)
dv[1] *= float64(sp.CellHeight / 2)
pX, pY := sp.SpaceToWorld(startX, startY)
p := vector.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[1] += dv[1]
} else {
p[0] += dv[0]
}
cx, cy := sp.WorldToSpace(p[0], p[1])
c := sp.Cell(cx, cy)
if c != cell {
cell = c
}
alternate = !alternate
}
}
return cells
}