This commit is contained in:
Josh Deprez 2021-07-31 19:49:24 +10:00 committed by Josh Deprez
parent 183b373867
commit d31fbd1e73
3 changed files with 34 additions and 7 deletions

View file

@ -1,6 +1,7 @@
package engine package engine
import ( import (
"encoding/gob"
"fmt" "fmt"
"image" "image"
"io/fs" "io/fs"
@ -11,20 +12,38 @@ import (
var ( var (
AssetFS fs.FS AssetFS fs.FS
imageCache = make(map[string]*ebiten.Image) animDefCache = make(map[string]*AnimDef)
imageCache = make(map[string]*ebiten.Image)
) )
// AnimRef
type AnimRef struct { type AnimRef struct {
Path string Path string
animdef *AnimDef anim *Anim
} }
func (r *AnimRef) Anim() *Anim { func (r *AnimRef) Anim() (*Anim, error) {
// TODO if r.anim != nil {
return &Anim{ return r.anim, nil
Def: r.animdef,
} }
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. // ImageRef loads images from the AssetFS into *ebiten.Image form.

View file

@ -14,6 +14,7 @@ func init() {
gob.Register(Tilemap{}) gob.Register(Tilemap{})
} }
// Tilemap renders a grid of tiles.
type Tilemap struct { type Tilemap struct {
Map [][]Tile Map [][]Tile
Src ImageRef // must be a horizontal tile set Src ImageRef // must be a horizontal tile set
@ -22,6 +23,7 @@ type Tilemap struct {
ZPos ZPos
} }
// Draw draws the tilemap.
func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) { func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
geom.Concat(t.Transform) geom.Concat(t.Transform)
for j, row := range t.Map { 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 sx := tile.TileIndex() * t.TileSize
im, err := t.Src.Image() im, err := t.Src.Image()
if err != nil { 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) src := im.SubImage(image.Rect(sx, 0, sx+t.TileSize, t.TileSize)).(*ebiten.Image)
screen.DrawImage(src, &op) 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 { func (t *Tilemap) Update() error {
for j := range t.Map { for j := range t.Map {
for i := range t.Map[j] { for i := range t.Map[j] {
@ -54,14 +57,17 @@ func (t *Tilemap) Update() error {
return nil return nil
} }
// Tile is the interface needed by Tilemap.
type Tile interface { type Tile interface {
TileIndex() int TileIndex() int
} }
// StaticTile returns a fixed tile index.
type StaticTile int type StaticTile int
func (s StaticTile) TileIndex() int { return int(s) } func (s StaticTile) TileIndex() int { return int(s) }
// AnimatedTile uses an Anim to choose a tile index.
type AnimatedTile struct { type AnimatedTile struct {
Anim Anim
} }

View file

@ -1,5 +1,7 @@
package engine package engine
// ZPos implements ZPositioner directly (as a float64 value).
type ZPos float64 type ZPos float64
// Z returns z as a float64.
func (z ZPos) Z() float64 { return float64(z) } func (z ZPos) Z() float64 { return float64(z) }