From 1d33ac227acc9f14adf4ab76c418130de8adecc7 Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Sun, 22 Aug 2021 20:27:08 +1000 Subject: [PATCH] move AssetFS into Game --- engine/asset.go | 13 +++++-------- engine/game.go | 4 +++- engine/interface.go | 2 +- main.go | 2 +- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/engine/asset.go b/engine/asset.go index 3952e83..1c7ee4b 100644 --- a/engine/asset.go +++ b/engine/asset.go @@ -4,16 +4,12 @@ import ( "compress/gzip" "encoding/gob" "image" - "io/fs" "log" "github.com/hajimehoshi/ebiten/v2" ) var ( - // Assets (usually embed.FS) - AssetFS fs.FS - // AnimDefs are easier to write as Go expressions - // so just set this. AnimDefs map[string]*AnimDef @@ -22,6 +18,7 @@ var ( // Ensure ref types satisfy interfaces. _ Loader = &ImageRef{} + _ Loader = &SceneRef{} _ Scener = &SceneRef{} ) @@ -71,14 +68,14 @@ func (r *ImageRef) Image() *ebiten.Image { // Load loads the image. Load is required before Image returns. // Loading the same path multiple times uses a cache to return // the same image. -func (r *ImageRef) Load() error { +func (r *ImageRef) Load(g *Game) error { // Fast path load from cache r.image = imageCache[r.Path] if r.image != nil { return nil } // Slow path - f, err := AssetFS.Open(r.Path) + f, err := g.AssetFS.Open(r.Path) if err != nil { return err } @@ -108,8 +105,8 @@ type SceneRef struct { } // Load loads the scene from the file. -func (r *SceneRef) Load() error { - f, err := AssetFS.Open(r.Path) +func (r *SceneRef) Load(g *Game) error { + f, err := g.AssetFS.Open(r.Path) if err != nil { return err } diff --git a/engine/game.go b/engine/game.go index 3636be9..b99b21d 100644 --- a/engine/game.go +++ b/engine/game.go @@ -2,6 +2,7 @@ package engine import ( "encoding/gob" + "io/fs" "github.com/hajimehoshi/ebiten/v2" ) @@ -14,6 +15,7 @@ func init() { // One component must be the designated root component - usually a // scene of some kind. type Game struct { + AssetFS fs.FS ScreenWidth int ScreenHeight int Root DrawUpdater // typically a *Scene or SceneRef though @@ -94,7 +96,7 @@ func (g *Game) Load() error { if !ok { return nil } - return l.Load() + return l.Load(g) }) } diff --git a/engine/interface.go b/engine/interface.go index c0cd333..4326948 100644 --- a/engine/interface.go +++ b/engine/interface.go @@ -39,7 +39,7 @@ type Identifier interface { // Loader components get the chance to load themselves. This happens // before preparation. type Loader interface { - Load() error + Load(game *Game) error } // ParallaxScaler components have a scaling factor. This is used for diff --git a/main.go b/main.go index 78561f8..42b70df 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,6 @@ func main() { ebiten.SetWindowSize(640, 480) ebiten.SetWindowTitle("TODO") - engine.AssetFS = assets // engine.AnimDefs set in game/anims.go denseTiles := [][]engine.Tile{ @@ -103,6 +102,7 @@ func main() { } game := &engine.Game{ + AssetFS: assets, ScreenHeight: 240, ScreenWidth: 320, Root: &engine.Scene{