/
face.go
143 lines (129 loc) · 3.05 KB
/
face.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
package cube
import (
"fmt"
)
const (
// FaceDown represents the bottom face of a block.
FaceDown Face = iota
// FaceUp represents the top face of a block.
FaceUp
// FaceNorth represents the north face of a block.
FaceNorth
// FaceSouth represents the south face of a block.
FaceSouth
// FaceWest represents the west face of the block.
FaceWest
// FaceEast represents the east face of the block.
FaceEast
)
// Face represents the face of a block or entity.
type Face int
// Direction converts the Face to a Direction and returns it, assuming the Face is horizontal and not FaceUp
// or FaceDown.
func (f Face) Direction() Direction {
return Direction(f - 2)
}
// FromString returns a Face by a string.
func (f Face) FromString(s string) (interface{}, error) {
switch s {
case "down":
return FaceDown, nil
case "up":
return FaceUp, nil
case "north":
return FaceNorth, nil
case "south":
return FaceSouth, nil
case "west":
return FaceWest, nil
case "east":
return FaceEast, nil
}
return nil, fmt.Errorf("unexpected facing '%v', expecting one of 'down', 'up', 'north', 'south', 'west' or 'east'", s)
}
// Opposite returns the opposite face. FaceDown will return up, north will return south and west will return east,
// and vice versa.
func (f Face) Opposite() Face {
switch f {
default:
return FaceUp
case FaceUp:
return FaceDown
case FaceNorth:
return FaceSouth
case FaceSouth:
return FaceNorth
case FaceWest:
return FaceEast
case FaceEast:
return FaceWest
}
}
// Axis returns the axis the face is facing. FaceEast and west correspond to the x axis, north and south to the z
// axis and up and down to the y axis.
func (f Face) Axis() Axis {
switch f {
default:
return Y
case FaceEast, FaceWest:
return X
case FaceNorth, FaceSouth:
return Z
}
}
// RotateRight rotates the face 90 degrees to the right horizontally and returns the new face.
func (f Face) RotateRight() Face {
switch f {
case FaceNorth:
return FaceEast
case FaceEast:
return FaceSouth
case FaceSouth:
return FaceWest
case FaceWest:
return FaceNorth
}
return f
}
// RotateLeft rotates the face 90 degrees to the left horizontally and returns the new face.
func (f Face) RotateLeft() Face {
switch f {
case FaceNorth:
return FaceWest
case FaceEast:
return FaceNorth
case FaceSouth:
return FaceEast
case FaceWest:
return FaceSouth
}
return f
}
// String returns the Face as a string.
func (f Face) String() string {
switch f {
case FaceUp:
return "up"
case FaceDown:
return "down"
case FaceNorth:
return "north"
case FaceSouth:
return "south"
case FaceWest:
return "west"
case FaceEast:
return "east"
}
panic("invalid face")
}
// Faces returns a list of all faces, starting with down, then up, then north to west.
func Faces() []Face {
return faces[:]
}
// HorizontalFaces returns a list of all horizontal faces, from north to west.
func HorizontalFaces() []Face {
return hFaces[:]
}
var hFaces = [...]Face{FaceNorth, FaceEast, FaceSouth, FaceWest}
var faces = [...]Face{FaceDown, FaceUp, FaceNorth, FaceEast, FaceSouth, FaceWest}