-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtilelayer.go
214 lines (172 loc) · 5.94 KB
/
tilelayer.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
package tilepix
import (
"errors"
"fmt"
"github.com/gopxl/pixel"
log "github.com/sirupsen/logrus"
)
/*
_____ _ _ _
|_ _(_) |___| | __ _ _ _ ___ _ _
| | | | / -_) |__/ _` | || / -_) '_|
|_| |_|_\___|____\__,_|\_, \___|_|
|__/
*/
// TileLayer is a TMX file structure which can hold any type of Tiled layer.
type TileLayer struct {
Name string `xml:"name,attr"`
Opacity float32 `xml:"opacity,attr"`
OffSetX float64 `xml:"offsetx,attr"`
OffSetY float64 `xml:"offsety,attr"`
Visible bool `xml:"visible,attr"`
Properties []*Property `xml:"properties>property"`
Data Data `xml:"data"`
// DecodedTiles is the attribute you should use instead of `Data`.
// Tile entry at (x,y) is obtained using l.DecodedTiles[y*map.Width+x].
DecodedTiles []*DecodedTile
// Tileset is only set when the layer uses a single tileset and NilLayer is false.
Tileset *Tileset
// Empty should be set when all entries of the layer are NilTile.
Empty bool
batch *pixel.Batch
isDirty bool
static bool
// parentMap is the map which contains this object
parentMap *Map
}
// Batch returns the batch with the picture data from the tileset associated with this layer.
func (l *TileLayer) Batch() (*pixel.Batch, error) {
if l.batch == nil {
log.Debug("TileLayer.Batch: batch not initialised, creating")
if l.Tileset == nil {
err := errors.New("cannot create sprite from nil tileset")
log.WithError(err).Error("TileLayer.Batch: layers' tileset is nil")
return nil, err
}
pictureData := l.Tileset.setSprite()
l.batch = pixel.NewBatch(&pixel.TrianglesData{}, pictureData)
}
l.batch.Clear()
return l.batch, nil
}
// Draw will use the TileLayers' batch to draw all tiles within the TileLayer to the target.
func (l *TileLayer) Draw(target pixel.Target) error {
// Only draw if the layer is dirty.
if l.isDirty {
// Initialise the batch
if _, err := l.Batch(); err != nil {
log.WithError(err).Error("TileLayer.Draw: could not get batch")
return err
}
ts := l.Tileset
numRows := ts.Tilecount / ts.Columns
// Loop through each decoded tile
for tileIndex, tile := range l.DecodedTiles {
// The Y component of the offset is set in Tiled from top down, setting here to negative because we want
// from the bottom up.
layerOffset := pixel.V(l.OffSetX, -l.OffSetY)
tile.Draw(tileIndex, ts.Columns, numRows, ts, l.batch, layerOffset)
}
// Batch is drawn to, layer is no longer dirty.
l.SetDirty(false)
}
l.batch.Draw(target)
// Reset the dirty flag if the layer is not static
if !l.static {
l.SetDirty(true)
}
return nil
}
// SetDirty will update the TileLayers' `dirty` property. If true, this will cause the TileLayers' batch be cleared and
// re-drawn next time `TileLayer.Draw` is called.
func (l *TileLayer) SetDirty(newVal bool) {
log.WithField("Dirty", newVal).Trace("TileLayer.SetDirty: setting dirty property")
l.isDirty = newVal
}
// SetStatic will update the TileLayers' `static` property. If false, this will set the dirty property to true each
// time after `TileLayer.Draw` is called, so that the layer is drawn everytime.
func (l *TileLayer) SetStatic(newVal bool) {
log.WithField("Static", newVal).Debug("TileLayer.SetStatic: setting static property")
l.static = newVal
}
func (l *TileLayer) String() string {
return fmt.Sprintf("TileLayer{Name: '%s', Properties: %v, TileCount: %d}", l.Name, l.Properties, len(l.DecodedTiles))
}
func (l *TileLayer) decode(width, height int) ([]GID, error) {
log.WithField("Encoding", l.Data.Encoding).Debug("TileLayer.decode: determining encoding")
l.SetStatic(true)
l.SetDirty(true)
if l.Tileset != nil {
l.Tileset.setSprite()
}
switch l.Data.Encoding {
case "csv":
return l.decodeLayerCSV(width, height)
case "base64":
return l.decodeLayerBase64(width, height)
case "":
// XML "encoding"
return l.decodeLayerXML(width, height)
}
log.WithError(ErrUnknownEncoding).Error("TileLayer.decode: unrecognised encoding")
return nil, ErrUnknownEncoding
}
func (l *TileLayer) decodeLayerXML(width, height int) ([]GID, error) {
if len(l.Data.DataTiles) != width*height {
log.WithError(ErrInvalidDecodedDataLen).WithFields(log.Fields{"Length datatiles": len(l.Data.DataTiles), "W*H": width * height}).Error("TileLayer.decodeLayerXML: data length mismatch")
return nil, ErrInvalidDecodedDataLen
}
gids := make([]GID, len(l.Data.DataTiles))
for i := 0; i < len(gids); i++ {
gids[i] = l.Data.DataTiles[i].GID
}
return gids, nil
}
func (l *TileLayer) decodeLayerCSV(width, height int) ([]GID, error) {
gids, err := l.Data.decodeCSV()
if err != nil {
log.WithError(err).Error("TileLayer.decodeLayerCSV: could not decode CSV")
return nil, err
}
if len(gids) != width*height {
log.WithError(ErrInvalidDecodedDataLen).WithFields(log.Fields{"Length GIDSs": len(gids), "W*H": width * height}).Error("TileLayer.decodeLayerCSV: data length mismatch")
return nil, ErrInvalidDecodedDataLen
}
return gids, nil
}
func (l *TileLayer) decodeLayerBase64(width, height int) ([]GID, error) {
dataBytes, err := l.Data.decodeBase64()
if err != nil {
log.WithError(err).Error("TileLayer.decodeLayerBase64: could not decode base64")
return nil, err
}
if len(dataBytes) != width*height*4 {
log.WithError(ErrInvalidDecodedDataLen).WithFields(log.Fields{"Length databytes": len(dataBytes), "W*H": width * height}).Error("TileLayer.decodeLayerBase64: data length mismatch")
return nil, ErrInvalidDecodedDataLen
}
gids := make([]GID, width*height)
j := 0
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
gid := GID(dataBytes[j]) +
GID(dataBytes[j+1])<<8 +
GID(dataBytes[j+2])<<16 +
GID(dataBytes[j+3])<<24
j += 4
gids[y*width+x] = gid
}
}
return gids, nil
}
func (l *TileLayer) setParent(m *Map) {
l.parentMap = m
for _, p := range l.Properties {
p.setParent(m)
}
for _, dt := range l.DecodedTiles {
dt.setParent(m)
}
if l.Tileset != nil {
l.Tileset.setParent(m)
}
}