From d8ecea05e5e380faa36fe73469f08913e064606e Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Sat, 31 Jul 2021 21:23:58 +1000 Subject: [PATCH] until I can think of a better way --- engine/asset.go | 30 +++++++++++------------------- main.go | 48 +++++++++++++++++++++++------------------------- 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/engine/asset.go b/engine/asset.go index ff0987b..83ad5c7 100644 --- a/engine/asset.go +++ b/engine/asset.go @@ -1,7 +1,6 @@ package engine import ( - "encoding/gob" "image" "io/fs" "log" @@ -10,15 +9,19 @@ import ( ) var ( + // Assets (usually embed.FS) AssetFS fs.FS - animDefCache = make(map[string]*AnimDef) - imageCache = make(map[string]*ebiten.Image) + // AnimDefs are easier to write as Go expressions - + // so just set this. + AnimDefCache map[string]*AnimDef + + imageCache = make(map[string]*ebiten.Image) ) -// AnimRef loads AnimDef from an asset and manages an Anim using it. +// AnimRef manages an Anim using a premade AnimDef from the cache. type AnimRef struct { - Path string + Key string anim *Anim } @@ -27,23 +30,12 @@ func (r *AnimRef) Anim() *Anim { if r.anim != nil { return r.anim } - if ad := animDefCache[r.Path]; ad != nil { + if ad := AnimDefCache[r.Key]; ad != nil { r.anim = &Anim{Def: ad} return r.anim } - f, err := AssetFS.Open(r.Path) - if err != nil { - log.Fatalf("Couldn't open asset: %v", err) - } - defer f.Close() - dec := gob.NewDecoder(f) - ad := &AnimDef{} - if err := dec.Decode(ad); err != nil { - log.Fatalf("Couldn't decode asset: %v", err) - } - animDefCache[r.Path] = ad - r.anim = &Anim{Def: ad} - return r.anim + log.Fatalf("Unknown AnimDef %q", r.Key) + return nil } // ImageRef loads images from the AssetFS into *ebiten.Image form. diff --git a/main.go b/main.go index fe132ab..4f208fc 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,23 @@ func main() { ebiten.SetWindowTitle("TODO") engine.AssetFS = assets + engine.AnimDefCache = map[string]*engine.AnimDef{ + "green_tiles": { + Frames: []engine.AnimFrame{ + {Frame: 0, Duration: 16}, + {Frame: 1, Duration: 16}, + {Frame: 2, Duration: 16}, + }, + }, + "red_tiles": { + Frames: []engine.AnimFrame{ + {Frame: 3, Duration: 12}, + {Frame: 4, Duration: 12}, + {Frame: 5, Duration: 12}, + {Frame: 6, Duration: 12}, + }, + }, + } staticTiles := [][]engine.StaticTile{ {0, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1}, @@ -46,31 +63,12 @@ func main() { } } - /* - tiles[4][5] = &engine.AnimatedTile{ - Anim: engine.Anim{ - Def: &engine.AnimDef{ - Frames: []engine.AnimFrame{ - {Frame: 0, Duration: 16}, - {Frame: 1, Duration: 16}, - {Frame: 2, Duration: 16}, - }, - }, - }, - } - tiles[6][7] = &engine.AnimatedTile{ - Anim: engine.Anim{ - Def: &engine.AnimDef{ - Frames: []engine.AnimFrame{ - {Frame: 3, Duration: 12}, - {Frame: 4, Duration: 12}, - {Frame: 5, Duration: 12}, - {Frame: 6, Duration: 12}, - }, - }, - }, - } - */ + tiles[4][5] = &engine.AnimatedTile{ + AnimRef: engine.AnimRef{Key: "green_tiles"}, + } + tiles[6][7] = &engine.AnimatedTile{ + AnimRef: engine.AnimRef{Key: "red_tiles"}, + } game := &engine.Game{ ScreenHeight: screenHeight,