entities will collide with blocks like door, glass pane, iron bars

This commit is contained in:
GameHerobrine 2023-10-27 16:28:32 +03:00
parent df54826e4c
commit f3d726cddf
10 changed files with 107 additions and 8 deletions

View file

@ -351,8 +351,6 @@ class Entity extends Position
"target" => $item->eid
)) !== false){
$item->close();
// $item->spawntime = 0;
// $this->server->schedule(15, array($item, "close"));
}
}
@ -572,11 +570,13 @@ class Entity extends Position
$water = $y == ($y1 - 1);
$this->fallDistance = 0;
}
if(StaticBlock::getIsSolid($b)){
$blockBounds = StaticBlock::getBoundingBoxForBlockCoords($b, $x, $y, $z);
$this->speedY = $blockBounds->calculateYOffset($this->boundingBox, $this->speedY);
$this->speedX = $blockBounds->calculateXOffset($this->boundingBox, $this->speedX);
$this->speedZ = $blockBounds->calculateZOffset($this->boundingBox, $this->speedZ);
if($b != 0){
$blockBounds = Block::$class[$b]::getCollisionBoundingBoxes($this->level, $x, $y, $z, $this); //StaticBlock::getBoundingBoxForBlockCoords($b, $x, $y, $z);
foreach($blockBounds as $blockBound){
$this->speedY = $blockBound->calculateYOffset($this->boundingBox, $this->speedY);
$this->speedX = $blockBound->calculateXOffset($this->boundingBox, $this->speedX);
$this->speedZ = $blockBound->calculateZOffset($this->boundingBox, $this->speedZ);
}
}
}
}

View file

