tilemap

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TileFlipH    uint32 = 1 << 31
	TileFlipV    uint32 = 1 << 30
	TileFlipD    uint32 = 1 << 29
	TileFlagMask uint32 = TileFlipH | TileFlipV | TileFlipD
)

GID flag bits (same convention as Tiled TMX format).

View Source
const MaxTilesPerDraw = 16383

MaxTilesPerDraw is the maximum number of tiles per DrawTriangles call.

Variables

View Source
var (
	// NewContainerFn creates a container node. Set by root package.
	NewContainerFn func(name string) *node.Node

	// NewLayerEmitFn, if set, is called after AddTileLayer creates a Layer so
	// root can inject the render-command emission logic as Layer.EmitFn.
	NewLayerEmitFn func(layer *Layer)
)

Function pointers wired by root to break dependency on Camera, Scene, etc.

View Source
var UVOrder = [8][4]int{
	{0, 1, 2, 3},
	{2, 0, 3, 1},
	{2, 3, 0, 1},
	{3, 2, 1, 0},
	{1, 0, 3, 2},
	{0, 2, 1, 3},
	{3, 2, 1, 0},
	{1, 3, 0, 2},
}

UVOrder defines vertex UV assignment for each combination of flip flags.

Functions

func EmitTilemapCommands

func EmitTilemapCommands(layer *Layer, commands *[]render.RenderCommand, viewTransform [6]float64, treeOrder *int)

EmitTilemapCommands transforms tile vertices into screen space and appends one or more CommandTilemap render commands (split at MaxTilesPerDraw) to the provided command slice. viewTransform is the camera's view matrix.

func EncodeGID added in v0.2.2

func EncodeGID(tileID uint32, flipH, flipV, flipD bool) uint32

EncodeGID combines a tile ID with flip flags into a single uint32 GID.

func RegionsFromGrid added in v0.2.2

func RegionsFromGrid(columns, tileW, tileH, margin, spacing, count int) []types.TextureRegion

RegionsFromGrid builds a TextureRegion slice from a regular grid tileset. Index 0 is a zero region (empty tile); indices 1..count map to tiles in row-major order. Margin is the outer border; spacing is the gap between tiles.

func SetTileUVs

func SetTileUVs(verts []ebiten.Vertex, region types.TextureRegion, flags uint32, tw, th float32)

SetTileUVs sets the UV (SrcX/SrcY) coordinates for 4 vertices of a tile.

Types

type AnimFrame

type AnimFrame struct {
	GID      uint32 // tile GID for this frame (no flag bits)
	Duration int    // milliseconds
}

AnimFrame describes a single frame in a tile animation sequence.

type CameraBoundsAdapter

type CameraBoundsAdapter struct {
	Cam *camera.Camera
}

CameraBoundsAdapter wraps *camera.Camera to satisfy VisibleBoundsProvider.

func (*CameraBoundsAdapter) ViewportHeight

func (a *CameraBoundsAdapter) ViewportHeight() float64

func (*CameraBoundsAdapter) ViewportWidth

func (a *CameraBoundsAdapter) ViewportWidth() float64

func (*CameraBoundsAdapter) VisibleBounds

func (a *CameraBoundsAdapter) VisibleBounds() types.Rect

type EmitCommandsFn

type EmitCommandsFn func(layer *Layer, sceneAny any, treeOrder *int)

EmitCommandsFn is the callback type for emitting tilemap render commands. The any parameter is the opaque *Scene, matching Node.CustomEmit signature.

type Layer

type Layer struct {
	Node_       *node.Node
	Data        []uint32
	Width       int
	Height      int
	Vertices    []ebiten.Vertex
	Indices     []uint16
	TileCount   int
	WorldX      []float32
	WorldY      []float32
	Regions     []types.TextureRegion
	BufStartCol int
	BufStartRow int
	BufCols     int
	BufRows     int
	BufDirty    bool
	Anims       map[uint32][]AnimFrame
	AtlasImage  *ebiten.Image
	Viewport_   *Viewport

	// EmitFn is called during rendering to emit tilemap commands.
	EmitFn EmitCommandsFn
}

Layer is a single layer of tile data.

func (*Layer) Dimensions

func (l *Layer) Dimensions() (cols, rows int)

Dimensions returns the layer size in tiles (cols, rows).

func (*Layer) EnsureBuffer

func (l *Layer) EnsureBuffer(cols, rows int)

EnsureBuffer grows the geometry buffer if needed.

func (*Layer) InvalidateBuffer

func (l *Layer) InvalidateBuffer()

