This commit is contained in:
Josh Deprez 2021-07-31 20:06:49 +10:00 committed by Josh Deprez
parent d31fbd1e73
commit d52753a6d0
3 changed files with 40 additions and 41 deletions

View file

@ -2,9 +2,9 @@ package engine
import (
"encoding/gob"
"fmt"
"image"
"io/fs"
"log"
"github.com/hajimehoshi/ebiten/v2"
)
@ -16,34 +16,34 @@ var (
imageCache = make(map[string]*ebiten.Image)
)
// AnimRef
// AnimRef loads AnimDef from an asset and manages an Anim using it.
type AnimRef struct {
Path string
anim *Anim
}
func (r *AnimRef) Anim() (*Anim, error) {
func (r *AnimRef) Anim() *Anim {
if r.anim != nil {
return r.anim, nil
return r.anim
}
if ad := animDefCache[r.Path]; ad != nil {
r.anim = &Anim{Def: ad}
return r.anim, nil
return r.anim
}
f, err := AssetFS.Open(r.Path)
if err != nil {
return nil, fmt.Errorf("open asset: %w", err)
log.Fatalf("Couldn't open asset: %v", err)
}
defer f.Close()
dec := gob.NewDecoder(f)
ad := &AnimDef{}
if err := dec.Decode(ad); err != nil {
return nil, fmt.Errorf("decode asset: %w", err)
log.Fatalf("Couldn't decode asset: %v", err)
}
animDefCache[r.Path] = ad
r.anim = &Anim{Def: ad}
return r.anim, nil
return r.anim
}
// ImageRef loads images from the AssetFS into *ebiten.Image form.
@ -57,24 +57,24 @@ type ImageRef struct {
// Image returns the image. If it hasn't been loaded yet, it loads.
// Multiple distinct ImageRefs can use the same path.
func (r *ImageRef) Image() (*ebiten.Image, error) {
func (r *ImageRef) Image() *ebiten.Image {
if r.image != nil {
return r.image, nil
return r.image
}
r.image = imageCache[r.Path]
if r.image != nil {
return r.image, nil
return r.image
}
f, err := AssetFS.Open(r.Path)
if err != nil {
return nil, fmt.Errorf("open asset: %w", err)
log.Fatalf("Couldn't open asset: %v", err)
}
defer f.Close()
i, _, err := image.Decode(f)
if err != nil {
return nil, fmt.Errorf("decode asset: %w", err)
log.Fatalf("Couldn't decode asset: %v", err)
}
r.image = ebiten.NewImageFromImage(i)
imageCache[r.Path] = r.image
return r.image, nil
return r.image
}

View file

@ -3,7 +3,6 @@ package engine
import (
"encoding/gob"
"image"
"log"
"github.com/hajimehoshi/ebiten/v2"
)
@ -33,11 +32,7 @@ func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
op.GeoM.Concat(geom)
sx := tile.TileIndex() * t.TileSize
im, err := t.Src.Image()
if err != nil {
log.Fatalf("Tilemap.Draw loading image: %v", err)
}
src := im.SubImage(image.Rect(sx, 0, sx+t.TileSize, t.TileSize)).(*ebiten.Image)
src := t.Src.Image().SubImage(image.Rect(sx, 0, sx+t.TileSize, t.TileSize)).(*ebiten.Image)
screen.DrawImage(src, &op)
}
}
@ -69,7 +64,9 @@ func (s StaticTile) TileIndex() int { return int(s) }
// AnimatedTile uses an Anim to choose a tile index.
type AnimatedTile struct {
Anim
AnimRef
}
func (a *AnimatedTile) TileIndex() int { return a.CurrentFrame() }
func (a *AnimatedTile) TileIndex() int { return a.Anim().CurrentFrame() }
func (a *AnimatedTile) Update() error { return a.Anim().Update() }

40
main.go
View file

@ -46,29 +46,31 @@ 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[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[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},
},
},
},
},
}
}
*/
game := &engine.Game{
ScreenHeight: screenHeight,