Skip to content

Commit 4bef7cb

Browse files
committed
AxisAlignedBB is now immutable
1 parent 45f99d8 commit 4bef7cb

File tree

1 file changed

+37
-106
lines changed

1 file changed

+37
-106
lines changed

src/AxisAlignedBB.php

Lines changed: 37 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
use function abs;
2727
use const PHP_INT_MAX;
2828

29-
final class AxisAlignedBB{
29+
final readonly class AxisAlignedBB{
3030

3131
public float $minX;
3232
public float $minY;
@@ -93,21 +93,14 @@ public function addCoord(float $x, float $y, float $z) : AxisAlignedBB{
9393
* @return $this
9494
*/
9595
public function expand(float $x, float $y, float $z){
96-
$this->minX -= $x;
97-
$this->minY -= $y;
98-
$this->minZ -= $z;
99-
$this->maxX += $x;
100-
$this->maxY += $y;
101-
$this->maxZ += $z;
102-
103-
return $this;
104-
}
105-
106-
/**
107-
* Returns an expanded clone of this AxisAlignedBB.
108-
*/
109-
public function expandedCopy(float $x, float $y, float $z) : AxisAlignedBB{
110-
return (clone $this)->expand($x, $y, $z);
96+
return new AxisAlignedBB(
97+
$this->minX - $x,
98+
$this->minY - $y,
99+
$this->minZ - $z,
100+
$this->maxX + $x,
101+
$this->maxY + $y,
102+
$this->maxZ + $z
103+
);
111104
}
112105

113106
/**
@@ -116,21 +109,14 @@ public function expandedCopy(float $x, float $y, float $z) : AxisAlignedBB{
116109
* @return $this
117110
*/
118111
public function offset(float $x, float $y, float $z) : AxisAlignedBB{
119-
$this->minX += $x;
120-
$this->minY += $y;
121-
$this->minZ += $z;
122-
$this->maxX += $x;
123-
$this->maxY += $y;
124-
$this->maxZ += $z;
125-
126-
return $this;
127-
}
128-
129-
/**
130-
* Returns an offset clone of this AxisAlignedBB.
131-
*/
132-
public function offsetCopy(float $x, float $y, float $z) : AxisAlignedBB{
133-
return (clone $this)->offset($x, $y, $z);
112+
return new AxisAlignedBB(
113+
$this->minX + $x,
114+
$this->minY + $y,
115+
$this->minZ + $z,
116+
$this->maxX + $x,
117+
$this->maxY + $y,
118+
$this->maxZ + $z
119+
);
134120
}
135121

136122
/**
@@ -144,34 +130,20 @@ public function offsetTowards(Facing $face, float $distance) : AxisAlignedBB{
144130
return $this->offset($offsetX * $distance, $offsetY * $distance, $offsetZ * $distance);
145131
}
146132

147-
/**
148-
* Returns an offset clone of this AxisAlignedBB.
149-
*/
150-
public function offsetTowardsCopy(Facing $face, float $distance) : AxisAlignedBB{
151-
return (clone $this)->offsetTowards($face, $distance);
152-
}
153-
154133
/**
155134
* Insets the bounds of this AxisAlignedBB by the specified X, Y and Z.
156135
*
157136
* @return $this
158137
*/
159138
public function contract(float $x, float $y, float $z) : AxisAlignedBB{
160-
$this->minX += $x;
161-
$this->minY += $y;
162-
$this->minZ += $z;
163-
$this->maxX -= $x;
164-
$this->maxY -= $y;
165-
$this->maxZ -= $z;
166-
167-
return $this;
168-
}
169-
170-
/**
171-
* Returns a contracted clone of this AxisAlignedBB.
172-
*/
173-
public function contractedCopy(float $x, float $y, float $z) : AxisAlignedBB{
174-
return (clone $this)->contract($x, $y, $z);
139+
return new AxisAlignedBB(
140+
$this->minX + $x,
141+
$this->minY + $y,
142+
$this->minZ + $z,
143+
$this->maxX - $x,
144+
$this->maxY - $y,
145+
$this->maxZ - $z
146+
);
175147
}
176148

177149
/**
@@ -182,24 +154,14 @@ public function contractedCopy(float $x, float $y, float $z) : AxisAlignedBB{
182154
* @return $this
183155
*/
184156
public function extend(Facing $face, float $distance) : AxisAlignedBB{
185-
match($face){
186-
Facing::DOWN => $this->minY -= $distance,
187-
Facing::UP => $this->maxY += $distance,
188-
Facing::NORTH => $this->minZ -= $distance,
189-
Facing::SOUTH => $this->maxZ += $distance,
190-
Facing::WEST => $this->minX -= $distance,
191-
Facing::EAST => $this->maxX += $distance,
157+
return match($face){
158+
Facing::DOWN => new AxisAlignedBB($this->minX, $this->minY - $distance, $this->minZ, $this->maxX, $this->maxY, $this->maxZ),
159+
Facing::UP => new AxisAlignedBB($this->minX, $this->minY, $this->minZ, $this->maxX + $distance, $this->maxY, $this->maxZ),
160+
Facing::NORTH => new AxisAlignedBB($this->minX, $this->minY, $this->minZ - $distance, $this->maxX, $this->maxY, $this->maxZ),
161+
Facing::SOUTH => new AxisAlignedBB($this->minX, $this->minY, $this->minZ, $this->maxX, $this->maxY, $this->maxZ + $distance),
162+
Facing::WEST => new AxisAlignedBB($this->minX - $distance, $this->minY, $this->minZ, $this->maxX, $this->maxY, $this->maxZ),
163+
Facing::EAST => new AxisAlignedBB($this->minX, $this->minY, $this->minZ, $this->maxX + $distance, $this->maxY, $this->maxZ)
192164
};
193-
194-
return $this;
195-
}
196-
197-
/**
198-
* Returns an extended clone of this bounding box.
199-
* @see AxisAlignedBB::extend()
200-
*/
201-
public function extendedCopy(Facing $face, float $distance) : AxisAlignedBB{
202-
return (clone $this)->extend($face, $distance);
203165
}
204166

205167
/**
@@ -214,14 +176,6 @@ public function trim(Facing $face, float $distance) : AxisAlignedBB{
214176
return $this->extend($face, -$distance);
215177
}
216178

217-
/**
218-
* Returns a trimmed clone of this bounding box.
219-
* @see AxisAlignedBB::trim()
220-
*/
221-
public function trimmedCopy(Facing $face, float $distance) : AxisAlignedBB{
222-
return $this->extendedCopy($face, -$distance);
223-
}
224-
225179
/**
226180
* Increases the dimension of the AABB along the given axis.
227181
*
@@ -230,26 +184,11 @@ public function trimmedCopy(Facing $face, float $distance) : AxisAlignedBB{
230184
* @return $this
231185
*/
232186
public function stretch(Axis $axis, float $distance) : AxisAlignedBB{
233-
if($axis === Axis::Y){
234-
$this->minY -= $distance;
235-
$this->maxY += $distance;
236-
}elseif($axis === Axis::Z){
237-
$this->minZ -= $distance;
238-
$this->maxZ += $distance;
239-
}elseif($axis === Axis::X){
240-
$this->minX -= $distance;
241-
$this->maxX += $distance;
242-
}
243-
244-
return $this;
245-
}
246-
247-
/**
248-
* Returns a stretched copy of this bounding box.
249-
* @see AxisAlignedBB::stretch()
250-
*/
251-
public function stretchedCopy(Axis $axis, float $distance) : AxisAlignedBB{
252-
return (clone $this)->stretch($axis, $distance);
187+
return match($axis){
188+
Axis::Y => new AxisAlignedBB($this->minX, $this->minY - $distance, $this->minZ, $this->maxX, $this->maxY + $distance, $this->maxZ),
189+
Axis::Z => new AxisAlignedBB($this->minX, $this->minY, $this->minZ - $distance, $this->maxX, $this->maxY, $this->maxZ + $distance),
190+
Axis::X => new AxisAlignedBB($this->minX - $distance, $this->minY, $this->minZ, $this->maxX + $distance, $this->maxY, $this->maxZ)
191+
};
253192
}
254193

255194
/**
@@ -262,14 +201,6 @@ public function squash(Axis $axis, float $distance) : AxisAlignedBB{
262201
return $this->stretch($axis, -$distance);
263202
}
264203

265-
/**
266-
* Returns a squashed copy of this bounding box.
267-
* @see AxisAlignedBB::squash()
268-
*/
269-
public function squashedCopy(Axis $axis, float $distance) : AxisAlignedBB{
270-
return $this->stretchedCopy($axis, -$distance);
271-
}
272-
273204
public function calculateXOffset(AxisAlignedBB $bb, float $x) : float{
274205
if($bb->maxY <= $this->minY or $bb->minY >= $this->maxY){
275206
return $x;

0 commit comments

Comments
 (0)