tiled

package module
v1.0.56 Latest Latest
Warning

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

Go to latest
Published: May 23, 2020 License: MIT Imports: 14 Imported by: 0

README

go-tiled

GoDoc Build Status

Go library to parse Tiled map editor file format (TMX) and render map to image. Currently supports only orthogonal rendering out-of-the-box.

Installing

$ go get github.com/shipa988/go-tiled

You can use go get -u to update the package. You can also just import and start using the package directly if you're using Go modules, and Go will then download the package on first compilation.

Basic Usage:

package main

import (
	"fmt"
	"os"

	"github.com/shipa988/go-tiled"
	"github.com/shipa988/go-tiled/render"
)

const mapPath = "maps/map.tmx" // Path to your Tiled Map.

func main() {
    // Parse .tmx file.
	gameMap, err := tiled.LoadFromFile(mapPath)

	if err != nil {
		fmt.Println("Error parsing map")
		os.Exit(2)
	}

	fmt.Println(gameMap)

	// You can also render the map to an in-memory image for direct
	// use with the default Renderer, or by making your own.
	renderer := render.NewRenderer(gameMap)

	// Render just layer 0 to the Renderer.
	renderer.RenderLayer(0)

	// Get a reference to the Renderer's output, an image.NRGBA struct.
	img := renderer.Result

	// Clear the render result after copying the output if separation of 
	// layers is desired.
	renderer.Clear()

	// And so on. You can also export the image to a file by using the
	// Renderer's Save functions.

}

Documentation

For further documentation, see https://pkg.go.dev/github.com/shipa988/go-tiled or run:

$ godoc github.com/shipa988/go-tiled

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidDecodedTileCount error is returned when layer data has invalid tile count
	ErrInvalidDecodedTileCount = errors.New("tiled: invalid decoded tile count")
	// ErrEmptyLayerData error is returned when layer contains no data
	ErrEmptyLayerData = errors.New("tiled: missing layer data")
	// ErrUnknownEncoding error is returned when kayer data has unknown encoding
	ErrUnknownEncoding = errors.New("tiled: unknown data encoding")
)
View Source
var (
	// ErrInvalidObjectPoint error is returned if there is error parsing object points
	ErrInvalidObjectPoint = errors.New("tiled: invalid object point")
)
View Source
var (
	// ErrInvalidTileGID error is returned when tile GID is not found
	ErrInvalidTileGID = errors.New("tiled: invalid tile GID")
)
View Source
var (
	// ErrUnknownCompression error is returned when file contains invalid compression method
	ErrUnknownCompression = errors.New("tiled: invalid compression method")
)
View Source
var NilLayerTile = &LayerTile{Nil: true}

NilLayerTile is reusable layer tile that is nil

Functions

func GetTileCollision

func GetTileCollision(id uint32, t *Tileset) (rs []image.Rectangle)

Types

type AnimationFrame

type AnimationFrame struct {
	// The local ID of a tile within the parent tileset.
	TileID uint32 `xml:"tileid,attr"`
	// How long (in milliseconds) this frame should be displayed before advancing to the next frame.
	Duration uint32 `xml:"duration,attr"`
}

AnimationFrame is single frame of animation

func GetTileAnimation added in v1.0.7

func GetTileAnimation(id uint32, t *Tileset) []*AnimationFrame

type Data

type Data struct {
	// The encoding used to encode the tile layer data. When used, it can be "base64" and "csv" at the moment.
	Encoding string `xml:"encoding,attr"`
	// The compression used to compress the tile layer data. Tiled Qt supports "gzip" and "zlib".
	Compression string `xml:"compression,attr"`
	// Raw data
	RawData []byte `xml:",innerxml"`
	// Only used when layer encoding is xml
	DataTiles []*DataTile `xml:"tile"`
}

Data is raw data

type DataTile

type DataTile struct {
	// The global tile ID.
	GID uint32 `xml:"gid,attr"`
}

DataTile defines the value of a single tile on a tile layer

type Ellipse

type Ellipse struct {
}

Ellipse is used to mark an object as an ellipse.

