This repository has been archived by the owner on Jun 4, 2019. It is now read-only.
forked from lafriks/go-tiled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmx_tileset.go
117 lines (105 loc) · 4.21 KB
/
tmx_tileset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package tiled
import (
"encoding/xml"
"os"
)
// Tileset is collection of tiles
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"`
}
// 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
type TilesetTileOffset struct {
// Horizontal offset in pixels
X int `xml:"x,attr"`
// Vertical offset in pixels (positive is down)
Y int `xml:"y,attr"`
}
// Terrain type
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"`
}
// TilesetTile information
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"`
}
// AnimationFrame is single frame of animation
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"`
}
func (ts *Tileset) initTileset(m *Map) error {
if ts.SourceLoaded || len(ts.Source) == 0 {
return nil
}
f, err := os.Open(m.GetFileFullPath(ts.Source))
if err != nil {
return err
}
defer f.Close()
d := xml.NewDecoder(f)
tse := &Tileset{}
if err := d.Decode(tse); err != nil {
return err
}
tse.Source = ts.Source
tse.SourceLoaded = true
tse.FirstGID = ts.FirstGID
*ts = *tse
return nil
}