tilemap

package
v0.0.0-...-2fe9ae2 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 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 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) 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) 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.

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 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) 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) SetCamera

func (v *Viewport) SetCamera(cam any)

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

func (*Viewport) UpdateAnimations

func (v *Viewport) UpdateAnimations()

UpdateAnimations scans layers for animated tiles and updates their UVs.

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