-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer.go
221 lines (187 loc) · 6.16 KB
/
layer.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
package egriden
import (
"github.com/greenthepear/imggg"
"github.com/hajimehoshi/ebiten/v2"
)
// For optimization there are a couple of ways a grid layer can be draw depending if it
// changes frequently (Static or not) and if it has many (Dense) or few (Sparse)
// gobjects most of the time.
type DrawMode int
const (
//Used for sparcely populated grids, ranges over a map for drawing.
Sparse DrawMode = iota
//Used for thickly populated grids, ranges over a slice for drawing.
Dense
//Used for layers that don't get updated often, creates ebiten.Image of the the entire layer.
//Can be refreshed with GridLayer.RefreshImage().
Static
)
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
staticImage *ebiten.Image
// 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]
numOfGobjects int
level Level
}
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,
staticImage: nil,
Anchor: anchor,
numOfGobjects: 0,
Padding: padding,
}
}
func (l *GridLayer) Static() bool {
return l.mode == Static
}
func (le *BaseLevel) addGridLayer(l *GridLayer) *GridLayer {
ln := len(le.gridLayers)
l.z = ln
le.gridLayers = append(le.gridLayers, l)
l.level = le
return le.gridLayers[ln]
}
// Creates a grid layer with square cells and no padding within the level.
// Also returns the pointer to it.
func (le *BaseLevel) CreateSimpleGridLayerOnTop(
name string, squareLength int, width, height int,
drawMode DrawMode, XOffset, YOffset float64) *GridLayer {
return le.addGridLayer(
newGridLayer(
name, 0,
Dimensions{squareLength, squareLength},
Dimensions{width, height},
drawMode,
imggg.Pt(XOffset, YOffset),
imggg.Pt(0.0, 0.0),
),
)
}
// Shorthand for [(*BaseLevel).CreateSimpleGridLayerOnTop]
// for the current level
func (g *EgridenAssets) CreateSimpleGridLayerOnTop(
name string, squareLength int, width, height int,
drawMode DrawMode, XOffset, YOffset float64) *GridLayer {
return g.Level().(*BaseLevel).CreateSimpleGridLayerOnTop(
name, squareLength, width, height, drawMode, XOffset, YOffset)
}
// Parameters for [(*BaseLevel).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 grid layer with custom parameters within the level and returns the pointer to it.
// If you want a simple square grid layer use [(*BaseLevel).CreateSimpleGridLayerOnTop].
func (le *BaseLevel) CreateGridLayerOnTop(name string, params GridLayerParameters) *GridLayer {
return le.addGridLayer(
newGridLayer(
name, 0,
params.CellDimensions,
params.GridDimensions,
params.Mode,
params.Anchor,
params.PaddingVector,
))
}
// Shorthand for [(*BaseLevel).CreateGridLayerOnTop]
// for the current level
func (g *EgridenAssets) CreateGridLayerOnTop(name string, params GridLayerParameters) *GridLayer {
return g.Level().(*BaseLevel).CreateGridLayerOnTop(name, params)
}
// False visibility disables drawing both the Sprites and custom draw scripts
// of all Gobjects.
func (l *GridLayer) SetVisibility(to bool) {
l.Visible = to
}
// Returns the Z level
func (l *GridLayer) Z() int {
return l.z
}
func (l *GridLayer) anchor() 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)
}
// Returns a GridLayer at z in the current Level, returns nil if out of bounds.
func (g EgridenAssets) GridLayer(z int) *GridLayer {
return g.Level().GridLayer(z)
}
func (g EgridenAssets) GridLayers() []*GridLayer {
return g.Level().GridLayers()
}
// Draw all GridLayers of the current Level in their Z order.
func (g EgridenAssets) DrawAllGridLayers(on *ebiten.Image) {
g.Level().(*BaseLevel).DrawAllGridLayers(on)
}
// Draw all free layers of the current Level in their Z order.
func (g EgridenAssets) DrawAllFreeLayers(on *ebiten.Image) {
g.Level().(*BaseLevel).DrawAllFreeLayers(on)
}