type Group

type Group struct {
	// Unique ID of the layer.
	// Each layer that added to a map gets a unique id. Even if a layer is deleted,
	// no layer ever gets the same ID. Can not be changed in Tiled. (since Tiled 1.2)
	ID uint32 `xml:"id,attr"`
	// The name of the group layer.
	Name string `xml:"name,attr"`
	// Rendering offset of the image layer in pixels. Defaults to 0. (since 0.15)
	OffsetX int `xml:"offsetx,attr"`
	// Rendering offset of the image layer in pixels. Defaults to 0. (since 0.15)
	OffsetY int `xml:"offsety,attr"`
	// The opacity of the layer as a value from 0 to 1. Defaults to 1.
	Opacity float32 `xml:"opacity,attr"`
	// Whether the layer is shown (1) or hidden (0). Defaults to 1.
	Visible bool `xml:"visible,attr"`
	// Custom properties
	Properties Properties `xml:"properties>property"`
	// Map layers
	Layers []*Layer `xml:"layer"`
	// Map object groups
	ObjectGroups []*ObjectGroup `xml:"objectgroup"`
	// Image layers
	ImageLayers []*ImageLayer `xml:"imagelayer"`
	// Group layers
	Groups []*Group `xml:"group"`
}

Group is a group layer, used to organize the layers of the map in a hierarchy.

func (*Group) UnmarshalXML

func (g *Group) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML decodes a single XML element beginning with the given start element.

type Image

type Image struct {
	// Used for embedded images, in combination with a data child element. Valid values are file extensions like png, gif, jpg, bmp, etc.
	Format string `xml:"format,attr"`
	// The reference to the tileset image file
	Source string `xml:"source,attr"`
	// Defines a specific color that is treated as transparent (example value: "#FF00FF" for magenta).
	// Up until Tiled 0.12, this value is written out without a # but this is planned to change.
	Trans string `xml:"trans,attr"`
	// The image width in pixels (optional, used for tile index correction when the image changes)
	Width int `xml:"width,attr"`
	// The image height in pixels (optional)
	Height int `xml:"height,attr"`
	// Embedded image content
	Data *Data `xml:"data,attr"`
}

Image source

type ImageLayer

type ImageLayer struct {
	// Unique ID of the layer.
	// Each layer that added to a map gets a unique id. Even if a layer is deleted,
	// no layer ever gets the same ID. Can not be changed in Tiled. (since Tiled 1.2)
	ID uint32 `xml:"id,attr"`
	// The name of the image layer.
	Name string `xml:"name,attr"`
	// Rendering offset of the image layer in pixels. Defaults to 0. (since 0.15)
	OffsetX int `xml:"offsetx,attr"`
	// Rendering offset of the image layer in pixels. Defaults to 0. (since 0.15)
	OffsetY int `xml:"offsety,attr"`
	// The x position of the image layer in pixels. (deprecated since 0.15)
	X int `xml:"x,attr"`
	// The y position of the image layer in pixels. (deprecated since 0.15)
	Y int `xml:"y,attr"`
	// The opacity of the layer as a value from 0 to 1. Defaults to 1.
	Opacity float32 `xml:"opacity,attr"`
	// Whether the layer is shown (1) or hidden (0). Defaults to 1.
	Visible bool `xml:"visible,attr"`
	// Custom properties
	Properties Properties `xml:"properties>property"`
	// The group image
	Image *Image `xml:"image"`
}

ImageLayer is a layer consisting of a single image.

func (*ImageLayer) UnmarshalXML

func (l *ImageLayer) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML decodes a single XML element beginning with the given start element.

type Layer

