-
Notifications
You must be signed in to change notification settings - Fork 2
/
directions.go
131 lines (123 loc) · 2.32 KB
/
directions.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
package geom
//go:generate go run github.com/dmarkham/enumer -type=DirectionType,RelativeDirection -json
// DirectionType enumerates directions.
type DirectionType int
// DirectionTypes represent the 6 edges of a hexagon.
const (
N DirectionType = iota
NE
SE
S
SW
NW
)
// Opposite provides the 180 degree opposite to any cardinal DirectionType.
var Opposite = map[DirectionType]DirectionType{
S: N,
SW: NE,
NW: SE,
N: S,
NE: SW,
SE: NW,
}
// FindDirection returns the direction one would have to travel in from origin to
// arrive at projection. Returned values are rounded when the precise value is
// not one of the 6 hexagonal directions.
func FindDirection(origin, projection Key) DirectionType {
if origin.M == projection.M {
// we know it's N or S
if origin.N > projection.N {
return N
}
return S
} else if origin.M < projection.M {
// We know it's NE or SE
if origin.M%2 == 0 {
if origin.N > projection.N {
return NE
}
return SE
}
if origin.N >= projection.N {
return NE
}
return SE
} else {
// We know it's NW or SW
if origin.M%2 == 0 {
if origin.N > projection.N {
return NW
}
return SW
} else {
if origin.N >= projection.N {
return NW
}
return SW
}
}
}
type RelativeDirection int
const (
Forward RelativeDirection = iota
Behind
ForwardLeft
ForwardRight
BackLeft
BackRight
)
// Actualize a relative direction and a concrete direction together to form a
// new concrete direction. N and Behind make S. S and Forward make S etc.
func Actualize(facing DirectionType, relative RelativeDirection) DirectionType {
switch relative {
case Behind:
return map[DirectionType]DirectionType{
N: S,
S: N,
NE: SW,
SW: NE,
NW: SE,
SE: NW,
}[facing]
case ForwardLeft:
return map[DirectionType]DirectionType{
N: NW,
S: SE,
NE: N,
SW: S,
NW: SW,
SE: NE,
}[facing]
case ForwardRight:
return map[DirectionType]DirectionType{
N: NE,
S: SW,
NE: SE,
SW: NW,
NW: N,
SE: S,
}[facing]
case BackLeft:
return map[DirectionType]DirectionType{
N: SW,
S: NE,
NE: NW,
SW: SE,
NW: S,
SE: N,
}[facing]
case BackRight:
return map[DirectionType]DirectionType{
N: SE,
S: NW,
NE: S,
SW: N,
NW: NE,
SE: SW,
}[facing]
case Forward:
fallthrough
default:
return facing
}
}