Clean up abhorrent mess of Slab placement code

This commit is contained in:
Dylan K. Taylor 2018-09-27 18:46:01 +01:00
parent 35d51570be
commit e038c4295d

View File

@ -78,47 +78,24 @@ abstract class Slab extends Transparent{
} }
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{
if($face === Facing::DOWN){ /* note these conditions can't be merged, since one targets clicked and the other replace */
if($blockClicked instanceof Slab and $blockClicked->isSameType($this) and $blockClicked->top){
$this->getLevel()->setBlock($blockClicked, $this->getDouble());
return true; if($blockClicked instanceof Slab and $blockClicked->isSameType($this) and (
}elseif($blockReplace->isSameType($this)){ ($face === Facing::DOWN and $blockClicked->top) or //Bottom face of top slab
$this->getLevel()->setBlock($blockReplace, $this->getDouble()); ($face === Facing::UP and !$blockClicked->top) //Top face of bottom slab
)){
return true; return $this->level->setBlock($blockClicked, $this->getDouble());
}else{
$this->top = true;
}
}elseif($face === Facing::UP){
if($blockClicked instanceof Slab and $blockClicked->isSameType($this) and !$blockClicked->top){
$this->getLevel()->setBlock($blockClicked, $this->getDouble());
return true;
}elseif($blockReplace->isSameType($this)){
$this->getLevel()->setBlock($blockReplace, $this->getDouble());
return true;
}
}else{ //TODO: collision
if($blockReplace->getId() === $this->getId()){
if($blockReplace->getVariant() === $this->variant){
$this->getLevel()->setBlock($blockReplace, $this->getDouble());
return true;
} }
return false; if($blockReplace instanceof Slab and $blockReplace->isSameType($this) and (
}else{ ($blockReplace->top and $clickVector->y <= 0.5) or
if($clickVector->y > 0.5){ (!$blockReplace->top and $clickVector->y >= 0.5)
$this->top = true; )){
} //Clicked in empty half of existing slab
} return $this->level->setBlock($blockReplace, $this->getDouble());
} }
if($blockReplace->getId() === $this->getId() and $blockClicked->getVariant() !== $this->variant){ $this->top = ($face !== Facing::UP && $clickVector->y > 0.5) || $face === Facing::DOWN;
return false;
}
return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player); return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player);
} }