type Layer struct {

	// Unique ID of the layer.
	// Each layer that added to a map gets a unique id. Even if a layer is deleted,
	// no layer ever gets the same ID. Can not be changed in Tiled. (since Tiled 1.2)
	ID uint32 `xml:"id,attr"`
	// The name of the layer.
	Name string `xml:"name,attr"`
	// The opacity of the layer as a value from 0 to 1. Defaults to 1.
	Opacity float32 `xml:"opacity,attr"`
	// Whether the layer is shown (1) or hidden (0). Defaults to 1.
	Visible bool `xml:"visible,attr"`
	// Rendering offset for this layer in pixels. Defaults to 0. (since 0.14)
	OffsetX int `xml:"offsetx,attr"`
	// Rendering offset for this layer in pixels. Defaults to 0. (since 0.14)
	OffsetY int `xml:"offsety,attr"`
	// Custom properties
	Properties Properties `xml:"properties>property"`
	// This is the attribute you'd like to use, not Data. Tile entry at (x,y) is obtained using l.DecodedTiles[y*map.Width+x].
	Tiles []*LayerTile
	// contains filtered or unexported fields
}

Layer is a map layer

func (*Layer) DecodeLayer

func (l *Layer) DecodeLayer(m *Map) error

DecodeLayer decodes layer data

func (*Layer) IsEmpty

func (l *Layer) IsEmpty() bool

IsEmpty checks if layer has tiles other than nil

func (*Layer) UnmarshalXML

func (l *Layer) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML decodes a single XML element beginning with the given start element.

type LayerTile

type LayerTile struct {
	// Tile ID
	ID uint32
	// Tile tileset
	Tileset *Tileset
	// Horizontal tile image flip
	HorizontalFlip bool
	// Vertical tile image flip
	VerticalFlip bool
	// Diagonal tile image flip
	DiagonalFlip bool
	// Tile is nil
	Nil bool
	//Collision cordinate
	Collision []image.Rectangle
	//Animation for tile
	Animation []*AnimationFrame
}

LayerTile is a layer tile

func (*LayerTile) IsNil

func (t *LayerTile) IsNil() bool

IsNil returs if tile is nil

type Loader

type Loader struct {
	// A FileSystem that is used for loading TMX files and any external
	// resources it may reference.
	//
	// A nil FileSystem uses the local file system.
	FileSystem http.FileSystem
}

Loader provides configuration on how TMX maps and resources are loaded.

func (*Loader) LoadFromFile

func (l *Loader) LoadFromFile(fileName string) (*Map, error)

LoadFromFile function loads tiled map in TMX format from file

func (*Loader) LoadFromReader

func (l *Loader) LoadFromReader(baseDir string, r io.Reader) (*Map, error)

LoadFromReader function loads tiled map in TMX format from io.Reader baseDir is used for loading additional tile data, current directory is used if empty

type Map

type Map struct {
	// Loader for loading additional data
	Loader *Loader

	//loader *Loader
	// The TMX format version, generally 1.0.
	Version string `xml:"title,attr"`
	// Map orientation. Tiled supports "orthogonal", "isometric", "staggered" (since 0.9) and "hexagonal" (since 0.11).
	Orientation string `xml:"orientation,attr"`
	// The order in which tiles on tile layers are rendered. Valid values are right-down (the default), right-up, left-down and left-up.
	// In all cases, the map is drawn row-by-row. (since 0.10, but only supported for orthogonal maps at the moment)
	RenderOrder string `xml:"renderorder,attr"`
	// The map width in tiles.
	Width int `xml:"width,attr"`
	// The map height in tiles.
	Height int `xml:"height,attr"`
	// The width of a tile.
	TileWidth int `xml:"tilewidth,attr"`
	// The height of a tile.
	TileHeight int `xml:"tileheight,attr"`
	// Only for hexagonal maps. Determines the width or height (depending on the staggered axis) of the tile's edge, in pixels.
	HexSideLength int `xml:"hexsidelength,attr"`
	// For staggered and hexagonal maps, determines which axis ("x" or "y") is staggered. (since 0.11)
	StaggerAxis int `xml:"staggeraxis,attr"`
	// For staggered and hexagonal maps, determines whether the "even" or "odd" indexes along the staggered axis are shifted. (since 0.11)
	StaggerIndex int `xml:"staggerindex,attr"`
	// The background color of the map. (since 0.9, optional, may include alpha value since 0.15 in the form #AARRGGBB)
	BackgroundColor string `xml:"backgroundcolor,attr"`
	// Stores the next available ID for new objects. This number is stored to prevent reuse of the same ID after objects have been removed. (since 0.11)
	NextObjectID uint32 `xml:"nextobjectid,attr"`
	// Custom properties
	Properties *Properties `xml:"properties>property"`
	// Map tilesets
	Tilesets []*Tileset `xml:"tileset"`
	// Map layers
	Layers []*Layer `xml:"layer"`
	// Map object groups
	ObjectGroups []*ObjectGroup `xml:"objectgroup"`
	// Image layers
	ImageLayers []*ImageLayer `xml:"imagelayer"`
	// Group layers
	Groups []*Group `xml:"group"`
	// contains filtered or unexported fields
}

