-
Notifications
You must be signed in to change notification settings - Fork 14
/
polygon.go
62 lines (48 loc) · 1.26 KB
/
polygon.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
package tilepix
import (
"fmt"
log "github.com/sirupsen/logrus"
)
/*
___ _
| _ \___| |_ _ __ _ ___ _ _
| _/ _ \ | || / _` / _ \ ' \
|_| \___/_|\_, \__, \___/_||_|
|__/|___/
*/
// Polygon is a TMX file structure representing a Tiled Polygon.
type Polygon struct {
Points string `xml:"points,attr"`
decodedPoints []*Point
// parentMap is the map which contains this object
parentMap *Map
}
// Decode will return a slice of points which make up this polygon.
func (p *Polygon) Decode() ([]*Point, error) {
if p.decodedPoints == nil {
dp, err := decodePoints(p.Points)
if err != nil {
log.WithError(err).Error("Polygon.Decode: could not decode points")
return nil, err
}
p.decodedPoints = dp
}
return p.decodedPoints, nil
}
func (p *Polygon) setParent(m *Map) {
p.parentMap = m
// Must decode points before they can be set
_, err := p.Decode()
if err != nil {
log.WithError(err).Error("Polygon.setParent: could not decode points")
return
}
for _, dp := range p.decodedPoints {
dp.setParent(m)
// We have to flip the Y co-ordinate here because the `tilepix.Point` is only used to provide `pixel.Vec`s
dp.flipY()
}
}
func (p *Polygon) String() string {
return fmt.Sprintf("Polygon{Points: %v}", p.decodedPoints)
}