From db7fb25196fb002b473a64ca3efb08316b2829d1 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 25 Sep 2020 18:40:13 +0100 Subject: [PATCH] Avoid more Vector3 mutations using withComponents() --- src/entity/Entity.php | 30 ++++++++++-------------------- src/entity/Living.php | 2 +- src/entity/Squid.php | 2 +- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index f1d7bc7e6..b02f877e3 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -953,15 +953,11 @@ abstract class Entity{ if($this->hasMovementUpdate()){ $this->tryChangeMovement(); - if(abs($this->motion->x) <= self::MOTION_THRESHOLD){ - $this->motion->x = 0; - } - if(abs($this->motion->y) <= self::MOTION_THRESHOLD){ - $this->motion->y = 0; - } - if(abs($this->motion->z) <= self::MOTION_THRESHOLD){ - $this->motion->z = 0; - } + $this->motion = $this->motion->withComponents( + abs($this->motion->x) <= self::MOTION_THRESHOLD ? 0 : null, + abs($this->motion->y) <= self::MOTION_THRESHOLD ? 0 : null, + abs($this->motion->z) <= self::MOTION_THRESHOLD ? 0 : null + ); if($this->motion->x != 0 or $this->motion->y != 0 or $this->motion->z != 0){ $this->move($this->motion->x, $this->motion->y, $this->motion->z); @@ -1205,17 +1201,11 @@ abstract class Entity{ $this->checkGroundState($movX, $movY, $movZ, $dx, $dy, $dz); $this->updateFallState($dy, $this->onGround); - if($movX != $dx){ - $this->motion->x = 0; - } - - if($movY != $dy){ - $this->motion->y = 0; - } - - if($movZ != $dz){ - $this->motion->z = 0; - } + $this->motion = $this->motion->withComponents( + $movX != $dx ? 0 : null, + $movY != $dy ? 0 : null, + $movZ != $dz ? 0 : null + ); //TODO: vehicle collision events (first we need to spawn them!) diff --git a/src/entity/Living.php b/src/entity/Living.php index dab60300c..85dbc6a05 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -288,7 +288,7 @@ abstract class Living extends Entity{ */ public function jump() : void{ if($this->onGround){ - $this->motion->y = $this->getJumpVelocity(); //Y motion should already be 0 if we're jumping from the ground. + $this->motion = $this->motion->withComponents(null, $this->getJumpVelocity(), null); //Y motion should already be 0 if we're jumping from the ground. } } diff --git a/src/entity/Squid.php b/src/entity/Squid.php index b39913bfa..04b81d4fc 100644 --- a/src/entity/Squid.php +++ b/src/entity/Squid.php @@ -97,7 +97,7 @@ class Squid extends WaterAnimal{ if($this->isAlive()){ if($this->location->y > 62 and $this->swimDirection !== null){ - $this->swimDirection->y = -0.5; + $this->swimDirection = $this->swimDirection->withComponents(null, -0.5, null); } $inWater = $this->isUnderwater();