-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtmxmap.go
356 lines (313 loc) · 8.24 KB
/
tmxmap.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package tmxmap
import (
"bytes"
"compress/gzip"
"compress/zlib"
"encoding/base64"
"encoding/xml"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
)
const (
horizontalFlip = 0x80000000
verticalFlip = 0x40000000
diagonalFlip = 0x20000000
)
var NilTile = &TileInfo{Nil: true}
type GID uint32
// Map represents the TMX Map Format https://doc.mapeditor.org/en/stable/reference/tmx-map-format/
type Map struct {
Version string `xml:"version,attr"`
TiledVersion string `xml:"tiledversion,attr"`
Orientation string `xml:"orientation,attr"`
RenderOrder string `xml:"renderorder,attr"`
Width int `xml:"width,attr"`
Height int `xml:"height,attr"`
TileWidth int `xml:"tilewidth,attr"`
TileHeight int `xml:"tileheight,attr"`
HexSideLength int `xml:"hexsidelength,attr"`
StaggerAxis int `xml:"staggeraxis,attr"`
StaggerIndex int `xml:"staggerindex,attr"`
BackgroundColor string `xml:"backgroundcolor,attr"`
NextLayerID int `xml:"nextlayerid,attr"`
NextObjectID int `xml:"nextobjectid,attr"`
Properties []Property `xml:"properties>property"`
TileSets []TileSet `xml:"tileset"`
Layers []Layer `xml:"layer"`
ObjectGroups []ObjectGroup `xml:"objectgroup"`
}
type Property struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
type TileSet struct {
FirstGID GID `xml:"firstgid,attr"`
Source string `xml:"source,attr"`
Name string `xml:"name,attr"`
TileWidth int `xml:"tilewidth,attr"`
TileHeight int `xml:"tileheight,attr"`
Spacing int `xml:"spacing,attr"`
Margin int `xml:"margin,attr"`
Properties []Property `xml:"properties>property"`
Image *Image `xml:"image"`
Tiles []Tile `xml:"tile"`
Tilecount int `xml:"tilecount,attr"`
Columns int `xml:"columns,attr"`
}
type Image struct {
Source string `xml:"source,attr"`
Trans string `xml:"trans,attr"`
Width int `xml:"width,attr"`
Height int `xml:"height,attr"`
Image image.Image
}
type Tile struct {
ID GID `xml:"id,attr"`
Image Image `xml:"image"`
}
type TileInfo struct {
ID GID
TileSet *TileSet
HorizontalFlip bool
VerticalFlip bool
DiagonalFlip bool
Nil bool
}
type Layer struct {
ID int `xml:"id,attr"`
Name string `xml:"name,attr"`
X int `xml:"x,attr"`
Y int `xml:"y,attr"`
Width int `xml:"width,attr"`
Height int `xml:"height,attr"`
Opacity float32 `xml:"opacity,attr"`
Visible bool `xml:"visible,attr"`
OffsetX int `xml:"offsetx,attr"`
OffsetY int `xml:"offsety,attr"`
Properties []Property `xml:"properties>property"`
Data Data `xml:"data"`
Tiles []*TileInfo
}
type Data struct {
Encoding string `xml:"encoding,attr"`
Compression string `xml:"compression,attr"`
RawData []byte `xml:",innerxml"`
DataTiles []DataTile `xml:"tile"`
Chunk []Chunk `xml:"chunk"`
}
type DataTile struct {
GID GID `xml:"gid,attr"`
}
type Chunk struct {
X int `xml:"x,attr"`
Y int `xml:"y,attr"`
Width int `xml:"width,attr"`
Height int `xml:"height,attr"`
DataTiles []DataTile `xml:"tile"`
}
type ObjectGroup struct {
Name string `xml:"name,attr"`
Color string `xml:"color,attr"`
Opacity float32 `xml:"opacity,attr"`
Visible bool `xml:"visible,attr"`
Properties []Property `xml:"properties>property"`
Objects []Object `xml:"object"`
}
type Object struct {
Name string `xml:"name,attr"`
Type string `xml:"type,attr"`
X int `xml:"x,attr"`
Y int `xml:"y,attr"`
Width int `xml:"width,attr"`
Height int `xml:"height,attr"`
GID int `xml:"gid,attr"`
Visible bool `xml:"visible,attr"`
Properties []Property `xml:"properties>property"`
Polygons []Polygon `xml:"polygon"`
PolyLines []PolyLine `xml:"polyline"`
}
type Polygon struct {
Points string `xml:"points,attr"`
}
type PolyLine struct {
Points string `xml:"points,attr"`
}
func (l *Layer) decodeXML() ([]GID, error) {
gids := make([]GID, l.Width*l.Height)
for i := 0; i < len(gids); i++ {
gids[i] = l.Data.DataTiles[i].GID
}
return gids, nil
}
func (l *Layer) decodeBase64() ([]GID, error) {
sanitized := bytes.TrimSpace(l.Data.RawData)
decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewReader(sanitized))
var reader io.Reader
var err error
switch l.Data.Compression {
case "":
reader = decoder
case "gzip":
reader, err = gzip.NewReader(decoder)
if err != nil {
return nil, err
}
case "zlib":
reader, err = zlib.NewReader(decoder)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported compression: %s", l.Data.Compression)
}
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
gids := make([]GID, l.Width*l.Height)
for i := 0; i < len(data)/4; i++ {
gids[i] = GID(data[i*4]) +
GID(data[i*4+1])<<8 +
GID(data[i*4+2])<<16 +
GID(data[i*4+3])<<24
}
return gids, nil
}
func (l *Layer) decodeCSV() ([]GID, error) {
sanitized := strings.Map(func(r rune) rune {
if (r >= '0' && r <= '9') || r == ',' {
return r
}
return -1
}, string(l.Data.RawData))
tokens := strings.Split(sanitized, ",")
gids := make([]GID, l.Width*l.Height)
for i, token := range tokens {
gid, err := strconv.Atoi(token)
if err != nil {
return nil, err
}
gids[i] = GID(gid)
}
return gids, nil
}
func (l *Layer) decode() ([]GID, error) {
switch l.Data.Encoding {
case "":
return l.decodeXML()
case "base64":
return l.decodeBase64()
case "csv":
return l.decodeCSV()
}
return nil, fmt.Errorf("unsupported encoding: %s", l.Data.Encoding)
}
func (i *Image) decode(baseDir string) error {
file, err := os.Open(filepath.Join(baseDir, i.Source))
if err != nil {
return err
}
defer file.Close()
i.Image, _, err = image.Decode(file)
if err != nil {
return err
}
return nil
}
func (ts *TileSet) decode(baseDir string) error {
if ts.Source == "" {
return nil
}
file, err := os.Open(filepath.Join(baseDir, ts.Source))
if err != nil {
return err
}
defer file.Close()
decoder := xml.NewDecoder(file)
if err := decoder.Decode(ts); err != nil {
return err
}
return nil
}
func (m *Map) decodeGID(gid GID) (*TileInfo, error) {
if gid == 0 {
return NilTile, nil
}
clearGID := gid &^ (horizontalFlip | verticalFlip | diagonalFlip)
for i := len(m.TileSets) - 1; i >= 0; i-- {
if m.TileSets[i].FirstGID <= clearGID {
return &TileInfo{
ID: clearGID - m.TileSets[i].FirstGID,
TileSet: &m.TileSets[i],
HorizontalFlip: gid&horizontalFlip != 0,
VerticalFlip: gid&verticalFlip != 0,
DiagonalFlip: gid&diagonalFlip != 0,
Nil: gid == 0,
}, nil
}
}
return nil, fmt.Errorf("invalid tile GID: %d\n", gid)
}
func (m *Map) decode(baseDir string) error {
for i := range m.TileSets {
if err := m.TileSets[i].decode(baseDir); err != nil {
return err
}
if err := m.TileSets[i].Image.decode(baseDir); err != nil {
return err
}
}
return nil
}
// Load
func Load(name string) (*Map, error) {
file, err := os.Open(name)
if err != nil {
return nil, err
}
defer file.Close()
tmx, err := Decode(file)
if err != nil {
return nil, err
}
baseDir, err := filepath.Abs(filepath.Dir(name))
if err != nil {
return nil, err
}
if err := tmx.decode(baseDir); err != nil {
return nil, err
}
return tmx, nil
}
// Load
func Decode(tileMap io.Reader) (*Map, error) {
tmx := &Map{}
decoder := xml.NewDecoder(tileMap)
if err := decoder.Decode(tmx); err != nil {
return nil, err
}
for i := range tmx.Layers {
layer := &tmx.Layers[i]
gids, err := layer.decode()
if err != nil {
return nil, err
}
layer.Tiles = make([]*TileInfo, len(gids))
for j := 0; j < len(layer.Tiles); j++ {
layer.Tiles[j], err = tmx.decodeGID(gids[j])
if err != nil {
return nil, err
}
}
}
return tmx, nil
}