ichigo/engine/tiles.go
2021-08-08 22:07:55 +10:00

112 lines
2.3 KiB
Go

package engine
import (
"encoding/gob"
"image"
"github.com/hajimehoshi/ebiten/v2"
)
func init() {
gob.Register(AnimatedTile{})
gob.Register(StaticTile(0))
gob.Register(Tilemap{})
}
// Tilemap renders a grid of tiles.
type Tilemap struct {
Disabled bool
Hidden bool
ID
Map [][]Tile
Ersatz bool // "fake wall"
Src ImageRef
TileSize int
ZPos
}
// CollidesWith implements Collider.
func (t *Tilemap) CollidesWith(r image.Rectangle) bool {
if t.Ersatz {
return false
}
// If we round down r.Min, and round up r.Max, to the nearest tile
// coordinates, that gives the full range of tiles to test.
sm1 := t.TileSize - 1
min := r.Min.Div(t.TileSize)
max := r.Max.Add(image.Pt(sm1, sm1)).Div(t.TileSize)
for j := min.Y; j <= max.Y && j < len(t.Map); j++ {
row := t.Map[j]
for i := min.X; i <= max.X && i < len(row); i++ {
if row[i] == nil {
continue
}
if r.Overlaps(image.Rect(i*t.TileSize, j*t.TileSize, (i+1)*t.TileSize, (j+1)*t.TileSize)) {
return true
}
}
}
return false
}
// Draw draws the tilemap.
func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
if t.Hidden {
return
}
src := t.Src.Image()
w, _ := src.Size()
for j, row := range t.Map {
for i, tile := range row {
if tile == nil {
continue
}
var op ebiten.DrawImageOptions
op.GeoM.Translate(float64(i*t.TileSize), float64(j*t.TileSize))
op.GeoM.Concat(geom)
s := tile.TileIndex() * t.TileSize
sx, sy := s%w, (s/w)*t.TileSize
src := src.SubImage(image.Rect(sx, sy, sx+t.TileSize, sy+t.TileSize)).(*ebiten.Image)
screen.DrawImage(src, &op)
}
}
}
// Update calls Update on any tiles that are Updaters, e.g. AnimatedTile.
func (t *Tilemap) Update() error {
if t.Disabled {
return nil
}
for _, row := range t.Map {
for _, tile := range row {
if u, ok := tile.(Updater); ok {
if err := u.Update(); err != nil {
return err
}
}
}
}
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 {
AnimRef
}
func (a *AnimatedTile) TileIndex() int { return a.Anim().CurrentFrame() }
func (a *AnimatedTile) Update() error { return a.Anim().Update() }