-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.go
108 lines (87 loc) · 2.23 KB
/
level.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
package egriden
import (
"github.com/greenthepear/gunc"
"github.com/hajimehoshi/ebiten/v2"
)
// Levels are essentially different collections of layers.
// They are often called scenes in game dev.
type Level interface {
Name() string
Index() int
GridLayer(int) *GridLayer
GridLayers() []*GridLayer
FreeLayer(int) *FreeLayer
FreeLayers() []*FreeLayer
addGobjectWithOnUpdate(o Gobject, l Layer)
}
type gobjectWithLayer struct {
o Gobject
l Layer
}
type BaseLevel struct {
name string
index int
gridLayers []*GridLayer
gobjectsWithUpdateScripts []gobjectWithLayer
freeLayers []*FreeLayer
}
// Initialize by creating slices for layers
func (le *BaseLevel) Init() {
le.gridLayers = make([]*GridLayer, 0)
le.gobjectsWithUpdateScripts = make([]gobjectWithLayer, 0)
}
func NewBaseLevel(name string) *BaseLevel {
le := &BaseLevel{name: name}
le.Init()
return le
}
func (le *BaseLevel) Name() string {
return le.name
}
func (le *BaseLevel) Index() int {
return le.index
}
// Returns a GridLayer at z, returns nil if out of bounds
func (le *BaseLevel) GridLayer(z int) *GridLayer {
if z >= len(le.gridLayers) || z < 0 {
return nil
}
return le.gridLayers[z]
}
// Returns slice of all current grid layers
func (le *BaseLevel) GridLayers() []*GridLayer {
return le.gridLayers
}
// Draws all grid layers according to their Z order
func (le *BaseLevel) DrawAllGridLayers(on *ebiten.Image) {
for _, l := range le.gridLayers {
l.Draw(on)
}
}
// Draws all free layers according to their Z order
func (le *BaseLevel) DrawAllFreeLayers(on *ebiten.Image) {
for _, l := range le.freeLayers {
l.Draw(on)
}
}
func (le *BaseLevel) addGobjectWithOnUpdate(o Gobject, l Layer) {
le.gobjectsWithUpdateScripts =
append(le.gobjectsWithUpdateScripts, gobjectWithLayer{o, l})
}
// UNTESTED! Run all the onUpdate() functions of Gobjects that have them
func (le *BaseLevel) RunUpdateScripts() {
marked := 0
for _, elem := range le.gobjectsWithUpdateScripts {
if elem.o.isMarkedForDeletion() {
marked++
continue
}
elem.o.OnUpdate()(elem.o, elem.l)
}
if marked > 0 {
le.gobjectsWithUpdateScripts = gunc.Filter(le.gobjectsWithUpdateScripts,
func(ol gobjectWithLayer) bool {
return ol.o.isMarkedForDeletion()
})
}
}