Map contains three different kinds of layers. Tile layers were once the only type, and are simply called layer, object layers have the objectgroup tag and image layers use the imagelayer tag. The order in which these layers appear is the order in which the layers are rendered by Tiled

func LoadFromFile

func LoadFromFile(fileName string) (*Map, error)

LoadFromFile function loads tiled map in TMX format from file

func LoadFromReader

func LoadFromReader(baseDir string, r io.Reader) (*Map, error)

LoadFromReader function loads tiled map in TMX format from io.Reader baseDir is used for loading additional tile data, current directory is used if empty

func (*Map) GetFileFullPath

func (m *Map) GetFileFullPath(fileName string) string

GetFileFullPath returns path to file relative to map file

func (*Map) TileGIDToTile

func (m *Map) TileGIDToTile(gid uint32) (*LayerTile, error)

TileGIDToTile is used to find tile data by GID

func (*Map) UnmarshalXML

func (m *Map) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML decodes a single XML element beginning with the given start element.

type Object

type Object struct {
	// Unique ID of the object. Each object that is placed on a map gets a unique id. Even if an object was deleted, no object gets the same ID.
	// Can not be changed in Tiled Qt. (since Tiled 0.11)
	ID uint32 `xml:"id,attr"`
	// The name of the object. An arbitrary string.
	Name string `xml:"name,attr"`
	// The type of the object. An arbitrary string.
	Type string `xml:"type,attr"`
	// The x coordinate of the object.
	X float64 `xml:"x,attr"`
	// The y coordinate of the object.
	Y float64 `xml:"y,attr"`
	// The width of the object (defaults to 0).
	Width float64 `xml:"width,attr"`
	// The height of the object (defaults to 0).
	Height float64 `xml:"height,attr"`
	// The rotation of the object in degrees clockwise (defaults to 0). (since 0.10)
	Rotation float64 `xml:"rotation,attr"`
	// An reference to a tile (optional).
	GID uint32 `xml:"gid,attr"`
	// Whether the object is shown (1) or hidden (0). Defaults to 1. (since 0.9)
	Visible bool `xml:"visible,attr"`
	// Custom properties
	Properties Properties `xml:"properties>property"`
	// Used to mark an object as an ellipse. The existing x, y, width and height attributes are used to determine the size of the ellipse.
	Ellipses []*Ellipse `xml:"ellipse"`
	// Polygons
	Polygons []*Polygon `xml:"polygon"`
	// Poly lines
	PolyLines []*PolyLine `xml:"polyline"`
	// Text
	Text *Text `xml:"text"`
}

Object is used to add custom information to your tile map, such as spawn points, warps, exits, etc.

type ObjectGroup

