-
Notifications
You must be signed in to change notification settings - Fork 14
/
object.go
212 lines (175 loc) · 6.45 KB
/
object.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
package tilepix
import (
"fmt"
"github.com/gopxl/pixel"
log "github.com/sirupsen/logrus"
)
/*
___ _ _ _
/ _ \| |__ (_)___ __| |_
| (_) | '_ \| / -_) _| _|
\___/|_.__// \___\__|\__|
|__/
*/
// Object is a TMX file struture holding a specific Tiled object.
type Object struct {
Name string `xml:"name,attr"`
Type string `xml:"type,attr"`
X float64 `xml:"x,attr"`
Y float64 `xml:"y,attr"`
Width float64 `xml:"width,attr"`
Height float64 `xml:"height,attr"`
GID ID `xml:"gid,attr"`
ID ID `xml:"id,attr"`
Visible bool `xml:"visible,attr"`
Polygon *Polygon `xml:"polygon"`
PolyLine *PolyLine `xml:"polyline"`
Properties []*Property `xml:"properties>property"`
Ellipse *struct{} `xml:"ellipse"`
Point *struct{} `xml:"point"`
objectType ObjectType
tile *DecodedTile
// parentMap is the map which contains this object
parentMap *Map
}
// GetEllipse will return a pixel.Circle representation of this object relative to the map (the co-ordinates will match
// those as drawn in Tiled). If the object type is not `EllipseObj` this function will return `pixel.C(pixel.ZV, 0)`
// and an error.
//
// Because there is no pixel geometry code for irregular ellipses, this function will average the width and height of
// the ellipse object from the TMX file, and return a regular circle about the centre of the ellipse.
func (o *Object) GetEllipse() (pixel.Circle, error) {
if o.GetType() != EllipseObj {
log.WithError(ErrInvalidObjectType).WithField("Object type", o.GetType()).Error("Object.GetEllipse: object type mismatch")
return pixel.C(pixel.ZV, 0), ErrInvalidObjectType
}
// In TMX files, ellipses are defined by the containing rectangle. The X, Y positions are the bottom-left (after we
// have flipped them).
// Because Pixel does not support irregular ellipses, we take the average of width and height.
radius := (o.Width + o.Height) / 4
// The centre should be the same as the ellipses drawn in Tiled, this will make outputs more intuitive.
centre := pixel.V(o.X+(o.Width/2), o.Y+(o.Height/2))
return pixel.C(centre, radius), nil
}
// GetPoint will return a pixel.Vec representation of this object relative to the map (the co-ordinates will match those
// as drawn in Tiled). If the object type is not `PointObj` this function will return `pixel.ZV` and an error.
func (o *Object) GetPoint() (pixel.Vec, error) {
if o.GetType() != PointObj {
log.WithError(ErrInvalidObjectType).WithField("Object type", o.GetType()).Error("Object.GetPoint: object type mismatch")
return pixel.ZV, ErrInvalidObjectType
}
return pixel.V(o.X, o.Y), nil
}
// GetRect will return a pixel.Rect representation of this object relative to the map (the co-ordinates will match those
// as drawn in Tiled). If the object type is not `RectangleObj` this function will return `pixel.R(0, 0, 0, 0)` and an
// error.
func (o *Object) GetRect() (pixel.Rect, error) {
if o.GetType() != RectangleObj {
log.WithError(ErrInvalidObjectType).WithField("Object type", o.GetType()).Error("Object.GetRect: object type mismatch")
return pixel.R(0, 0, 0, 0), ErrInvalidObjectType
}
return pixel.R(o.X, o.Y, o.X+o.Width, o.Y+o.Height), nil
}
// GetPolygon will return a pixel.Vec slice representation of this object relative to the map (the co-ordinates will match
// those as drawn in Tiled). If the object type is not `PolygonObj` this function will return `nil` and an error.
func (o *Object) GetPolygon() ([]pixel.Vec, error) {
if o.GetType() != PolygonObj {
log.WithError(ErrInvalidObjectType).WithField("Object type", o.GetType()).Error("Object.GetPolygon: object type mismatch")
return nil, ErrInvalidObjectType
}
points, err := o.Polygon.Decode()
if err != nil {
log.WithError(err).Error("Object.GetPolygon: could not decode Polygon")
return nil, err
}
var pixelPoints []pixel.Vec
for _, p := range points {
pixelPoints = append(pixelPoints, p.V())
}
return pixelPoints, nil
}
// GetPolyLine will return a pixel.Vec slice representation of this object relative to the map (the co-ordinates will match
// those as drawn in Tiled). If the object type is not `PolylineObj` this function will return `nil` and an error.
func (o *Object) GetPolyLine() ([]pixel.Vec, error) {
if o.GetType() != PolylineObj {
log.WithError(ErrInvalidObjectType).WithField("Object type", o.GetType()).Error("Object.GetPolyLine: object type mismatch")
return nil, ErrInvalidObjectType
}
points, err := o.PolyLine.Decode()
if err != nil {
log.WithError(err).Error("Object.GetPolyLine: could not decode Polyline")
return nil, err
}
var pixelPoints []pixel.Vec
for _, p := range points {
pixelPoints = append(pixelPoints, p.V())
}
return pixelPoints, nil
}
// GetTile will return the object decoded into a DecodedTile struct. If this
// object is not a DecodedTile, this function will return `nil` and an error.
func (o *Object) GetTile() (*DecodedTile, error) {
if o.GetType() != TileObj {
log.WithError(ErrInvalidObjectType).WithField("Object type", o.GetType()).Error("Object.GetTile: object type mismatch")
return nil, ErrInvalidObjectType
}
if o.tile == nil {
// Setting tileset to the first tileset in the map. Will need updating when dealing with multiple
// tilesets.
ts := o.parentMap.Tilesets[0]
o.tile = &DecodedTile{
ID: o.GID,
Tileset: ts,
parentMap: o.parentMap,
}
numRows := ts.Tilecount / ts.Columns
o.tile.setSprite(ts.Columns, numRows, ts)
}
return o.tile, nil
}
// GetType will return the ObjectType constant type of this object.
func (o *Object) GetType() ObjectType {
return o.objectType
}
func (o *Object) String() string {
return fmt.Sprintf("Object{%s, Name: '%s'}", o.objectType, o.Name)
}
func (o *Object) flipY() {
o.Y = o.parentMap.pixelHeight() - o.Y - o.Height
}
// hydrateType will work out what type this object is.
func (o *Object) hydrateType() {
if o.Polygon != nil {
o.objectType = PolygonObj
return
}
if o.PolyLine != nil {
o.objectType = PolylineObj
return
}
if o.Ellipse != nil {
o.objectType = EllipseObj
return
}
if o.Point != nil {
o.objectType = PointObj
return
}
if o.GID != 0 {
o.objectType = TileObj
return
}
o.objectType = RectangleObj
}
func (o *Object) setParent(m *Map) {
o.parentMap = m
if o.Polygon != nil {
o.Polygon.setParent(m)
}
if o.PolyLine != nil {
o.PolyLine.setParent(m)
}
for _, p := range o.Properties {
p.setParent(m)
}
}