InvalidateBuffer forces a full buffer rebuild on the next frame.

func (*Layer) LateRebuildCheck

func (l *Layer) LateRebuildCheck()

LateRebuildCheck detects whether the camera moved after the Update pass.

func (*Layer) Node

func (l *Layer) Node() *node.Node

Node returns the layer's scene graph node.

func (*Layer) RawTileAt

func (l *Layer) RawTileAt(col, row int) uint32

RawTileAt returns the raw GID at (col, row) including flip flags. Returns 0 for out-of-bounds or empty tiles.

func (*Layer) RebuildBuffer

func (l *Layer) RebuildBuffer(startCol, startRow, bufCols, bufRows int)

RebuildBuffer fills the vertex buffer with tile data for the given range.

func (*Layer) SetAnimations

func (l *Layer) SetAnimations(anims map[uint32][]AnimFrame)

SetAnimations sets the animation definitions for this layer.

func (*Layer) SetData

func (l *Layer) SetData(data []uint32, w, h int)

SetData replaces the entire tile data array and forces a full buffer rebuild.

func (*Layer) SetTile

func (l *Layer) SetTile(col, row int, newGID uint32)

SetTile updates a single tile in the given layer.

func (*Layer) TileAt

func (l *Layer) TileAt(col, row int) uint32

TileAt returns the GID at (col, row) with flip flags stripped. Returns 0 for out-of-bounds or empty tiles.

func (*Layer) TileSize

func (l *Layer) TileSize() (w, h int)

TileSize returns the pixel dimensions of a single tile.

type LayerConfig

type LayerConfig struct {
	Name       string
	Width      int
	Height     int
	Data       []uint32
	Regions    []types.TextureRegion
	AtlasImage *ebiten.Image
}

LayerConfig holds the parameters for creating a tile layer.

type TileQuery

type TileQuery struct {
	Cols, Rows   int
	TileW, TileH int
	TileAt       func(col, row int) uint32
}

TileQuery provides a read-only view of tilemap data suitable for external systems like pathfinding or collision. No Willow imports needed.

type Viewport

type Viewport struct {
	TileWidth   int
	TileHeight  int
	MaxZoomOut  float64
	MarginTiles int

	// CameraBoundsFn resolves the camera to a VisibleBoundsProvider.
	CameraBoundsFn func(cam any) VisibleBoundsProvider
	// contains filtered or unexported fields
}

Viewport is a scene graph node that manages a viewport into a tilemap.

func NewViewport

func NewViewport(name string, tileWidth, tileHeight int) *Viewport

NewViewport creates a new tilemap viewport node.

func (*Viewport) AddChild

func (v *Viewport) AddChild(child *node.Node)

AddChild adds a regular node container as a child (sandwich layers).

func (*Viewport) AddTileLayer

func (v *Viewport) AddTileLayer(cfg LayerConfig) *Layer

AddTileLayer creates a tile layer and adds it as a child of the viewport.

func (*Viewport) Layer

func (v *Viewport) Layer(index int) *Layer

Layer returns the tile layer at the given index, or nil if out of range.

func (*Viewport) LayerCount

func (v *Viewport) LayerCount() int

LayerCount returns the number of tile layers.

func (*Viewport) Layers

func (v *Viewport) Layers() []*Layer

Layers returns the tile layers.

func (*Viewport) Node

func (v *Viewport) Node() *node.Node

Node returns the underlying scene graph node.

func (*Viewport) Query

func (v *Viewport) Query(layerIndex int) TileQuery

Query returns a TileQuery for the given layer index, usable by external packages. Returns a zero TileQuery if the index is out of range.

func (*Viewport) SetCamera

func (v *Viewport) SetCamera(cam any)

SetCamera binds this viewport to a specific camera (opaque).

func (*Viewport) TileToWorld

func (v *Viewport) TileToWorld(col, row int) (x, y float64)

TileToWorld converts tile coordinates to world-space pixel coordinates (top-left corner of the tile).

func (*Viewport) UpdateAnimations

func (v *Viewport) UpdateAnimations()

UpdateAnimations scans layers for animated tiles and updates their UVs.

func (*Viewport) WorldToTile

func (v *Viewport) WorldToTile(x, y float64) (col, row int)

WorldToTile converts world-space coordinates to tile coordinates.

type VisibleBoundsProvider

type VisibleBoundsProvider interface {
	VisibleBounds() types.Rect
	ViewportWidth() float64
	ViewportHeight() float64
}

VisibleBoundsProvider is the interface for camera visible bounds.

Jump to

Keyboard shortcuts

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