Fall damage is now working

This commit is contained in:
Shoghi Cervantes 2014-07-10 16:16:11 +02:00
parent c9535162bd
commit 828f7f1590
5 changed files with 59 additions and 34 deletions

View File

@ -1298,6 +1298,17 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
}else{*/ }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)){ if(!$this->setPositionAndRotation($newPos, $packet->yaw, $packet->pitch)){
$pk = new MovePlayerPacket(); $pk = new MovePlayerPacket();
$pk->eid = 0; $pk->eid = 0;
@ -1309,7 +1320,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->yaw = $this->yaw; $pk->yaw = $this->yaw;
$this->directDataPacket($pk); $this->directDataPacket($pk);
} }
//}
break; break;
case ProtocolInfo::PLAYER_EQUIPMENT_PACKET: case ProtocolInfo::PLAYER_EQUIPMENT_PACKET:

View File

@ -663,10 +663,13 @@ abstract class Entity extends Position implements Metadatable{
} }
public function move($dx, $dy, $dz){ public function move($dx, $dy, $dz){
$boundingBox = clone $this->boundingBox;
//$collision = []; //$collision = [];
//$this->checkBlockCollision($collision); //$this->checkBlockCollision($collision);
if($dx == 0 and $dz == 0 and $dy == 0){ if($dx == 0 and $dz == 0 and $dy == 0){
return; return true;
} }
$ox = $this->x; $ox = $this->x;
@ -713,7 +716,7 @@ abstract class Entity extends Position implements Metadatable{
//TODO: big messy loop //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; $dy = 0;
$dx = 0; $dx = 0;
$dz = 0; $dz = 0;
@ -722,7 +725,7 @@ abstract class Entity extends Position implements Metadatable{
$fallingFlag = $this->onGround or ($dy != $movY and $movY < 0); $fallingFlag = $this->onGround or ($dy != $movY and $movY < 0);
if($dx != 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; $dy = 0;
$dx = 0; $dx = 0;
$dz = 0; $dz = 0;
@ -730,7 +733,7 @@ abstract class Entity extends Position implements Metadatable{
} }
if($dz != 0){ 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; $dy = 0;
$dx = 0; $dx = 0;
$dz = 0; $dz = 0;
@ -745,15 +748,15 @@ abstract class Entity extends Position implements Metadatable{
$dx = $movX; $dx = $movX;
$dy = 0; $dy = 0;
$dz = $movZ; $dz = $movZ;
$oldBB = clone $this->boundingBox; $oldBB = clone $boundingBox;
$list = $this->getLevel()->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox($movX, $dy, $movZ)); $list = $this->getLevel()->getCollisionCubes($this, $boundingBox->getOffsetBoundingBox($movX, $dy, $movZ));
foreach($list as $bb){ 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){ if($movY != $dy){
$dx = 0; $dx = 0;
@ -762,10 +765,10 @@ abstract class Entity extends Position implements Metadatable{
} }
foreach($list as $bb){ 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){ if($movX != $dx){
$dx = 0; $dx = 0;
@ -774,10 +777,10 @@ abstract class Entity extends Position implements Metadatable{
} }
foreach($list as $bb){ 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){ if($movZ != $dz){
$dx = 0; $dx = 0;
@ -788,23 +791,22 @@ abstract class Entity extends Position implements Metadatable{
if($movY != $dy){ if($movY != $dy){
$dy = 0; $dy = 0;
foreach($list as $bb){ foreach($list as $bb){
$dy = $bb->calculateYOffset($this->boundingBox, $dy); $dy = $bb->calculateYOffset($boundingBox, $dy);
} }
$boundingBox->addCoord(0, $dy, 0);
$this->boundingBox->addCoord(0, $dy, 0);
} }
if($cx * $cx + $cz * $cz > $dx * $dx + $dz * $dz){ if($cx * $cx + $cz * $cz > $dx * $dx + $dz * $dz){
$dx = $cx; $dx = $cx;
$dy = $cy; $dy = $cy;
$dz = $cz; $dz = $cz;
$this->boundingBox->setBB($oldBB); $boundingBox->setBB($oldBB);
} }
} }
$this->x = ($this->boundingBox->minX + $this->boundingBox->maxX) / 2; $x = ($boundingBox->minX + $boundingBox->maxX) / 2;
$this->y = $this->boundingBox->minY + $this->height; $y = $boundingBox->minY + $this->height;
$this->z = ($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2; $z = ($boundingBox->minZ + $boundingBox->maxZ) / 2;
$this->onGround = $movY != $dy and $movY < 0; $this->onGround = $movY != $dy and $movY < 0;
$this->updateFallState($dy, $this->onGround); $this->updateFallState($dy, $this->onGround);
@ -821,18 +823,23 @@ abstract class Entity extends Position implements Metadatable{
$this->motionZ = 0; $this->motionZ = 0;
} }
$this->boundingBox->addCoord($dx, $dy, $dz); $boundingBox->addCoord($dx, $dy, $dz);
$this->x += $dx; $x += $dx;
$this->y += $dy; $y += $dy;
$this->z += $dz; $z += $dz;
$cx = $this->x - $ox; if(!$this->setPosition(new Vector3($x, $y, $z))){
$cy = $this->y - $oy; return false;
$cz = $this->z - $oz; }
//$cx = $this->x - $ox;
//$cy = $this->y - $oy;
//$cz = $this->z - $oz;
//TODO: vehicle collision events (first we need to spawn them!) //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->yaw = $yaw;
$this->pitch = $pitch; $this->pitch = $pitch;
$this->scheduleUpdate(); $this->scheduleUpdate();
return true;
} }
public function setPosition(Vector3 $pos, $force = false){ public function setPosition(Vector3 $pos, $force = false){

View File

@ -64,12 +64,12 @@ abstract class Living extends Entity implements Damageable{
$pk->event = 2; //Ouch! $pk->event = 2; //Ouch!
Server::broadcastPacket($this->hasSpawned, $pk); Server::broadcastPacket($this->hasSpawned, $pk);
$this->setLastDamageCause($source); $this->setLastDamageCause($source);
$motion = new Vector3(0, 0.25, 0); $motion = new Vector3(0, 0, 0);
if($source instanceof EntityDamageByEntityEvent){ if($source instanceof EntityDamageByEntityEvent){
$e = $source->getDamager(); $e = $source->getDamager();
$deltaX = $this->x - $e->x; $deltaX = $this->x - $e->x;
$deltaZ = $this->z - $e->z; $deltaZ = $this->z - $e->z;
$yaw = atan2($deltaX, $deltaZ); $yaw = atan2($deltaX, $deltaZ);
$motion->x = sin($yaw) * 0.5; $motion->x = sin($yaw) * 0.5;
$motion->z = cos($yaw) * 0.5; $motion->z = cos($yaw) * 0.5;
} }

View File

@ -624,7 +624,7 @@ class Level implements ChunkManager, Metadatable{
//TODO: fix this //TODO: fix this
foreach($this->getCollidingEntities($bb->expand(0.25, 0.25, 0.25), $entity) as $ent){ foreach($this->getCollidingEntities($bb->expand(0.25, 0.25, 0.25), $entity) as $ent){
$collides[] = $ent->boundingBox; $collides[] = clone $ent->boundingBox;
} }
return $collides; return $collides;
@ -639,9 +639,11 @@ class Level implements ChunkManager, Metadatable{
* @return Block * @return Block
*/ */
public function getBlock(Vector3 $pos){ public function getBlock(Vector3 $pos){
$blockId = null; $blockId = 0;
$meta = null; $meta = 0;
$this->getChunkAt($pos->x >> 4, $pos->z >> 4, true)->getBlock($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f, $blockId, $meta); 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)); return Block::get($blockId, $meta, Position::fromObject(clone $pos, $this));
} }
@ -658,6 +660,10 @@ class Level implements ChunkManager, Metadatable{
* @return bool * @return bool
*/ */
public function setBlock(Vector3 $pos, Block $block, $direct = false, $update = true){ 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($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)){ if(!($pos instanceof Position)){
$pos = new Position($pos->x, $pos->y, $pos->z, $this); $pos = new Position($pos->x, $pos->y, $pos->z, $this);

View File

@ -153,7 +153,7 @@ abstract class BaseChunk implements Chunk{
} }
public function getBlock($x, $y, $z, &$blockId, &$meta = null){ 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){ public function setBlock($x, $y, $z, $blockId = null, $meta = null){