tilemap

package
v0.0.0-...-94fff03 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tilemap loads Tiled maps as assets and draws their tile and image layers through the render queue.

It has a data half and a runtime half. The data half is the asset types Tileset (*.tsj), Map (*.tmj) and World (*.world), which RegisterLoaders makes loadable; a Map is a self-contained, read-only value with its layers flattened in draw order, a GID table for O(1) tile lookups and the map's objects and custom properties as data. The runtime half is the TileLayer component, Spawn and SpawnWorld, which create one entity per drawable layer, and TilemapExtract, which queues one render.Item per visible tile. NewCollisionGrid builds a solid-cell lookup from the tiles' collision shapes.

File parsing and per-file checks are package tiled's job; the sprites of a single-image tileset are cut by the render atlas loader (the same *.tsj key loaded as a render.Atlas). The package makes no Ebiten calls.

The engine gives no meaning to objects, classes or custom properties: game code reads them through the Map's query API. The guide is doc/guide/engine/tilemaps.md.

Index

Constants

View Source
const (
	// PropShader is the .kage key of the layer's shader, relative to the map
	// file like an image layer's image.
	PropShader = "shader"
	// PropShaderParams is up to four comma-separated numbers for the
	// shader's Params uniform; missing ones are 0.
	PropShaderParams = "shaderParams"
)

Layer properties that put a tile or image layer through a shader (see Layer.Shader).

Variables

This section is empty.

Functions

func HoldMap

func HoldMap(sc *asset.Scope, h MapHandle)

HoldMap adds sc as an owner of h, like asset.Hold.

func HoldTileset

func HoldTileset(sc *asset.Scope, h TilesetHandle)

HoldTileset adds sc as an owner of h, like asset.Hold.

func HoldWorld

func HoldWorld(sc *asset.Scope, h WorldHandle)

HoldWorld adds sc as an owner of h, like asset.Hold.

func ObjectPos

func ObjectPos(l *Layer, o *tiled.Object, mapPos gfx.Vec2) gfx.Vec2

