80 lines
1.3 KiB
Go
80 lines
1.3 KiB
Go
package world
|
|
|
|
import (
|
|
"github.com/beefsack/go-astar"
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
)
|
|
|
|
const TileSize = 4
|
|
const TileSizeEnvironment = 16
|
|
|
|
type Tile struct {
|
|
X float64
|
|
Y float64
|
|
Walkable bool
|
|
|
|
Sprite *ebiten.Image
|
|
}
|
|
|
|
func (t Tile) Up() int {
|
|
tx, ty := t.X, t.Y-1
|
|
return PathTileIndex(tx, ty)
|
|
}
|
|
|
|
func (t Tile) Down() int {
|
|
tx, ty := t.X, t.Y+1
|
|
return PathTileIndex(tx, ty)
|
|
}
|
|
|
|
func (t Tile) Left() int {
|
|
tx, ty := t.X-1, t.Y
|
|
return PathTileIndex(tx, ty)
|
|
}
|
|
|
|
func (t Tile) Right() int {
|
|
tx, ty := t.X+1, t.Y
|
|
return PathTileIndex(tx, ty)
|
|
}
|
|
|
|
func (t Tile) PathNeighbors() []astar.Pather {
|
|
var neighbors []astar.Pather
|
|
i := t.Up()
|
|
if i != -1 && Map[i].Walkable {
|
|
neighbors = append(neighbors, Map[i])
|
|
}
|
|
i = t.Down()
|
|
if i != -1 && Map[i].Walkable {
|
|
neighbors = append(neighbors, Map[i])
|
|
}
|
|
i = t.Left()
|
|
if i != -1 && Map[i].Walkable {
|
|
neighbors = append(neighbors, Map[i])
|
|
}
|
|
i = t.Right()
|
|
if i != -1 && Map[i].Walkable {
|
|
neighbors = append(neighbors, Map[i])
|
|
}
|
|
return neighbors
|
|
}
|
|
|
|
func (t Tile) PathNeighborCost(to astar.Pather) float64 {
|
|
toT := to.(Tile)
|
|
if !toT.Walkable {
|
|
return 0
|
|
}
|
|
return 1
|
|
}
|
|
|
|
func (t Tile) PathEstimatedCost(to astar.Pather) float64 {
|
|
toT := to.(Tile)
|
|
x := toT.X - t.X
|
|
if x < 0 {
|
|
x = -x
|
|
}
|
|
y := toT.Y - t.Y
|
|
if y < 0 {
|
|
y = -y
|
|
}
|
|
return float64(x + y)
|
|
}
|