Avoid more Vector3 mutations using withComponents()

This commit is contained in:
Dylan K. Taylor 2020-09-25 18:40:13 +01:00
parent 1d8e7abdd4
commit db7fb25196
3 changed files with 12 additions and 22 deletions

View File

@ -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!)

View File

@ -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.
}
}

View File

@ -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();