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-07-23 17:17:56 +10:00
|
|
|
"image"
|
|
|
|
|
|
|
|
"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.
|
|
|
|
var (
|
|
|
|
_ Identifier = &Tilemap{}
|
|
|
|
_ Collider = &Tilemap{}
|
|
|
|
_ Drawer = &Tilemap{}
|
|
|
|
_ DrawOrderer = &Tilemap{}
|
2021-08-23 11:00:51 +10:00
|
|
|
_ Hider = &Tilemap{}
|
2021-08-20 17:05:47 +10:00
|
|
|
_ Scanner = &Tilemap{}
|
2021-08-18 16:34:51 +10:00
|
|
|
_ Updater = &Tilemap{}
|
|
|
|
)
|
|
|
|
|
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 10:57:17 +10:00
|
|
|
// Tilemap renders a grid of square tiles.
|
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 10:57:17 +10:00
|
|
|
Map map[image.Point]Tile // tilespace coordinate -> tile
|
|
|
|
Ersatz bool // "fake wall"
|
|
|
|
Offset image.Point // world coordinates
|
2021-08-08 22:07:55 +10:00
|
|
|
Src ImageRef
|
|
|
|
TileSize int
|
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.
|
|
|
|
func (t *Tilemap) CollidesWith(r image.Rectangle) bool {
|
|
|
|
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-08-15 16:14:50 +10:00
|
|
|
r = r.Sub(t.Offset)
|
2021-08-06 11:39:19 +10:00
|
|
|
min := r.Min.Div(t.TileSize)
|
2021-08-26 10:57:17 +10:00
|
|
|
max := r.Max.Sub(image.Pt(1, 1)).Div(t.TileSize) // 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-08-12 14:06:01 +10:00
|
|
|
func (t *Tilemap) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
|
2021-08-01 17:08:26 +10:00
|
|
|
if t.Hidden {
|
|
|
|
return
|
|
|
|
}
|
2021-08-02 16:32:27 +10:00
|
|
|
src := t.Src.Image()
|
|
|
|
w, _ := src.Size()
|
2021-08-12 14:06:01 +10:00
|
|
|
og := opts.GeoM
|
|
|
|
var geom ebiten.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-08-15 16:14:50 +10:00
|
|
|
geom.Reset()
|
|
|
|
geom.Translate(float64(p.X*t.TileSize+t.Offset.X), float64(p.Y*t.TileSize+t.Offset.Y))
|
|
|
|
geom.Concat(og)
|
|
|
|
opts.GeoM = 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, &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-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)
|
|
|
|
c[0] = &t.Src
|
|
|
|
for _, tile := range t.Map {
|
|
|
|
c = append(c, tile)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2021-08-20 17:05:47 +10:00
|
|
|
|
2021-07-31 19:49:24 +10:00
|
|
|
// Update calls Update on any tiles that are Updaters, e.g. AnimatedTile.
|
2021-07-29 15:09:00 +10:00
|
|
|
func (t *Tilemap) Update() error {
|
2021-08-02 14:38:48 +10:00
|
|
|
if t.Disabled {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-15 16:14:50 +10:00
|
|
|
for _, tile := range t.Map {
|
|
|
|
if u, ok := tile.(Updater); ok {
|
|
|
|
if err := u.Update(); err != nil {
|
|
|
|
return err
|
2021-07-29 15:09:00 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return t.Map[wc.Sub(t.Offset).Div(t.TileSize)]
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTileAt sets the tile at the given world coordinate.
|
|
|
|
func (t *Tilemap) SetTileAt(wc image.Point, tile Tile) {
|
|
|
|
t.Map[wc.Sub(t.Offset).Div(t.TileSize)] = tile
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
p := wc.Sub(t.Offset).Div(t.TileSize).Mul(t.TileSize).Add(t.Offset)
|
|
|
|
return image.Rectangle{p, p.Add(image.Pt(t.TileSize, t.TileSize))}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
TileIndex() int
|
|
|
|
}
|
|
|
|
|
2021-08-18 16:34:51 +10:00
|
|
|
// Ensure StaticTile and AnimatedTile satisfy Tile.
|
|
|
|
var (
|
2021-08-23 20:41:13 +10:00
|
|
|
_ Tile = StaticTile(0)
|
|
|
|
_ Tile = AnimatedTile{}
|
|
|
|
_ Scanner = AnimatedTile{}
|
2021-08-18 16:34:51 +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
|
|
|
|
|
|
|
|
func (s StaticTile) TileIndex() int { return int(s) }
|
|
|
|
|
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-08-23 20:28:49 +10:00
|
|
|
Animer
|
2021-07-29 15:09:00 +10:00
|
|
|
}
|
|
|
|
|
2021-08-23 20:38:40 +10:00
|
|
|
func (a AnimatedTile) TileIndex() int { return a.CurrentFrame() }
|
|
|
|
|
|
|
|
// Scan returns a.Animer. (It could be a Loader.)
|
|
|
|
func (a AnimatedTile) Scan() []interface{} { return []interface{}{a.Animer} }
|