tiled

package module
v0.0.0-...-bc8d805 Latest Latest
Warning

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

Go to latest
Published: May 8, 2019 License: MIT Imports: 12 Imported by: 0

README

go-tiled

GoDoc Build Status

Go library to parse Tiled map editor file format (TMX) and render map to image

Curently supports only orthogonal rendering

Installing

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

You can use go get -u to update the package.

Basic Usage:

package main

import (
	"fmt"
	"os"

	"github.com/lafriks/go-tiled"
)

const mapPath = "maps/map.tmx" // path to your map

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

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

	fmt.Print(gameMap)
}

Documentation

For docs, see https://godoc.org/github.com/lafriks/go-tiled or run:

$ godoc github.com/lafriks/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

This section is empty.

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

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 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"`
	// Embeded immage content
	Data *Data `xml:"data,attr"`
}

Image source

type Layer

type Layer struct {

	// 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 attiribute you'd like to use, not Data. Tile entry at (x,y) is obtained using l.Tiles[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
}

LayerTile is a layer tile

func (*LayerTile) IsNil

func (t *LayerTile) IsNil() bool

IsNil returs if tile is nil

type Map

type Map struct {
	// 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"`
	// 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"`
	// Pollygons
	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 {
	// 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 int
	// Point Y
	Y int
}

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 {
	Text          string `xml:",chardata"`
	FontFamily    string `xml:"fontfamily,attr"`
	Size          int    `xml:"pixelsize,attr"`
	Wrap          bool   `xml:"wrap,attr"`
	Color         string `xml:"color,attr"`
	Bold          bool   `xml:"bold,attr"`
	Italic        bool   `xml:"italic,attr"`
	Underline     bool   `xml:"underline,attr"`
	Strikethrough bool   `xml:"strikeout,attr"`
	HAlign        string `xml:"halign,attr"`
	VAlign        string `xml:"valign,attr"`
}

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

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"`
}

Tileset is collection of tiles

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