Block: rework addVelocityToEntity() to avoid vector3 mutation

This commit is contained in:
Dylan K. Taylor
2020-06-27 21:38:24 +01:00
parent ff00595a48
commit f1048aeaa3
4 changed files with 14 additions and 13 deletions

View File

@@ -338,8 +338,8 @@ class Block{
return false;
}
public function addVelocityToEntity(Entity $entity, Vector3 $vector) : void{
public function addVelocityToEntity(Entity $entity) : ?Vector3{
return null;
}
final public function getPos() : Position{

View File

@@ -234,13 +234,11 @@ abstract class Liquid extends Transparent{
return $this->flowVector = $vector->normalize();
}
public function addVelocityToEntity(Entity $entity, Vector3 $vector) : void{
public function addVelocityToEntity(Entity $entity) : ?Vector3{
if($entity->canBeMovedByCurrents()){
$flow = $this->getFlowVector();
$vector->x += $flow->x;
$vector->y += $flow->y;
$vector->z += $flow->z;
return $this->getFlowVector();
}
return null;
}
abstract public function tickRate() : int;

View File

@@ -1262,13 +1262,16 @@ abstract class Entity{
}
protected function checkBlockCollision() : void{
$vector = new Vector3(0, 0, 0);
$vectors = [];
foreach($this->getBlocksAround() as $block){
$block->onEntityInside($this);
$block->addVelocityToEntity($this, $vector);
if(($v = $block->addVelocityToEntity($this)) !== null){
$vectors[] = $v;
}
}
$vector = Vector3::sum(...$vectors);
if($vector->lengthSquared() > 0){
$d = 0.014;
$this->motion = $this->motion->addVector($vector->normalize()->multiply($d));