grid

package
v0.7.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultSVGOpts = &SVGOpts{
	WeightColors: [][2]string{
		{"0.3", "#bbdefb"},
		{"1.0", "#c8e6c9"},
		{"2.0", "#fff9c4"},
		{"5.0", "#ffcc80"},
		{"+Inf", "#ef5350"},
	},
	LegendEntries: [][2]string{
		{"#bbdefb", "Road ≤0.3"},
		{"#c8e6c9", "Grass ≤1.0"},
		{"#fff9c4", "Sand ≤2.0"},
		{"#ffcc80", "Swamp ≤5.0"},
		{"#ef5350", ">5.0 Extreme"},
		{"#555555", "Wall"},
	},
}

DefaultSVGOpts is the default SVG rendering config, used when nil is passed.

Functions

func NewMatrix added in v0.3.0

func NewMatrix(w, h int) [][]int

NewMatrix creates an empty (all zero) matrix with the given dimensions. All cells are 0 (walkable), ready to pass to NewOrthogonalGrid, NewHexGrid, or NewStaggeredGrid.

Types

type Grid added in v0.3.0

type Grid interface {
	// Width returns the number of tiles horizontally in the grid.
	Width() int
	// Height returns the number of tiles vertically in the grid.
	Height() int
	// IsInside reports whether the tile (x, y) is within the grid bounds.
	IsInside(x, y int) bool
	// IsWalkableAt reports whether the tile (x, y) is walkable.
	// Tiles outside the grid are reported as not walkable.
	IsWalkableAt(x, y int) bool
	// SetWalkableAt sets the walkability of the tile (x, y).
	SetWalkableAt(x, y int, walkable bool)
	// GetNodeAt returns the node at tile (x, y). Panics if outside the grid.
	GetNodeAt(x, y int) *finder.Node
	// ObstacleCount returns the number of non-walkable tiles.
	ObstacleCount() int
	// TileIndex returns the flat array index for tile (x, y).
	// Equivalent to y*Width + x. Panics if outside the grid.
	TileIndex(x, y int) int
	// TileXY returns the tile coordinates for a flat array index.
	// Equivalent to (index % Width, index / Width).
	TileXY(index int) (int, int)
	// Clone returns a deep copy of grid with independent node data.
	Clone() Grid

	// SetWeightAt sets the per-tile movement cost multiplier for tile (x, y).
	// Weight 1.0 is the default; higher values make movement more expensive.
	SetWeightAt(x, y int, weight float64)
	// GetWeightAt returns the movement cost multiplier for tile (x, y).
	// Returns 1.0 for tiles outside the grid.
	GetWeightAt(x, y int) float64

	// WorldToTile converts a world-space coordinate to tile-space.
	// Results outside the grid should be checked with IsInside.
	WorldToTile(wx, wy float32) (int, int)
	// TileToWorld converts a tile coordinate to world-space, returning
	// the centre point of the tile.
	TileToWorld(tx, ty int) (float32, float32)
	// IsWalkableAtWorld reports whether the tile at world position (wx, wy) is walkable.
	IsWalkableAtWorld(wx, wy float32) bool
	// SetWalkableAtWorld sets the walkability of the tile at world position (wx, wy).
	SetWalkableAtWorld(wx, wy float32, walkable bool)

	// FindNearestWalkable is like FindNearestWalkable but returns tile
	// coordinates instead of edge-clamped world coordinates.
	FindNearestWalkable(wx, wy float32, maxRadius int) (int, int, bool)
	// FindNearestWalkableWorld finds the nearest walkable tile within maxRadius
	// (Chebyshev distance in tiles) from world position (wx, wy).
	// Returns the closest point on the edge of the nearest walkable tile, or
	// (0, 0, false) if no walkable tile exists within the search radius.
	FindNearestWalkableWorld(wx, wy float32, maxRadius int, edgeInset float32) (float32, float32, bool)

	// Finder returns the pathfinder used by this grid.
	Finder() finder.Finder
	// FindPath finds a path between two tile positions.
	// Result is backed by an internal buffer — valid only until the next FindPath call.
	FindPath(x1, y1, x2, y2 int) [][2]int
	// FindPathWorld finds a path between two world positions through the grid.
	// Uses the grid's Finder. Returns the path in world coordinates.
	// Result is backed by an internal buffer — valid only until the next FindPathWorld call.
	FindPathWorld(wx1, wy1, wx2, wy2 float32) [][2]float32
	// FindSmoothPath finds a tile path and smooths it via LOS string-pulling.
	// Same buffer contract as FindPath.
	FindSmoothPath(x1, y1, x2, y2 int) [][2]int
	// FindSmoothPathWorld finds a path and smooths it via LOS string-pulling.
	// Same buffer contract as FindPath.
	FindSmoothPathWorld(wx1, wy1, wx2, wy2 float32) [][2]float32
	// SmoothenPath removes unnecessary waypoints from a tile path
	// by checking line-of-sight between each point.
	SmoothenPath(path [][2]int) [][2]int

	// RenderSVG renders the grid and paths as an SVG string.
	// Tiles are coloured by their Weight value (see DefaultSVGOpts).
	// Multiple paths are drawn in different colours (1st=blue, 2nd=red dashed).
	// Start/end markers are derived from the first path.
	// Pass nil to use DefaultSVGOpts.
	RenderSVG(cfg *SVGOpts, paths ...[][2]int) string
}

