until I can think of a better way
This commit is contained in:
parent
d52753a6d0
commit
d8ecea05e5
2 changed files with 34 additions and 44 deletions
|
@ -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)
|
||||
// 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.
|
||||
|
|
40
main.go
40
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},
|
||||
},
|
||||
},
|
||||
},
|
||||
AnimRef: engine.AnimRef{Key: "green_tiles"},
|
||||
}
|
||||
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},
|
||||
},
|
||||
},
|
||||
},
|
||||
AnimRef: engine.AnimRef{Key: "red_tiles"},
|
||||
}
|
||||
*/
|
||||
|
||||
game := &engine.Game{
|
||||
ScreenHeight: screenHeight,
|
||||
|
|
Loading…
Reference in a new issue