-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayer.go
More file actions
345 lines (294 loc) · 8.54 KB
/
layer.go
File metadata and controls
345 lines (294 loc) · 8.54 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package egriden
import (
"container/list"
"fmt"
"iter"
"github.com/greenthepear/imggg"
"github.com/hajimehoshi/ebiten/v2"
)
type Layer interface {
// Draw the Gobject's sprite based on the position in the layer and offset.
DrawSprite(o Gobject, on *ebiten.Image)
// Draw any ebiten.Image with applied position of the Gobject within the
// layer.
DrawLikeSprite(img *ebiten.Image, o Gobject, on *ebiten.Image)
CurrentAnchor() imggg.Point[float64]
// Iterator for all Gobjects in a layer.
//
// In case of a GridLayer in Sparse draw mode it iterates over a map so the
// order will be random. Use [GridLayer.AllCells] to avoid this.
AllGobjects() iter.Seq[Gobject]
// Run OnUpdateFunc for Gobjects that have it, in order of the Gobjects'
// addition to the layer.
RunThinkers()
// Delete all Gobjects in the layer. This does not trigger Gobjects'
// OnDelete.
Clear()
}
// For optimization there are a couple of ways a grid layer can be draw
// depending on if it has many (Dense) or few (Sparse) gobjects
// most of the time.
type DrawMode int
const (
// Used for sparsely populated grids, ranges over a map for drawing.
Sparse DrawMode = iota
// Used for thickly populated grids, ranges over a slice for drawing.
Dense
)
type Dimensions struct {
Width, Height int
}
// Width and height as ints
func (d Dimensions) WH() (int, int) {
return d.Width, d.Height
}
type GridLayer struct {
Name string // Name of the layer, for convenience sake
z int
cellDimensions Dimensions
layerDimensions Dimensions
// Defines the "gaps" between cells:
// point's X for horizontal gaps length and Y for vertical.
Padding imggg.Point[float64]
// If false no sprite will be drawn, nor layers' gobjects draw scripts
// executed.
Visible bool
mode DrawMode
mapMat map[imggg.Point[int]]Gobject
sliceMat [][]Gobject
// Anchor is the top left point from which the layer is drawn,
// default being (0,0). Can be anywhere, off screen or not.
Anchor imggg.Point[float64]
thinkers *list.List
}
func newGridLayer(
name string, z int,
cellDims Dimensions, gridDims Dimensions,
drawMode DrawMode,
anchor imggg.Point[float64], padding imggg.Point[float64]) *GridLayer {
var mapMat map[imggg.Point[int]]Gobject = nil
var sliceMat [][]Gobject = nil
if drawMode == Sparse {
mapMat = make(
map[imggg.Point[int]]Gobject, gridDims.Width*gridDims.Height)
} else {
sliceMat = make([][]Gobject, gridDims.Height)
for i := range sliceMat {
sliceMat[i] = make([]Gobject, gridDims.Width)
}
}
return &GridLayer{
Name: name,
z: z,
cellDimensions: cellDims,
layerDimensions: gridDims,
Visible: true,
mode: drawMode,
mapMat: mapMat,
sliceMat: sliceMat,
Anchor: anchor,
Padding: padding,
thinkers: list.New(),
}
}
// Parameters for [Level.CreateGridLayerOnTop].
type GridLayerParameters struct {
// Width and height of the layer's grid
GridDimensions Dimensions
// Width and height of individual cells
CellDimensions Dimensions
// Defines the "gaps" between cells:
// point's X for horizontal gaps length and Y for vertical.
PaddingVector imggg.Point[float64]
// Layer's [(GridLayer).Anchor]
Anchor imggg.Point[float64]
// Layer's [DrawMode]
Mode DrawMode
}
// Creates a new GridLayer. Before adding to a level, it will have Z of 0.
//
// GridDimensions and CellDimensions width and height needs to be bigger than 0,
// panics if they aren't.
func NewGridLayer(name string, params GridLayerParameters) *GridLayer {
if params.GridDimensions.Width <= 0 || params.GridDimensions.Height <= 0 {
panic("GridLayer grid width and height need to be bigger than 0")
}
if params.CellDimensions.Width <= 0 || params.CellDimensions.Height <= 0 {
panic("GridLayer cell width and height need to be bigger than 0")
}
return newGridLayer(
name, 0,
params.CellDimensions,
params.GridDimensions,
params.Mode,
params.Anchor,
params.PaddingVector,
)
}
// Returns the Z level
func (l *GridLayer) Z() int {
return l.z
}
func (l GridLayer) CurrentAnchor() imggg.Point[float64] {
return l.Anchor
}
// Layer's anchor point as two floats.
func (l *GridLayer) AnchorXYf() (float64, float64) {
return float64(l.Anchor.X), float64(l.Anchor.Y)
}
// Logical width and height of the grid.
func (l *GridLayer) Dimensions() (int, int) {
return l.layerDimensions.Width, l.layerDimensions.Height
}
// Logical width and height of the grid as a point.
func (l *GridLayer) DimensionsPt() imggg.Point[int] {
return imggg.Pt(l.layerDimensions.Width, l.layerDimensions.Height)
}
// Dimensions of the cell within the grid.
func (l *GridLayer) CellDimensions() (int, int) {
return l.cellDimensions.Width, l.cellDimensions.Height
}
// Dimensions of the cell within the grid as a point.
func (l *GridLayer) CellDimensionsPt() imggg.Point[int] {
return imggg.Pt(l.cellDimensions.Width, l.cellDimensions.Height)
}
func (l GridLayer) AllGobjects() iter.Seq[Gobject] {
return func(yield func(Gobject) bool) {
if l.mode == Sparse {
for _, o := range l.mapMat {
if !yield(o) {
return
}
}
} else {
for cell := range l.AllCells() {
o := cell.Gobject()
if o != nil {
if !yield(o) {
return
}
}
}
}
}
}
// Deletes all gobjects in the layer. This does not trigger OnDelete.
func (l *GridLayer) Clear() {
for o := range l.AllGobjects() {
x, y := o.GridPos().XY()
l.internalDeleteAt(x, y, false, true)
}
}
func (l *GridLayer) RunThinkers() {
var next *list.Element
for e := l.thinkers.Front(); e != nil; e = next {
next = e.Next()
o, ok := e.Value.(Gobject)
if !ok {
panic("non-gobject in thinker list")
}
o.OnUpdate()(o, l)
}
}
// Returns Gobject at x y, nil if empty. Panics if out of bounds.
func (l GridLayer) GobjectAt(x, y int) Gobject {
if !l.IsXYwithinBounds(x, y) {
panic(fmt.Sprintf("GobjectAt() panic! (%d , %d) Out of bounds.", x, y))
}
if l.mode == Sparse {
return l.mapMat[imggg.Point[int]{X: x, Y: y}]
}
return l.sliceMat[y][x]
}
func (l GridLayer) IsOccupiedAt(x, y int) bool {
return l.GobjectAt(x, y) != nil
}
func (l *GridLayer) internalAddGobject(
o Gobject, x, y int) {
o.setGridPos(x, y)
if o.OnUpdate() != nil {
o.setThinkerElement(l.thinkers.PushBack(o))
}
if l.mode == Sparse {
l.mapMat[imggg.Pt(x, y)] = o
return
}
l.sliceMat[y][x] = o
}
// Adds Gobject to the layer at x y.
// Will overwrite the any existing Gobject there.
func (l *GridLayer) AddGobject(o Gobject, x, y int) {
l.internalAddGobject(o, x, y)
if o.OnAdd() != nil {
o.OnAdd()(o, l)
}
}
func (l *GridLayer) internalDeleteAt(x, y int,
triggerDelete bool, removeFromThinkers bool) {
if !l.IsXYwithinBounds(x, y) {
panic("not within layer bounds")
}
o := l.GobjectAt(x, y)
if o != nil {
if removeFromThinkers && o.thinkerElement() != nil {
l.thinkers.Remove(o.thinkerElement())
}
if triggerDelete && o.OnDelete() != nil {
o.OnDelete()(o, l)
}
}
if l.mode == Sparse {
delete(l.mapMat, imggg.Pt(x, y))
return
}
l.sliceMat[y][x] = nil
}
func (l *GridLayer) DeleteAt(x, y int) {
l.internalDeleteAt(x, y, true, true)
}
// Moves Gobject by first finding itself in the layer with its XY coordinates,
// but will panic if the Gobject in that cell is not the same, so you cannot use
// this with Gobjects that are not in the layer, obviously.
//
// Will override any Gobject.
func (l *GridLayer) MoveGobjectTo(o Gobject, x, y int) {
if !l.IsXYwithinBounds(x, y) {
panic("not within layer bounds")
}
fromX, fromY := o.GridPos().XY()
fromGobject := l.GobjectAt(fromX, fromY)
if fromGobject != o {
panic(fmt.Sprintf(
`Gobject '%s' is not the same as in the layer (%p != %p).
Are you referencing one from another layer or one that wasn't added yet?`,
o.Name(), o, fromGobject,
))
}
l.internalDeleteAt(fromX, fromY, false, true)
l.AddGobject(o, x, y)
}
// Swaps objects between two grid positions, if either is empty it will be
// basically the same as moving the object. Panics if out of bounds.
func (l *GridLayer) SwapGobjectsAt(x1, y1, x2, y2 int) {
o1 := l.GobjectAt(x1, y1)
o2 := l.GobjectAt(x2, y2)
if o1 != nil {
l.internalAddGobject(o1, x2, y2)
if o2 == nil {
l.internalDeleteAt(x1, y1, false, false)
}
}
if o2 != nil {
l.internalAddGobject(o2, x1, y1)
if o1 == nil {
l.internalDeleteAt(x2, y2, false, false)
}
}
}
// Swaps objects between two cells, if either is empty it will be basically the
// same as moving the object. Panics if out of bounds.
func (l *GridLayer) SwapObjectsAtCells(cell1, cell2 Cell) {
x1, y1 := cell1.XY()
x2, y2 := cell2.XY()
l.SwapGobjectsAt(x1, y1, x2, y2)
}