yes
This commit is contained in:
parent
d31fbd1e73
commit
d52753a6d0
3 changed files with 40 additions and 41 deletions
|
@ -2,9 +2,9 @@ package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"fmt"
|
|
||||||
"image"
|
"image"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
|
@ -16,34 +16,34 @@ var (
|
||||||
imageCache = make(map[string]*ebiten.Image)
|
imageCache = make(map[string]*ebiten.Image)
|
||||||
)
|
)
|
||||||
|
|
||||||
// AnimRef
|
// AnimRef loads AnimDef from an asset and manages an Anim using it.
|
||||||
type AnimRef struct {
|
type AnimRef struct {
|
||||||
Path string
|
Path string
|
||||||
|
|
||||||
anim *Anim
|
anim *Anim
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *AnimRef) Anim() (*Anim, error) {
|
func (r *AnimRef) Anim() *Anim {
|
||||||
if r.anim != nil {
|
if r.anim != nil {
|
||||||
return r.anim, nil
|
return r.anim
|
||||||
}
|
}
|
||||||
if ad := animDefCache[r.Path]; ad != nil {
|
if ad := animDefCache[r.Path]; ad != nil {
|
||||||
r.anim = &Anim{Def: ad}
|
r.anim = &Anim{Def: ad}
|
||||||
return r.anim, nil
|
return r.anim
|
||||||
}
|
}
|
||||||
f, err := AssetFS.Open(r.Path)
|
f, err := AssetFS.Open(r.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("open asset: %w", err)
|
log.Fatalf("Couldn't open asset: %v", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
dec := gob.NewDecoder(f)
|
dec := gob.NewDecoder(f)
|
||||||
ad := &AnimDef{}
|
ad := &AnimDef{}
|
||||||
if err := dec.Decode(ad); err != nil {
|
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
|
animDefCache[r.Path] = ad
|
||||||
r.anim = &Anim{Def: ad}
|
r.anim = &Anim{Def: ad}
|
||||||
return r.anim, nil
|
return r.anim
|
||||||
}
|
}
|
||||||
|
|
||||||
// ImageRef loads images from the AssetFS into *ebiten.Image form.
|
// 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.
|
// Image returns the image. If it hasn't been loaded yet, it loads.
|
||||||
// Multiple distinct ImageRefs can use the same path.
|
// 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 {
|
if r.image != nil {
|
||||||
return r.image, nil
|
return r.image
|
||||||
}
|
}
|
||||||
r.image = imageCache[r.Path]
|
r.image = imageCache[r.Path]
|
||||||
if r.image != nil {
|
if r.image != nil {
|
||||||
return r.image, nil
|
return r.image
|
||||||
}
|
}
|
||||||
f, err := AssetFS.Open(r.Path)
|
f, err := AssetFS.Open(r.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("open asset: %w", err)
|
log.Fatalf("Couldn't open asset: %v", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
i, _, err := image.Decode(f)
|
i, _, err := image.Decode(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("decode asset: %w", err)
|
log.Fatalf("Couldn't decode asset: %v", err)
|
||||||
}
|
}
|
||||||
r.image = ebiten.NewImageFromImage(i)
|
r.image = ebiten.NewImageFromImage(i)
|
||||||
imageCache[r.Path] = r.image
|
imageCache[r.Path] = r.image
|
||||||
return r.image, nil
|
return r.image
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package engine
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"image"
|
"image"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
|
@ -33,11 +32,7 @@ func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
||||||
op.GeoM.Concat(geom)
|
op.GeoM.Concat(geom)
|
||||||
|
|
||||||
sx := tile.TileIndex() * t.TileSize
|
sx := tile.TileIndex() * t.TileSize
|
||||||
im, err := t.Src.Image()
|
src := t.Src.Image().SubImage(image.Rect(sx, 0, sx+t.TileSize, t.TileSize)).(*ebiten.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)
|
|
||||||
screen.DrawImage(src, &op)
|
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.
|
// AnimatedTile uses an Anim to choose a tile index.
|
||||||
type AnimatedTile struct {
|
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
40
main.go
|
@ -46,29 +46,31 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tiles[4][5] = &engine.AnimatedTile{
|
/*
|
||||||
Anim: engine.Anim{
|
tiles[4][5] = &engine.AnimatedTile{
|
||||||
Def: &engine.AnimDef{
|
Anim: engine.Anim{
|
||||||
Frames: []engine.AnimFrame{
|
Def: &engine.AnimDef{
|
||||||
{Frame: 0, Duration: 16},
|
Frames: []engine.AnimFrame{
|
||||||
{Frame: 1, Duration: 16},
|
{Frame: 0, Duration: 16},
|
||||||
{Frame: 2, Duration: 16},
|
{Frame: 1, Duration: 16},
|
||||||
|
{Frame: 2, Duration: 16},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
}
|
tiles[6][7] = &engine.AnimatedTile{
|
||||||
tiles[6][7] = &engine.AnimatedTile{
|
Anim: engine.Anim{
|
||||||
Anim: engine.Anim{
|
Def: &engine.AnimDef{
|
||||||
Def: &engine.AnimDef{
|
Frames: []engine.AnimFrame{
|
||||||
Frames: []engine.AnimFrame{
|
{Frame: 3, Duration: 12},
|
||||||
{Frame: 3, Duration: 12},
|
{Frame: 4, Duration: 12},
|
||||||
{Frame: 4, Duration: 12},
|
{Frame: 5, Duration: 12},
|
||||||
{Frame: 5, Duration: 12},
|
{Frame: 6, Duration: 12},
|
||||||
{Frame: 6, Duration: 12},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
}
|
*/
|
||||||
|
|
||||||
game := &engine.Game{
|
game := &engine.Game{
|
||||||
ScreenHeight: screenHeight,
|
ScreenHeight: screenHeight,
|
||||||
|
|
Loading…
Reference in a new issue