AnimRef
This commit is contained in:
parent
183b373867
commit
d31fbd1e73
3 changed files with 34 additions and 7 deletions
|
@ -1,6 +1,7 @@
|
|||
package engine
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"image"
|
||||
"io/fs"
|
||||
|
@ -11,20 +12,38 @@ import (
|
|||
var (
|
||||
AssetFS fs.FS
|
||||
|
||||
imageCache = make(map[string]*ebiten.Image)
|
||||
animDefCache = make(map[string]*AnimDef)
|
||||
imageCache = make(map[string]*ebiten.Image)
|
||||
)
|
||||
|
||||
// AnimRef
|
||||
type AnimRef struct {
|
||||
Path string
|
||||
|
||||
animdef *AnimDef
|
||||
anim *Anim
|
||||
}
|
||||
|
||||
func (r *AnimRef) Anim() *Anim {
|
||||
// TODO
|
||||
return &Anim{
|
||||
Def: r.animdef,
|
||||
func (r *AnimRef) Anim() (*Anim, error) {
|
||||
if r.anim != nil {
|
||||
return r.anim, nil
|
||||
}
|
||||
if ad := animDefCache[r.Path]; ad != nil {
|
||||
r.anim = &Anim{Def: ad}
|
||||
return r.anim, nil
|
||||
}
|
||||
f, err := AssetFS.Open(r.Path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open asset: %w", 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)
|
||||
}
|
||||
animDefCache[r.Path] = ad
|
||||
r.anim = &Anim{Def: ad}
|
||||
return r.anim, nil
|
||||
}
|
||||
|
||||
// ImageRef loads images from the AssetFS into *ebiten.Image form.
|
||||
|
|
|
@ -14,6 +14,7 @@ func init() {
|
|||
gob.Register(Tilemap{})
|
||||
}
|
||||
|
||||
// Tilemap renders a grid of tiles.
|
||||
type Tilemap struct {
|
||||
Map [][]Tile
|
||||
Src ImageRef // must be a horizontal tile set
|
||||
|
@ -22,6 +23,7 @@ type Tilemap struct {
|
|||
ZPos
|
||||
}
|
||||
|
||||
// Draw draws the tilemap.
|
||||
func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
||||
geom.Concat(t.Transform)
|
||||
for j, row := range t.Map {
|
||||
|
@ -33,7 +35,7 @@ func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
|||
sx := tile.TileIndex() * t.TileSize
|
||||
im, err := t.Src.Image()
|
||||
if err != nil {
|
||||
log.Fatalf("Loading image from reference: %v", err)
|
||||
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)
|
||||
|
@ -41,6 +43,7 @@ func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
|||
}
|
||||
}
|
||||
|
||||
// Update calls Update on any tiles that are Updaters, e.g. AnimatedTile.
|
||||
func (t *Tilemap) Update() error {
|
||||
for j := range t.Map {
|
||||
for i := range t.Map[j] {
|
||||
|
@ -54,14 +57,17 @@ func (t *Tilemap) Update() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Tile is the interface needed by Tilemap.
|
||||
type Tile interface {
|
||||
TileIndex() int
|
||||
}
|
||||
|
||||
// StaticTile returns a fixed tile index.
|
||||
type StaticTile int
|
||||
|
||||
func (s StaticTile) TileIndex() int { return int(s) }
|
||||
|
||||
// AnimatedTile uses an Anim to choose a tile index.
|
||||
type AnimatedTile struct {
|
||||
Anim
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package engine
|
||||
|
||||
// ZPos implements ZPositioner directly (as a float64 value).
|
||||
type ZPos float64
|
||||
|
||||
// Z returns z as a float64.
|
||||
func (z ZPos) Z() float64 { return float64(z) }
|
||||
|
|
Loading…
Reference in a new issue