-
Notifications
You must be signed in to change notification settings - Fork 14
/
point.go
77 lines (61 loc) · 1.77 KB
/
point.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
package tilepix
import (
"fmt"
"strconv"
"strings"
"github.com/gopxl/pixel"
log "github.com/sirupsen/logrus"
)
/*
___ _ _
| _ \___(_)_ _| |_
| _/ _ \ | ' \ _|
|_| \___/_|_||_\__|
*/
// Point is a TMX file structure holding a Tiled Point object.
type Point struct {
X int
Y int
// parentMap is the map which contains this object
parentMap *Map
}
func (p *Point) String() string {
return fmt.Sprintf("Point{%d, %d}", p.X, p.Y)
}
// V converts the Tiled Point to a Pixel Vector.
func (p *Point) V() pixel.Vec {
return pixel.V(float64(p.X), float64(p.Y))
}
func (p *Point) setParent(m *Map) {
p.parentMap = m
}
func decodePoints(s string) ([]*Point, error) {
pointStrings := strings.Split(s, " ")
var points []*Point
var err error
for _, pointString := range pointStrings {
coordStrings := strings.Split(pointString, ",")
if len(coordStrings) != 2 {
log.WithError(ErrInvalidPointsField).WithField("Co-ordinate strings", coordStrings).Error("decodePoints: mismatch co-ordinates string length")
return nil, ErrInvalidPointsField
}
point := &Point{}
point.X, err = strconv.Atoi(coordStrings[0])
if err != nil {
log.WithError(err).WithField("Point string", coordStrings[0]).Error("decodePoints: could not parse X co-ordinate string")
return nil, err
}
point.Y, err = strconv.Atoi(coordStrings[1])
if err != nil {
log.WithError(err).WithField("Point string", coordStrings[1]).Error("decodePoints: could not parse X co-ordinate string")
return nil, err
}
points = append(points, point)
}
return points, nil
}
// flipY will get the inverse Y co-ordinate based on the parent maps' size. This is because Tiled draws from the
// top-right instead of the bottom-left.
func (p *Point) flipY() {
p.Y = int(p.parentMap.pixelHeight()) - p.Y
}