Grid defines the interface that all grid types implement. It focuses on grid-consumer operations: coordinate conversion, walkability queries, pathfinding, and rendering. This is separate from finder.Grid, which only exposes what pathfinding algorithms need.

type GridType

type GridType int

GridType identifies the tile grid layout.

const (
	// Orthogonal is a standard square-tile grid (4-directional or 8-directional).
	Orthogonal GridType = iota
	// Staggered is a 45-degree isometric/staggered grid (diamond-shaped tiles).
	Staggered
	// Hexagonal is a hexagonal grid (both pointy-top and flat-top layouts).
	Hexagonal
)

type HexGrid

type HexGrid struct {
	// contains filtered or unexported fields
}

HexGrid is a hexagonal grid using axial coordinates (X, Y). Supports both flat-top (stagger on X) and pointy-top (stagger on Y) layouts, matching Tiled's "hexagonal" orientation.

Derived layout parameters (sideOffX/Y, colW, rowH) are precomputed in NewHexGrid to avoid per-call overhead.

func NewHexGrid

func NewHexGrid(matrix [][]int, opts ...HexOption) *HexGrid

NewHexGrid creates a HexGrid from a walkability matrix (0=walkable, non-zero=obstacle). Default tile size is 64×64 pixels. Default finder is AStarFinder.

func (*HexGrid) Clone

func (g *HexGrid) Clone() Grid

Clone returns a deep copy of the hex grid, including a copy of all nodes with parent references cleared. The clone shares the finder and precomputed layout parameters but owns its own node slice.

func (*HexGrid) FindNearestWalkable added in v0.3.0

func (g *HexGrid) FindNearestWalkable(wx, wy float32, maxRadius int) (int, int, bool)

FindNearestWalkableTile finds the nearest walkable tile within maxRadius (tile rings) from the given world position (wx, wy). Returns the tile coordinates and true if found; returns (0, 0, false) if no walkable tile exists within the search radius.

func (*HexGrid) FindNearestWalkableWorld added in v0.6.1

func (g *HexGrid) FindNearestWalkableWorld(wx, wy float32, maxRadius int, edgeInset float32) (float32, float32, bool)

FindNearestWalkable finds the nearest walkable tile within maxRadius (tile rings) from the given world position (wx, wy). See Grid.FindNearestWalkable for details.

func (*HexGrid) FindPath