ObjectPos returns the world position of object o of layer l for a map spawned at mapPos: mapPos + the layer's Offset + the object's X, Y. It is the top-left corner of rectangles, ellipses and text, the point of points, polygons and polylines, and the bottom-left corner of tile objects (Tiled's convention). Parallax is ignored: objects are gameplay data.

func RegisterLoaders

func RegisterLoaders(srv *asset.Server)

RegisterLoaders registers Tileset (*.tsj), Map (*.tmj) and World (*.world) and their loaders on srv. Run render.RegisterLoaders first: it panics like asset.StoreOf ("asset: type not registered: render.Atlas") if the render loaders are missing, and like asset.Register if a type is already registered.

func Spawn

func Spawn(w *ecs.World, maps MapStore, h MapHandle, opts SpawnOptions) ([]ecs.Entity, error)

Spawn creates one entity per tile and image layer of map h, in draw order, each with a gfx.Transform at opts.Pos and a TileLayer. Object and group layers create none: their data stays in the Map. A layer that is invisible in Tiled spawns Hidden.

The map must be Ready: Spawn returns an error, and creates nothing, for a map that is loading, failed, zero or stale. Deleting the entities is the caller's job (game.Engine.UnloadLevel does it for maps it loaded).

func SpawnWorld

func SpawnWorld(w *ecs.World, worlds WorldStore, maps MapStore, h WorldHandle, opts SpawnOptions) ([]ecs.Entity, error)

SpawnWorld spawns every map of world h like Spawn, each at opts.Pos plus the map's position in the world, and returns all their entities. The world must be Ready (which means all its maps are).

Types

type AnimFrame

type AnimFrame struct {
	Sprite   gfx.SpriteHandle
	Duration time.Duration
}

AnimFrame is one frame of an animated Tile.

type Collision

type Collision uint8

Collision is what a cell of a CollisionGrid holds.

const (
	// CollisionNone means no collision shape touches the cell.
	CollisionNone Collision = iota
	// CollisionPartial means some shape overlaps the cell without covering
	// it: test against the shapes yourself (Tile.Shapes of the tiles in and
	// around the cell).
	CollisionPartial
	// CollisionFull means one unrotated rectangle covers the whole cell.
	CollisionFull
)

Collision values.

func (Collision) Solid

func (c Collision) Solid() bool

Solid reports whether c is not CollisionNone.

func (Collision) String

func (c Collision) String() string

String returns "none", "partial" or "full".

type CollisionGrid

type CollisionGrid struct {
	// Origin is the cell of the grid's first entry; non-zero only for
	// infinite maps. W and H are the grid size in cells: the union of the
	// source layers' grids. Cells outside it are CollisionNone.
	Origin image.Point
	W, H   int
	// CellSize is the map's cell size in pixels.
	CellSize gfx.Vec2
	// contains filtered or unexported fields
}

CollisionGrid is a solid-cell lookup built from the collision shapes of a Map's tiles (Tile.Shapes): one Collision per map cell, for the broad phase of tile collision. Build it with NewCollisionGrid once the map is Ready.

Cells are map cells and positions are map pixels, as for objects: cell (cx, cy) covers [cx*CellSize.X, (cx+1)*CellSize.X) and the same in Y. Unlike Map.CellAt, no layer offset is subtracted: the offset of each source layer is applied when the grid is built.

A grid is a copy: it stays valid after the map is unloaded or reloaded, and changing it with CollisionGrid.Set (a broken wall) does not touch the map. Lookups are O(1) and do not allocate.

func NewCollisionGrid

func NewCollisionGrid(m *Map, layers ...*Layer) *CollisionGrid

NewCollisionGrid builds the grid of map m from the given tile layers, or from every tile layer of m when none are given. Hidden layers count: a layer of collider tiles is usually hidden.

Each shape of the tile in a cell is placed the way TilemapExtract draws the tile (bottom-left anchor, tileset offset, grid scale, flip flags about the tile's center, then the layer's Offset), and marks every cell its bounding box overlaps: CollisionFull where it is an unrotated rectangle covering the cell, CollisionPartial elsewhere. A tile reaching outside its cell therefore marks its neighbors; parts of shapes outside the grid are dropped. Points, polylines and text count by their bounding box, like rectangles, ellipses and polygons. Animated tiles use the shapes of the tile itself (Tiled's rule). Parallax is ignored.

It panics if a layer is not a tile layer of m. The cost is proportional to the cells of the layers; the grid holds one byte per cell.

func (*CollisionGrid) At

func (g *CollisionGrid) At(cx, cy int) Collision

At returns the collision of cell (cx, cy); CollisionNone outside the grid.

func (*CollisionGrid) CellOf

func (g *CollisionGrid) CellOf(pos gfx.Vec2) (cx, cy int)

CellOf returns the cell that contains pos, in map pixels.

func (*CollisionGrid) CellRect

func (g *CollisionGrid) CellRect(cx, cy int) gfx.Rect

CellRect returns the rectangle of cell (cx, cy) in map pixels.

func (*CollisionGrid) CollisionAt

func (g *CollisionGrid) CollisionAt(pos gfx.Vec2) Collision

CollisionAt returns the collision of the cell that contains pos, in map pixels.

func (*CollisionGrid) Set

func (g *CollisionGrid) Set(cx, cy int, c Collision) bool

Set changes the collision of cell (cx, cy), for maps that change at run time. It reports false, and changes nothing, outside the grid.

func (*CollisionGrid) Solid

func (g *CollisionGrid) Solid(cx, cy int) bool

Solid reports whether any collision shape touches cell (cx, cy).

func (*CollisionGrid) SolidAt

func (g *CollisionGrid) SolidAt(pos gfx.Vec2) bool

SolidAt reports whether the cell that contains pos, in map pixels, is solid.

func (*CollisionGrid) SolidIn

func (g *CollisionGrid) SolidIn(r gfx.Rect) iter.Seq2[image.Point, Collision]

SolidIn yields the solid cells that r (map pixels, half-open as gfx.Rect.Contains) overlaps, row by row, with their collision: the cells to test a moving box against. The only allocation is the iterator's closure.

type Layer

type Layer struct {
	Kind LayerKind
	ID   int
	Name string
	// Path is the names of the enclosing groups and the layer, joined by
	// '/': "entity-group/entities". Paths need not be unique.
	Path string
	// Parent is the index in Map.Layers of the enclosing group, or -1.
	Parent int
	Class  string
	// Props are the layer's own properties; Tiled does not inherit
	// properties from groups.
	Props tiled.Properties

	// Offset shifts the layer, in pixels.
	Offset gfx.Vec2
	// Opacity is in [0, 1].
	Opacity float32
	// Tint multiplies the layer's colors; zero means none. Opacity is not
	// folded in.
	Tint gfx.Color
	// Parallax is the scroll factor; (1, 1) moves with the camera.
	Parallax gfx.Vec2
	Visible  bool

	// Origin is the cell of GIDs[0] (non-zero only in infinite maps), W and
	// H the size of the dense grid GIDs, row by row, raw with flag bits. An
	// infinite map's chunks are merged into one grid covering all of them.
	Origin image.Point
	W, H   int
	GIDs   []uint32

	// Objects are the objects of an object layer in file order, in map
	// pixels relative to the layer (see [ObjectPos]). DrawOrder is "topdown"
	// or "index".
	Objects   []tiled.Object
	DrawOrder string

	// Image is the sprite of an image layer (zero if the layer has none),
	// ImageSize its size in pixels.
	Image            gfx.SpriteHandle
	ImageSize        gfx.Vec2
	RepeatX, RepeatY bool

	// Shader and Params come from the layer properties "shader" and
	// "shaderParams" of a tile or image layer (see [PropShader]); Spawn
	// copies them into the [TileLayer]. The zero Shader draws normally.
	Shader gfx.ShaderHandle
	Params [4]float32
}

Layer is one layer of a Map. Offset, Opacity, Tint, Parallax and Visible are effective values: composed with every group the layer is in, so a layer of an invisible group is invisible.

type LayerKind

type LayerKind uint8

LayerKind is the type of a Layer.

const (
	Tiles LayerKind = iota
	Objects
	Image
	Group
)

Layer kinds.

func (LayerKind) String

func (k LayerKind) String() string

String returns the lower-case kind name.

type Map

type Map struct {
	// Width and Height are the map size in cells, TileW and TileH the cell
	// size in pixels. For an infinite map the size is informational; each
	// tile layer's Origin, W and H bound its cells.
	Width, Height int
	TileW, TileH  int
	Infinite      bool
	RenderOrder   RenderOrder
	// ParallaxOrigin is where parallax layers line up with the others, in
	// map pixels.
	ParallaxOrigin gfx.Vec2
	// Background is the map's background color; zero means none. The engine
	// does not draw it.
	Background gfx.Color
	Class      string
	Props      tiled.Properties
	// Layers are every layer in draw order: depth-first, bottom to top, a
	// group before its children.
	Layers []Layer
	// Tilesets are the map's tilesets, by FirstGID.
	Tilesets []MapTileset
	// contains filtered or unexported fields
}

Map is a loaded Tiled map (*.tmj): its layers flattened in draw order, its tilesets, a GID table and the map's data (objects, classes, custom properties). Load it with LoadMap; draw it by spawning its layers with Spawn.

A Map is a self-contained, read-only value. Pointers into it follow the rule of asset.Store.Get: valid until the next asset.Server.Update.

func (*Map) Bounds

func (m *Map) Bounds() gfx.Rect

Bounds returns the map's rectangle in map pixels (add the map's world position, such as a WorldMap's Pos, to place it in the world): from (0,0) to (Width*TileW, Height*TileH). For an infinite map it is the union of its tile layers' cells, which may start at negative coordinates (or the informational Width x Height when it has no cells). Layer offsets and tiles taller than their cell are not included. Use it, for example, for camera.Rig.Bounds.

func (*Map) Cell

func (m *Map) Cell(l *Layer, cx, cy int) (gid uint32, flip tiled.Flip, ok bool)

Cell returns the tile of cell (cx, cy) of tile layer l, in map cells (negative cells exist in infinite maps). gid is 0 for an empty cell. ok is false if l is not a tile layer or the cell is outside its grid.

func (*Map) CellAt

func (m *Map) CellAt(l *Layer, pos gfx.Vec2) (gid uint32, flip tiled.Flip, ok bool)

CellAt is Map.Cell for a position in map pixels, the space objects are in (subtract the map's world position first). The layer's Offset is taken into account; parallax is not.

func (*Map) FindObject

func (m *Map) FindObject(name string) (*Layer, *tiled.Object, bool)

FindObject returns the first object called name, with its layer.

func (*Map) LayerByName

func (m *Map) LayerByName(name string) (*Layer, bool)

LayerByName returns the first layer in draw order called name.

func (*Map) LayerByPath

func (m *Map) LayerByPath(path string) (*Layer, bool)

LayerByPath returns the first layer in draw order whose Path is path ("entity-group/entities"). Tiled allows equal names, so a group may hold a tile layer and an object layer of the same name; use Map.LayerByPathKind to tell them apart.

func (*Map) LayerByPathKind

func (m *Map) LayerByPathKind(path string, k LayerKind) (*Layer, bool)

LayerByPathKind is Map.LayerByPath restricted to layers of kind k.

func (*Map) Objects

func (m *Map) Objects() iter.Seq2[*Layer, *tiled.Object]

Objects yields every object of every object layer, in draw order of the layers and file order inside a layer.

func (*Map) ObjectsOfClass

func (m *Map) ObjectsOfClass(class string) iter.Seq2[*Layer, *tiled.Object]

ObjectsOfClass yields the objects whose Class is class, with their layers.

func (*Map) Tile

func (m *Map) Tile(gid uint32) (*Tile, bool)

Tile returns the tile of a GID (flag bits are ignored). ok is false for 0 and for GIDs that are not a tile of the map's tilesets.

type MapHandle

type MapHandle struct{ asset.Ref }

MapHandle refers to a Map. It is 8 bytes, comparable and pointer-free; the zero value means "none".

func LoadMap

func LoadMap(sc *asset.Scope, key string) MapHandle

LoadMap returns the handle for a map key ("level-1.tmj") and adds a hold for sc. It never blocks and panics like asset.Load.

func LoadMapSync

func LoadMapSync(sc *asset.Scope, key string) (MapHandle, error)

LoadMapSync is LoadMap followed by loading until the map (and everything it depends on) is ready or failed, like asset.LoadSync. It blocks: use it for boot screens, tests and tools.

type MapStore

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

MapStore resolves MapHandle values. Get one with MapStoreOf. It is a small value; copy it freely.

func MapStoreOf

func MapStoreOf(srv *asset.Server) MapStore

MapStoreOf returns the map store of srv. It panics like asset.StoreOf if RegisterLoaders has not run on srv.

func (MapStore) Err

func (s MapStore) Err(h MapHandle) error

Err returns why the map failed to load, or nil.

func (MapStore) Generic

func (s MapStore) Generic() *asset.Store[Map]

Generic returns the underlying generic store, for loaders and tools that work with asset.Handle[Map].

func (MapStore) Get

func (s MapStore) Get(h MapHandle) *Map

Get returns the map, or the placeholder (an empty Map); see asset.Store.Get for the pointer validity rule. It does not allocate.

func (MapStore) Key

func (s MapStore) Key(h MapHandle) asset.Key

Key returns the key h was loaded from, or the zero Key for a zero or stale handle.

func (MapStore) Lookup

func (s MapStore) Lookup(h MapHandle) (*Map, asset.State)

Lookup is Get plus the load state; see asset.Store.Lookup. Use it to tell a loaded map from the placeholder.

func (MapStore) Ready

func (s MapStore) Ready(h MapHandle) bool

Ready reports whether h refers to a loaded map.

type MapTileset

type MapTileset struct {
	// FirstGID is the GID of the tileset's local tile 0.
	FirstGID uint32
	// Tileset is the external tileset; zero for a tileset embedded in the
	// map (whose tiles are only in the map's GID table).
	Tileset TilesetHandle
	Name    string
}

MapTileset is a tileset of a Map.

type RenderOrder

type RenderOrder uint8

RenderOrder is the order in which a tile layer's cells are drawn.

const (
	RightDown RenderOrder = iota
	RightUp
	LeftDown
	LeftUp
)

Render orders.

type SpawnOptions

type SpawnOptions struct {
	// Pos is the world position of the map's top-left corner (of the world's
	// origin for SpawnWorld).
	Pos gfx.Vec2
	// BaseLayer is the render layer of the map's bottom drawable layer.
	BaseLayer int16
	// LayerFor, if set, decides for each tile and image layer of map m
	// whether to spawn it and on which render layer. drawIndex counts the
	// drawable layers bottom to top from 0; the default render layer is
	// BaseLayer + drawIndex. This is where game code applies its own layer
	// properties ("renderPass", "merge into the sprites' layer"); m gives
	// access to the enclosing groups (m.Layers[l.Parent]).
	LayerFor func(m *Map, l *Layer, drawIndex int) (layer int16, spawn bool)
	// Chunked spawns every tile layer with TileLayer.Chunked set: its static
	// tiles are pre-rendered into chunk images. Change the field per layer
	// after spawning to chunk only some layers.
	Chunked bool
	// NoDocuments loads a map alone: game.Engine's loaders then skip the
	// <map>.level.json documents they would spawn with it, and a group's own
	// document. Read by game.Engine's loaders; [Spawn] and [SpawnWorld] ignore it.
	NoDocuments bool
	// SkipDocuments lists the asset keys of GOD documents the loaders must not
	// spawn, for a tool that spawns that file itself. Read by game.Engine's
	// loaders; [Spawn] and [SpawnWorld] ignore it.
	SkipDocuments []string
}

SpawnOptions configures Spawn and SpawnWorld.

type Tile

type Tile struct {
	// Sprite is the tile's image; zero for an ID the tileset does not have.
	Sprite gfx.SpriteHandle
	// Size is the size of the tile's image in pixels.
	Size gfx.Vec2
	// Offset is the tileset's tileoffset, added when the tile is drawn.
	Offset gfx.Vec2
	// Scale is (1, 1), or the factor that fits the tile to the map's cell for
	// tilesets with tilerendersize "grid" (in a Map's GID table only).
	Scale gfx.Vec2
	Class string
	Props tiled.Properties
	// Shapes are the tile's collision shapes, in tile pixels.
	Shapes []tiled.Object
	// Anim lists the frames of an animated tile, nil for a static one, and
	// AnimTotal is the sum of their durations.
	Anim      []AnimFrame
	AnimTotal time.Duration
}

Tile is one tile, of a Tileset or of a Map's GID table. Its slices are shared between the tileset and every map that uses it: read-only.

func (*Tile) FrameAt

func (t *Tile) FrameAt(clock time.Duration) gfx.SpriteHandle

FrameAt returns the sprite shown t into the tile's animation, which loops; the tile's own Sprite for a static tile. All cells with the same tile show the same frame, as in Tiled. It does not allocate.

type TileLayer

type TileLayer struct {
	// Map is the map the layer belongs to. A map that is not Ready draws
	// nothing.
	Map MapHandle
	// Index is the layer's index in Map.Layers.
	Index int32
	// Layer is the render layer of the queued items.
	Layer int16
	// Hidden skips the layer. Spawn sets it for layers that are invisible in
	// Tiled; toggling it at run time is a field write, as for gfx.Sprite.
	Hidden bool
	// Chunked pre-renders the layer's static tiles into chunk images, so
	// that it queues one item per visible chunk (16x16 cells) instead of one
	// per visible tile; animated tiles are still queued one by one. Use it
	// for large layers whose cells do not change. It is ignored for image
	// layers, in a render layer configured with YSort (a chunk has one
	// SortY) and without a *render.Canvases resource; those draw tile by
	// tile. Spawn sets it from SpawnOptions.Chunked.
	Chunked bool
	// Tint multiplies the colors of the layer's tiles. Spawn sets it to the
	// layer's effective tint with its opacity folded into the alpha. Zero
	// means no tint.
	Tint gfx.Color
	// Shader draws the layer's tiles (or image) through a Kage shader, with
	// the same inputs as gfx.Sprite.Shader: the tile image as imageSrc0, Tint
	// as the color argument and Params as the "Params" vec4. Spawn sets it
	// from the layer property "shader" (see [PropShader]); assigning it at
	// run time works like any field write. The zero handle, and a shader that
	// is loading or failed, draw normally.
	//
	// An unchunked layer makes one shader draw per visible tile. A Chunked
	// layer shades each baked chunk image instead, one draw per visible chunk
	// and animated tile: the shader runs every frame (Time animates it) but
	// sees the chunk's pixels, so a shader that samples neighbouring texels
	// meets chunk borders. Prefer Chunked for shaded layers.
	Shader gfx.ShaderHandle
	// Params are the shader's Params uniform. Spawn sets them from the layer
	// property "shaderParams".
	Params [4]float32
	// contains filtered or unexported fields
}

TileLayer draws one tile or image layer of a map: TilemapExtract queues its visible tiles each tick. The entity's gfx.Transform.Pos is the world position of the map's top-left corner, so moving the Transform moves the layer; the Transform's rotation and scale are ignored.

Create layers with Spawn or SpawnWorld. TileLayer is pointer-free and a plain component, not a Game Object component: maps are not GOD data.

type TilemapExtract

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

TilemapExtract queues the visible tiles of every TileLayer entity: one world-space sprite item per non-empty cell inside the camera bounds, in the map's render order, and one item per visible repetition of an image layer. Run it in the render stage after extract.ResetQueue and before extract.SpriteExtract, so that in a layer without Y-sorting tiles draw under the sprites of the same layer (insertion order breaks sort ties).

Placement follows Tiled: a tile sits on the bottom-left corner of its cell, shifted by the tileset's tileoffset; flip flags become the item's rotation and mirroring about the tile's center. A diagonal flip of a non-square tile therefore rotates about its center, where Tiled keeps the bottom-left corner. Item.SortY is the bottom of the cell, so tiles also work in Y-sorted layers. Parallax uses Tiled's model, offset = (camera center - map position - ParallaxOrigin) * (1 - parallax), with the camera's previous view for Item.PrevPos so camera interpolation stays smooth.

Animated tiles share one clock, the sum of the tick deltas since the system started, so every cell with the same tile shows the same frame. Hidden layers, maps that are not Ready and layers whose index is out of range push nothing.

A layer with TileLayer.Chunked set queues one render.KindCanvas item per visible chunk of chunkCells x chunkCells cells instead, its static tiles pre-rendered into a render.Canvases canvas the first time the chunk is visible, plus one sprite item per visible animated tile (see chunks.go). Chunks not drawn for a while are freed.

Run does not allocate, except while it pre-renders a chunk (the queue grows to its steady size during the first ticks).

It needs the resources *render.Queue, *render.Camera, *extract.Screen and *asset.Server (with RegisterLoaders run on it). Chunked layers also need *render.Canvases, and read the optional *render.LayerConfig: without canvases, and in a Y-sorted render layer, they draw tile by tile.

func NewTilemapExtract

func NewTilemapExtract(w *ecs.World) *TilemapExtract

NewTilemapExtract returns the system for w.

func (*TilemapExtract) Init

func (s *TilemapExtract) Init(ctx *system.Context)

Init implements system.Initializer. It caches the resources and the stores, so Run does no lookups.

func (*TilemapExtract) Run

func (s *TilemapExtract) Run(ctx *system.Context)

Run implements system.Runner.

type Tileset

type Tileset struct {
	Name  string
	Class string
	// TileSize is the tileset's grid cell size in pixels.
	TileSize gfx.Vec2
	// GridScale is set for tilerendersize "grid": maps scale the tiles to
	// their own cell size.
	GridScale bool
	// Atlas is the atlas of a single-image tileset; zero for a collection.
	Atlas render.AtlasHandle
	// Tiles is indexed by local tile ID. A collection may skip IDs; those
	// entries have a zero Sprite.
	Tiles []Tile
	Props tiled.Properties
	// File is the decoded file, for data the fields above leave out
	// (probabilities, image paths, object alignment). Read-only.
	File *tiled.Tileset
}

Tileset is a loaded Tiled tileset (*.tsj): its tiles with sprites, sizes, collision shapes, animations and custom properties. Load it with LoadTileset; maps load their external tilesets themselves, and a tileset shared by several maps is loaded once.

A single-image tileset takes its sprites from the render.Atlas of the same key ("tiles/temple.tsj#92" is the sprite of local tile 92); an image collection has one whole-image render.Sprite per tile. A Tileset is immutable once loaded.

type TilesetHandle

type TilesetHandle struct{ asset.Ref }

TilesetHandle refers to a Tileset. It is 8 bytes, comparable and pointer-free; the zero value means "none".

func LoadTileset

func LoadTileset(sc *asset.Scope, key string) TilesetHandle

LoadTileset returns the handle for a tileset key ("tiles/temple.tsj") and adds a hold for sc. It never blocks and panics like asset.Load.

func LoadTilesetSync

func LoadTilesetSync(sc *asset.Scope, key string) (TilesetHandle, error)

LoadTilesetSync is LoadTileset followed by loading until the tileset (and everything it depends on) is ready or failed, like asset.LoadSync. It blocks: use it for boot screens, tests and tools.

type TilesetStore

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

TilesetStore resolves TilesetHandle values. Get one with TilesetStoreOf. It is a small value; copy it freely.

func TilesetStoreOf

func TilesetStoreOf(srv *asset.Server) TilesetStore

TilesetStoreOf returns the tileset store of srv. It panics like asset.StoreOf if RegisterLoaders has not run on srv.

func (TilesetStore) Err

func (s TilesetStore) Err(h TilesetHandle) error

Err returns why the tileset failed to load, or nil.

func (TilesetStore) Generic

func (s TilesetStore) Generic() *asset.Store[Tileset]

Generic returns the underlying generic store, for loaders and tools that work with asset.Handle[Tileset].

func (TilesetStore) Get

Get returns the tileset, or the placeholder (an empty Tileset); see asset.Store.Get for the pointer validity rule. It does not allocate.

func (TilesetStore) Key

Key returns the key h was loaded from, or the zero Key for a zero or stale handle.

func (TilesetStore) Lookup

func (s TilesetStore) Lookup(h TilesetHandle) (*Tileset, asset.State)

Lookup is Get plus the load state; see asset.Store.Lookup. Use it to tell a loaded tileset from the placeholder.

func (TilesetStore) Ready

func (s TilesetStore) Ready(h TilesetHandle) bool

Ready reports whether h refers to a loaded tileset.

type World

type World struct {
	Maps []WorldMap
}

World is a loaded Tiled world (*.world): maps placed side by side. Load it with LoadWorld; it is Ready when every map is, and fails if any map fails. Spawn it with SpawnWorld.

type WorldHandle

type WorldHandle struct{ asset.Ref }

WorldHandle refers to a World. It is 8 bytes, comparable and pointer-free; the zero value means "none".

func LoadWorld

func LoadWorld(sc *asset.Scope, key string) WorldHandle

LoadWorld returns the handle for a world key ("world-1.world") and adds a hold for sc. It never blocks and panics like asset.Load.

func LoadWorldSync

func LoadWorldSync(sc *asset.Scope, key string) (WorldHandle, error)

LoadWorldSync is LoadWorld followed by loading until the world (and everything it depends on) is ready or failed, like asset.LoadSync. It blocks: use it for boot screens, tests and tools.

type WorldMap

type WorldMap struct {
	Map MapHandle
	// Pos is the map's top-left corner relative to the world's origin, in
	// pixels.
	Pos gfx.Vec2
}

WorldMap is one map of a World.

type WorldStore

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

WorldStore resolves WorldHandle values. Get one with WorldStoreOf. It is a small value; copy it freely.

func WorldStoreOf

func WorldStoreOf(srv *asset.Server) WorldStore

WorldStoreOf returns the world store of srv. It panics like asset.StoreOf if RegisterLoaders has not run on srv.

func (WorldStore) Err

func (s WorldStore) Err(h WorldHandle) error

Err returns why the world failed to load, or nil.

func (WorldStore) Generic

func (s WorldStore) Generic() *asset.Store[World]

Generic returns the underlying generic store, for loaders and tools that work with asset.Handle[World].

func (WorldStore) Get

func (s WorldStore) Get(h WorldHandle) *World

Get returns the world, or the placeholder (an empty World); see asset.Store.Get for the pointer validity rule. It does not allocate.

func (WorldStore) Key

func (s WorldStore) Key(h WorldHandle) asset.Key

Key returns the key h was loaded from, or the zero Key for a zero or stale handle.

func (WorldStore) Lookup

func (s WorldStore) Lookup(h WorldHandle) (*World, asset.State)

Lookup is Get plus the load state; see asset.Store.Lookup. Use it to tell a loaded world from the placeholder.

func (WorldStore) Ready

func (s WorldStore) Ready(h WorldHandle) bool

Ready reports whether h refers to a loaded world.

Jump to

Keyboard shortcuts

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