@ -157,6 +157,9 @@ abstract class Block extends Position{
public static function onRandomTick(Level $level, $x, $y, $z){}
public static function fallOn(Level $level, $x, $y, $z, Entity $entity, $fallDistance){}
public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){
return [new AxisAlignedBB($x, $y, $z, $x, $y, $z)];
}
public function __construct($id, $meta = 0, $name = "Unknown"){
$this->id = (int) $id;
$this->meta = (int) $meta;

View file

@ -10,6 +10,29 @@ class DoorBlock extends TransparentBlock{
parent::__construct($id, $meta, $name);
$this->isSolid = false;
}
//TODO opened doors fix
public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){
$aabb = new AxisAlignedBB(0, 0, 0, 1, 2, 1);
switch(self::getState($level->level->getBlockDamage($x, $y, $z))){
case 0:
$aabb->setBounds(0, 0, 0, 1, 1, 0.1875);
break;
case 1:
$aabb->setBounds(1 - 0.1875, 0, 0, 1, 1, 1);
break;
case 2:
$aabb->setBounds(0, 0, 1 - 0.1875, 1, 1, 1);
break;
case 3:
$aabb->setBounds(0, 0, 0, 0.1875, 1, 1);
break;
}
return [$aabb->offset($x, $y, $z)];
}
public static function getState($meta){
return ($meta & 4) == 0 ? ($meta - 1) & 3 : $meta & 3;
}
/**
* @param int $type

View file

@ -11,4 +11,8 @@ class SolidBlock extends GenericBlock{
$this->isSolid = true;
$this->isFullBlock = true;
}
public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){
return [new AxisAlignedBB($x, $y, $z, $x + 1, $y + 1, $z + 1)];
}
}

View file

@ -1,12 +1,13 @@
<?php
class FenceBlock extends TransparentBlock{
public function __construct(){
parent::__construct(FENCE, 0, "Fence");
$this->isFullBlock = false;
$this->hardness = 15;
}
//TODO public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity)
}

View file

@ -1,6 +1,9 @@
<?php
class FenceGateBlock extends TransparentBlock{
//TODO public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity)
public function __construct($meta = 0){
parent::__construct(FENCE_GATE, $meta, "Fence Gate");
$this->isActivable = true;

View file

@ -6,6 +6,34 @@ class GlassPaneBlock extends TransparentBlock{
$this->isFullBlock = false;
$this->isSolid = false;
}
public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){
$var8 = self::canConnectTo($level->level->getBlockID($x, $y, $z - 1));
$var9 = self::canConnectTo($level->level->getBlockID($x, $y, $z + 1));
$var10 = self::canConnectTo($level->level->getBlockID($x - 1, $y, $z));
$var11 = self::canConnectTo($level->level->getBlockID($x + 1, $y, $z));
$aabb = new AxisAlignedBB($x, $y, $z, $x, $y, $z);
$arr = [];
if((!$var10 || !$var11) && ($var10 || $var11 || $var8 || $var9)){
if($var10 && !$var11) $arr[] = $aabb->addMinMax(0, 0, 0.4375, 0.5, 1, 0.5625);
elseif(!$var10 && $var11) $arr[] = $aabb->addMinMax(0.5, 0, 0.4375, 1, 1, 0.5625);
}else{
$arr[] = $aabb->addMinMax(0, 0, 0.4375, 1, 1, 0.5625);
}
if((!$var8 || !$var9) && ($var10 || $var11 || $var8 || $var9)){
if($var8 && !$var9) $arr[] = $aabb->addMinMax(0.4375, 0, 0, 0.5625, 1, 0.5);
elseif(!$var8 && $var9) $arr[] = $aabb->addMinMax(0.4375, 0, 0.5, 0.5625, 1, 1);
}else{
$arr[] = $aabb->addMinMax(0.4375, 0, 0, 0.5625, 1, 1);
}
return $arr;
}
public static function canConnectTo($blockID) : bool{
return StaticBlock::getIsSolid($blockID) || $blockID == GLASS_PANE || $blockID == GLASS;
}
public function getDrops(Item $item, Player $player){
return array(
array(GLASS_PANE, 0, 0),

View file

@ -7,4 +7,32 @@ class IronBarsBlock extends TransparentBlock{
$this->isSolid = false;
}
public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){
$var8 = self::canConnectTo($level->level->getBlockID($x, $y, $z - 1));
$var9 = self::canConnectTo($level->level->getBlockID($x, $y, $z + 1));
$var10 = self::canConnectTo($level->level->getBlockID($x - 1, $y, $z));
$var11 = self::canConnectTo($level->level->getBlockID($x + 1, $y, $z));
$aabb = new AxisAlignedBB($x, $y, $z, $x, $y, $z);
$arr = [];
if((!$var10 || !$var11) && ($var10 || $var11 || $var8 || $var9)){
if($var10 && !$var11) $arr[] = $aabb->addMinMax(0, 0, 0.4375, 0.5, 1, 0.5625);
elseif(!$var10 && $var11) $arr[] = $aabb->addMinMax(0.5, 0, 0.4375, 1, 1, 0.5625);
}else{
$arr[] = $aabb->addMinMax(0, 0, 0.4375, 1, 1, 0.5625);
}
if((!$var8 || !$var9) && ($var10 || $var11 || $var8 || $var9)){
if($var8 && !$var9) $arr[] = $aabb->addMinMax(0.4375, 0, 0, 0.5625, 1, 0.5);
elseif(!$var8 && $var9) $arr[] = $aabb->addMinMax(0.4375, 0, 0.5, 0.5625, 1, 1);
}else{
$arr[] = $aabb->addMinMax(0.4375, 0, 0, 0.5625, 1, 1);
}
return $arr;
}
public static function canConnectTo($blockID) : bool{
return StaticBlock::getIsSolid($blockID) || $blockID == GLASS_PANE || $blockID == GLASS; //glass_pane & glass, in 1.5.2 iron fence is just BlockPane which is glass pane
}
}

View file

@ -22,6 +22,11 @@ class SlabBlock extends TransparentBlock{
$this->hardness = 30;
}
public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){
if(($level->level->getBlockDamage($x, $y, $z) & 0x08) == 0x08) return [new AxisAlignedBB($x, $y, $z, $x + 1, $y + 1, $z + 1)];
return [new AxisAlignedBB($x, $y, $z, $x + 1, $y + 0.5, $z + 1)];
}
public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
$this->meta &= 0x07;
if($face === 0){

View file

@ -28,6 +28,10 @@ class AxisAlignedBB{
return $this;
}
public function addMinMax($minX, $minY, $minZ, $maxX, $maxY, $maxZ){
return new AxisAlignedBB($this->minX + $minX, $this->minY + $minY, $this->minZ + $minZ, $this->maxX + $maxX, $this->maxY + $maxY, $this->maxZ + $maxZ);
}
public function addCoord($x, $y, $z){
$minX = $this->minX;
$minY = $this->minY;