-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtmx.go
222 lines (207 loc) · 5.21 KB
/
tmx.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
// Package tmx implements access to Tiled's tmx (Tile Map XML) files.
//
// Documentation and specification of the TMX file format has been based on the
// information available at:
// https://github.com/bjorn/tiled/wiki/TMX-Map-Format
package tmx
import (
"compress/gzip"
"compress/zlib"
"encoding/base64"
"encoding/binary"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
)
// Open reads the provided tmx file and returns a parsed Map, based on the TMX
// file format.
func Open(tmxPath string) (m *Map, err error) {
fr, err := os.Open(tmxPath)
if err != nil {
return nil, err
}
defer fr.Close()
return NewFile(fr)
}
// NewFile reads from the provided io.Reader and returns a parsed Map, based on
// the TMX file format.
func NewFile(r io.Reader) (m *Map, err error) {
d := xml.NewDecoder(r)
m = new(Map)
err = d.Decode(m)
if err != nil {
return nil, err
}
for _, l := range m.Layers {
err = l.Data.decode(m.Width, m.Height)
if err != nil {
return nil, err
}
}
return m, nil
}
// decode decodes the GIDs that are stored in the <data> XML-tag of a layer. It
// will handle the various encodings and compression methods.
func (data *Data) decode(cols, rows int) (err error) {
if data.gids != nil {
// data has already been decoded.
return nil
}
// alloc
data.gids = make([][]GID, cols)
for i := range data.gids {
data.gids[i] = make([]GID, rows)
}
// decode
switch data.Encoding {
case "base64":
err = data.decodeBase64(cols, rows)
if err != nil {
return err
}
case "csv":
err = data.decodeCsv(cols, rows)
if err != nil {
return err
}
case "": // XML encoding
err = data.decodeXML(cols, rows)
if err != nil {
return err
}
default:
return fmt.Errorf("decodeData: encoding '%s' not yet implemented.", data.Encoding)
}
return nil
}
// decodeBase64 decodes the GIDs that are stored as a base64-encoded array of
// unsigned 32-bit integers, using little-endian byte ordering. This array may
// be compressed using gzip or zlib.
func (data *Data) decodeBase64(cols, rows int) (err error) {
s := strings.TrimSpace(data.RawData)
r := base64.NewDecoder(base64.StdEncoding, strings.NewReader(s))
switch data.Compression {
case "gzip":
z, err := gzip.NewReader(r)
if err != nil {
return err
}
defer z.Close()
r = z
case "zlib":
z, err := zlib.NewReader(r)
if err != nil {
return err
}
defer z.Close()
r = z
case "": // no compression.
break
default:
return fmt.Errorf("decodeBase64: compression '%s' not yet implemented.", data.Compression)
}
buf, err := ioutil.ReadAll(r)
// We should have one GID for each tile.
if len(buf)/4 != cols*rows {
return fmt.Errorf("decodeBase64: wrong number of GIDs. Got %d, wanted %d.", len(buf)/4, cols*rows)
}
i := 0
for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
gid := binary.LittleEndian.Uint32(buf[i*4:])
data.gids[col][row] = GID(gid)
i++
}
}
return nil
}
// decodeCvs decodes the GIDs that are stored as comma-separated values.
func (data *Data) decodeCsv(cols, rows int) (err error) {
cleanData := strings.Map(clean, data.RawData)
rawGIDs := strings.Split(cleanData, ",")
// We should have one GID for each tile.
if len(rawGIDs) != cols*rows {
return fmt.Errorf("decodeCsv: wrong number of GIDs. Got %d, wanted %d.", len(rawGIDs), cols*rows)
}
i := 0
for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
gid, err := strconv.Atoi(rawGIDs[i])
if err != nil {
return err
}
data.gids[col][row] = GID(gid)
i++
}
}
return nil
}
// clean cleans the csv data from superfluous runes.
func clean(r rune) rune {
if r >= '0' && r <= '9' || r == ',' {
return r
}
// skip rune.
return -1
}
// decodeXML decodes the GIDs that are stored in the <tile> XML-tags' 'gid'
// attribute.
func (data *Data) decodeXML(cols, rows int) (err error) {
if len(data.Tiles) != cols*rows {
return fmt.Errorf("decodeXML: wrong number of GIDs. Got %d, wanted %d.", len(data.Tiles), cols*rows)
}
i := 0
for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
data.gids[col][row] = data.Tiles[i].GID
i++
}
}
return nil
}
// GetGID returns the global tile ID at a given coordinate, after clearing the
// flip flags.
func (l *Layer) GetGID(col, row int) int {
return l.Data.gids[col][row].GlobalTileID()
}
// GetRawGID returns the global tile ID at a given coordinate, without clearing
// the flip flags.
func (l *Layer) GetRawGID(col, row int) GID {
return l.Data.gids[col][row]
}
// GlobalTileID returns the GID after clearing the flip flags.
func (gid GID) GlobalTileID() int {
return int(gid &^ FlagFlip)
}
// IsDiagonalFlip returns true if the GID is flipped diagonally.
func (gid GID) IsDiagonalFlip() bool {
if gid&FlagDiagonalFlip != 0 {
return true
}
return false
}
// IsVerticalFlip returns true if the GID is flipped vertically.
func (gid GID) IsVerticalFlip() bool {
if gid&FlagVerticalFlip != 0 {
return true
}
return false
}
// IsHorizontalFlip returns true if the GID is flipped horizontally.
func (gid GID) IsHorizontalFlip() bool {
if gid&FlagHorizontalFlip != 0 {
return true
}
return false
}
// IsFlip returns true if the GID is flipped.
func (gid GID) IsFlip() bool {
if gid&FlagFlip != 0 {
return true
}
return false
}