func (g *HexGrid) FindPath(x1, y1, x2, y2 int) [][2]int

FindPath finds a path between two world positions through the hex grid. The returned [][2]float32 is backed by an internal buffer and is only valid until the next FindPath/FindSmoothPath call on the same grid. FindPath finds a path between two tile positions.

func (*HexGrid) FindPathWorld added in v0.6.1

func (g *HexGrid) FindPathWorld(wx1, wy1, wx2, wy2 float32) [][2]float32

func (*HexGrid) FindSmoothPath

func (g *HexGrid) FindSmoothPath(x1, y1, x2, y2 int) [][2]int

FindSmoothPath finds a path between two world positions and smooths it. Same buffer contract as FindPath.

func (*HexGrid) FindSmoothPathWorld added in v0.6.1

func (g *HexGrid) FindSmoothPathWorld(wx1, wy1, wx2, wy2 float32) [][2]float32

FindSmoothPathWorld finds a path between two world positions and smooths it.

func (*HexGrid) Finder

func (g *HexGrid) Finder() finder.Finder

Finder returns the pathfinder associated with this grid.

func (*HexGrid) GetNeighbors

func (g *HexGrid) GetNeighbors(node *finder.Node, _ finder.DiagonalMovement, buffer []*finder.Node) []*finder.Node

GetNeighbors returns the 6 hex neighbors. Diagonal parameter is ignored because hex grids have no separate diagonal concept — all 6 are cardinal moves.

func (*HexGrid) GetNodeAt

func (g *HexGrid) GetNodeAt(x, y int) *finder.Node

GetNodeAt returns the node at the given tile coordinates. The coordinates must be inside the grid; the caller should check IsInside first.

func (*HexGrid) GetWeightAt added in v0.5.0

func (g *HexGrid) GetWeightAt(x, y int) float64

GetWeightAt returns the movement cost multiplier for the tile at (x, y).

func (*HexGrid) Height

func (g *HexGrid) Height() int

Height returns the number of tiles along the Y axis.

func (*HexGrid) IsInside

func (g *HexGrid) IsInside(x, y int) bool

IsInside reports whether the given tile coordinates are within the grid bounds.

func (*HexGrid) IsWalkableAt

func (g *HexGrid) IsWalkableAt(x, y int) bool

IsWalkableAt reports whether the tile at the given coordinates is walkable. Coordinates outside the grid bounds are considered not walkable.

func (*HexGrid) IsWalkableAtWorld

func (g *HexGrid) IsWalkableAtWorld(wx, wy float32) bool

IsWalkableAtWorld checks walkability at a world position.

func (*HexGrid) ObstacleCount added in v0.3.0

func (g *HexGrid) ObstacleCount() int

ObstacleCount returns the number of non-walkable tiles in the grid.

func (*HexGrid) RenderSVG

func (g *HexGrid) RenderSVG(cfg *SVGOpts, paths ...[][2]int) string

RenderSVG renders the hex grid with weight-colored tiles and paths.

func (*HexGrid) SetWalkableAt

func (g *HexGrid) SetWalkableAt(x, y int, walkable bool)

SetWalkableAt sets the walkability of the tile at the given coordinates.

func (*HexGrid) SetWalkableAtWorld

func (g *HexGrid) SetWalkableAtWorld(wx, wy float32, walkable bool)

SetWalkableAtWorld sets walkability at a world position.

func (*HexGrid) SetWeightAt added in v0.5.0

func (g *HexGrid) SetWeightAt(x, y int, weight float64)

SetWeightAt sets the movement cost multiplier for the tile at (x, y).

func (*HexGrid) SmoothenPath added in v0.6.1

func (g *HexGrid) SmoothenPath(path [][2]int) [][2]int

