maps

package
v0.0.0-...-5d524ee Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewParseFunc

func NewParseFunc[TileType Tile](parseTile func(r rune) (TileType, error), options ...MapOption) func([]byte) (*Map[TileType], error)

NewParseFunc creates a new parser that will parse exactly one map from the given byte slice.

If more than one map is found, an error will be returned. If you want to parse multiple maps then use NewStreamingParseFunc.

func NewStreamingParseFunc

func NewStreamingParseFunc[TileType Tile](parseTile func(r rune) (TileType, error), options ...MapOption) func([]byte) stream.Stream[*Map[TileType]]

NewStreamingParseFunc creates a new stream parser that will parse one or more maps from an original byte slice.

If you only need to parse a single map use NewParseFunc.

func Rotate

func Rotate[TileType Tile](m *Map[TileType], direction Direction)

Rotate rotates the map in the given direction, which is one of North, East, South & West, which is the equivalent of rotating the map 0, 90, 180 & 270 degrees

Any other direction will result in a panic.

func Tilt

func Tilt[TileType Tile](m *Map[TileType], direction Direction, MovableTile TileType)

Tilt tilts the map in the given direction so that the MovableTile will move in that direction until it hits another tile type which isn't an empty tile.

Types

type AnyTile

type AnyTile interface {
	// Valid returns true if this tile is a valid tile.
	Valid() bool

	// Rune should return the single rune that represents
	// this tile.
	Rune() rune

	// Colour is the colour of the tile when displayed
	// in an animation.
	Colour() color.Color
}

AnyTile represents an untyped tile that can be used in a Map.

type Direction

type Direction uint8

Direction represents a direction on a map.

const (
	// North is the direction up.
	North Direction = iota

	// East is the direction right.
	East

	// South is the direction down.
	South

	// West is the direction left.
	West
)

func (Direction) FlipHorizontal

func (d Direction) FlipHorizontal() Direction

func (Direction) String

func (d Direction) String() string

type Map

type Map[TileType Tile] struct {
	Width  int // The width of the map
	Height int // The height of the map

	// Tiles are all the [Tile]'s that make up the map.
	//
	// The tiles are laid out by rows, such that
	// indexes 0 -> Width are row 1, and indexes
	// Width -> Width * 2 are row 2.
	Tiles []TileType

	// EmptyType represents empty space in the map and
	// is by default the zero value of [Tile].
	EmptyType TileType

	TileRender  TileRender        // How to render the tiles
	TilePalette []color.Color     // The palette of colours to use when rendering the tiles
	MinTileSize int               // The minimum size of the map when rendered
	MaxTileSize int               // The maximum size of the map when rendered
	Frames      []frame[TileType] // The frames of the we've captured
	// contains filtered or unexported fields
}

Map represents a two dimensional map of some set of Values.

The zero value of Tile is expected to be "empty" (i.e. in the case of flood filling, it's not a wall). If you want to change this override the [EmptyType] field.

func From2DSlices

func From2DSlices[TileType Tile](tiles [][]TileType, options ...MapOption) (*Map[TileType], error)

From2DSlices creates a new map from the given 2D slice of tiles. where the first index is the y position, and the second index is the x position.

func New

func New[TileType Tile](width, height int, options ...MapOption) *Map[TileType]

New creates a new map with the given width and height will all tiles set to the zero value of Tile.

func (*Map[TileType]) AddFlagAt

func (m *Map[TileType]) AddFlagAt(pos Pos, flag TileType)

AddFlagAt adds the given flag to the tile at the given x, y position.

func (*Map[TileType]) CaptureFrame

func (m *Map[TileType]) CaptureFrame(label string, delay int)

CaptureFrame captures the current state of the map as a frame with the given label and delay if and only if we are currently capturing frames. Otherwise this function does nothing.

func (*Map[TileType]) Get

func (m *Map[TileType]) Get(pos Pos) (rtn TileType, valid bool)

Get returns the tile at the given x, y position.

If the position is out of bounds, then valid will be false, and rtn will be the zero value of Tile.

Otherwise valid will be true, and rtn will be the tile at the given position.

func (*Map[TileType]) InBounds

func (m *Map[TileType]) InBounds(pos Pos) bool

InBounds returns true if the given position is in bounds of the map.

func (*Map[TileType]) IndexOf

func (m *Map[TileType]) IndexOf(pos Pos) int

IndexOf returns the index of the given x, y position.

func (*Map[TileType]) Neighbours

func (m *Map[TileType]) Neighbours(pos Pos) (rtn []Pos)

Neighbours returns the neighbours of the given position.

func (*Map[TileType]) PositionOf

func (m *Map[TileType]) PositionOf(idx int) Pos

PositionOf returns the x, y position of the given index.

func (*Map[TileType]) RemoveFlagAt

func (m *Map[TileType]) RemoveFlagAt(pos Pos, flag TileType)

RemoveFlagAt removes the given flag from the tile at the given x, y position.

func (*Map[TileType]) SaveAnimationGIF

func (m *Map[TileType]) SaveAnimationGIF(ctx *runner.Context) error

func (*Map[TileType]) Set

func (m *Map[TileType]) Set(pos Pos, tile TileType) (valid bool)

Set sets the tile at the given x, y position.

If the position is out of bounds, then valid will be false, otherwise valid will be true.

func (*Map[TileType]) StartCapturingFrames

func (m *Map[TileType]) StartCapturingFrames(ctx *runner.Context)

StartCapturingFrames starts capturing frames for the map starting with the current state of the map

func (*Map[TileType]) StopCapturingFrames

func (m *Map[TileType]) StopCapturingFrames(label string)

StopCapturingFrames stops capturing frames for the map

func (*Map[TileType]) String

func (m *Map[TileType]) String() string

String returns a string representation of the map.

type MapOption

type MapOption func(cfg *mapCfg)

MapOption represents an option that can be applied to a map

func WithColourPalette

func WithColourPalette(palette []color.Color) MapOption

WithColourPalette sets the colour palette for the map

If not set, the map will compute the palette from the tiles by iterating from the zero value, until it gets false from a call to [Tile.Valid]

func WithMaxTileSize

func WithMaxTileSize(sizeInPixels int) MapOption

WithMaxTileSize sets the maximum tile size for the map when rendering

func WithMinTileSize

func WithMinTileSize(sizeInPixels int) MapOption

WithMinTileSize sets the minimum tile size for the map when rendering

func WithTileRender

func WithTileRender(render TileRender) MapOption

WithTileRender sets the tile render function for the map

If not set, the map will render each tile fully in the colour of the tile

type Pos

type Pos = vec2.Vec2

Pos represents a position on the map with x, y coordinates.

type Tile

type Tile interface {
	~int | ~uint | ~int8 | ~uint8 | ~int16 | ~uint16 | ~int32 | ~uint32 | ~int64 | ~uint64
	AnyTile
}

Tile represents a single tile in a Map.

type TileRender

type TileRender func(tile AnyTile, img draw.Image, x, y, size int)

TileRender sets the tile render function for the map

Jump to

Keyboard shortcuts

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