ichigo/engine/tiles.go

113 lines
2.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-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
Hidden bool
2021-08-01 17:08:26 +10:00
ID
2021-08-08 22:07:55 +10:00
Map [][]Tile
Ersatz bool // "fake wall"
Src ImageRef
TileSize int
2021-07-23 17:05:05 +10:00
ZPos
}
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
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 {
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-07-29 17:07:38 +10:00
func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
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-07-23 17:17:56 +10:00
for j, row := range t.Map {
for i, tile := range row {
2021-08-02 15:56:25 +10:00
if tile == nil {
continue
}
2021-07-23 17:17:56 +10:00
var op ebiten.DrawImageOptions
op.GeoM.Translate(float64(i*t.TileSize), float64(j*t.TileSize))
2021-07-29 17:07:38 +10:00
op.GeoM.Concat(geom)
2021-07-23 17:05:05 +10:00
2021-08-02 16:32:27 +10:00
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)
2021-07-23 17:17:56 +10:00
screen.DrawImage(src, &op)
}
}
2021-07-23 17:05:05 +10:00
}
2021-07-29 15:09:00 +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-02 15:56:25 +10:00
for _, row := range t.Map {
for _, tile := range row {
if u, ok := tile.(Updater); ok {
if err := u.Update(); err != nil {
2021-07-29 15:09:00 +10:00
return err
}
}
}
}
return nil
}
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-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() }