forked from EngoEngine/engo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmx_level.go
372 lines (318 loc) · 10.8 KB
/
tmx_level.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package common
import (
"bytes"
"compress/zlib"
"encoding/base64"
"encoding/binary"
"encoding/xml"
"fmt"
"io"
"path"
"sort"
"strconv"
"strings"
"engo.io/engo"
)
// TMXTilesetSrc is just used to create levelTileset->Image
type TMXTilesetSrc struct {
// Source holds the URI of the tileset image
Source string `xml:"source,attr"`
// Width of each tile in the tileset image
Width int `xml:"width,attr"`
// Height of each tile in the tileset image
Height int `xml:"height,attr"`
}
// TMXTileset contains the tileset resource parsed from the TileMap XML
type TMXTileset struct {
// Firstgid is the first assigned gid of the tileset
Firstgid int `xml:"firstgid,attr"`
// Name of the tileset in Tiled
Name string `xml:"name,attr"`
// TileWidth defines the width of each tile
TileWidth int `xml:"tilewidth,attr"`
// TileHeight defines the height of each tile
TileHeight int `xml:"tileheight,attr"`
// TileCount holds the total tile count in this tileset
TileCount int `xml:"tilecount,attr"`
// ImageSrc contains the TMXTilesetSrc which defines the tileset image
ImageSrc TMXTilesetSrc `xml:"image"`
// Image holds the reference of the tileset's TextureResource
Image *TextureResource
}
// TMXTileLayer represents a tile layer parsed from the TileMap XML
type TMXTileLayer struct {
// Name of the tile layer in Tiled
Name string `xml:"name,attr"`
// Width is the integer width of each tile in this layer
Width int `xml:"width,attr"`
// Height is the integer height of each tile in this layer
Height int `xml:"height,attr"`
// TileMapping contains the generated tilemapping list
TileMapping []uint32
// CompData is a temporary list used to fill TileMapping
CompData []byte `xml:"data"`
}
// TMXImageLayer represents an image layer parsed from the TileMap XML
type TMXImageLayer struct {
// Name of the image layer in Tiled
Name string `xml:"name,attr"`
// X holds the defined X coordinate in Tiled
X float64 `xml:"x,attr"`
// Y holds the defined Y coordinate in Tiled
Y float64 `xml:"y,attr"`
// ImageSrc contains the TMXImageSrc which defines the image filename
ImageSrc TMXImageSrc `xml:"image"`
}
// TMXObjectLayer following the Object Layer naming convention in Tiled
type TMXObjectLayer struct {
// Name of the object layer in Tiled
Name string `xml:"name,attr"`
// Objects contains the list all objects in this layer
Objects []TMXObject `xml:"object"`
// OffSetX is the parsed X offset for the object layer
OffSetX float32 `xml:"offsetx"`
// OffSetY is the parsed Y offset for the object layer
OffSetY float32 `xml:"offsety"`
//TODO add all object attr available in Tiled
// 'visible' attr only appears in XML if false --> default 1
// 'opacity' attr only appears in XML if < 1 --> default 1
// Visible int `xml:"visible,attr"`
// Opacity float32 `xml:"visible,attr"`
}
// TMXObject represents a TMX object with all default Tiled attributes
type TMXObject struct {
// Id is the unique ID of each object defined by Tiled
Id int `xml:"id,attr"`
// Name defines the name of the object given in Tiled
Name string `xml:"name,attr"`
// Type contains the string type which was given in Tiled
Type string `xml:"type,attr"`
// X holds the X float64 coordinate of the object in the map
X float64 `xml:"x,attr"`
// Y holds the Y float64 coordinate of the object in the map
Y float64 `xml:"y,attr"`
// Width is the integer width of the object
Width int `xml:"width,attr"`
// Height is the integer height of the object
Height int `xml:"height,attr"`
// Polyline contains the TMXPolyline object if the parsed object has a polyline points string
Polyline TMXPolyline `xml:"polyline"`
}
// TMXPolyline represents a TMX Polyline object with its Points values
type TMXPolyline struct {
// Points contains the original, unaltered points string from the TMZ XML
Points string `xml:"points,attr"`
}
// TMXImageSrc represents the actual image source of an image layer
type TMXImageSrc struct {
// Source holds the URI of the image
Source string `xml:"source,attr"`
// Width is the integer width of the image
Width int `xml:"width,attr"`
// Height is the integer height of the image
Height int `xml:"height,attr"`
}
// TMXLevel containing all layers and default Tiled attributes
type TMXLevel struct {
// Orientation is the parsed level orientation from the TMX XML, like orthogonal
Orientation string `xml:"orientation,attr"`
// RenderOrder is the in Tiled specified TileMap render order, like right-down
RenderOrder string `xml:"renderorder,attr"`
// Width is the integer width of the parsed level
Width int `xml:"width,attr"`
// Height is the integer height of the parsed level
Height int `xml:"height,attr"`
// TileWidth defines the width of each tile in the level
TileWidth int `xml:"tilewidth,attr"`
// TileHeight defines the height of each tile in the level
TileHeight int `xml:"tileheight,attr"`
// NextObjectId is the next free Object ID defined by Tiled
NextObjectId int `xml:"nextobjectid,attr"`
// Tilesets conatins a list of all parsed TMXTileset objects
Tilesets []TMXTileset `xml:"tileset"`
// TileLayers conatins a list of all parsed TMXTileLayer objects
TileLayers []TMXTileLayer `xml:"layer"`
// ImageLayers conatins a list of all parsed TMXImageLayer objects
ImageLayers []TMXImageLayer `xml:"imagelayer"`
// ObjectLayers conatins a list of all parsed TMXObjectLayer objects
ObjectLayers []TMXObjectLayer `xml:"objectgroup"`
}
type ByFirstgid []TMXTileset
// Len returns the length of t
func (t ByFirstgid) Len() int { return len(t) }
// Swap exchanges t's elements i and j
func (t ByFirstgid) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
// Less returns if t's i Firstgid is less than t's j
func (t ByFirstgid) Less(i, j int) bool { return t[i].Firstgid < t[j].Firstgid }
// MUST BE base64 ENCODED and COMPRESSED WITH zlib!
func createLevelFromTmx(tmxBytes []byte, tmxUrl string) (*Level, error) {
tmxLevel := &TMXLevel{}
level := &Level{}
if err := xml.Unmarshal(tmxBytes, &tmxLevel); err != nil {
return nil, err
}
// Extract the tile mappings from the compressed data at each layer
for i := range tmxLevel.TileLayers {
layer := &tmxLevel.TileLayers[i]
// Trim leading/trailing whitespace ( inneficient )
layer.CompData = []byte(strings.TrimSpace(string(layer.CompData)))
// Decode it out of base64
if _, err := base64.StdEncoding.Decode(layer.CompData, layer.CompData); err != nil {
return nil, err
}
// Decompress
b := bytes.NewReader(layer.CompData)
zlr, err := zlib.NewReader(b)
if err != nil {
return nil, err
}
defer zlr.Close()
tm := make([]uint32, 0)
var nextInt uint32
for {
err = binary.Read(zlr, binary.LittleEndian, &nextInt)
if err != nil {
// EOF or unexpected EOF error
if err == io.EOF {
break
}
return nil, err
}
tm = append(tm, nextInt)
}
layer.TileMapping = tm
}
// Load in the images needed for the tilesets
for i, ts := range tmxLevel.Tilesets {
url := path.Join(path.Dir(tmxUrl), ts.ImageSrc.Source)
if err := engo.Files.Load(url); err != nil {
return nil, err
}
image, err := engo.Files.Resource(url)
if err != nil {
return nil, err
}
texResource, ok := image.(TextureResource)
if !ok {
return nil, fmt.Errorf("resource is not of type 'TextureResource': %q", url)
}
ts.Image = &texResource
tmxLevel.Tilesets[i] = ts
}
level.width = tmxLevel.Width
level.height = tmxLevel.Height
level.TileWidth = tmxLevel.TileWidth
level.TileHeight = tmxLevel.TileHeight
level.Orientation = tmxLevel.Orientation
level.RenderOrder = tmxLevel.RenderOrder
level.NextObjectId = tmxLevel.NextObjectId
// get the tilesheets in order and in generic format
sort.Sort(ByFirstgid(tmxLevel.Tilesets))
ts := make([]*tilesheet, len(tmxLevel.Tilesets))
for i, tts := range tmxLevel.Tilesets {
ts[i] = &tilesheet{tts.Image, tts.Firstgid}
}
levelTileset := createTileset(level, ts)
levelTileLayers := make([]*layer, len(tmxLevel.TileLayers))
for i, tileLayer := range tmxLevel.TileLayers {
levelTileLayers[i] = &layer{
tileLayer.Name,
tileLayer.Width,
tileLayer.Height,
tileLayer.TileMapping,
}
}
// create tile layers with tiles
level.TileLayers = createLevelTiles(level, levelTileLayers, levelTileset)
// create object layers
for _, objectLayer := range tmxLevel.ObjectLayers {
levelObjectLayer := &ObjectLayer{
Name: objectLayer.Name,
OffSetX: objectLayer.OffSetX,
OffSetY: objectLayer.OffSetY,
}
// check all objects in layer
for _, tmxObject := range objectLayer.Objects {
// check if object is a Polyline object
if tmxObject.Polyline.Points != "" {
points := tmxObject.Polyline.Points
polylineObject := &PolylineObject{
Id: tmxObject.Id,
Name: tmxObject.Name,
Type: tmxObject.Type,
X: tmxObject.X,
Y: tmxObject.Y,
Points: points,
}
polylineObject.LineBounds =
append(
polylineObject.LineBounds,
pointStringToLines(points, tmxObject.X, tmxObject.Y)...,
)
levelObjectLayer.PolyObjects =
append(levelObjectLayer.PolyObjects, polylineObject)
} else {
// non-Polyline object
object := &Object{
tmxObject.Id,
tmxObject.Name,
tmxObject.Type,
tmxObject.X,
tmxObject.Y,
tmxObject.Width,
tmxObject.Height,
}
levelObjectLayer.Objects = append(levelObjectLayer.Objects, object)
}
}
level.ObjectLayers = append(level.ObjectLayers, levelObjectLayer)
}
// One image per image layer
for _, tmxImageLayer := range tmxLevel.ImageLayers {
imageLayer := &ImageLayer{
Name: tmxImageLayer.Name,
Width: tmxImageLayer.ImageSrc.Width,
Height: tmxImageLayer.ImageSrc.Height,
Source: tmxImageLayer.ImageSrc.Source,
}
url := path.Base(tmxImageLayer.ImageSrc.Source)
if err := engo.Files.Load(url); err != nil {
return nil, err
}
curImg, err := LoadedSprite(url)
if err != nil {
return nil, err
}
// create image tile
imageTile := &tile{
engo.Point{float32(tmxImageLayer.X), float32(tmxImageLayer.Y)},
curImg,
}
imageLayer.Images = append(imageLayer.Images, imageTile)
level.ImageLayers = append(level.ImageLayers, imageLayer)
}
return level, nil
}
func pointStringToLines(str string, xOff, yOff float64) []*engo.Line {
pts := strings.Split(str, " ")
floatPts := make([][]float64, len(pts))
for i, x := range pts {
pt := strings.Split(x, ",")
floatPts[i] = make([]float64, 2)
floatPts[i][0], _ = strconv.ParseFloat(pt[0], 64)
floatPts[i][1], _ = strconv.ParseFloat(pt[1], 64)
}
lines := make([]*engo.Line, len(floatPts)-1)
// Now to globalize line coordinates
for i := 0; i < len(floatPts)-1; i++ {
x1 := float32(floatPts[i][0] + xOff)
y1 := float32(floatPts[i][1] + yOff)
x2 := float32(floatPts[i+1][0] + xOff)
y2 := float32(floatPts[i+1][1] + yOff)
p1 := engo.Point{x1, y1}
p2 := engo.Point{x2, y2}
newLine := &engo.Line{p1, p2}
lines[i] = newLine
}
return lines
}