forked from lafriks/go-tiled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmx_map.go
213 lines (184 loc) · 6.55 KB
/
tmx_map.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Copyright (c) 2017 Lauris Bukšis-Haberkorns <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package tiled
import (
"encoding/xml"
"errors"
"path/filepath"
)
const (
tileHorizontalFlipMask = 0x80000000
tileVerticalFlipMask = 0x40000000
tileDiagonalFlipMask = 0x20000000
tileFlip = tileHorizontalFlipMask | tileVerticalFlipMask | tileDiagonalFlipMask
tileGIDMask = 0x0fffffff
)
// ErrInvalidTileGID error is returned when tile GID is not found
var ErrInvalidTileGID = errors.New("tiled: invalid tile GID")
// Axis type
type Axis string
const (
// AxisX is X axis
AxisX Axis = "x"
// AxisY is Y axis
AxisY Axis = "y"
)
// StaggerIndexType is stagger axis index type
type StaggerIndexType string
const (
// StaggerIndexOdd is odd stagger index
StaggerIndexOdd StaggerIndexType = "odd"
// StaggerIndexEven is even stagger index
StaggerIndexEven StaggerIndexType = "even"
)
// 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
type Map struct {
// Loader for loading additional data
loader *loader
// Base directory for loading additional data
baseDir string
// The TMX format version, generally 1.0.
Version string `xml:"version,attr"`
// The Tiled version used to generate this file
TiledVersion string `xml:"tiledversion,attr"`
// The class of this map (since 1.9, defaults to "").
Class string `xml:"class,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 Axis `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 StaggerIndexType `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 *HexColor `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"`
}
func (m *Map) initTileset(ts *Tileset) error {
if ts.SourceLoaded {
return nil
}
if len(ts.Source) == 0 {
ts.baseDir = m.baseDir
ts.SourceLoaded = true
return nil
}
sourcePath := m.GetFileFullPath(ts.Source)
f, err := m.loader.open(sourcePath)
if err != nil {
return err
}
defer f.Close()
d := xml.NewDecoder(f)
if err := d.Decode(ts); err != nil {
return err
}
ts.baseDir = filepath.Dir(sourcePath)
ts.SourceLoaded = true
return nil
}
// TileGIDToTile is used to find tile data by GID
func (m *Map) TileGIDToTile(gid uint32) (*LayerTile, error) {
if gid == 0 {
return NilLayerTile, nil
}
gidBare := gid &^ tileFlip
for i := len(m.Tilesets) - 1; i >= 0; i-- {
if m.Tilesets[i].FirstGID <= gidBare {
ts := m.Tilesets[i]
if err := m.initTileset(ts); err != nil {
return nil, err
}
return &LayerTile{
ID: gidBare - ts.FirstGID,
Tileset: ts,
HorizontalFlip: gid&tileHorizontalFlipMask != 0,
VerticalFlip: gid&tileVerticalFlipMask != 0,
DiagonalFlip: gid&tileDiagonalFlipMask != 0,
Nil: false,
}, nil
}
}
return nil, ErrInvalidTileGID
}
// GetFileFullPath returns path to file relative to map file
func (m *Map) GetFileFullPath(fileName string) string {
return filepath.Join(m.baseDir, fileName)
}
// UnmarshalXML decodes a single XML element beginning with the given start element.
func (m *Map) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
item := aliasMap{
loader: m.loader,
baseDir: m.baseDir,
}
item.SetDefaults()
if err := d.DecodeElement(&item, &start); err != nil {
return err
}
// Decode Groups data
for i := 0; i < len(item.Groups); i++ {
g := item.Groups[i]
if err := g.DecodeGroup((*Map)(&item)); err != nil {
return err
}
}
// Decode layers data
for i := 0; i < len(item.Layers); i++ {
l := item.Layers[i]
if err := l.DecodeLayer((*Map)(&item)); err != nil {
return err
}
}
// Decode object groups.
for _, g := range item.ObjectGroups {
if err := g.DecodeObjectGroup((*Map)(&item)); err != nil {
return err
}
}
*m = (Map)(item)
return nil
}