move AssetFS into Game
This commit is contained in:
parent
66e0fcd1fa
commit
1d33ac227a
4 changed files with 10 additions and 11 deletions
|
@ -4,16 +4,12 @@ import (
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"image"
|
"image"
|
||||||
"io/fs"
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Assets (usually embed.FS)
|
|
||||||
AssetFS fs.FS
|
|
||||||
|
|
||||||
// AnimDefs are easier to write as Go expressions -
|
// AnimDefs are easier to write as Go expressions -
|
||||||
// so just set this.
|
// so just set this.
|
||||||
AnimDefs map[string]*AnimDef
|
AnimDefs map[string]*AnimDef
|
||||||
|
@ -22,6 +18,7 @@ var (
|
||||||
|
|
||||||
// Ensure ref types satisfy interfaces.
|
// Ensure ref types satisfy interfaces.
|
||||||
_ Loader = &ImageRef{}
|
_ Loader = &ImageRef{}
|
||||||
|
_ Loader = &SceneRef{}
|
||||||
_ Scener = &SceneRef{}
|
_ Scener = &SceneRef{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -71,14 +68,14 @@ func (r *ImageRef) Image() *ebiten.Image {
|
||||||
// Load loads the image. Load is required before Image returns.
|
// Load loads the image. Load is required before Image returns.
|
||||||
// Loading the same path multiple times uses a cache to return
|
// Loading the same path multiple times uses a cache to return
|
||||||
// the same image.
|
// the same image.
|
||||||
func (r *ImageRef) Load() error {
|
func (r *ImageRef) Load(g *Game) error {
|
||||||
// Fast path load from cache
|
// Fast path load from cache
|
||||||
r.image = imageCache[r.Path]
|
r.image = imageCache[r.Path]
|
||||||
if r.image != nil {
|
if r.image != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// Slow path
|
// Slow path
|
||||||
f, err := AssetFS.Open(r.Path)
|
f, err := g.AssetFS.Open(r.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -108,8 +105,8 @@ type SceneRef struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load loads the scene from the file.
|
// Load loads the scene from the file.
|
||||||
func (r *SceneRef) Load() error {
|
func (r *SceneRef) Load(g *Game) error {
|
||||||
f, err := AssetFS.Open(r.Path)
|
f, err := g.AssetFS.Open(r.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
|
"io/fs"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
|
@ -14,6 +15,7 @@ func init() {
|
||||||
// One component must be the designated root component - usually a
|
// One component must be the designated root component - usually a
|
||||||
// scene of some kind.
|
// scene of some kind.
|
||||||
type Game struct {
|
type Game struct {
|
||||||
|
AssetFS fs.FS
|
||||||
ScreenWidth int
|
ScreenWidth int
|
||||||
ScreenHeight int
|
ScreenHeight int
|
||||||
Root DrawUpdater // typically a *Scene or SceneRef though
|
Root DrawUpdater // typically a *Scene or SceneRef though
|
||||||
|
@ -94,7 +96,7 @@ func (g *Game) Load() error {
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return l.Load()
|
return l.Load(g)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ type Identifier interface {
|
||||||
// Loader components get the chance to load themselves. This happens
|
// Loader components get the chance to load themselves. This happens
|
||||||
// before preparation.
|
// before preparation.
|
||||||
type Loader interface {
|
type Loader interface {
|
||||||
Load() error
|
Load(game *Game) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParallaxScaler components have a scaling factor. This is used for
|
// ParallaxScaler components have a scaling factor. This is used for
|
||||||
|
|
2
main.go
2
main.go
|
@ -20,7 +20,6 @@ func main() {
|
||||||
ebiten.SetWindowSize(640, 480)
|
ebiten.SetWindowSize(640, 480)
|
||||||
ebiten.SetWindowTitle("TODO")
|
ebiten.SetWindowTitle("TODO")
|
||||||
|
|
||||||
engine.AssetFS = assets
|
|
||||||
// engine.AnimDefs set in game/anims.go
|
// engine.AnimDefs set in game/anims.go
|
||||||
|
|
||||||
denseTiles := [][]engine.Tile{
|
denseTiles := [][]engine.Tile{
|
||||||
|
@ -103,6 +102,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
game := &engine.Game{
|
game := &engine.Game{
|
||||||
|
AssetFS: assets,
|
||||||
ScreenHeight: 240,
|
ScreenHeight: 240,
|
||||||
ScreenWidth: 320,
|
ScreenWidth: 320,
|
||||||
Root: &engine.Scene{
|
Root: &engine.Scene{
|
||||||
|
|
Loading…
Reference in a new issue