-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
62 lines (53 loc) · 1.6 KB
/
game.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
package egriden
// Egriden components to be embedded in your Game{} struct.
//
// You need to run [InitEgridenAssets()] in the main function before
// your game starts.
type EgridenAssets struct {
Levels []Level
CurrentLevelIndex int
}
// Get the current level
func (g *EgridenAssets) Level() Level {
return g.Levels[g.CurrentLevelIndex]
}
// Get a level by it's name. Returns nil if not found.
func (g *EgridenAssets) LevelByName(name string) Level {
for _, le := range g.Levels {
if le.Name() == name {
return le
}
}
return nil
}
// Append level to the end of the list and return it
func (g *EgridenAssets) AddLevel(le Level) Level {
g.Levels = append(g.Levels, le)
idx := len(g.Levels) - 1
le.(*BaseLevel).index = idx
return g.Levels[idx]
}
// Sets the current game's level or adds it if it's not in the assets already
func (g *EgridenAssets) SetLevelTo(le Level) {
for i, rangeLe := range g.Levels {
if rangeLe == le {
g.CurrentLevelIndex = i
return
}
}
//If not found, add that level I guess
g.AddLevel(le)
g.CurrentLevelIndex = len(g.Levels) - 1
}
// Set the next level by iterating the level index
func (g *EgridenAssets) NextLevel() {
g.CurrentLevelIndex = (g.CurrentLevelIndex + 1) % len(g.Levels)
}
// Run this while initializing the game, before adding any layers. Creates a level called `Default`
func (g *EgridenAssets) InitEgridenAssets() {
g.AddLevel(NewBaseLevel("Default"))
}
// UNTESTED! Run all the onUpdate() functions of Gobjects that have them within the current level
func (g *EgridenAssets) RunUpdateScripts() {
g.Level().(*BaseLevel).RunUpdateScripts()
}