This commit is contained in:
Josh Deprez 2021-08-20 16:31:06 +10:00
parent c352221606
commit 45e2b300e8
5 changed files with 19 additions and 23 deletions

View file

@ -99,8 +99,7 @@ type SceneRef struct {
scene *Scene // not exported for gob reasons scene *Scene // not exported for gob reasons
} }
// Load loads the scene from the file and then calls Load // Load loads the scene from the file.
// on the freshly-loaded Scene.
func (r *SceneRef) Load() error { func (r *SceneRef) Load() error {
f, err := AssetFS.Open(r.Path) f, err := AssetFS.Open(r.Path)
if err != nil { if err != nil {
@ -116,9 +115,6 @@ func (r *SceneRef) Load() error {
if err := gob.NewDecoder(gz).Decode(sc); err != nil { if err := gob.NewDecoder(gz).Decode(sc); err != nil {
return err return err
} }
if err := sc.Load(); err != nil {
return err
}
r.scene = sc r.scene = sc
return nil return nil
} }

View file

@ -61,7 +61,7 @@ func (g *Game) UnregisterComponent(c interface{}) {
func (g *Game) Component(id string) interface{} { return g.componentsByID[id] } func (g *Game) Component(id string) interface{} { return g.componentsByID[id] }
// Scan implements Scanner. // Scan implements Scanner.
func (g *Game) Scan() []interface{} { return []interface{}{g.Scene} } func (g *Game) Scan() []interface{} { return []interface{}{g.Scener} }
// Walk calls v with every component reachable from c via Scan, recursively, // Walk calls v with every component reachable from c via Scan, recursively,
// for as long as visit returns nil. // for as long as visit returns nil.
@ -81,17 +81,29 @@ func Walk(c interface{}, v func(interface{}) error) error {
return nil return nil
} }
// Load calls Load on all Loaders reachable via Scan (using Walk).
// It stops on the first error.
func (g *Game) Load() error {
return Walk(g.Scener, func(c interface{}) error {
l, ok := c.(Loader)
if !ok {
return nil
}
return l.Load()
})
}
// Prepare builds the component database (using Walk) and then calls // Prepare builds the component database (using Walk) and then calls
// Prepare on every Preparer. You must call Prepare before any calls // Prepare on every Preparer. You must call Prepare before any calls
// to Component. You may call Prepare again (e.g. as an alternative to // to Component. You may call Prepare again (e.g. as an alternative to
// fastidiously calling RegisterComponent/UnregisterComponent). // fastidiously calling RegisterComponent/UnregisterComponent).
func (g *Game) Prepare() { func (g *Game) Prepare() {
g.componentsByID = make(map[string]interface{}) g.componentsByID = make(map[string]interface{})
Walk(g, func(c interface{}) error { Walk(g.Scener, func(c interface{}) error {
g.RegisterComponent(c) g.RegisterComponent(c)
return nil return nil
}) })
Walk(g, func(c interface{}) error { Walk(g.Scener, func(c interface{}) error {
if p, ok := c.(Prepper); ok { if p, ok := c.(Prepper); ok {
p.Prepare(g) p.Prepare(g)
} }

View file

@ -70,7 +70,6 @@ type Scener interface {
Drawer Drawer
DrawOrderer DrawOrderer
Identifier Identifier
Loader
Prepper Prepper
Scanner Scanner
Updater Updater

View file

@ -39,20 +39,6 @@ func (s *Scene) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
} }
} }
// Load loads any subcomponents that need loading.
func (s *Scene) Load() error {
for _, i := range s.Components {
l, ok := i.(Loader)
if !ok {
continue
}
if err := l.Load(); err != nil {
return err
}
}
return nil
}
// Prepare does an initial Z-order sort. // Prepare does an initial Z-order sort.
func (s *Scene) Prepare(game *Game) { s.sortByDrawOrder() } func (s *Scene) Prepare(game *Game) { s.sortByDrawOrder() }

View file

@ -43,6 +43,9 @@ func main() {
tiles := make(map[image.Point]engine.Tile) tiles := make(map[image.Point]engine.Tile)
for j, row := range denseTiles { for j, row := range denseTiles {
for i, tile := range row { for i, tile := range row {
if tile == nil {
continue
}
tiles[image.Pt(i, j)] = tile tiles[image.Pt(i, j)] = tile
} }
} }