tiled

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: 15 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 tiled reads the Tiled JSON formats (https://doc.mapeditor.org/en/stable/reference/json-map-format/): maps (*.tmj, DecodeMap), external tilesets (*.tsj, DecodeTileset) and worlds (*.world, DecodeWorld). It decodes one file at a time into a plain Go model and checks what can be checked from that file alone; it loads no images, resolves no paths and imports the standard library only.

Package render (the *.tsj atlas format) and package tilemap (the Tileset, Map and World assets) are thin adapters over this package, and command-line tools can use it without Ebiten.

Decoding is lenient about fields it does not model: Tiled adds fields between versions and its files are written by a tool, so unknown fields are ignored. Features the engine does not support (non-orthogonal maps, zstd, object templates, world patterns, embedded single-image tilesets, image sub-rectangles, transparent color keys, a format version other than 1.x) are errors, never silently dropped. Every problem of a file is reported at once, joined with errors.Join.

The guide is doc/guide/engine/tilemaps.md.

Index

Constants

View Source
const (

	// GIDMask clears the flag bits of a raw GID.
	GIDMask uint32 = 0x0fffffff
)

Raw GID bits, as stored in tile layer data and tile objects.

View Source
const MapSuffix = ".tmj"

MapSuffix is the file name suffix of a map.

View Source
const TilesetSuffix = ".tsj"

TilesetSuffix is the file name suffix of an external tileset.

View Source
const WorldSuffix = ".world"

WorldSuffix is the file name suffix of a world.

Variables

This section is empty.

Functions

func JoinGID

func JoinGID(gid uint32, f Flip) uint32

JoinGID is the inverse of SplitGID.

Types

type Chunk

type Chunk struct {
	X, Y          int
	Width, Height int
	// Data holds Width*Height raw GIDs, row by row.
	Data []uint32
}

Chunk is a rectangle of tile data of an infinite map. X and Y are the cell of its top-left corner and may be negative.

type Color

type Color uint32

Color is a packed 0xRRGGBBAA color, non-premultiplied, with the same layout as gfx.Color, so gfx.Color(c) converts it. The zero Color means "not set" (an absent tint or background); "#00000000" decodes to it too.

func ParseColor

func ParseColor(s string) (Color, error)

ParseColor reads a Tiled color: "#RRGGBB" (opaque) or "#AARRGGBB". The '#' is optional. The empty string is the zero Color.

func (Color) RGBA

func (c Color) RGBA() (r, g, b, a uint8)

RGBA returns the channels.

func (Color) String

func (c Color) String() string

String returns the color as Tiled writes it, "#AARRGGBB".

type Flip

type Flip uint8

Flip holds the transformation flags Tiled stores in the high bits of a global tile ID (GID).

const (
	FlipH Flip = 1 << iota
	FlipV
	FlipD
	FlipHex
)

Flip flags. A tile is transformed by applying FlipD (transpose: swap x and y), then FlipH, then FlipV. FlipHex is the 120° rotation of hexagonal maps; orthogonal maps never set it, and the engine ignores it.

func SplitGID

func SplitGID(raw uint32) (gid uint32, f Flip)

SplitGID separates a raw GID into the tile's GID (0 means "no tile") and its flip flags.

type Frame

type Frame struct {
	// TileID is the local ID of the tile shown, in the same tileset.
	TileID   int
	Duration time.Duration
}

Frame is one frame of a tile animation.

type Layer

type Layer struct {
	Kind  LayerKind
	ID    int
	Name  string
	Class string
	// Offset shifts the layer, in pixels.
	Offset Point
	// Parallax is the parallax factor; (1, 1), the default, scrolls with the
	// camera.
	Parallax Point
	Opacity  float64
	Visible  bool
	// Tint multiplies the layer's colors; zero means none.
	Tint  Color
	Props Properties

	// Width and Height are the layer size in cells. For a finite map they
	// equal the map size and Data holds Width*Height raw GIDs, row by row.
	// For an infinite map, StartX, StartY, Width and Height bound the
	// layer's Chunks and Data is nil.
	StartX, StartY int
	Width, Height  int
	Data           []uint32
	Chunks         []Chunk

	// DrawOrder is "topdown" (sort by Y, the default) or "index" for object
	// layers; Objects are in file order.
	DrawOrder string
	Objects   []Object

	// Image is the image of an image layer, relative to the map file.
	Image                   string
	ImageWidth, ImageHeight int
	RepeatX, RepeatY        bool

	// Layers are the children of a group, bottom to top.
	Layers []Layer
}

Layer is one layer of a map, as the file nests it. Offset, Parallax, Opacity, Tint and Visible are the layer's own values; a group's values apply to its children on top of theirs (package tilemap composes them).

type LayerKind

type LayerKind uint8

LayerKind is the type of a Layer.

const (
	TileLayer LayerKind = iota
	ObjectLayer
	ImageLayer
	GroupLayer
)

Layer kinds.

func (LayerKind) String

func (k LayerKind) String() string

String returns the kind as Tiled writes it ("tilelayer", ...).

type Map

type Map struct {
	// Width and Height are the size in cells; for an infinite map they are
	// informational (the layers' chunks hold the cells).
	Width, Height int
	// TileWidth and TileHeight are the cell size in pixels.
	TileWidth, TileHeight int
	Infinite              bool
	// RenderOrder is the order tiles are drawn in: "right-down" (the
	// default), "right-up", "left-down" or "left-up".
	RenderOrder string
	// ParallaxOrigin is the point where parallax layers line up with the
	// others, in pixels.
	ParallaxOrigin Point
	// Background is the background color; zero means none.
	Background Color
	Class      string
	Props      Properties
	// Tilesets are sorted by FirstGID.
	Tilesets []MapTileset
	// Layers are the top-level layers, bottom to top.
	Layers []Layer
}

Map is a Tiled map. Only orthogonal maps decode.

func DecodeMap

func DecodeMap(data []byte) (*Map, error)

DecodeMap decodes a map (*.tmj). It decodes the tile data of every layer and checks what the file alone can tell; whether every GID belongs to a tileset needs the external tilesets and is checked by package tilemap.

type MapTileset

type MapTileset struct {
	// FirstGID is the GID of the tileset's local tile 0.
	FirstGID uint32
	// Source is the path of an external tileset (*.tsj) relative to the map
	// file; empty for an embedded tileset.
	Source string
	// Embedded is the tileset itself when it is stored in the map. Only
	// image collections can be embedded (see the package doc).
	Embedded *Tileset
}

MapTileset is a tileset reference of a map.

type Object

type Object struct {
	ID   int
	Name string
	// Class is the object's "class", or its "type" when the file has no class
	// (Tiled before 1.9 and files saved for compatibility write "type").
	Class string
	Shape Shape
	X, Y  float64
	// Width and Height are the size of rectangles, ellipses, text boxes and
	// tile objects. Zero for points, polygons and polylines.
	Width, Height float64
	Rotation      float64
	Visible       bool
	// Points are the vertices of polygons and polylines, relative to X, Y.
	Points []Point
	// GID and Flip select the tile of a ShapeTile object.
	GID  uint32
	Flip Flip
	// Text is set for ShapeText objects.
	Text  *Text
	Props Properties
}

Object is an object of an object layer, or a collision shape of a tile. Positions are pixels in the coordinate space of its container: the map for object layers, the tile for collision shapes. Rotation is in degrees, clockwise, around X, Y.

type Point

type Point struct {
	X, Y float64
}

Point is a position or size in pixels.

type Properties

type Properties []Property

Properties is an ordered list of custom properties: file order for properties, name order for the members of a class value. Defaults of custom types (the propertyTypes of a .tiled-project file) are not applied, because the project file is not read: a member the file does not set is absent.

func (Properties) Bool

func (p Properties) Bool(name string) (bool, bool)

Bool returns a "bool" property (or an untyped bool member).

func (Properties) Class

func (p Properties) Class(name string) (Properties, bool)

Class returns the members of a "class" property (or of an untyped nested class member).

func (Properties) Color

func (p Properties) Color(name string) (Color, bool)

Color returns a "color" property, or an untyped member whose string parses as a color.

func (Properties) File

func (p Properties) File(name string) (string, bool)

File returns a "file" property (or an untyped string member), a path relative to the file that declares it.

func (Properties) Float

func (p Properties) Float(name string) (float64, bool)

Float returns a "float" property. An "int" property (or an untyped number member) widens to float64, because Tiled writes a float with an integral value, such as 1.0, as 1.

func (Properties) Get

func (p Properties) Get(name string) (*Property, bool)

Get returns the property called name.

func (Properties) Int

func (p Properties) Int(name string) (int, bool)

Int returns an "int" property (or an untyped integral member).

func (Properties) Object

func (p Properties) Object(name string) (int, bool)

Object returns an "object" property: the ID of the referenced object, or 0 for none.

func (Properties) String

func (p Properties) String(name string) (string, bool)

String returns a "string" or "file" property (or an untyped string member).

type Property

type Property struct {
	Name string
	Type string
	// PropertyType is the name of the custom class or enum, for "class"
	// values and enum-backed "string" or "int" values.
	PropertyType string
	Value        any
}

Property is one custom property. Value holds a Go value that matches Type:

"string"  string
"file"    string (a path relative to the file that declares it)
"color"   Color
"int"     int
"float"   float64
"bool"    bool
"object"  int (an object ID; 0 means none)
"class"   Properties (the members the file sets)

Members of a class value carry no type in the file, so their Type is empty and Value is inferred from the JSON: string, bool, int (an integral number), float64 or Properties (a nested class). The getters accept such untyped members.

type Shape

type Shape uint8

Shape is the kind of an Object.

const (
	// ShapeRect is a rectangle: X, Y is the top-left corner (before Rotation)
	// and Width, Height its size. A zero-size rectangle is valid.
	ShapeRect Shape = iota
	// ShapePoint is a point at X, Y.
	ShapePoint
	// ShapeEllipse is the ellipse inside the rectangle of a ShapeRect.
	ShapeEllipse
	// ShapePolygon is a closed polygon; Points are relative to X, Y.
	ShapePolygon
	// ShapePolyline is an open polyline; Points are relative to X, Y.
	ShapePolyline
	// ShapeText is a text box; see [Object.Text].
	ShapeText
	// ShapeTile is a tile object: GID and Flip select the tile, X, Y is its
	// bottom-left corner (Tiled's anchor for tile objects in orthogonal maps)
	// and Width, Height its drawn size.
	ShapeTile
)

Object shapes.

func (Shape) String

func (s Shape) String() string

String returns the lower-case shape name.

type Text

type Text struct {
	Text       string
	FontFamily string
	PixelSize  int
	Wrap       bool
	Color      Color
	Bold       bool
	Italic     bool
	Underline  bool
	Strikeout  bool
	Kerning    bool
	// HAlign is "left", "center", "right" or "justify"; VAlign is "top",
	// "center" or "bottom".
	HAlign, VAlign string
}

Text is the text of a ShapeText object.

type Tile

type Tile struct {
	// ID is the local tile ID inside the tileset.
	ID int
	// Class is the tile's "class", or its "type" (see [Object.Class]).
	Class       string
	Probability float64
	// Image is the tile's own image, relative to the tileset file, in an
	// image collection; empty in a single-image tileset.
	Image                   string
	ImageWidth, ImageHeight int
	// Animation lists the frames of an animated tile; nil if it is static.
	Animation []Frame
	// Collision holds the tile's collision shapes, in tile pixels.
	Collision []Object
	Props     Properties
}

Tile is the per-tile data of a tileset.

type Tileset

type Tileset struct {
	Name  string
	Class string
	// TileWidth and TileHeight are the size of a grid cell of the image, and
	// the largest tile of a collection.
	TileWidth, TileHeight int
	// TileCount is the number of tiles. For a single-image tileset, local
	// tile IDs are 0..TileCount-1, row by row.
	TileCount int
	// Columns is the number of tiles per row of a single-image tileset; 0
	// for a collection.
	Columns int
	// Margin is the border around the image's tile grid, Spacing the gap
	// between tiles, in pixels.
	Margin, Spacing int
	// Image is the path of the tileset image relative to the tileset file;
	// empty for a collection, whose images are on its Tiles.
	Image                   string
	ImageWidth, ImageHeight int
	// TileOffset shifts every tile when drawn, in pixels.
	TileOffset Point
	// TileRenderSize is "tile" (draw tiles at their own size, the default)
	// or "grid" (scale them to the map's cell size). FillMode is "stretch".
	TileRenderSize string
	FillMode       string
	// ObjectAlignment is the anchor of tile objects; "unspecified" (the
	// default) means bottom-left in orthogonal maps.
	ObjectAlignment string
	Props           Properties
	// Tiles lists the tiles the file has data for, sorted by ID: every tile
	// of a collection, and only the tiles with properties, a class, an
	// animation or collision shapes of a single-image tileset.
	Tiles []Tile
}

Tileset is a Tiled tileset: either one image cut into a grid of tiles (single-image) or one image per tile (an image collection).

func DecodeTileset

func DecodeTileset(data []byte) (*Tileset, error)

DecodeTileset decodes an external tileset (*.tsj). It checks what the file alone can tell: positive tile sizes, a consistent grid, tile IDs in range, animation frames that refer to existing tiles, and unsupported features.

func (*Tileset) IsCollection

func (ts *Tileset) IsCollection() bool

IsCollection reports whether the tileset is an image collection.

func (*Tileset) Tile

func (ts *Tileset) Tile(id int) (*Tile, bool)

Tile returns the data of local tile id, if the file has any.

type World

type World struct {
	Maps []WorldMap
}

World is a Tiled world: maps placed side by side.

func DecodeWorld

func DecodeWorld(data []byte) (*World, error)

DecodeWorld decodes a world (*.world). Worlds that place maps by file name patterns are not supported: list the maps in "maps".

type WorldMap

type WorldMap struct {
	// FileName is the map's path relative to the world file.
	FileName string
	// X and Y are the map's top-left corner in world pixels; Width and Height
	// its size in pixels (informational: the map file is authoritative).
	X, Y          int
	Width, Height int
}

WorldMap is one map of a World.

Jump to

Keyboard shortcuts

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