Skip to content

Commit 7513b95

Browse files
committed
Facing and Axis to enum, but with way less destruction
the rest of the changes in #91 were desirable, but it's just way too much to deal with all at once. This reduced changeset will give 80% of the benefits with 20% of the work.
1 parent b9ee896 commit 7513b95

7 files changed

Lines changed: 80 additions & 121 deletions

File tree

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ parameters:
33
treatPhpDocTypesAsCertain: false
44
paths:
55
- src
6+
- tests

src/Axis.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,18 @@
2323

2424
namespace pocketmine\math;
2525

26-
final class Axis{
27-
private function __construct(){
28-
//NOOP
29-
}
26+
use function strtolower;
27+
28+
enum Axis : int{
3029

31-
public const Y = 0;
32-
public const Z = 1;
33-
public const X = 2;
30+
case Y = 0;
31+
case Z = 1;
32+
case X = 2;
3433

3534
/**
3635
* Returns a human-readable string representation of the given axis.
3736
*/
38-
public static function toString(int $axis) : string{
39-
return match($axis){
40-
Axis::Y => "y",
41-
Axis::Z => "z",
42-
Axis::X => "x",
43-
default => throw new \InvalidArgumentException("Invalid axis $axis")
44-
};
37+
public static function toString(Axis $axis) : string{
38+
return strtolower($axis->name);
4539
}
4640
}

src/AxisAlignedBB.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ public function offsetCopy(float $x, float $y, float $z) : AxisAlignedBB{
118118
/**
119119
* Returns a copy of the AxisAlignedBB offset in the given direction by the specified distance.
120120
*/
121-
public function offsetTowardsCopy(int $face, float $distance) : AxisAlignedBB{
122-
[$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$face];
121+
public function offsetTowardsCopy(Facing $face, float $distance) : AxisAlignedBB{
122+
[$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$face->value];
123123

124124
return $this->offsetCopy($offsetX * $distance, $offsetY * $distance, $offsetZ * $distance);
125125
}
@@ -143,7 +143,7 @@ public function contractedCopy(float $x, float $y, float $z) : AxisAlignedBB{
143143
*
144144
* @param float $distance Negative values pull the face in, positive values push out.
145145
*/
146-
public function extendedCopy(int $face, float $distance) : AxisAlignedBB{
146+
public function extendedCopy(Facing $face, float $distance) : AxisAlignedBB{
147147
$minX = $this->minX;
148148
$minY = $this->minY;
149149
$minZ = $this->minZ;
@@ -158,7 +158,6 @@ public function extendedCopy(int $face, float $distance) : AxisAlignedBB{
158158
Facing::SOUTH => $maxZ += $distance,
159159
Facing::WEST => $minX -= $distance,
160160
Facing::EAST => $maxX += $distance,
161-
default => throw new \InvalidArgumentException("Invalid face $face"),
162161
};
163162

164163
return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
@@ -170,17 +169,16 @@ public function extendedCopy(int $face, float $distance) : AxisAlignedBB{
170169
*
171170
* @param float $distance Positive values pull the face in, negative values push out.
172171
*/
173-
public function trimmedCopy(int $face, float $distance) : AxisAlignedBB{
172+
public function trimmedCopy(Facing $face, float $distance) : AxisAlignedBB{
174173
return $this->extendedCopy($face, -$distance);
175174
}
176175

177176
/**
178177
* Returns a copy of the AxisAlignedBB stretched along the given axis.
179178
*
180-
* @param int $axis one of the Axis::* constants
181179
* @param float $distance Negative values reduce width, positive values increase width.
182180
*/
183-
public function stretchedCopy(int $axis, float $distance) : AxisAlignedBB{
181+
public function stretchedCopy(Axis $axis, float $distance) : AxisAlignedBB{
184182
$minX = $this->minX;
185183
$minY = $this->minY;
186184
$minZ = $this->minZ;
@@ -206,7 +204,7 @@ public function stretchedCopy(int $axis, float $distance) : AxisAlignedBB{
206204
* Inverse of stretchedCopy().
207205
* @see AxisAlignedBB::stretchedCopy()
208206
*/
209-
public function squashedCopy(int $axis, float $distance) : AxisAlignedBB{
207+
public function squashedCopy(Axis $axis, float $distance) : AxisAlignedBB{
210208
return $this->stretchedCopy($axis, -$distance);
211209
}
212210

@@ -386,28 +384,27 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu
386384
$v6 = null;
387385
}
388386

389-
$vector = null;
390387
$distance = PHP_INT_MAX;
391-
$face = -1;
388+
$vectorAndFace = null;
392389

393390
foreach([
394-
Facing::WEST => $v1,
395-
Facing::EAST => $v2,
396-
Facing::DOWN => $v3,
397-
Facing::UP => $v4,
398-
Facing::NORTH => $v5,
399-
Facing::SOUTH => $v6
400-
] as $f => $v){
391+
[Facing::WEST, $v1],
392+
[Facing::EAST, $v2],
393+
[Facing::DOWN, $v3],
394+
[Facing::UP, $v4],
395+
[Facing::NORTH, $v5],
396+
[Facing::SOUTH, $v6],
397+
] as [$f, $v]){
401398
if($v !== null and ($d = $pos1->distanceSquared($v)) < $distance){
402-
$vector = $v;
403399
$distance = $d;
404-
$face = $f;
400+
$vectorAndFace = [$v, $f];
405401
}
406402
}
407403

408-
if($vector === null){
404+
if($vectorAndFace === null){
409405
return null;
410406
}
407+
[$vector, $face] = $vectorAndFace;
411408

412409
return new RayTraceResult($this, $face, $vector);
413410
}

src/Facing.php

Lines changed: 43 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,18 @@
2323

2424
namespace 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
}

src/RayTraceResult.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,17 @@
2828
*/
2929
readonly class RayTraceResult{
3030

31-
/**
32-
* @param int $hitFace one of the Facing::* constants
33-
*/
3431
public function __construct(
3532
public AxisAlignedBB $bb,
36-
public int $hitFace,
33+
public Facing $hitFace,
3734
public Vector3 $hitVector
3835
){}
3936

4037
public function getBoundingBox() : AxisAlignedBB{
4138
return $this->bb;
4239
}
4340

44-
public function getHitFace() : int{
41+
public function getHitFace() : Facing{
4542
return $this->hitFace;
4643
}
4744

src/Vector3.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ public function abs() : Vector3{
118118
/**
119119
* @return Vector3
120120
*/
121-
public function getSide(int $side, int $step = 1){
122-
[$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$side] ?? [0, 0, 0];
121+
public function getSide(Facing $side, int $step = 1){
122+
[$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$side->value] ?? [0, 0, 0];
123123

124124
return $this->add($offsetX * $step, $offsetY * $step, $offsetZ * $step);
125125
}
@@ -176,7 +176,7 @@ public function east(int $step = 1){
176176
*/
177177
public function sides(int $step = 1) : \Generator{
178178
foreach(Facing::ALL as $facing){
179-
yield $facing => $this->getSide($facing, $step);
179+
yield $facing->value => $this->getSide($facing, $step);
180180
}
181181
}
182182

@@ -192,15 +192,13 @@ public function sidesArray(bool $keys = false, int $step = 1) : array{
192192
/**
193193
* Yields vectors stepped out from this one in directions except those on the given axis.
194194
*
195-
* @param int $axis Facing directions on this axis will be excluded
196-
*
197195
* @return \Generator|Vector3[]
198196
* @phpstan-return \Generator<int, Vector3, void, void>
199197
*/
200-
public function sidesAroundAxis(int $axis, int $step = 1) : \Generator{
198+
public function sidesAroundAxis(Axis $axis, int $step = 1) : \Generator{
201199
foreach(Facing::ALL as $facing){
202200
if(Facing::axis($facing) !== $axis){
203-
yield $facing => $this->getSide($facing, $step);
201+
yield $facing->value => $this->getSide($facing, $step);
204202
}
205203
}
206204
}

0 commit comments

Comments
 (0)