From 828f7f159068737f87706bc0166feefa98f581c6 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Thu, 10 Jul 2014 16:16:11 +0200 Subject: [PATCH] Fall damage is now working --- src/pocketmine/Player.php | 12 +++- src/pocketmine/entity/Entity.php | 61 +++++++++++-------- src/pocketmine/entity/Living.php | 4 +- src/pocketmine/level/Level.php | 14 +++-- .../level/format/generic/BaseChunk.php | 2 +- 5 files changed, 59 insertions(+), 34 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 86b3a7873..27dcd3b50 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -1298,6 +1298,17 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } }else{*/ + $dy = $newPos->y - $this->y; + + if(count($this->getLevel()->getCollisionBlocks($this->boundingBox->getOffsetBoundingBox(0, $dy - 0.1, 0))) > 0){ + $isColliding = true; + }else{ + $isColliding = false; + } + + $this->onGround = ($dy <= 0 and $isColliding); + $this->updateFallState($dy, $this->onGround); + if(!$this->setPositionAndRotation($newPos, $packet->yaw, $packet->pitch)){ $pk = new MovePlayerPacket(); $pk->eid = 0; @@ -1309,7 +1320,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $pk->yaw = $this->yaw; $this->directDataPacket($pk); } - //} break; case ProtocolInfo::PLAYER_EQUIPMENT_PACKET: diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 0eef27859..610b8a911 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -663,10 +663,13 @@ abstract class Entity extends Position implements Metadatable{ } public function move($dx, $dy, $dz){ + + $boundingBox = clone $this->boundingBox; + //$collision = []; //$this->checkBlockCollision($collision); if($dx == 0 and $dz == 0 and $dy == 0){ - return; + return true; } $ox = $this->x; @@ -713,7 +716,7 @@ abstract class Entity extends Position implements Metadatable{ //TODO: big messy loop }*/ - if(count($this->getLevel()->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox(0, $dy, 0))) > 0){ + if(count($this->getLevel()->getCollisionCubes($this, $boundingBox->getOffsetBoundingBox(0, $dy, 0))) > 0){ $dy = 0; $dx = 0; $dz = 0; @@ -722,7 +725,7 @@ abstract class Entity extends Position implements Metadatable{ $fallingFlag = $this->onGround or ($dy != $movY and $movY < 0); if($dx != 0){ - if(count($this->getLevel()->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox($dx, 0, 0))) > 0){ + if(count($this->getLevel()->getCollisionCubes($this, $boundingBox->getOffsetBoundingBox($dx, 0, 0))) > 0){ $dy = 0; $dx = 0; $dz = 0; @@ -730,7 +733,7 @@ abstract class Entity extends Position implements Metadatable{ } if($dz != 0){ - if(count($this->getLevel()->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox(0, 0, $dz))) > 0){ + if(count($this->getLevel()->getCollisionCubes($this, $boundingBox->getOffsetBoundingBox(0, 0, $dz))) > 0){ $dy = 0; $dx = 0; $dz = 0; @@ -745,15 +748,15 @@ abstract class Entity extends Position implements Metadatable{ $dx = $movX; $dy = 0; $dz = $movZ; - $oldBB = clone $this->boundingBox; - $list = $this->getLevel()->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox($movX, $dy, $movZ)); + $oldBB = clone $boundingBox; + $list = $this->getLevel()->getCollisionCubes($this, $boundingBox->getOffsetBoundingBox($movX, $dy, $movZ)); foreach($list as $bb){ - $dy = $bb->calculateYOffset($this->boundingBox, $dy); + $dy = $bb->calculateYOffset($boundingBox, $dy); } - $this->boundingBox->addCoord(0, $dy, 0); + $boundingBox->addCoord(0, $dy, 0); if($movY != $dy){ $dx = 0; @@ -762,10 +765,10 @@ abstract class Entity extends Position implements Metadatable{ } foreach($list as $bb){ - $dx = $bb->calculateXOffset($this->boundingBox, $dx); + $dx = $bb->calculateXOffset($boundingBox, $dx); } - $this->boundingBox->addCoord($dx, 0, 0); + $boundingBox->addCoord($dx, 0, 0); if($movX != $dx){ $dx = 0; @@ -774,10 +777,10 @@ abstract class Entity extends Position implements Metadatable{ } foreach($list as $bb){ - $dz = $bb->calculateZOffset($this->boundingBox, $dz); + $dz = $bb->calculateZOffset($boundingBox, $dz); } - $this->boundingBox->addCoord(0, 0, $dz); + $boundingBox->addCoord(0, 0, $dz); if($movZ != $dz){ $dx = 0; @@ -788,23 +791,22 @@ abstract class Entity extends Position implements Metadatable{ if($movY != $dy){ $dy = 0; foreach($list as $bb){ - $dy = $bb->calculateYOffset($this->boundingBox, $dy); + $dy = $bb->calculateYOffset($boundingBox, $dy); } - - $this->boundingBox->addCoord(0, $dy, 0); + $boundingBox->addCoord(0, $dy, 0); } if($cx * $cx + $cz * $cz > $dx * $dx + $dz * $dz){ $dx = $cx; $dy = $cy; $dz = $cz; - $this->boundingBox->setBB($oldBB); + $boundingBox->setBB($oldBB); } } - $this->x = ($this->boundingBox->minX + $this->boundingBox->maxX) / 2; - $this->y = $this->boundingBox->minY + $this->height; - $this->z = ($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2; + $x = ($boundingBox->minX + $boundingBox->maxX) / 2; + $y = $boundingBox->minY + $this->height; + $z = ($boundingBox->minZ + $boundingBox->maxZ) / 2; $this->onGround = $movY != $dy and $movY < 0; $this->updateFallState($dy, $this->onGround); @@ -821,18 +823,23 @@ abstract class Entity extends Position implements Metadatable{ $this->motionZ = 0; } - $this->boundingBox->addCoord($dx, $dy, $dz); - $this->x += $dx; - $this->y += $dy; - $this->z += $dz; + $boundingBox->addCoord($dx, $dy, $dz); + $x += $dx; + $y += $dy; + $z += $dz; - $cx = $this->x - $ox; - $cy = $this->y - $oy; - $cz = $this->z - $oz; + if(!$this->setPosition(new Vector3($x, $y, $z))){ + return false; + } + + //$cx = $this->x - $ox; + //$cy = $this->y - $oy; + //$cz = $this->z - $oz; //TODO: vehicle collision events (first we need to spawn them!) + return true; } /** @@ -873,6 +880,8 @@ abstract class Entity extends Position implements Metadatable{ $this->yaw = $yaw; $this->pitch = $pitch; $this->scheduleUpdate(); + + return true; } public function setPosition(Vector3 $pos, $force = false){ diff --git a/src/pocketmine/entity/Living.php b/src/pocketmine/entity/Living.php index d62504ef1..27bba8bcc 100644 --- a/src/pocketmine/entity/Living.php +++ b/src/pocketmine/entity/Living.php @@ -64,12 +64,12 @@ abstract class Living extends Entity implements Damageable{ $pk->event = 2; //Ouch! Server::broadcastPacket($this->hasSpawned, $pk); $this->setLastDamageCause($source); - $motion = new Vector3(0, 0.25, 0); + $motion = new Vector3(0, 0, 0); if($source instanceof EntityDamageByEntityEvent){ $e = $source->getDamager(); $deltaX = $this->x - $e->x; $deltaZ = $this->z - $e->z; - $yaw = atan2($deltaX, $deltaZ); + $yaw = atan2($deltaX, $deltaZ); $motion->x = sin($yaw) * 0.5; $motion->z = cos($yaw) * 0.5; } diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index bce55d5ed..ffd584d1c 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -624,7 +624,7 @@ class Level implements ChunkManager, Metadatable{ //TODO: fix this foreach($this->getCollidingEntities($bb->expand(0.25, 0.25, 0.25), $entity) as $ent){ - $collides[] = $ent->boundingBox; + $collides[] = clone $ent->boundingBox; } return $collides; @@ -639,9 +639,11 @@ class Level implements ChunkManager, Metadatable{ * @return Block */ public function getBlock(Vector3 $pos){ - $blockId = null; - $meta = null; - $this->getChunkAt($pos->x >> 4, $pos->z >> 4, true)->getBlock($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f, $blockId, $meta); + $blockId = 0; + $meta = 0; + if($pos->y >= 0 and $pos->y < 128){ + $this->getChunkAt($pos->x >> 4, $pos->z >> 4, true)->getBlock($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f, $blockId, $meta); + } return Block::get($blockId, $meta, Position::fromObject(clone $pos, $this)); } @@ -658,6 +660,10 @@ class Level implements ChunkManager, Metadatable{ * @return bool */ public function setBlock(Vector3 $pos, Block $block, $direct = false, $update = true){ + if($pos->y < 0 or $pos->y >= 128){ + return false; + } + if($this->getChunkAt($pos->x >> 4, $pos->z >> 4, true)->setBlock($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f, $block->getID(), $block->getDamage())){ if(!($pos instanceof Position)){ $pos = new Position($pos->x, $pos->y, $pos->z, $this); diff --git a/src/pocketmine/level/format/generic/BaseChunk.php b/src/pocketmine/level/format/generic/BaseChunk.php index e6057c4b3..9e6d8109c 100644 --- a/src/pocketmine/level/format/generic/BaseChunk.php +++ b/src/pocketmine/level/format/generic/BaseChunk.php @@ -153,7 +153,7 @@ abstract class BaseChunk implements Chunk{ } public function getBlock($x, $y, $z, &$blockId, &$meta = null){ - return $this->sections[$y >> 4]->getBlock($x, $y & 0x0f, $z, $blockId, $meta); + $this->sections[$y >> 4]->getBlock($x, $y & 0x0f, $z, $blockId, $meta); } public function setBlock($x, $y, $z, $blockId = null, $meta = null){