SmoothenTilePath smooths a tile-coordinate hex path by removing unnecessary waypoints. Writes the smoothed result in-place over the input (finder's cached pathBuf).

func (*HexGrid) TileIndex added in v0.7.2

func (g *HexGrid) TileIndex(x, y int) int

TileIndex returns the flat array index for tile (x, y).

func (*HexGrid) TileToWorld

func (g *HexGrid) TileToWorld(tx, ty int) (float32, float32)

TileToWorld converts hex tile coordinates to world (pixel) position (tile center).

func (*HexGrid) TileXY added in v0.7.3

func (g *HexGrid) TileXY(index int) (int, int)

TileXY returns the tile coordinates for a flat array index.

func (*HexGrid) Width

func (g *HexGrid) Width() int

Width returns the number of tiles along the X axis.

func (*HexGrid) WorldToTile

func (g *HexGrid) WorldToTile(wx, wy float32) (int, int)

WorldToTile converts world (pixel) coordinates to hex tile coordinates.

type HexOption

type HexOption func(*HexGrid)

HexOption configures a HexGrid.

func WithHexEvenStagger

func WithHexEvenStagger() HexOption

WithHexEvenStagger configures even-index stagger offset. Default is odd-index.

func WithHexFinder

func WithHexFinder(f finder.Finder) HexOption

WithHexFinder sets the pathfinder and returns the grid.

func WithHexFlatTop

func WithHexFlatTop() HexOption

WithHexFlatTop configures flat-top hexagons (stagger on X axis). Default is pointy-top.

func WithHexSide

func WithHexSide(side int) HexOption

WithHexSide sets the hex side length in pixels.

func WithHexTileSize

func WithHexTileSize(w, h int) HexOption

WithHexTileSize sets the pixel dimensions of the hex tile.

type OrthogonalGrid

type OrthogonalGrid struct {
	// contains filtered or unexported fields
}

OrthogonalGrid is a standard rectangular grid. Coordinates are in tile space. World-space helpers convert using tileW/tileH.

func NewOrthogonalGrid

func NewOrthogonalGrid(matrix [][]int, opts ...OrthogonalOption) *OrthogonalGrid

NewOrthogonalGrid creates an OrthogonalGrid from a matrix (0=walkable, non-zero=obstacle). Default tile size is 1×1 world unit. Default finder is AStarFinder.

func (*OrthogonalGrid) Clone

func (g *OrthogonalGrid) Clone() Grid

Clone creates a deep copy of the grid. The returned grid has its own node slice; each node is copied, but the parent pointer is cleared. The finder reference and tile dimensions are shared from the original.

func (*OrthogonalGrid) FindNearestWalkable added in v0.3.0

func (g *OrthogonalGrid) FindNearestWalkable(wx, wy float32, maxRadius int) (int, int, bool)

FindNearestWalkableTile finds the nearest walkable tile within maxRadius (tile rings) from the given world position (wx, wy). Returns the tile coordinates and true if found; returns (0, 0, false) if no walkable tile exists within the search radius.

func (*OrthogonalGrid) FindNearestWalkableWorld added in v0.6.1

func (g *OrthogonalGrid) FindNearestWalkableWorld(wx, wy float32, maxRadius int, edgeInset float32) (float32, float32, bool)

FindNearestWalkable finds the nearest walkable tile within maxRadius (tile rings) from the given world position (wx, wy). See the method doc on Grid for details.

func (*OrthogonalGrid) FindPath

func (g *OrthogonalGrid) FindPath(x1, y1, x2, y2 int) [][2]int

FindPath finds a path between two world positions through the grid. The returned [][2]float32 is backed by an internal buffer and is only valid until the next FindPath/FindSmoothPath call on the same grid. Automatically smooths the path via SmoothenTilePath when smoothing is enabled (default). Disable with WithGridSmoothing(false) when using a finder that already does its own smoothing, such as WaypointFinder. FindPath finds a path between two tile positions.

func (*OrthogonalGrid) FindPathWorld added in v0.6.1

func (g *OrthogonalGrid) FindPathWorld(wx1, wy1, wx2, wy2 float32) [][2]float32

func (*OrthogonalGrid) FindSmoothPath

func (g *OrthogonalGrid) FindSmoothPath(x1, y1, x2, y2 int) [][2]int

FindSmoothPath finds a path between two world positions and smooths it. Same buffer contract as FindPath.

func (*OrthogonalGrid) FindSmoothPathWorld added in v0.6.1

func (g *OrthogonalGrid) FindSmoothPathWorld(wx1, wy1, wx2, wy2 float32) [][2]float32

FindSmoothPathWorld finds a path between two world positions and smooths it.

func (*OrthogonalGrid) Finder

func (g *OrthogonalGrid) Finder() finder.Finder

Finder returns the pathfinder associated with this grid.

func (*OrthogonalGrid) GetNeighbors

func (g *OrthogonalGrid) GetNeighbors(node *finder.Node, diagonal finder.DiagonalMovement, buffer []*finder.Node) []*finder.Node

GetNeighbors returns the walkable neighbors of the given node. The diagonal parameter controls which diagonal moves are permitted. The result is written into the provided buffer slice (which is resliced to zero and reused), so the caller must not hold references past the next call.

func (*OrthogonalGrid) GetNodeAt

func (g *OrthogonalGrid) GetNodeAt(x, y int) *finder.Node

GetNodeAt returns the node at tile coordinate (x, y). The caller must ensure the coordinate is inside the grid (see IsInside); otherwise the method panics.

func (*OrthogonalGrid) GetWeightAt added in v0.5.0

func (g *OrthogonalGrid) GetWeightAt(x, y int) float64

GetWeightAt returns the movement cost multiplier for the tile at (x, y). Returns the default weight (1.0) for tiles outside the grid.

func (*OrthogonalGrid) Height

func (g *OrthogonalGrid) Height() int

Height returns the number of tiles vertically in the grid.

func (*OrthogonalGrid) IsInside

func (g *OrthogonalGrid) IsInside(x, y int) bool

IsInside reports whether the tile coordinate (x, y) is within the grid bounds.

func (*OrthogonalGrid) IsWalkableAt

func (g *OrthogonalGrid) IsWalkableAt(x, y int) bool

IsWalkableAt reports whether the tile at (x, y) is walkable. Coordinates outside the grid are reported as not walkable.

func (*OrthogonalGrid) IsWalkableAtWorld

func (g *OrthogonalGrid) IsWalkableAtWorld(wx, wy float32) bool

IsWalkableAtWorld reports whether the tile at the given world-space coordinate is walkable. The coordinate is converted to tile-space via WorldToTile before checking.

func (*OrthogonalGrid) ObstacleCount added in v0.3.0

func (g *OrthogonalGrid) ObstacleCount() int

ObstacleCount returns the number of non-walkable tiles in the grid.

func (*OrthogonalGrid) RenderSVG

func (g *OrthogonalGrid) RenderSVG(cfg *SVGOpts, paths ...[][2]int) string

RenderSVG renders the grid with weight-colored tiles and one or more paths. Each path in paths is drawn in a different color. The first path uses the default blue; additional paths cycle through red, green, and purple. Walkable tiles are colored by their Weight field:

weight=1.0 (default): light green
weight<1.0 (road):    light blue
weight>1.0 (swamp):   yellow → orange gradient

Blocked tiles are shown in dark gray with a hatch pattern.

func (*OrthogonalGrid) SetWalkableAt

func (g *OrthogonalGrid) SetWalkableAt(x, y int, walkable bool)

SetWalkableAt sets the walkability of the tile at (x, y). The caller must ensure the coordinate is inside the grid; otherwise the method panics.

func (*OrthogonalGrid) SetWalkableAtWorld

func (g *OrthogonalGrid) SetWalkableAtWorld(wx, wy float32, walkable bool)

SetWalkableAtWorld sets the walkability of the tile at the given world-space coordinate. The coordinate is converted to tile-space via WorldToTile before applying the change.

func (*OrthogonalGrid) SetWeightAt added in v0.5.0

func (g *OrthogonalGrid) SetWeightAt(x, y int, weight float64)

SetWeightAt sets the movement cost multiplier for the tile at (x, y). A weight of 1.0 is the default; higher values make the tile more costly to traverse. Panics if (x, y) is outside the grid.

func (*OrthogonalGrid) SmoothenPath added in v0.6.1

func (g *OrthogonalGrid) SmoothenPath(path [][2]int) [][2]int

SmoothenTilePath smooths a tile-coordinate path by removing unnecessary waypoints. Writes the result in-place over the input buffer (which is the finder's cached pathBuf), so the smoothed path reuses the same allocation.

func (*OrthogonalGrid) SupportsJPSCanonicalPruning

func (g *OrthogonalGrid) SupportsJPSCanonicalPruning() bool

SupportsJPSCanonicalPruning returns true — orthogonal grids have axis-aligned topology and support the standard JPS pruning rules.

func (*OrthogonalGrid) TileHeight

func (g *OrthogonalGrid) TileHeight() int

TileHeight returns the world-space height of a single tile.

func (*OrthogonalGrid) TileIndex added in v0.7.2

func (g *OrthogonalGrid) TileIndex(x, y int) int

TileIndex returns the flat array index for tile (x, y).

func (*OrthogonalGrid) TileToWorld

func (g *OrthogonalGrid) TileToWorld(tx, ty int) (float32, float32)

TileToWorld converts a tile-space coordinate to world-space, returning the center point of the tile (offset by half the tile dimensions).

func (*OrthogonalGrid) TileWidth

func (g *OrthogonalGrid) TileWidth() int

TileWidth returns the world-space width of a single tile.

func (*OrthogonalGrid) TileXY added in v0.7.3

func (g *OrthogonalGrid) TileXY(index int) (int, int)

TileXY returns the tile coordinates for a flat array index.

func (*OrthogonalGrid) Width

func (g *OrthogonalGrid) Width() int

Width returns the number of tiles horizontally in the grid.

func (*OrthogonalGrid) WorldToTile

func (g *OrthogonalGrid) WorldToTile(wx, wy float32) (int, int)

WorldToTile converts a world-space coordinate to a tile-space coordinate. The conversion uses integer truncation; results outside the grid should be checked with IsInside before use.

type OrthogonalOption

type OrthogonalOption func(*OrthogonalGrid)

OrthogonalOption configures an OrthogonalGrid.

func WithOrthogonalFinder

func WithOrthogonalFinder(f finder.Finder) OrthogonalOption

WithOrthogonalFinder sets the pathfinder associated with this grid.

func WithOrthogonalTileSize

func WithOrthogonalTileSize(w, h int) OrthogonalOption

WithOrthogonalTileSize sets the tile dimensions for world coordinate conversion.

type SVGOpts added in v0.5.0

type SVGOpts struct {
	// WeightColors maps weight thresholds to tile fill colors.
	// Each entry is (threshold, color). Applied in order: weight <= threshold → color.
	// The last entry with a threshold of +Inf catches everything above the second-last.
	// The second-to-last entry with a threshold of +Inf catches exactly the last threshold.
	// Example:
	//   {{"0.3", "#bbdefb"}, {"1.0", "#c8e6c9"}, {"2.0", "#fff9c4"}, {"5.0", "#ffcc80"}, {"+Inf", "#ef5350"}}
	// Set to nil for defaults: road(0.3)→blue, grass(1.0)→green, sand(2.0)→yellow,
	// swamp(5.0)→orange, >5.0→red.
	WeightColors [][2]string
	// LegendEntries overrides the legend labels. Each entry is (color_hex, label).
	// If nil, labels are auto-generated from WeightColors as "≤threshold".
	LegendEntries [][2]string
}

Grid defines the interface that all grid types implement. It focuses on grid-consumer operations: coordinate conversion, walkability queries, pathfinding, and rendering. This is separate from finder.Grid, which only exposes what pathfinding algorithms need. SVGOpts configures SVG rendering behavior. nil means use defaults.

type StaggerOption

type StaggerOption func(*StaggeredGrid)

StaggerOption configures a StaggeredGrid.

func WithStaggerAxis

func WithStaggerAxis(axis string) StaggerOption

WithStaggerAxis sets the stagger axis ("x" or "y"). Default is "y".

func WithStaggerEvenIndex

func WithStaggerEvenIndex() StaggerOption

WithStaggerEvenIndex configures even-index stagger offset. Default is odd-index.

func WithStaggerFinder

func WithStaggerFinder(f finder.Finder) StaggerOption

WithStaggerFinder sets the pathfinder and returns the grid.

func WithStaggerTileSize

func WithStaggerTileSize(w, h int) StaggerOption

WithStaggerTileSize sets the pixel dimensions of the staggered tile.

type StaggeredGrid

type StaggeredGrid struct {
	// contains filtered or unexported fields
}

StaggeredGrid is a 45-degree isometric/staggered grid (diamond-shaped tiles). It mirrors Tiled's "staggered" orientation, using the HexagonalRenderer math but overrides screenToTileCoords with a 4-corner detection + 45° rotation (exactly as Tiled's StaggeredRenderer does).

func NewStaggeredGrid

func NewStaggeredGrid(matrix [][]int, opts ...StaggerOption) *StaggeredGrid

NewStaggeredGrid creates a StaggeredGrid from a matrix (0=walkable, non-zero=obstacle). Default tile size is 64x64. Default stagger axis is "y" (odd rows shift right). Default finder is AStarFinder.

func (*StaggeredGrid) Clone

func (g *StaggeredGrid) Clone() Grid

Clone returns a deep copy of the staggered grid with independent node data.

func (*StaggeredGrid) FindNearestWalkable added in v0.3.0

func (g *StaggeredGrid) FindNearestWalkable(wx, wy float32, maxRadius int) (int, int, bool)

FindNearestWalkableTile finds the nearest walkable tile within maxRadius (tile rings) from the given world position (wx, wy). Returns the tile coordinates and true if found; returns (0, 0, false) if no walkable tile exists within the search radius.

func (*StaggeredGrid) FindNearestWalkableWorld added in v0.6.1

func (g *StaggeredGrid) FindNearestWalkableWorld(wx, wy float32, maxRadius int, edgeInset float32) (float32, float32, bool)

FindNearestWalkable finds the nearest walkable tile within maxRadius (tile rings) from the given world position (wx, wy). See Grid.FindNearestWalkable for details.

func (*StaggeredGrid) FindPath

func (g *StaggeredGrid) FindPath(x1, y1, x2, y2 int) [][2]int

FindPath finds a path between two world positions through the staggered grid. The returned [][2]float32 is backed by an internal buffer and is only valid until the next FindPath/FindSmoothPath call on the same grid. FindPath finds a path between two tile positions.

func (*StaggeredGrid) FindPathWorld added in v0.6.1

func (g *StaggeredGrid) FindPathWorld(wx1, wy1, wx2, wy2 float32) [][2]float32

func (*StaggeredGrid) FindSmoothPath

func (g *StaggeredGrid) FindSmoothPath(x1, y1, x2, y2 int) [][2]int

FindSmoothPath finds a path between two world positions and smooths it. Same buffer contract as FindPath.

func (*StaggeredGrid) FindSmoothPathWorld added in v0.6.1

func (g *StaggeredGrid) FindSmoothPathWorld(wx1, wy1, wx2, wy2 float32) [][2]float32

FindSmoothPathWorld finds a path between two world positions and smooths it.

func (*StaggeredGrid) Finder

func (g *StaggeredGrid) Finder() finder.Finder

Finder returns the pathfinder associated with this grid.

func (*StaggeredGrid) GetNeighbors

func (g *StaggeredGrid) GetNeighbors(node *finder.Node, diagonal finder.DiagonalMovement, buffer []*finder.Node) []*finder.Node

GetNeighbors returns neighbors for a node on a staggered isometric grid, respecting the diagonal movement rule. The result is written into the provided buffer.

func (*StaggeredGrid) GetNodeAt

func (g *StaggeredGrid) GetNodeAt(x, y int) *finder.Node

GetNodeAt returns the node at the given tile coordinates.

func (*StaggeredGrid) GetWeightAt added in v0.5.0

func (g *StaggeredGrid) GetWeightAt(x, y int) float64

GetWeightAt returns the movement cost multiplier for the tile at (x, y).

func (*StaggeredGrid) Height

func (g *StaggeredGrid) Height() int

Height returns the number of tiles vertically in the staggered grid.

func (*StaggeredGrid) IsInside

func (g *StaggeredGrid) IsInside(x, y int) bool

IsInside checks whether the tile coordinates (x, y) are within the grid bounds.

func (*StaggeredGrid) IsWalkableAt

func (g *StaggeredGrid) IsWalkableAt(x, y int) bool

IsWalkableAt returns whether the tile at (x, y) is walkable (no obstacle).

func (*StaggeredGrid) IsWalkableAtWorld

func (g *StaggeredGrid) IsWalkableAtWorld(wx, wy float32) bool

IsWalkableAtWorld checks walkability at a world position.

func (*StaggeredGrid) ObstacleCount added in v0.3.0

func (g *StaggeredGrid) ObstacleCount() int

ObstacleCount returns the number of non-walkable tiles in the grid.

func (*StaggeredGrid) RenderSVG

func (g *StaggeredGrid) RenderSVG(cfg *SVGOpts, paths ...[][2]int) string

RenderSVG renders the staggered grid with weight-colored tiles and paths.

func (*StaggeredGrid) SetWalkableAt

func (g *StaggeredGrid) SetWalkableAt(x, y int, walkable bool)

SetWalkableAt sets the walkability of the tile at (x, y).

func (*StaggeredGrid) SetWalkableAtWorld

func (g *StaggeredGrid) SetWalkableAtWorld(wx, wy float32, walkable bool)

SetWalkableAtWorld sets walkability at a world position.

func (*StaggeredGrid) SetWeightAt added in v0.5.0

func (g *StaggeredGrid) SetWeightAt(x, y int, weight float64)

SetWeightAt sets the movement cost multiplier for the tile at (x, y).

func (*StaggeredGrid) SmoothenPath added in v0.6.1

func (g *StaggeredGrid) SmoothenPath(path [][2]int) [][2]int

SmoothenTilePath smooths a tile-coordinate staggered path by removing unnecessary waypoints. Writes the smoothed result in-place over the input (finder's cached pathBuf).

func (*StaggeredGrid) TileIndex added in v0.7.2

func (g *StaggeredGrid) TileIndex(x, y int) int

TileIndex returns the flat array index for tile (x, y).

func (*StaggeredGrid) TileToWorld

func (g *StaggeredGrid) TileToWorld(tx, ty int) (float32, float32)

TileToWorld converts tile coordinates to world position.

func (*StaggeredGrid) TileXY added in v0.7.3

func (g *StaggeredGrid) TileXY(index int) (int, int)

TileXY returns the tile coordinates for a flat array index.

func (*StaggeredGrid) Width

func (g *StaggeredGrid) Width() int

Width returns the number of tiles horizontally in the staggered grid.

func (*StaggeredGrid) WorldToTile

func (g *StaggeredGrid) WorldToTile(wx, wy float32) (int, int)

WorldToTile converts world coordinates to staggered tile coordinates. Mirrors Tiled's StaggeredRenderer::screenToTileCoords.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL