2323
2424namespace pocketmine \math ;
2525
26- use function in_array ;
27-
28- final class Facing{
29- private function __construct (){
30- //NOOP
31- }
26+ use function strtolower ;
3227
28+ enum Facing : int{
3329 public const FLAG_AXIS_POSITIVE = 1 ;
3430
3531 /* most significant 2 bits = axis, least significant bit = is positive direction */
36- public const DOWN = Axis::Y << 1 ;
37- public const UP = (Axis::Y << 1 ) | self ::FLAG_AXIS_POSITIVE ;
38- public const NORTH = Axis::Z << 1 ;
39- public const SOUTH = (Axis::Z << 1 ) | self ::FLAG_AXIS_POSITIVE ;
40- public const WEST = Axis::X << 1 ;
41- public const EAST = (Axis::X << 1 ) | self ::FLAG_AXIS_POSITIVE ;
32+ case DOWN = Axis::Y-> value << 1 ;
33+ case UP = (Axis::Y-> value << 1 ) | self ::FLAG_AXIS_POSITIVE ;
34+ case NORTH = Axis::Z-> value << 1 ;
35+ case SOUTH = (Axis::Z-> value << 1 ) | self ::FLAG_AXIS_POSITIVE ;
36+ case WEST = Axis::X-> value << 1 ;
37+ case EAST = (Axis::X-> value << 1 ) | self ::FLAG_AXIS_POSITIVE ;
4238
4339 public const ALL = [
4440 self ::DOWN ,
@@ -57,119 +53,95 @@ private function __construct(){
5753 ];
5854
5955 public const OFFSET = [
60- self ::DOWN => [ 0 , -1 , 0 ],
61- self ::UP => [ 0 , +1 , 0 ],
62- self ::NORTH => [ 0 , 0 , -1 ],
63- self ::SOUTH => [ 0 , 0 , +1 ],
64- self ::WEST => [-1 , 0 , 0 ],
65- self ::EAST => [+1 , 0 , 0 ]
56+ self ::DOWN -> value => [ 0 , -1 , 0 ],
57+ self ::UP -> value => [ 0 , +1 , 0 ],
58+ self ::NORTH -> value => [ 0 , 0 , -1 ],
59+ self ::SOUTH -> value => [ 0 , 0 , +1 ],
60+ self ::WEST -> value => [-1 , 0 , 0 ],
61+ self ::EAST -> value => [+1 , 0 , 0 ]
6662 ];
6763
6864 private const CLOCKWISE = [
69- Axis::Y => [
70- self ::NORTH => self ::EAST ,
71- self ::EAST => self ::SOUTH ,
72- self ::SOUTH => self ::WEST ,
73- self ::WEST => self ::NORTH
65+ Axis::Y-> value => [
66+ self ::NORTH -> value => self ::EAST ,
67+ self ::EAST -> value => self ::SOUTH ,
68+ self ::SOUTH -> value => self ::WEST ,
69+ self ::WEST -> value => self ::NORTH
7470 ],
75- Axis::Z => [
76- self ::UP => self ::EAST ,
77- self ::EAST => self ::DOWN ,
78- self ::DOWN => self ::WEST ,
79- self ::WEST => self ::UP
71+ Axis::Z-> value => [
72+ self ::UP -> value => self ::EAST ,
73+ self ::EAST -> value => self ::DOWN ,
74+ self ::DOWN -> value => self ::WEST ,
75+ self ::WEST -> value => self ::UP
8076 ],
81- Axis::X => [
82- self ::UP => self ::NORTH ,
83- self ::NORTH => self ::DOWN ,
84- self ::DOWN => self ::SOUTH ,
85- self ::SOUTH => self ::UP
77+ Axis::X-> value => [
78+ self ::UP -> value => self ::NORTH ,
79+ self ::NORTH -> value => self ::DOWN ,
80+ self ::DOWN -> value => self ::SOUTH ,
81+ self ::SOUTH -> value => self ::UP
8682 ]
8783 ];
8884
8985 /**
9086 * Returns the axis of the given direction.
9187 */
92- public static function axis (int $ direction ) : int {
93- return $ direction >> 1 ; //shift off positive/negative bit
88+ public static function axis (Facing $ direction ) : Axis {
89+ return Axis:: from ( $ direction-> value >> 1 ) ; //shift off positive/negative bit
9490 }
9591
9692 /**
9793 * Returns whether the direction is facing the positive of its axis.
9894 */
99- public static function isPositive (int $ direction ) : bool {
100- return ($ direction & self ::FLAG_AXIS_POSITIVE ) === self ::FLAG_AXIS_POSITIVE ;
95+ public static function isPositive (Facing $ direction ) : bool {
96+ return ($ direction-> value & self ::FLAG_AXIS_POSITIVE ) === self ::FLAG_AXIS_POSITIVE ;
10197 }
10298
10399 /**
104100 * Returns the opposite Facing of the specified one.
105- *
106- * @param int $direction 0-5 one of the Facing::* constants
107101 */
108- public static function opposite (int $ direction ) : int {
109- return $ direction ^ self ::FLAG_AXIS_POSITIVE ;
102+ public static function opposite (Facing $ direction ) : Facing {
103+ return self :: from ( $ direction-> value ^ self ::FLAG_AXIS_POSITIVE ) ;
110104 }
111105
112106 /**
113107 * Rotates the given direction around the axis.
114108 *
115109 * @throws \InvalidArgumentException if not possible to rotate $direction around $axis
116110 */
117- public static function rotate (int $ direction , int $ axis , bool $ clockwise ) : int {
118- if (!isset (self ::CLOCKWISE [$ axis ])){
119- throw new \InvalidArgumentException ("Invalid axis $ axis " );
120- }
121- if (!isset (self ::CLOCKWISE [$ axis ][$ direction ])){
111+ public static function rotate (Facing $ direction , Axis $ axis , bool $ clockwise ) : Facing {
112+ if (!isset (self ::CLOCKWISE [$ axis ->value ][$ direction ->value ])){
122113 throw new \InvalidArgumentException ("Cannot rotate facing \"" . self ::toString ($ direction ) . "\" around axis \"" . Axis::toString ($ axis ) . "\"" );
123114 }
124115
125- $ rotated = self ::CLOCKWISE [$ axis ][$ direction ];
116+ $ rotated = self ::CLOCKWISE [$ axis-> value ][$ direction-> value ];
126117 return $ clockwise ? $ rotated : self ::opposite ($ rotated );
127118 }
128119
129120 /**
130121 * @throws \InvalidArgumentException
131122 */
132- public static function rotateY (int $ direction , bool $ clockwise ) : int {
123+ public static function rotateY (Facing $ direction , bool $ clockwise ) : Facing {
133124 return self ::rotate ($ direction , Axis::Y, $ clockwise );
134125 }
135126
136127 /**
137128 * @throws \InvalidArgumentException
138129 */
139- public static function rotateZ (int $ direction , bool $ clockwise ) : int {
130+ public static function rotateZ (Facing $ direction , bool $ clockwise ) : Facing {
140131 return self ::rotate ($ direction , Axis::Z, $ clockwise );
141132 }
142133
143134 /**
144135 * @throws \InvalidArgumentException
145136 */
146- public static function rotateX (int $ direction , bool $ clockwise ) : int {
137+ public static function rotateX (Facing $ direction , bool $ clockwise ) : Facing {
147138 return self ::rotate ($ direction , Axis::X, $ clockwise );
148139 }
149140
150- /**
151- * Validates the given integer as a Facing direction.
152- *
153- * @throws \InvalidArgumentException if the argument is not a valid Facing constant
154- */
155- public static function validate (int $ facing ) : void {
156- if (!in_array ($ facing , self ::ALL , true )){
157- throw new \InvalidArgumentException ("Invalid direction $ facing " );
158- }
159- }
160-
161141 /**
162142 * Returns a human-readable string representation of the given Facing direction.
163143 */
164- public static function toString (int $ facing ) : string {
165- return match ($ facing ){
166- self ::DOWN => "down " ,
167- self ::UP => "up " ,
168- self ::NORTH => "north " ,
169- self ::SOUTH => "south " ,
170- self ::WEST => "west " ,
171- self ::EAST => "east " ,
172- default => throw new \InvalidArgumentException ("Invalid facing $ facing " )
173- };
144+ public static function toString (Facing $ facing ) : string {
145+ return strtolower ($ facing ->name );
174146 }
175147}
0 commit comments