-
Notifications
You must be signed in to change notification settings - Fork 14
/
tileset.go
197 lines (162 loc) · 4.4 KB
/
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
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
package tilepix
import (
"encoding/xml"
"fmt"
"io"
"os"
"path/filepath"
"github.com/gopxl/pixel"
log "github.com/sirupsen/logrus"
)
/*
_____ _ _ _
|_ _(_) |___ ___ ___| |_
| | | | / -_|_-</ -_) _|
|_| |_|_\___/__/\___|\__|
*/
// Tileset is a TMX file structure which represents a Tiled Tileset
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"`
sprite *pixel.Sprite
picture pixel.Picture
// parentMap is the map which contains this object
parentMap *Map
// dir is the directory the tsx file is located in. This is used to access assets via a relative path.
dir string
}
func readTileset(r io.Reader, dir string) (*Tileset, error) {
log.Debug("readTileset: reading from io.Reader")
d := xml.NewDecoder(r)
var t Tileset
if err := d.Decode(&t); err != nil {
log.WithError(err).Error("readTileset: could not decode to Tileset")
return nil, err
}
t.dir = dir
return validate(t)
}
func readTilesetFile(filePath string) (*Tileset, error) {
log.WithField("Filepath", filePath).Debug("readTilesetFile: reading file")
f, err := os.Open(filePath)
if err != nil {
log.WithError(err).Error("ReadFile: could not open file")
return nil, err
}
defer f.Close()
dir := filepath.Dir(filePath)
return readTileset(f, dir)
}
// GenerateTileObjectLayer will create a new ObjectGroup for the mapping of Objects to individual tiles.
func (ts Tileset) GenerateTileObjectLayer(tileLayers []*TileLayer) ObjectGroup {
group := ObjectGroup{Name: fmt.Sprintf("%s-objectgroup", ts.Name)}
objs := ts.TileObjects()
// Loop all TileLayers in map.
for _, tl := range tileLayers {
// Loop all DecodedTiles in the TileLayer
for ind, t := range tl.DecodedTiles {
if t.Nil {
// Skip blank tiles.
continue
}
// Try get the Tiles' ObjectGroup.
og, ok := objs[t.ID]
if !ok {
// Not object groups for this Tile.
continue
}
// Loop all objects in the Tiles' ObjectGroup.
for _, obs := range og.Objects {
// Create a new Object based on the relative position of the Object and the DecodedTile.
o := *obs
tilePos := t.Position(ind, &ts)
o.X += tilePos.X
o.Y += tilePos.Y
group.Objects = append(group.Objects, &o)
}
}
}
return group
}
func validate(t Tileset) (*Tileset, error) {
if t.Columns < 1 {
return nil, fmt.Errorf("Tileset columns value not valid")
}
return &t, nil
}
func (ts *Tileset) String() string {
return fmt.Sprintf(
"TileSet{Name: %s, Tile size: %dx%d, Tile spacing: %d, Tilecount: %d, Properties: %v}",
ts.Name,
ts.TileWidth,
ts.TileHeight,
ts.Spacing,
ts.Tilecount,
ts.Properties,
)
}
func (ts *Tileset) setParent(m *Map) {
ts.parentMap = m
for _, p := range ts.Properties {
p.setParent(m)
}
for _, t := range ts.Tiles {
t.setParent(m)
}
if ts.Image != nil {
ts.Image.setParent(m)
}
}
func (ts *Tileset) setSprite() pixel.Picture {
if ts.sprite != nil {
// Return if sprite already set
return ts.picture
}
dir := ts.dir
if dir == "" {
dir = ts.parentMap.dir
}
sprite, pictureData, err := loadSpriteFromFile(filepath.Join(dir, ts.Image.Source))
if err != nil {
log.WithField("Filepath", filepath.Join(dir, ts.Image.Source)).WithError(err).Error("Tileset.setSprite: could not load sprite from file")
return nil
}
ts.sprite = sprite
ts.picture = pictureData
return ts.picture
}
// TileObjects will return all ObjectGroups contained in Tiles.
func (ts Tileset) TileObjects() map[ID]*ObjectGroup {
objs := make(map[ID]*ObjectGroup)
for _, t := range ts.Tiles {
if t.ObjectGroup != nil {
objs[t.ID] = t.ObjectGroup
}
}
return objs
}
func getTileset(l *TileLayer) (tileset *Tileset, isEmpty, usesMultipleTilesets bool) {
for _, tile := range l.DecodedTiles {
if !tile.Nil {
if tileset == nil {
tileset = tile.Tileset
} else if tileset != tile.Tileset {
return tileset, false, true
}
}
}
if tileset == nil {
return nil, true, false
}
return tileset, false, false
}