tilemap

package
v0.0.0-...-bcfcd92 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToInts

func ToInts(v rune) int

func ToRunes

func ToRunes(v rune) rune

Types

type Container

type Container[T comparable] struct {
	Value T
	// contains filtered or unexported fields
}

Container represents the intermediate object stored in the tile map. This container type implements astar.Pather with the following assumptions:

* Neighbors on the cardinal direction are reachable if they exist in the tile map * The cost to traverse any neighbor is always 1 * The cost estimate between any two points in the map is the manhattan distance between them

To override these, override Map.NeighborFunc, Map.CostFunc, and Map.EstimateFunc

func (Container[T]) Location

func (t Container[T]) Location() (int, int)

func (Container[T]) PathEstimatedCost

func (t Container[T]) PathEstimatedCost(to astar.Pather) float64

func (Container[T]) PathNeighborCost

func (t Container[T]) PathNeighborCost(to astar.Pather) float64

func (Container[T]) PathNeighbors

func (t Container[T]) PathNeighbors() (results []astar.Pather)

type Map

type Map[T comparable] struct {
	NeighborFunc func(container Container[T]) []Container[T]
	CostFunc     func(a, b Container[T]) float64
	EstimateFunc func(a, b Container[T]) float64
	// contains filtered or unexported fields
}

Map represents a fixed size grid of tiles. The top-left tile is [0,0], the bottom-right tile is [w, h]. Tiles are stored internally in a Container which implements astar.Pather with the following assumptions:

* Neighbors on the cardinal direction are reachable if they exist in the tile map * The cost to traverse any neighbor is always 1 * The cost estimate between any two points in the map is the manhattan distance between them

To override these, override Map.NeighborFunc, Map.CostFunc, and Map.EstimateFunc

func FromInput

func FromInput(input io.Reader) *Map[rune]

FromInput creates a Map of runes from the input where each line is one row in the map and each rune in each line is a column.

func FromInputOf

func FromInputOf[T comparable](input io.Reader, convert func(rune) T) *Map[T]

FromInputOf creates a Map of the specified type from the input where each line is one row in the map and each rune in each line is a column whose value is computed using the provided conversion function.

func Of

func Of[T comparable](w, h int) *Map[T]

Of creates a new empty tile map of the specified type and size

func (*Map[T]) AllContainersWith

func (t *Map[T]) AllContainersWith(v T) (results []Container[T])

func (*Map[T]) AllNeighbors

func (t *Map[T]) AllNeighbors(x, y int) iter.Seq2[T, Point]

AllNeighbors returns an iter.Seq2 over the 8 surrounding tiles of the specified tile, if they also exist in the map.

It is OK to change the value of tiles while iterating the returned sequence, but tiles should not be added to or removed from the map while iterating.

func (*Map[T]) CardinalNeighbors

func (t *Map[T]) CardinalNeighbors(x, y int) iter.Seq2[T, Point]

CardinalNeighbors returns an iter.Seq2 over neighbors directly North, South, East, or West of the specified location in the map, if they also exist in the map.

It is OK to change the value of tiles while iterating the returned sequence, but tiles should not be added to or removed from the map while iterating.

func (*Map[T]) ContainerAt

func (t *Map[T]) ContainerAt(x, y int) (Container[T], bool)

ContainerAt returns the tile container at the specified coordinates in the tile map

func (*Map[T]) FirstContainerWith

func (t *Map[T]) FirstContainerWith(v T) (Container[T], bool)

func (*Map[T]) PathBetween

func (t *Map[T]) PathBetween(startX, startY, endX, endY int) ([]astar.Pather, float64, bool)

PathBetween uses the A-Star path finding algorithm to find the most efficient path between the two locations in the map. By default, the following constraints are used for finding the path:

* Neighbors on the cardinal direction are reachable if they exist in the tile map * The cost to traverse any neighbor is always 1 * The cost estimate between any two points in the map is the manhattan distance between them

To override these, override Map.NeighborFunc, Map.CostFunc, and Map.EstimateFunc

func (*Map[T]) SetTile

func (t *Map[T]) SetTile(x, y int, tile T)

SetTile stores the specified tile at location (X,Y) in the tile map

func (*Map[T]) Size

func (t *Map[T]) Size() (int, int)

Size returns the width and height of the tile map

func (*Map[T]) TileAt

func (t *Map[T]) TileAt(x, y int) (T, bool)

TileAt returns the value of the Tile at the specified coordinates in the map

func (*Map[T]) Values

func (t *Map[T]) Values() iter.Seq2[T, Point]

Values returns an iter.Seq2 over all values in the map starting at 0,0 one row at a time. Each tile will be visited exactly once.

It is OK to change the value of tiles while walking.

type Point

type Point struct{ X, Y int }

Jump to

Keyboard shortcuts

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