type ObjectGroup struct {
	// Unique ID of the layer.
	// Each layer that added to a map gets a unique id. Even if a layer is deleted,
	// no layer ever gets the same ID. Can not be changed in Tiled. (since Tiled 1.2)
	ID uint32 `xml:"id,attr"`
	// The name of the object group.
	Name string `xml:"name,attr"`
	// The color used to display the objects in this group.
	Color string `xml:"color,attr"`
	// The opacity of the layer as a value from 0 to 1. Defaults to 1.
	Opacity float32 `xml:"opacity,attr"`
	// Whether the layer is shown (1) or hidden (0). Defaults to 1.
	Visible bool `xml:"visible,attr"`
	// Rendering offset for this layer in pixels. Defaults to 0. (since 0.14)
	OffsetX int `xml:"offsetx,attr"`
	// Rendering offset for this layer in pixels. Defaults to 0. (since 0.14)
	OffsetY int `xml:"offsety,attr"`
	// Whether the objects are drawn according to the order of appearance ("index") or sorted by their y-coordinate ("topdown"). Defaults to "topdown".
	DrawOrder string `xml:"draworder,attr"`
	// Custom properties
	Properties Properties `xml:"properties>property"`
	// Group objects
	Objects []*Object `xml:"object"`
}

ObjectGroup is in fact a map layer, and is hence called "object layer" in Tiled Qt

type Point

type Point struct {
	// Point X
	X float64
	// Point Y
	Y float64
}

Point is point

type Points

type Points []*Point

Points is array of points

func (*Points) UnmarshalXMLAttr

func (m *Points) UnmarshalXMLAttr(attr xml.Attr) error

UnmarshalXMLAttr decodes a single XML element beginning with the given start element.

type PolyLine

type PolyLine struct {
	// A list of x,y coordinates in pixels.
	Points *Points `xml:"points,attr"`
}

PolyLine follows the same placement definition as a polygon object.

type Polygon

type Polygon struct {
	// A list of x,y coordinates in pixels.
	Points *Points `xml:"points,attr"`
}

Polygon object is made up of a space-delimited list of x,y coordinates. The origin for these coordinates is the location of the parent object. By default, the first point is created as 0,0 denoting that the point will originate exactly where the object is placed.

type Properties

type Properties []*Property

Properties wraps any number of custom properties

func (Properties) Get

func (p Properties) Get(name string) []string

Get finds all properties by specified name

func (Properties) GetBool

func (p Properties) GetBool(name string) bool

GetBool finds first bool property by specified name

func (Properties) GetString

func (p Properties) GetString(name string) string

GetString finds first string property by specified name

type Property

type Property struct {
	// The name of the property.
	Name string `xml:"name,attr"`
	// The type of the property. Can be string (default), int, float, bool, color or file (since 0.16, with color and file added in 0.17).
	Type string `xml:"type,attr"`
	// The value of the property.
	// Boolean properties have a value of either "true" or "false".
	// Color properties are stored in the format #AARRGGBB.
	// File properties are stored as paths relative from the location of the map file.
	Value string `xml:"value,attr"`
}

Property is used for custom properties

type Terrain

type Terrain struct {
	// The name of the terrain type.
	Name string `xml:"name,attr"`
	// The local tile-id of the tile that represents the terrain visually.
	Tile uint32 `xml:"tile,attr"`
	// Custom properties
	Properties Properties `xml:"properties>property"`
}

Terrain type

type Text

type Text struct {
	// The actual text
	Text string `xml:",chardata"`
	// The font family used (default: "sans-serif")
	FontFamily string `xml:"fontfamily,attr"`
	// The size of the font in pixels (not using points, because other sizes in the TMX format are also using pixels) (default: 16)
	Size int `xml:"pixelsize,attr"`
	// Whether word wrapping is enabled (1) or disabled (0). Defaults to 0.
	Wrap bool `xml:"wrap,attr"`
	// Color of the text in #AARRGGBB or #RRGGBB format (default: #000000)
	Color string `xml:"color,attr"`
	// Whether the font is bold (1) or not (0). Defaults to 0.
	Bold bool `xml:"bold,attr"`
	// Whether the font is italic (1) or not (0). Defaults to 0.
	Italic bool `xml:"italic,attr"`
	// Whether a line should be drawn below the text (1) or not (0). Defaults to 0.
	Underline bool `xml:"underline,attr"`
	// Whether a line should be drawn through the text (1) or not (0). Defaults to 0.
	Strikethrough bool `xml:"strikeout,attr"`
	// Whether kerning should be used while rendering the text (1) or not (0). Default to 1.
	Kerning bool `xml:"kerning,attr"`
	// Horizontal alignment of the text within the object (left (default), center, right or justify (since Tiled 1.2.1))
	HAlign string `xml:"halign,attr"`
	// Vertical alignment of the text within the object (top (default), center or bottom)
	VAlign string `xml:"valign,attr"`
}

