-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirection.go
127 lines (117 loc) · 2.28 KB
/
direction.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
package raumata
import (
"strings"
"github.com/REANNZ/raumata/internal"
"github.com/REANNZ/raumata/vec"
)
// Helper type for handling compass directions
type direction int
const (
directionNone direction = iota
directionN
directionNE
directionE
directionSE
directionS
directionSW
directionW
directionNW
)
func directionFromString(s string) direction {
switch strings.ToLower(s) {
case "n", "north":
return directionN
case "ne", "northeast", "north-east":
return directionNE
case "e", "east":
return directionE
case "se", "southeast", "south-east":
return directionSE
case "s", "south":
return directionS
case "sw", "southwest", "south-west":
return directionSW
case "w", "west":
return directionW
case "nw", "northwest", "north-west":
return directionNW
default:
return directionNone
}
}
func (d direction) Opposite() direction {
switch d {
case directionN:
return directionS
case directionNE:
return directionSW
case directionE:
return directionW
case directionSE:
return directionNW
case directionS:
return directionN
case directionSW:
return directionNE
case directionW:
return directionE
case directionNW:
return directionSE
default:
return d
}
}
// Returns the direction as a vector.
// Y-values increase as you go south
func (d direction) AsVec() vec.Vec2 {
switch d {
case directionN:
return vec.Vec2{X: 0, Y: -1}
case directionNE:
return vec.Vec2{X: 1, Y: -1}
case directionE:
return vec.Vec2{X: 1, Y: 0}
case directionSE:
return vec.Vec2{X: 1, Y: 1}
case directionS:
return vec.Vec2{X: 0, Y: 1}
case directionSW:
return vec.Vec2{X: -1, Y: 1}
case directionW:
return vec.Vec2{X: -1, Y: 0}
case directionNW:
return vec.Vec2{X: -1, Y: -1}
default:
return vec.Vec2{}
}
}
// Returns the given grid position moved by the direction
func (d direction) moveGridPos(p internal.GridPos) internal.GridPos {
v := d.AsVec()
return internal.GridPos{
X: p.X + int16(v.X),
Y: p.Y + int16(v.Y),
}
}
func (d direction) String() string {
switch d {
case directionN:
return "n"
case directionNE:
return "ne"
case directionE:
return "e"
case directionSE:
return "se"
case directionS:
return "s"
case directionSW:
return "sw"
case directionW:
return "w"
case directionNW:
return "nw"
default:
return ""
}
}