Skip to content

Commit 40d3fdd

Browse files
authored
Facing and Axis to enum (#91)
1 parent 22f0a61 commit 40d3fdd

File tree

6 files changed

+123
-188
lines changed

6 files changed

+123
-188
lines changed

src/Axis.php

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

2424
namespace pocketmine\math;
2525

26-
final class Axis{
27-
private function __construct(){
28-
//NOOP
29-
}
30-
31-
public const Y = 0;
32-
public const Z = 1;
33-
public const X = 2;
34-
35-
/**
36-
* Returns a human-readable string representation of the given axis.
37-
*/
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-
};
45-
}
26+
enum Axis{
27+
case Y;
28+
case Z;
29+
case X;
4630
}

src/AxisAlignedBB.php

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,18 @@ public function offsetCopy(float $x, float $y, float $z) : AxisAlignedBB{
136136
/**
137137
* Offsets this AxisAlignedBB in the given direction by the specified distance.
138138
*
139-
* @param int $face one of the Facing::* constants
140-
*
141139
* @return $this
142140
*/
143-
public function offsetTowards(int $face, float $distance) : AxisAlignedBB{
144-
[$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$face] ?? throw new \InvalidArgumentException("Invalid Facing $face");
141+
public function offsetTowards(Facing $face, float $distance) : AxisAlignedBB{
142+
[$offsetX, $offsetY, $offsetZ] = $face->offset();
145143

146144
return $this->offset($offsetX * $distance, $offsetY * $distance, $offsetZ * $distance);
147145
}
148146

149147
/**
150148
* Returns an offset clone of this AxisAlignedBB.
151149
*/
152-
public function offsetTowardsCopy(int $face, float $distance) : AxisAlignedBB{
150+
public function offsetTowardsCopy(Facing $face, float $distance) : AxisAlignedBB{
153151
return (clone $this)->offsetTowards($face, $distance);
154152
}
155153

@@ -182,17 +180,15 @@ public function contractedCopy(float $x, float $y, float $z) : AxisAlignedBB{
182180
* @param float $distance Negative values pull the face in, positive values push out.
183181
*
184182
* @return $this
185-
* @throws \InvalidArgumentException
186183
*/
187-
public function extend(int $face, float $distance) : AxisAlignedBB{
184+
public function extend(Facing $face, float $distance) : AxisAlignedBB{
188185
match($face){
189186
Facing::DOWN => $this->minY -= $distance,
190187
Facing::UP => $this->maxY += $distance,
191188
Facing::NORTH => $this->minZ -= $distance,
192189
Facing::SOUTH => $this->maxZ += $distance,
193190
Facing::WEST => $this->minX -= $distance,
194191
Facing::EAST => $this->maxX += $distance,
195-
default => throw new \InvalidArgumentException("Invalid face $face"),
196192
};
197193

198194
return $this;
@@ -201,10 +197,8 @@ public function extend(int $face, float $distance) : AxisAlignedBB{
201197
/**
202198
* Returns an extended clone of this bounding box.
203199
* @see AxisAlignedBB::extend()
204-
*
205-
* @throws \InvalidArgumentException
206200
*/
207-
public function extendedCopy(int $face, float $distance) : AxisAlignedBB{
201+
public function extendedCopy(Facing $face, float $distance) : AxisAlignedBB{
208202
return (clone $this)->extend($face, $distance);
209203
}
210204

@@ -215,32 +209,27 @@ public function extendedCopy(int $face, float $distance) : AxisAlignedBB{
215209
* @param float $distance Positive values pull the face in, negative values push out.
216210
*
217211
* @return $this
218-
* @throws \InvalidArgumentException
219212
*/
220-
public function trim(int $face, float $distance) : AxisAlignedBB{
213+
public function trim(Facing $face, float $distance) : AxisAlignedBB{
221214
return $this->extend($face, -$distance);
222215
}
223216

224217
/**
225218
* Returns a trimmed clone of this bounding box.
226219
* @see AxisAlignedBB::trim()
227-
*
228-
* @throws \InvalidArgumentException
229220
*/
230-
public function trimmedCopy(int $face, float $distance) : AxisAlignedBB{
221+
public function trimmedCopy(Facing $face, float $distance) : AxisAlignedBB{
231222
return $this->extendedCopy($face, -$distance);
232223
}
233224

234225
/**
235226
* Increases the dimension of the AABB along the given axis.
236227
*
237-
* @param int $axis one of the Axis::* constants
238228
* @param float $distance Negative values reduce width, positive values increase width.
239229
*
240230
* @return $this
241-
* @throws \InvalidArgumentException
242231
*/
243-
public function stretch(int $axis, float $distance) : AxisAlignedBB{
232+
public function stretch(Axis $axis, float $distance) : AxisAlignedBB{
244233
if($axis === Axis::Y){
245234
$this->minY -= $distance;
246235
$this->maxY += $distance;
@@ -250,19 +239,16 @@ public function stretch(int $axis, float $distance) : AxisAlignedBB{
250239
}elseif($axis === Axis::X){
251240
$this->minX -= $distance;
252241
$this->maxX += $distance;
253-
}else{
254-
throw new \InvalidArgumentException("Invalid axis $axis");
255242
}
243+
256244
return $this;
257245
}
258246

259247
/**
260248
* Returns a stretched copy of this bounding box.
261249
* @see AxisAlignedBB::stretch()
262-
*
263-
* @throws \InvalidArgumentException
264250
*/
265-
public function stretchedCopy(int $axis, float $distance) : AxisAlignedBB{
251+
public function stretchedCopy(Axis $axis, float $distance) : AxisAlignedBB{
266252
return (clone $this)->stretch($axis, $distance);
267253
}
268254

@@ -271,19 +257,16 @@ public function stretchedCopy(int $axis, float $distance) : AxisAlignedBB{
271257
* @see AxisAlignedBB::stretch()
272258
*
273259
* @return $this
274-
* @throws \InvalidArgumentException
275260
*/
276-
public function squash(int $axis, float $distance) : AxisAlignedBB{
261+
public function squash(Axis $axis, float $distance) : AxisAlignedBB{
277262
return $this->stretch($axis, -$distance);
278263
}
279264

280265
/**
281266
* Returns a squashed copy of this bounding box.
282267
* @see AxisAlignedBB::squash()
283-
*
284-
* @throws \InvalidArgumentException
285268
*/
286-
public function squashedCopy(int $axis, float $distance) : AxisAlignedBB{
269+
public function squashedCopy(Axis $axis, float $distance) : AxisAlignedBB{
287270
return $this->stretchedCopy($axis, -$distance);
288271
}
289272

@@ -463,29 +446,28 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu
463446
$v6 = null;
464447
}
465448

466-
$vector = null;
467449
$distance = PHP_INT_MAX;
468-
$face = -1;
450+
$hitInfo = null;
469451

470452
foreach([
471-
Facing::WEST => $v1,
472-
Facing::EAST => $v2,
473-
Facing::DOWN => $v3,
474-
Facing::UP => $v4,
475-
Facing::NORTH => $v5,
476-
Facing::SOUTH => $v6
477-
] as $f => $v){
453+
[Facing::WEST, $v1],
454+
[Facing::EAST, $v2],
455+
[Facing::DOWN, $v3],
456+
[Facing::UP, $v4],
457+
[Facing::NORTH, $v5],
458+
[Facing::SOUTH, $v6]
459+
] as [$facing, $v]){
478460
if($v !== null and ($d = $pos1->distanceSquared($v)) < $distance){
479-
$vector = $v;
480461
$distance = $d;
481-
$face = $f;
462+
$hitInfo = [$facing, $v];
482463
}
483464
}
484465

485-
if($vector === null){
466+
if($hitInfo === null){
486467
return null;
487468
}
488469

470+
[$face, $vector] = $hitInfo;
489471
return new RayTraceResult($this, $face, $vector);
490472
}
491473

0 commit comments

Comments
 (0)