Basic entity motion on water

This commit is contained in:
Shoghi Cervantes 2014-09-27 00:09:38 +02:00
parent 529bf743db
commit 48041b2f19
6 changed files with 65 additions and 7 deletions

View File

@ -149,7 +149,10 @@ class Liquid extends Transparent{
}
public function addVelocityToEntity(Entity $entity, Vector3 $vector){
$vector->add($this->getFlowVector());
$flow = $this->getFlowVector();
$vector->x += $flow->x;
$vector->y += $flow->y;
$vector->z += $flow->z;
}
public function tickRate(){

View File

@ -50,8 +50,8 @@ class Torch extends Flowable{
];
if($this->getSide($faces[$side])->isTransparent === true and !($side === 0 and $this->getSide(0)->getID() === self::FENCE)){ //Replace with common break method
$this->getLevel()->dropItem($this, Item::get(Item::TORCH));
$this->getLevel()->setBlock($this, new Air(), true);
$this->getLevel()->dropItem($this->add(0.5, 0.5, 0.5), Item::get(Item::TORCH));
return Level::BLOCK_UPDATE_NORMAL;
}

View File

@ -34,6 +34,7 @@ use pocketmine\event\entity\EntityMoveEvent;
use pocketmine\event\entity\EntitySpawnEvent;
use pocketmine\event\entity\EntityTeleportEvent;
use pocketmine\event\Timings;
use pocketmine\item\Item;
use pocketmine\level\format\Chunk;
use pocketmine\level\format\FullChunk;
use pocketmine\level\Level;
@ -457,8 +458,9 @@ abstract class Entity extends Position implements Metadatable{
//TODO: check vehicles
$this->justCreated = false;
$isPlayer = $this instanceof Player;
if($this->dead === true and !($this instanceof Player)){
if($this->dead === true and !$isPlayer){
$this->close();
return false;
@ -471,7 +473,7 @@ abstract class Entity extends Position implements Metadatable{
$this->checkBlockCollision();
if($this->handleWaterMovement()){
if(!$isPlayer and $this->handleWaterMovement()){
$this->fallDistance = 0;
$this->inWater = true;
$this->extinguish();
@ -674,8 +676,8 @@ abstract class Entity extends Position implements Metadatable{
}
}
public function handleWaterMovement(){ //TODO
public function handleWaterMovement(){
return $this->getLevel()->handleBlockAcceleration($this->boundingBox->grow(0, -0.4, 0)->shrink(0.001, 0.001, 0.001), Block::WATER, $this);
}
public function handleLavaMovement(){ //TODO

View File

@ -65,7 +65,7 @@ class FallingBlock extends Entity{
public function onUpdate(){
$this->entityBaseTick();
if($this->closed !== false){
if($this->closed or $this->dead){
return false;
}

View File

@ -35,6 +35,7 @@ use pocketmine\block\Grass;
use pocketmine\block\Ice;
use pocketmine\block\Leaves;
use pocketmine\block\Leaves2;
use pocketmine\block\Liquid;
use pocketmine\block\MelonStem;
use pocketmine\block\Mycelium;
use pocketmine\block\Potato;
@ -948,6 +949,7 @@ class Level implements ChunkManager, Metadatable{
* @param Vector3 $source
* @param Item $item
* @param Vector3 $motion
* @param int $delay
*/
public function dropItem(Vector3 $source, Item $item, Vector3 $motion = null, $delay = 10){
$motion = $motion === null ? new Vector3(lcg_value() * 0.2 - 0.1, 0.2, lcg_value() * 0.2 - 0.1) : $motion;
@ -2041,6 +2043,50 @@ class Level implements ChunkManager, Metadatable{
}
}
/**
* @param AxisAlignedBB $boundingBox
* @param int $blockMaterial
* @param Entity $entity
*
* @return boolean
*/
public function handleBlockAcceleration(AxisAlignedBB $boundingBox, $blockMaterial, Entity $entity){
$minX = Math::floorFloat($boundingBox->minX);
$minY = Math::floorFloat($boundingBox->minY);
$minZ = Math::floorFloat($boundingBox->minZ);
$maxX = Math::floorFloat($boundingBox->maxX + 1);
$maxY = Math::floorFloat($boundingBox->maxY + 1);
$maxZ = Math::floorFloat($boundingBox->maxZ + 1);
$vector = new Vector3(0, 0, 0);
$flag = false;
for($z = $minZ; $z <= $maxZ; ++$z){
for($x = $minX; $x <= $maxX; ++$x){
for($y = $minY; $y <= $maxY; ++$y){
$block = $this->getBlock(new Vector3($x, $y, $z));
if($block !== null and $block->getID() === $blockMaterial){
$d = $y + 1 - ($block instanceof Liquid ? $block->getFluidHeightPercent() : 1);
if($maxY >= $d){
$block->addVelocityToEntity($entity, $vector);
$flag = true;
}
}
}
}
}
if($vector->length() > 0){
$vector = $vector->normalize();
$d = 0.014;
$entity->motionX += $vector->x * $d;
$entity->motionY += $vector->y * $d;
$entity->motionZ += $vector->z * $d;
}
return $flag;
}
public function setMetadata($metadataKey, MetadataValue $metadataValue){
$this->server->getPlayerMetadata()->setMetadata($this, $metadataKey, $metadataValue);

View File

@ -267,6 +267,13 @@ class McRegion extends BaseLevelProvider{
$chunk->setX($chunkX);
$chunk->setZ($chunkZ);
if($this->getChunk($chunkX, $chunkZ, false) !== $chunk){
$this->unloadChunk($chunkX, $chunkZ, false);
}
$this->chunks[Level::chunkHash($chunkX, $chunkZ)] = $chunk;
}