Text contains a text and attributes such as bold, color, etc.

func (*Text) UnmarshalXML

func (t *Text) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML decodes a single XML element beginning with the given start element.

type Tileset

type Tileset struct {

	// The first global tile ID of this tileset (this global ID maps to the first tile in this tileset).
	FirstGID uint32 `xml:"firstgid,attr"`
	// If this tileset is stored in an external TSX (Tile Set XML) file, this attribute refers to that file.
	// That TSX file has the same structure as the <tileset> element described here. (There is the firstgid
	// attribute missing and this source attribute is also not there. These two attributes are kept in the
	// TMX map, since they are map specific.)
	Source string `xml:"source,attr"`
	// External TSX source loaded.
	SourceLoaded bool `xml:"-"`
	// The name of this tileset.
	Name string `xml:"name,attr"`
	// The (maximum) width of the tiles in this tileset.
	TileWidth int `xml:"tilewidth,attr"`
	// The (maximum) height of the tiles in this tileset.
	TileHeight int `xml:"tileheight,attr"`
	// The spacing in pixels between the tiles in this tileset (applies to the tileset image).
	Spacing int `xml:"spacing,attr"`
	// The margin around the tiles in this tileset (applies to the tileset image).
	Margin int `xml:"margin,attr"`
	// The number of tiles in this tileset (since 0.13)
	TileCount int `xml:"tilecount,attr"`
	// The number of tile columns in the tileset. For image collection tilesets it is editable and is used when displaying the tileset. (since 0.15)
	Columns int `xml:"columns,attr"`
	// Offset in pixels, to be applied when drawing a tile from the related tileset. When not present, no offset is applied.
	TileOffset *TilesetTileOffset `xml:"tileoffset"`
	// Custom properties
	Properties Properties `xml:"properties>property"`
	// Embedded image
	Image *Image `xml:"image"`
	// Defines an array of terrain types, which can be referenced from the terrain of the tile element.
	TerrainTypes []*Terrain `xml:"terraintypes>terrain"`
	// Tiles in tileset
	Tiles []*TilesetTile `xml:"tile"`
	// contains filtered or unexported fields
}

Tileset is collection of tiles

func (*Tileset) GetFileFullPath

func (ts *Tileset) GetFileFullPath(fileName string) string

GetFileFullPath returns path to file relative to tileset file

type TilesetTile

type TilesetTile struct {
	// The local tile ID within its tileset.
	ID uint32 `xml:"id,attr"`
	// The type of the tile. Refers to an object type and is used by tile objects. (optional) (since 1.0)
	Type string `xml:"type,attr"`
	// Defines the terrain type of each corner of the tile, given as comma-separated indexes in the terrain types
	// array in the order top-left, top-right, bottom-left, bottom-right.
	// Leaving out a value means that corner has no terrain. (optional) (since 0.9)
	Terrain string `xml:"terrain,attr"`
	// A percentage indicating the probability that this tile is chosen when it competes with others while editing with the terrain tool. (optional) (since 0.9)
	Probability int `xml:"probability,attr"`
	// Custom properties
	Properties Properties `xml:"properties>property"`
	// Embedded image
	Image *Image `xml:"image"`
	// Tile object groups
	ObjectGroups []*ObjectGroup `xml:"objectgroup"`
	// List of animation frames
	Animation []*AnimationFrame `xml:"animation>frame"`
}

TilesetTile information

type TilesetTileOffset

type TilesetTileOffset struct {
	// Horizontal offset in pixels
	X int `xml:"x,attr"`
	// Vertical offset in pixels (positive is down)
	Y int `xml:"y,attr"`
}

TilesetTileOffset is used to specify an offset in pixels, to be applied when drawing a tile from the related tileset. When not present, no offset is applied

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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