ichigo/engine/tiles.go

149 lines
3.4 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.
var (
_ Identifier = &Tilemap{}
_ Collider = &Tilemap{}
_ Drawer = &Tilemap{}
_ DrawOrderer = &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() {
gob.Register(AnimatedTile{})
gob.Register(StaticTile(0))
gob.Register(Tilemap{})
}
2021-07-31 19:49:24 +10:00
// Tilemap renders a grid of tiles.
2021-07-23 17:05:05 +10:00
type Tilemap struct {
2021-08-02 14:38:48 +10:00
Disabled bool
2021-08-18 16:34:51 +10:00
ZOrder
2021-08-18 15:23:02 +10:00
Hidden bool
2021-08-01 17:08:26 +10:00
ID
Map map[image.Point]Tile
Ersatz bool // "fake wall"
Offset image.Point // world coordinates
2021-08-08 22:07:55 +10:00
Src ImageRef
TileSize int
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
// 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
r = r.Sub(t.Offset)
2021-08-06 11:39:19 +10:00
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++ {
for i := min.X; i <= max.X; i++ {
if t.Map[image.Pt(i, j)] == nil {
2021-08-04 12:40:51 +10:00
continue
}
if r.Overlaps(image.Rect(i*t.TileSize, j*t.TileSize, (i+1)*t.TileSize, (j+1)*t.TileSize)) {
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
for p, tile := range t.Map {
if tile == nil {
continue
2021-07-23 17:17:56 +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-20 17:05:47 +10:00
// Scan returns a slice containing Src.
func (t *Tilemap) Scan() []interface{} { return []interface{}{&t.Src} }
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
}
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 (
_ Tile = StaticTile(0)
_ Tile = &AnimatedTile{}
)
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-07-31 20:06:49 +10:00
AnimRef
2021-07-29 15:09:00 +10:00
}
2021-07-31 20:06:49 +10:00
func (a *AnimatedTile) TileIndex() int { return a.Anim().CurrentFrame() }
func (a *AnimatedTile) Update() error { return a.Anim().Update() }