{"modulePath":"github.com/weakpixel/engine","version":"v0.0.0-20261205071607-94fff0387040","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package tilemap\n\nPackage tilemap loads Tiled maps as assets and draws their tile and image layers through the render queue.\n\nIt has a data half and a runtime half. The data half is the asset types [Tileset](#Tileset) (\\*.tsj), [Map](#Map) (\\*.tmj) and [World](#World) (\\*.world), which [RegisterLoaders](#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](#TileLayer) component, [Spawn](#Spawn) and [SpawnWorld](#SpawnWorld), which create one entity per drawable layer, and [TilemapExtract](#TilemapExtract), which queues one render.Item per visible tile. [NewCollisionGrid](#NewCollisionGrid) builds a solid-cell lookup from the tiles' collision shapes.\n\nFile 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.\n\nThe 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.\n\n## Constants\n\n```go\nconst (\n\t// PropShader is the .kage key of the layer's shader, relative to the map\n\t// file like an image layer's image.\n\tPropShader = \"shader\"\n\t// PropShaderParams is up to four comma-separated numbers for the\n\t// shader's Params uniform; missing ones are 0.\n\tPropShaderParams = \"shaderParams\"\n)\n```\nLayer properties that put a tile or image layer through a shader (see [Layer.Shader](#Layer.Shader)).\n\n## Functions\n\n```go\nfunc HoldMap(sc *asset.Scope, h MapHandle)\n```\nHoldMap adds sc as an owner of h, like [asset.Hold](/github.com/weakpixel/asset#Hold).\n\n```go\nfunc HoldTileset(sc *asset.Scope, h TilesetHandle)\n```\nHoldTileset adds sc as an owner of h, like [asset.Hold](/github.com/weakpixel/asset#Hold).\n\n```go\nfunc HoldWorld(sc *asset.Scope, h WorldHandle)\n```\nHoldWorld adds sc as an owner of h, like [asset.Hold](/github.com/weakpixel/asset#Hold).\n\n```go\nfunc ObjectPos(l *Layer, o *tiled.Object, mapPos gfx.Vec2) gfx.Vec2\n```\nObjectPos 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.\n\n```go\nfunc RegisterLoaders(srv *asset.Server)\n```\nRegisterLoaders registers [Tileset](#Tileset) (\\*.tsj), [Map](#Map) (\\*.tmj) and [World](#World) (\\*.world) and their loaders on srv. Run render.RegisterLoaders first: it panics like [asset.StoreOf](/github.com/weakpixel/asset#StoreOf) (\"asset: type not registered: render.Atlas\") if the render loaders are missing, and like [asset.Register](/github.com/weakpixel/asset#Register) if a type is already registered.\n\n```go\nfunc Spawn(w *ecs.World, maps MapStore, h MapHandle, opts SpawnOptions) ([]ecs.Entity, error)\n```\nSpawn 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](#TileLayer). Object and group layers create none: their data stays in the Map. A layer that is invisible in Tiled spawns Hidden.\n\nThe 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).\n\n```go\nfunc SpawnWorld(w *ecs.World, worlds WorldStore, maps MapStore, h WorldHandle, opts SpawnOptions) ([]ecs.Entity, error)\n```\nSpawnWorld spawns every map of world h like [Spawn](#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).\n\n## Types\n\n```go\ntype AnimFrame struct {\n\tSprite   gfx.SpriteHandle\n\tDuration time.Duration\n}\n```\nAnimFrame is one frame of an animated [Tile](#Tile).\n\n```go\ntype Collision uint8\n```\nCollision is what a cell of a [CollisionGrid](#CollisionGrid) holds.\n\n```go\nconst (\n\t// CollisionNone means no collision shape touches the cell.\n\tCollisionNone Collision = iota\n\t// CollisionPartial means some shape overlaps the cell without covering\n\t// it: test against the shapes yourself (Tile.Shapes of the tiles in and\n\t// around the cell).\n\tCollisionPartial\n\t// CollisionFull means one unrotated rectangle covers the whole cell.\n\tCollisionFull\n)\n```\nCollision values.\n\n```go\nfunc (c Collision) Solid() bool\n```\nSolid reports whether c is not CollisionNone.\n\n```go\nfunc (c Collision) String() string\n```\nString returns \"none\", \"partial\" or \"full\".\n\n```go\ntype CollisionGrid struct {\n\t// Origin is the cell of the grid's first entry; non-zero only for\n\t// infinite maps. W and H are the grid size in cells: the union of the\n\t// source layers' grids. Cells outside it are CollisionNone.\n\tOrigin image.Point\n\tW, H   int\n\t// CellSize is the map's cell size in pixels.\n\tCellSize gfx.Vec2\n\t// contains filtered or unexported fields\n}\n```\nCollisionGrid is a solid-cell lookup built from the collision shapes of a [Map](#Map)'s tiles ([Tile](#Tile).Shapes): one [Collision](#Collision) per map cell, for the broad phase of tile collision. Build it with [NewCollisionGrid](#NewCollisionGrid) once the map is Ready.\n\nCells 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](#Map.CellAt), no layer offset is subtracted: the offset of each source layer is applied when the grid is built.\n\nA grid is a copy: it stays valid after the map is unloaded or reloaded, and changing it with [CollisionGrid.Set](#CollisionGrid.Set) (a broken wall) does not touch the map. Lookups are O(1) and do not allocate.\n\n```go\nfunc NewCollisionGrid(m *Map, layers ...*Layer) *CollisionGrid\n```\nNewCollisionGrid 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.\n\nEach shape of the tile in a cell is placed the way [TilemapExtract](#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.\n\nIt 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.\n\n```go\nfunc (g *CollisionGrid) At(cx, cy int) Collision\n```\nAt returns the collision of cell (cx, cy); CollisionNone outside the grid.\n\n```go\nfunc (g *CollisionGrid) CellOf(pos gfx.Vec2) (cx, cy int)\n```\nCellOf returns the cell that contains pos, in map pixels.\n\n```go\nfunc (g *CollisionGrid) CellRect(cx, cy int) gfx.Rect\n```\nCellRect returns the rectangle of cell (cx, cy) in map pixels.\n\n```go\nfunc (g *CollisionGrid) CollisionAt(pos gfx.Vec2) Collision\n```\nCollisionAt returns the collision of the cell that contains pos, in map pixels.\n\n```go\nfunc (g *CollisionGrid) Set(cx, cy int, c Collision) bool\n```\nSet changes the collision of cell (cx, cy), for maps that change at run time. It reports false, and changes nothing, outside the grid.\n\n```go\nfunc (g *CollisionGrid) Solid(cx, cy int) bool\n```\nSolid reports whether any collision shape touches cell (cx, cy).\n\n```go\nfunc (g *CollisionGrid) SolidAt(pos gfx.Vec2) bool\n```\nSolidAt reports whether the cell that contains pos, in map pixels, is solid.\n\n```go\nfunc (g *CollisionGrid) SolidIn(r gfx.Rect) iter.Seq2[image.Point, Collision]\n```\nSolidIn 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.\n\n```go\ntype Layer struct {\n\tKind LayerKind\n\tID   int\n\tName string\n\t// Path is the names of the enclosing groups and the layer, joined by\n\t// '/': \"entity-group/entities\". Paths need not be unique.\n\tPath string\n\t// Parent is the index in Map.Layers of the enclosing group, or -1.\n\tParent int\n\tClass  string\n\t// Props are the layer's own properties; Tiled does not inherit\n\t// properties from groups.\n\tProps tiled.Properties\n\n\t// Offset shifts the layer, in pixels.\n\tOffset gfx.Vec2\n\t// Opacity is in [0, 1].\n\tOpacity float32\n\t// Tint multiplies the layer's colors; zero means none. Opacity is not\n\t// folded in.\n\tTint gfx.Color\n\t// Parallax is the scroll factor; (1, 1) moves with the camera.\n\tParallax gfx.Vec2\n\tVisible  bool\n\n\t// Origin is the cell of GIDs[0] (non-zero only in infinite maps), W and\n\t// H the size of the dense grid GIDs, row by row, raw with flag bits. An\n\t// infinite map's chunks are merged into one grid covering all of them.\n\tOrigin image.Point\n\tW, H   int\n\tGIDs   []uint32\n\n\t// Objects are the objects of an object layer in file order, in map\n\t// pixels relative to the layer (see [ObjectPos]). DrawOrder is \"topdown\"\n\t// or \"index\".\n\tObjects   []tiled.Object\n\tDrawOrder string\n\n\t// Image is the sprite of an image layer (zero if the layer has none),\n\t// ImageSize its size in pixels.\n\tImage            gfx.SpriteHandle\n\tImageSize        gfx.Vec2\n\tRepeatX, RepeatY bool\n\n\t// Shader and Params come from the layer properties \"shader\" and\n\t// \"shaderParams\" of a tile or image layer (see [PropShader]); Spawn\n\t// copies them into the [TileLayer]. The zero Shader draws normally.\n\tShader gfx.ShaderHandle\n\tParams [4]float32\n}\n```\nLayer is one layer of a [Map](#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.\n\n```go\ntype LayerKind uint8\n```\nLayerKind is the type of a [Layer](#Layer).\n\n```go\nconst (\n\tTiles LayerKind = iota\n\tObjects\n\tImage\n\tGroup\n)\n```\nLayer kinds.\n\n```go\nfunc (k LayerKind) String() string\n```\nString returns the lower-case kind name.\n\n```go\ntype Map struct {\n\t// Width and Height are the map size in cells, TileW and TileH the cell\n\t// size in pixels. For an infinite map the size is informational; each\n\t// tile layer's Origin, W and H bound its cells.\n\tWidth, Height int\n\tTileW, TileH  int\n\tInfinite      bool\n\tRenderOrder   RenderOrder\n\t// ParallaxOrigin is where parallax layers line up with the others, in\n\t// map pixels.\n\tParallaxOrigin gfx.Vec2\n\t// Background is the map's background color; zero means none. The engine\n\t// does not draw it.\n\tBackground gfx.Color\n\tClass      string\n\tProps      tiled.Properties\n\t// Layers are every layer in draw order: depth-first, bottom to top, a\n\t// group before its children.\n\tLayers []Layer\n\t// Tilesets are the map's tilesets, by FirstGID.\n\tTilesets []MapTileset\n\t// contains filtered or unexported fields\n}\n```\nMap 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](#LoadMap); draw it by spawning its layers with [Spawn](#Spawn).\n\nA 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.\n\n```go\nfunc (m *Map) Bounds() gfx.Rect\n```\nBounds 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.\n\n```go\nfunc (m *Map) Cell(l *Layer, cx, cy int) (gid uint32, flip tiled.Flip, ok bool)\n```\nCell 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.\n\n```go\nfunc (m *Map) CellAt(l *Layer, pos gfx.Vec2) (gid uint32, flip tiled.Flip, ok bool)\n```\nCellAt is [Map.Cell](#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.\n\n```go\nfunc (m *Map) FindObject(name string) (*Layer, *tiled.Object, bool)\n```\nFindObject returns the first object called name, with its layer.\n\n```go\nfunc (m *Map) LayerByName(name string) (*Layer, bool)\n```\nLayerByName returns the first layer in draw order called name.\n\n```go\nfunc (m *Map) LayerByPath(path string) (*Layer, bool)\n```\nLayerByPath 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](#Map.LayerByPathKind) to tell them apart.\n\n```go\nfunc (m *Map) LayerByPathKind(path string, k LayerKind) (*Layer, bool)\n```\nLayerByPathKind is [Map.LayerByPath](#Map.LayerByPath) restricted to layers of kind k.\n\n```go\nfunc (m *Map) Objects() iter.Seq2[*Layer, *tiled.Object]\n```\nObjects yields every object of every object layer, in draw order of the layers and file order inside a layer.\n\n```go\nfunc (m *Map) ObjectsOfClass(class string) iter.Seq2[*Layer, *tiled.Object]\n```\nObjectsOfClass yields the objects whose Class is class, with their layers.\n\n```go\nfunc (m *Map) Tile(gid uint32) (*Tile, bool)\n```\nTile 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.\n\n```go\ntype MapHandle struct{ asset.Ref }\n```\nMapHandle refers to a [Map](#Map). It is 8 bytes, comparable and pointer-free; the zero value means \"none\".\n\n```go\nfunc LoadMap(sc *asset.Scope, key string) MapHandle\n```\nLoadMap returns the handle for a map key (\"level-1.tmj\") and adds a hold for sc. It never blocks and panics like [asset.Load](/github.com/weakpixel/asset#Load).\n\n```go\nfunc LoadMapSync(sc *asset.Scope, key string) (MapHandle, error)\n```\nLoadMapSync is LoadMap followed by loading until the map (and everything it depends on) is ready or failed, like [asset.LoadSync](/github.com/weakpixel/asset#LoadSync). It blocks: use it for boot screens, tests and tools.\n\n```go\ntype MapStore struct {\n\t// contains filtered or unexported fields\n}\n```\nMapStore resolves [MapHandle](#MapHandle) values. Get one with [MapStoreOf](#MapStoreOf). It is a small value; copy it freely.\n\n```go\nfunc MapStoreOf(srv *asset.Server) MapStore\n```\nMapStoreOf returns the map store of srv. It panics like [asset.StoreOf](/github.com/weakpixel/asset#StoreOf) if [RegisterLoaders](#RegisterLoaders) has not run on srv.\n\n```go\nfunc (s MapStore) Err(h MapHandle) error\n```\nErr returns why the map failed to load, or nil.\n\n```go\nfunc (s MapStore) Generic() *asset.Store[Map]\n```\nGeneric returns the underlying generic store, for loaders and tools that work with asset.Handle\\[Map].\n\n```go\nfunc (s MapStore) Get(h MapHandle) *Map\n```\nGet returns the map, or the placeholder (an empty Map); see [asset.Store.Get](/github.com/weakpixel/asset#Store.Get) for the pointer validity rule. It does not allocate.\n\n```go\nfunc (s MapStore) Key(h MapHandle) asset.Key\n```\nKey returns the key h was loaded from, or the zero Key for a zero or stale handle.\n\n```go\nfunc (s MapStore) Lookup(h MapHandle) (*Map, asset.State)\n```\nLookup is Get plus the load state; see [asset.Store.Lookup](/github.com/weakpixel/asset#Store.Lookup). Use it to tell a loaded map from the placeholder.\n\n```go\nfunc (s MapStore) Ready(h MapHandle) bool\n```\nReady reports whether h refers to a loaded map.\n\n```go\ntype MapTileset struct {\n\t// FirstGID is the GID of the tileset's local tile 0.\n\tFirstGID uint32\n\t// Tileset is the external tileset; zero for a tileset embedded in the\n\t// map (whose tiles are only in the map's GID table).\n\tTileset TilesetHandle\n\tName    string\n}\n```\nMapTileset is a tileset of a [Map](#Map).\n\n```go\ntype RenderOrder uint8\n```\nRenderOrder is the order in which a tile layer's cells are drawn.\n\n```go\nconst (\n\tRightDown RenderOrder = iota\n\tRightUp\n\tLeftDown\n\tLeftUp\n)\n```\nRender orders.\n\n```go\ntype SpawnOptions struct {\n\t// Pos is the world position of the map's top-left corner (of the world's\n\t// origin for SpawnWorld).\n\tPos gfx.Vec2\n\t// BaseLayer is the render layer of the map's bottom drawable layer.\n\tBaseLayer int16\n\t// LayerFor, if set, decides for each tile and image layer of map m\n\t// whether to spawn it and on which render layer. drawIndex counts the\n\t// drawable layers bottom to top from 0; the default render layer is\n\t// BaseLayer + drawIndex. This is where game code applies its own layer\n\t// properties (\"renderPass\", \"merge into the sprites' layer\"); m gives\n\t// access to the enclosing groups (m.Layers[l.Parent]).\n\tLayerFor func(m *Map, l *Layer, drawIndex int) (layer int16, spawn bool)\n\t// Chunked spawns every tile layer with TileLayer.Chunked set: its static\n\t// tiles are pre-rendered into chunk images. Change the field per layer\n\t// after spawning to chunk only some layers.\n\tChunked bool\n\t// NoDocuments loads a map alone: game.Engine's loaders then skip the\n\t// \u003cmap\u003e.level.json documents they would spawn with it, and a group's own\n\t// document. Read by game.Engine's loaders; [Spawn] and [SpawnWorld] ignore it.\n\tNoDocuments bool\n\t// SkipDocuments lists the asset keys of GOD documents the loaders must not\n\t// spawn, for a tool that spawns that file itself. Read by game.Engine's\n\t// loaders; [Spawn] and [SpawnWorld] ignore it.\n\tSkipDocuments []string\n}\n```\nSpawnOptions configures [Spawn](#Spawn) and [SpawnWorld](#SpawnWorld).\n\n```go\ntype Tile struct {\n\t// Sprite is the tile's image; zero for an ID the tileset does not have.\n\tSprite gfx.SpriteHandle\n\t// Size is the size of the tile's image in pixels.\n\tSize gfx.Vec2\n\t// Offset is the tileset's tileoffset, added when the tile is drawn.\n\tOffset gfx.Vec2\n\t// Scale is (1, 1), or the factor that fits the tile to the map's cell for\n\t// tilesets with tilerendersize \"grid\" (in a Map's GID table only).\n\tScale gfx.Vec2\n\tClass string\n\tProps tiled.Properties\n\t// Shapes are the tile's collision shapes, in tile pixels.\n\tShapes []tiled.Object\n\t// Anim lists the frames of an animated tile, nil for a static one, and\n\t// AnimTotal is the sum of their durations.\n\tAnim      []AnimFrame\n\tAnimTotal time.Duration\n}\n```\nTile is one tile, of a [Tileset](#Tileset) or of a [Map](#Map)'s GID table. Its slices are shared between the tileset and every map that uses it: read-only.\n\n```go\nfunc (t *Tile) FrameAt(clock time.Duration) gfx.SpriteHandle\n```\nFrameAt 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.\n\n```go\ntype TileLayer struct {\n\t// Map is the map the layer belongs to. A map that is not Ready draws\n\t// nothing.\n\tMap MapHandle\n\t// Index is the layer's index in Map.Layers.\n\tIndex int32\n\t// Layer is the render layer of the queued items.\n\tLayer int16\n\t// Hidden skips the layer. Spawn sets it for layers that are invisible in\n\t// Tiled; toggling it at run time is a field write, as for gfx.Sprite.\n\tHidden bool\n\t// Chunked pre-renders the layer's static tiles into chunk images, so\n\t// that it queues one item per visible chunk (16x16 cells) instead of one\n\t// per visible tile; animated tiles are still queued one by one. Use it\n\t// for large layers whose cells do not change. It is ignored for image\n\t// layers, in a render layer configured with YSort (a chunk has one\n\t// SortY) and without a *render.Canvases resource; those draw tile by\n\t// tile. Spawn sets it from SpawnOptions.Chunked.\n\tChunked bool\n\t// Tint multiplies the colors of the layer's tiles. Spawn sets it to the\n\t// layer's effective tint with its opacity folded into the alpha. Zero\n\t// means no tint.\n\tTint gfx.Color\n\t// Shader draws the layer's tiles (or image) through a Kage shader, with\n\t// the same inputs as gfx.Sprite.Shader: the tile image as imageSrc0, Tint\n\t// as the color argument and Params as the \"Params\" vec4. Spawn sets it\n\t// from the layer property \"shader\" (see [PropShader]); assigning it at\n\t// run time works like any field write. The zero handle, and a shader that\n\t// is loading or failed, draw normally.\n\t//\n\t// An unchunked layer makes one shader draw per visible tile. A Chunked\n\t// layer shades each baked chunk image instead, one draw per visible chunk\n\t// and animated tile: the shader runs every frame (Time animates it) but\n\t// sees the chunk's pixels, so a shader that samples neighbouring texels\n\t// meets chunk borders. Prefer Chunked for shaded layers.\n\tShader gfx.ShaderHandle\n\t// Params are the shader's Params uniform. Spawn sets them from the layer\n\t// property \"shaderParams\".\n\tParams [4]float32\n\t// contains filtered or unexported fields\n}\n```\nTileLayer draws one tile or image layer of a map: [TilemapExtract](#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.\n\nCreate layers with [Spawn](#Spawn) or [SpawnWorld](#SpawnWorld). TileLayer is pointer-free and a plain component, not a Game Object component: maps are not GOD data.\n\n```go\ntype TilemapExtract struct {\n\t// contains filtered or unexported fields\n}\n```\nTilemapExtract queues the visible tiles of every [TileLayer](#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).\n\nPlacement 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.\n\nAnimated 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.\n\nA layer with [TileLayer.Chunked](#TileLayer.Chunked) set queues one [render.KindCanvas](/github.com/weakpixel/engine/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.\n\nRun does not allocate, except while it pre-renders a chunk (the queue grows to its steady size during the first ticks).\n\nIt needs the resources \\*render.Queue, \\*render.Camera, \\*extract.Screen and \\*asset.Server (with [RegisterLoaders](#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.\n\n```go\nfunc NewTilemapExtract(w *ecs.World) *TilemapExtract\n```\nNewTilemapExtract returns the system for w.\n\n```go\nfunc (s *TilemapExtract) Init(ctx *system.Context)\n```\nInit implements system.Initializer. It caches the resources and the stores, so Run does no lookups.\n\n```go\nfunc (s *TilemapExtract) Run(ctx *system.Context)\n```\nRun implements system.Runner.\n\n```go\ntype Tileset struct {\n\tName  string\n\tClass string\n\t// TileSize is the tileset's grid cell size in pixels.\n\tTileSize gfx.Vec2\n\t// GridScale is set for tilerendersize \"grid\": maps scale the tiles to\n\t// their own cell size.\n\tGridScale bool\n\t// Atlas is the atlas of a single-image tileset; zero for a collection.\n\tAtlas render.AtlasHandle\n\t// Tiles is indexed by local tile ID. A collection may skip IDs; those\n\t// entries have a zero Sprite.\n\tTiles []Tile\n\tProps tiled.Properties\n\t// File is the decoded file, for data the fields above leave out\n\t// (probabilities, image paths, object alignment). Read-only.\n\tFile *tiled.Tileset\n}\n```\nTileset is a loaded Tiled tileset (\\*.tsj): its tiles with sprites, sizes, collision shapes, animations and custom properties. Load it with [LoadTileset](#LoadTileset); maps load their external tilesets themselves, and a tileset shared by several maps is loaded once.\n\nA 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.\n\n```go\ntype TilesetHandle struct{ asset.Ref }\n```\nTilesetHandle refers to a [Tileset](#Tileset). It is 8 bytes, comparable and pointer-free; the zero value means \"none\".\n\n```go\nfunc LoadTileset(sc *asset.Scope, key string) TilesetHandle\n```\nLoadTileset returns the handle for a tileset key (\"tiles/temple.tsj\") and adds a hold for sc. It never blocks and panics like [asset.Load](/github.com/weakpixel/asset#Load).\n\n```go\nfunc LoadTilesetSync(sc *asset.Scope, key string) (TilesetHandle, error)\n```\nLoadTilesetSync is LoadTileset followed by loading until the tileset (and everything it depends on) is ready or failed, like [asset.LoadSync](/github.com/weakpixel/asset#LoadSync). It blocks: use it for boot screens, tests and tools.\n\n```go\ntype TilesetStore struct {\n\t// contains filtered or unexported fields\n}\n```\nTilesetStore resolves [TilesetHandle](#TilesetHandle) values. Get one with [TilesetStoreOf](#TilesetStoreOf). It is a small value; copy it freely.\n\n```go\nfunc TilesetStoreOf(srv *asset.Server) TilesetStore\n```\nTilesetStoreOf returns the tileset store of srv. It panics like [asset.StoreOf](/github.com/weakpixel/asset#StoreOf) if [RegisterLoaders](#RegisterLoaders) has not run on srv.\n\n```go\nfunc (s TilesetStore) Err(h TilesetHandle) error\n```\nErr returns why the tileset failed to load, or nil.\n\n```go\nfunc (s TilesetStore) Generic() *asset.Store[Tileset]\n```\nGeneric returns the underlying generic store, for loaders and tools that work with asset.Handle\\[Tileset].\n\n```go\nfunc (s TilesetStore) Get(h TilesetHandle) *Tileset\n```\nGet returns the tileset, or the placeholder (an empty Tileset); see [asset.Store.Get](/github.com/weakpixel/asset#Store.Get) for the pointer validity rule. It does not allocate.\n\n```go\nfunc (s TilesetStore) Key(h TilesetHandle) asset.Key\n```\nKey returns the key h was loaded from, or the zero Key for a zero or stale handle.\n\n```go\nfunc (s TilesetStore) Lookup(h TilesetHandle) (*Tileset, asset.State)\n```\nLookup is Get plus the load state; see [asset.Store.Lookup](/github.com/weakpixel/asset#Store.Lookup). Use it to tell a loaded tileset from the placeholder.\n\n```go\nfunc (s TilesetStore) Ready(h TilesetHandle) bool\n```\nReady reports whether h refers to a loaded tileset.\n\n```go\ntype World struct {\n\tMaps []WorldMap\n}\n```\nWorld is a loaded Tiled world (\\*.world): maps placed side by side. Load it with [LoadWorld](#LoadWorld); it is Ready when every map is, and fails if any map fails. Spawn it with [SpawnWorld](#SpawnWorld).\n\n```go\ntype WorldHandle struct{ asset.Ref }\n```\nWorldHandle refers to a [World](#World). It is 8 bytes, comparable and pointer-free; the zero value means \"none\".\n\n```go\nfunc LoadWorld(sc *asset.Scope, key string) WorldHandle\n```\nLoadWorld returns the handle for a world key (\"world-1.world\") and adds a hold for sc. It never blocks and panics like [asset.Load](/github.com/weakpixel/asset#Load).\n\n```go\nfunc LoadWorldSync(sc *asset.Scope, key string) (WorldHandle, error)\n```\nLoadWorldSync is LoadWorld followed by loading until the world (and everything it depends on) is ready or failed, like [asset.LoadSync](/github.com/weakpixel/asset#LoadSync). It blocks: use it for boot screens, tests and tools.\n\n```go\ntype WorldMap struct {\n\tMap MapHandle\n\t// Pos is the map's top-left corner relative to the world's origin, in\n\t// pixels.\n\tPos gfx.Vec2\n}\n```\nWorldMap is one map of a [World](#World).\n\n```go\ntype WorldStore struct {\n\t// contains filtered or unexported fields\n}\n```\nWorldStore resolves [WorldHandle](#WorldHandle) values. Get one with [WorldStoreOf](#WorldStoreOf). It is a small value; copy it freely.\n\n```go\nfunc WorldStoreOf(srv *asset.Server) WorldStore\n```\nWorldStoreOf returns the world store of srv. It panics like [asset.StoreOf](/github.com/weakpixel/asset#StoreOf) if [RegisterLoaders](#RegisterLoaders) has not run on srv.\n\n```go\nfunc (s WorldStore) Err(h WorldHandle) error\n```\nErr returns why the world failed to load, or nil.\n\n```go\nfunc (s WorldStore) Generic() *asset.Store[World]\n```\nGeneric returns the underlying generic store, for loaders and tools that work with asset.Handle\\[World].\n\n```go\nfunc (s WorldStore) Get(h WorldHandle) *World\n```\nGet returns the world, or the placeholder (an empty World); see [asset.Store.Get](/github.com/weakpixel/asset#Store.Get) for the pointer validity rule. It does not allocate.\n\n```go\nfunc (s WorldStore) Key(h WorldHandle) asset.Key\n```\nKey returns the key h was loaded from, or the zero Key for a zero or stale handle.\n\n```go\nfunc (s WorldStore) Lookup(h WorldHandle) (*World, asset.State)\n```\nLookup is Get plus the load state; see [asset.Store.Lookup](/github.com/weakpixel/asset#Store.Lookup). Use it to tell a loaded world from the placeholder.\n\n```go\nfunc (s WorldStore) Ready(h WorldHandle) bool\n```\nReady reports whether h refers to a loaded world.\n\n","path":"github.com/weakpixel/engine/tilemap","name":"tilemap","synopsis":"Package tilemap loads Tiled maps as assets and draws their tile and image layers through the render queue.","isRedistributable":true}
