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
|
package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
|
||||||
"image"
|
"image"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
|
@ -10,15 +9,19 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// Assets (usually embed.FS)
|
||||||
AssetFS fs.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)
|
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 {
|
type AnimRef struct {
|
||||||
Path string
|
Key string
|
||||||
|
|
||||||
anim *Anim
|
anim *Anim
|
||||||
}
|
}
|
||||||
|
@ -27,23 +30,12 @@ func (r *AnimRef) Anim() *Anim {
|
||||||
if r.anim != nil {
|
if r.anim != nil {
|
||||||
return r.anim
|
return r.anim
|
||||||
}
|
}
|
||||||
if ad := animDefCache[r.Path]; ad != nil {
|
if ad := AnimDefCache[r.Key]; ad != nil {
|
||||||
r.anim = &Anim{Def: ad}
|
r.anim = &Anim{Def: ad}
|
||||||
return r.anim
|
return r.anim
|
||||||
}
|
}
|
||||||
f, err := AssetFS.Open(r.Path)
|
log.Fatalf("Unknown AnimDef %q", r.Key)
|
||||||
if err != nil {
|
return 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ImageRef loads images from the AssetFS into *ebiten.Image form.
|
// 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")
|
ebiten.SetWindowTitle("TODO")
|
||||||
|
|
||||||
engine.AssetFS = assets
|
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{
|
staticTiles := [][]engine.StaticTile{
|
||||||
{0, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1},
|
{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{
|
tiles[4][5] = &engine.AnimatedTile{
|
||||||
Anim: engine.Anim{
|
AnimRef: engine.AnimRef{Key: "green_tiles"},
|
||||||
Def: &engine.AnimDef{
|
|
||||||
Frames: []engine.AnimFrame{
|
|
||||||
{Frame: 0, Duration: 16},
|
|
||||||
{Frame: 1, Duration: 16},
|
|
||||||
{Frame: 2, Duration: 16},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
tiles[6][7] = &engine.AnimatedTile{
|
tiles[6][7] = &engine.AnimatedTile{
|
||||||
Anim: engine.Anim{
|
AnimRef: engine.AnimRef{Key: "red_tiles"},
|
||||||
Def: &engine.AnimDef{
|
|
||||||
Frames: []engine.AnimFrame{
|
|
||||||
{Frame: 3, Duration: 12},
|
|
||||||
{Frame: 4, Duration: 12},
|
|
||||||
{Frame: 5, Duration: 12},
|
|
||||||
{Frame: 6, Duration: 12},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
game := &engine.Game{
|
game := &engine.Game{
|
||||||
ScreenHeight: screenHeight,
|
ScreenHeight: screenHeight,
|
||||||
|
|
Loading…
Reference in a new issue