ichigo/engine/tiles.go

131 lines
3 KiB
Go
Raw Normal View History

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.
2021-08-27 11:49:11 +10:00
var _ interface {
Identifier
Collider
Drawer
Hider
Scanner
} = &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
} = 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 {
ID
2021-08-23 11:10:46 +10:00
Disabled
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.
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.
r = r.Sub(t.Offset)
2021-08-26 13:54:22 +10:00
min := div2(r.Min, t.Sheet.CellSize)
max := div2(r.Max.Sub(image.Pt(1, 1)), t.Sheet.CellSize) // NB: fencepost
2021-08-06 11:39:19 +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.
func (t *Tilemap) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
2021-08-12 14:06:01 +10:00
og := opts.GeoM
var geom ebiten.GeoM
for p, tile := range t.Map {
if tile == nil {
continue
2021-07-23 17:17:56 +10:00
}
geom.Reset()
2021-08-26 13:54:22 +10:00
geom.Translate(float2(mul2(p, t.Sheet.CellSize).Add(t.Offset)))
geom.Concat(og)
opts.GeoM = geom
2021-08-26 13:54:22 +10:00
src := t.Sheet.SubImage(tile.CellIndex())
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)
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-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-08-26 13:54:22 +10:00
return t.Map[div2(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-08-26 13:54:22 +10:00
t.Map[div2(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-08-26 13:54:22 +10:00
p := mul2(div2(wc.Sub(t.Offset), t.Sheet.CellSize), t.Sheet.CellSize).Add(t.Offset)
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-08-26 13:54:22 +10:00
CellIndex() 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-08-26 13:54:22 +10:00
func (s StaticTile) CellIndex() 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-08-23 20:28:49 +10:00
Animer
2021-07-29 15:09:00 +10:00
}
2021-08-26 13:54:22 +10:00
func (a AnimatedTile) CellIndex() int { return a.CurrentFrame() }
2021-08-23 20:38:40 +10:00
// Scan returns a.Animer. (It could be a Loader.)
func (a AnimatedTile) Scan() []interface{} { return []interface{}{a.Animer} }