2021-07-23 17:05:05 +10:00
|
|
|
package engine
|
|
|
|
|
2021-07-23 17:17:56 +10:00
|
|
|
import (
|
2021-07-30 17:26:23 +10:00
|
|
|
"encoding/gob"
|
2021-09-02 11:53:04 +10:00
|
|
|
"fmt"
|
2021-07-23 17:17:56 +10:00
|
|
|
"image"
|
2021-09-02 11:53:04 +10:00
|
|
|
"io/fs"
|
2021-07-23 17:17:56 +10:00
|
|
|
|
2021-09-08 20:08:57 +10:00
|
|
|
"drjosh.dev/gurgle/geom"
|
2021-07-23 17:17:56 +10:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
2021-07-23 17:05:05 +10:00
|
|
|
|
2021-08-18 16:34:51 +10:00
|
|
|
// Ensure Tilemap satisfies interfaces.
|
2021-08-27 11:49:11 +10:00
|
|
|
var _ interface {
|
|
|
|
Identifier
|
|
|
|
Collider
|
|
|
|
Drawer
|
|
|
|
Hider
|
|
|
|
Scanner
|
2021-09-01 13:46:26 +10:00
|
|
|
Transformer
|
2021-08-27 11:49:11 +10:00
|
|
|
} = &Tilemap{}
|
|
|
|
|
|
|
|
// Ensure StaticTile and AnimatedTile satisfy Tile.
|
2021-08-18 16:34:51 +10:00
|
|
|
var (
|
2021-08-27 11:49:11 +10:00
|
|
|
_ Tile = StaticTile(0)
|
|
|
|
_ interface {
|
|
|
|
Tile
|
|
|
|
Scanner
|
2021-09-02 11:53:04 +10:00
|
|
|
} = &AnimatedTile{}
|
2021-08-18 16:34:51 +10:00
|
|
|
)
|
|
|
|
|
2021-07-30 17:26:23 +10:00
|
|
|
func init() {
|
2021-08-25 15:04:38 +10:00
|
|
|
gob.Register(&AnimatedTile{})
|
2021-07-30 17:26:23 +10:00
|
|
|
gob.Register(StaticTile(0))
|
2021-08-25 15:04:38 +10:00
|
|
|
gob.Register(&Tilemap{})
|
2021-07-30 17:26:23 +10:00
|
|
|
}
|
|
|
|
|
2021-08-26 13:54:22 +10:00
|
|
|
// Tilemap renders a grid of rectangular tiles at equal Z position.
|
2021-07-23 17:05:05 +10:00
|
|
|
type Tilemap struct {
|
2021-08-23 11:00:51 +10:00
|
|
|
ID
|
2021-08-23 11:10:46 +10:00
|
|
|
Disabled
|
2021-08-23 11:00:51 +10:00
|
|
|
Hidden
|
2021-08-26 13:54:22 +10:00
|
|
|
Map map[image.Point]Tile // tilespace coordinate -> tile
|
2021-08-27 12:00:20 +10:00
|
|
|
Ersatz bool // disables collisions ("fake wall")
|
2021-08-26 13:54:22 +10:00
|
|
|
Offset image.Point // world coordinates
|
|
|
|
Sheet Sheet
|
2021-08-23 11:10:46 +10:00
|
|
|
ZOrder
|
2021-07-23 17:05:05 +10:00
|
|
|
}
|
|
|
|
|
2021-08-04 12:40:51 +10:00
|
|
|
// CollidesWith implements Collider.
|
2021-09-08 20:08:57 +10:00
|
|
|
func (t *Tilemap) CollidesWith(b geom.Box) bool {
|
2021-08-04 12:40:51 +10:00
|
|
|
if t.Ersatz {
|
|
|
|
return false
|
|
|
|
}
|
2021-08-06 11:39:19 +10:00
|
|
|
|
2021-08-26 10:57:17 +10:00
|
|
|
// Probe the map at all tilespace coordinates overlapping the rect.
|
2021-09-02 20:17:45 +10:00
|
|
|
r := b.XY().Sub(t.Offset) // TODO: pretend tilemap is a plane in 3D?
|
2021-09-08 20:08:57 +10:00
|
|
|
min := geom.CDiv(r.Min, t.Sheet.CellSize)
|
|
|
|
max := geom.CDiv(r.Max.Sub(image.Pt(1, 1)), t.Sheet.CellSize) // NB: fencepost
|
2021-08-06 11:39:19 +10:00
|
|
|
|
2021-08-15 16:14:50 +10:00
|
|
|
for j := min.Y; j <= max.Y; j++ {
|
|
|
|
for i := min.X; i <= max.X; i++ {
|
2021-08-26 10:57:17 +10:00
|
|
|
if t.Map[image.Pt(i, j)] != nil {
|
2021-08-04 12:40:51 +10:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:49:24 +10:00
|
|
|
// Draw draws the tilemap.
|
2021-09-01 09:48:01 +10:00
|
|
|
func (t *Tilemap) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
|
2021-08-12 14:06:01 +10:00
|
|
|
og := opts.GeoM
|
2021-08-15 16:14:50 +10:00
|
|
|
for p, tile := range t.Map {
|
|
|
|
if tile == nil {
|
|
|
|
continue
|
2021-07-23 17:17:56 +10:00
|
|
|
}
|
2021-09-08 20:08:57 +10:00
|
|
|
var mat ebiten.GeoM
|
|
|
|
mat.Translate(geom.CFloat(geom.CMul(p, t.Sheet.CellSize)))
|
|
|
|
mat.Concat(og)
|
|
|
|
opts.GeoM = mat
|
2021-08-15 16:14:50 +10:00
|
|
|
|
2021-09-02 11:53:04 +10:00
|
|
|
src := t.Sheet.SubImage(tile.Cell())
|
2021-09-01 09:48:01 +10:00
|
|
|
screen.DrawImage(src, opts)
|
2021-07-23 17:17:56 +10:00
|
|
|
}
|
2021-07-23 17:05:05 +10:00
|
|
|
}
|
2021-07-29 15:09:00 +10:00
|
|
|
|
2021-09-02 11:53:04 +10:00
|
|
|
// Load instantiates animations for all AnimatedTiles.
|
|
|
|
func (t *Tilemap) Load(fs.FS) error {
|
|
|
|
for _, tile := range t.Map {
|
|
|
|
at, ok := tile.(*AnimatedTile)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-02 11:55:16 +10:00
|
|
|
at.anim = t.Sheet.NewAnim(at.AnimKey)
|
|
|
|
if at.anim == nil {
|
|
|
|
return fmt.Errorf("missing anim %q", at.AnimKey)
|
2021-09-02 11:53:04 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-23 20:38:40 +10:00
|
|
|
// Scan returns a slice containing Src and all non-nil tiles.
|
|
|
|
func (t *Tilemap) Scan() []interface{} {
|
|
|
|
c := make([]interface{}, 1, len(t.Map)+1)
|
2021-08-26 13:54:22 +10:00
|
|
|
c[0] = &t.Sheet
|
2021-08-23 20:38:40 +10:00
|
|
|
for _, tile := range t.Map {
|
|
|
|
c = append(c, tile)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2021-08-20 17:05:47 +10:00
|
|
|
|
2021-09-03 09:42:50 +10:00
|
|
|
// Transform returns a translation by t.Offset.
|
2021-09-07 14:00:50 +10:00
|
|
|
func (t *Tilemap) Transform() (opts ebiten.DrawImageOptions) {
|
2021-09-08 20:08:57 +10:00
|
|
|
opts.GeoM.Translate(geom.CFloat(t.Offset))
|
2021-09-07 14:00:50 +10:00
|
|
|
return opts
|
2021-09-01 13:46:26 +10:00
|
|
|
}
|
|
|
|
|
2021-08-15 16:40:01 +10:00
|
|
|
// TileAt returns the tile present at the given world coordinate.
|
|
|
|
func (t *Tilemap) TileAt(wc image.Point) Tile {
|
2021-09-08 20:08:57 +10:00
|
|
|
return t.Map[geom.CDiv(wc.Sub(t.Offset), t.Sheet.CellSize)]
|
2021-08-15 16:40:01 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetTileAt sets the tile at the given world coordinate.
|
|
|
|
func (t *Tilemap) SetTileAt(wc image.Point, tile Tile) {
|
2021-09-08 20:08:57 +10:00
|
|
|
t.Map[geom.CDiv(wc.Sub(t.Offset), t.Sheet.CellSize)] = tile
|
2021-08-15 16:40:01 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// TileBounds returns a rectangle describing the tile boundary for the tile
|
|
|
|
// at the given world coordinate.
|
|
|
|
func (t *Tilemap) TileBounds(wc image.Point) image.Rectangle {
|
2021-09-08 20:08:57 +10:00
|
|
|
p := geom.CMul(geom.CDiv(wc.Sub(t.Offset), t.Sheet.CellSize), t.Sheet.CellSize).Add(t.Offset)
|
2021-08-26 13:54:22 +10:00
|
|
|
return image.Rectangle{p, p.Add(t.Sheet.CellSize)}
|
2021-08-15 16:40:01 +10:00
|
|
|
}
|
|
|
|
|
2021-07-31 19:49:24 +10:00
|
|
|
// Tile is the interface needed by Tilemap.
|
2021-07-29 15:09:00 +10:00
|
|
|
type Tile interface {
|
2021-09-02 11:53:04 +10:00
|
|
|
Cell() int
|
2021-07-29 15:09:00 +10:00
|
|
|
}
|
|
|
|
|
2021-07-31 19:49:24 +10:00
|
|
|
// StaticTile returns a fixed tile index.
|
2021-07-29 15:09:00 +10:00
|
|
|
type StaticTile int
|
|
|
|
|
2021-09-02 11:53:04 +10:00
|
|
|
func (s StaticTile) Cell() int { return int(s) }
|
2021-07-29 15:09:00 +10:00
|
|
|
|
2021-07-31 19:49:24 +10:00
|
|
|
// AnimatedTile uses an Anim to choose a tile index.
|
2021-07-29 15:09:00 +10:00
|
|
|
type AnimatedTile struct {
|
2021-09-02 11:53:04 +10:00
|
|
|
AnimKey string
|
|
|
|
|
|
|
|
anim *Anim
|
2021-07-29 15:09:00 +10:00
|
|
|
}
|
|
|
|
|
2021-09-02 11:53:04 +10:00
|
|
|
func (a *AnimatedTile) Cell() int { return a.anim.Cell() }
|
2021-08-23 20:38:40 +10:00
|
|
|
|
2021-09-02 11:53:04 +10:00
|
|
|
// Scan returns a.anim.
|
|
|
|
func (a *AnimatedTile) Scan() []interface{} { return []interface{}{a.anim} }
|