entity collisions

This commit is contained in:
GameHerobrine 2024-03-24 01:52:15 +03:00
parent 2ce333af5e
commit 3b15c26b0c
13 changed files with 104 additions and 34 deletions

View file

@ -10,7 +10,6 @@ class Entity extends Position
public $counter = 0;
public $fallDistance = 0;
public static $updateOnTick, $allowedAI;
public $isCollidable;
public $canBeAttacked;
public $moveTime, $lookTime, $idleTime, $knockbackTime = 0;
public $needsUpdate = true;
@ -167,6 +166,7 @@ class Entity extends Position
$this->hasGravity = true;
$this->canBeAttacked = true;
$this->fireResistance = 20;
break;
case ENTITY_OBJECT:
$this->x = isset($this->data["TileX"]) ? $this->data["TileX"] : $this->x;
@ -367,6 +367,14 @@ class Entity extends Position
}
}
/**
* Can entity be collided with or not
* @return boolean
*/
public function isPickable(){
return $this->isPlayer() ? !$this->dead : false;
}
public function handleLavaMovement(){ //TODO maybe try merging with water?
$this->inLava = $this->level->isLavaInBB($this->boundingBox->expand(-0.1, -0.4, -0.1));
return $this->inLava;

View file

@ -30,6 +30,10 @@ abstract class Living extends Entity implements Pathfindable{
//if(self::$despawnMobs) $this->server->schedule(self::$despawnTimer, [$this, "close"]); //900*20
}
public function isPickable(){
return !$this->dead;
}
public function fall(){
$dmg = ceil($this->fallDistance - 3);
if($dmg > 0){

View file

@ -15,7 +15,12 @@ class TaskEatTileGoal extends TaskBase
public function onUpdate(EntityAI $ai)
{
if (--$this->selfCounter === 4)
if($ai->isStarted("TaskEatTileGoal")){
$this->reset();
return false;
}
if (--$this->selfCounter == 4)
{
$id = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y, $ai->entity->z);
$idb = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y - 1, $ai->entity->z);
@ -32,7 +37,7 @@ class TaskEatTileGoal extends TaskBase
public function canBeExecuted(EntityAI $ai)
{
if($ai->isStarted("TaskRandomWalk")) return false;
if($ai->isStarted("TaskRandomWalk") || $ai->isStarted("TaskEatTileGoal")) return false;
if(mt_rand(0, ($ai->entity instanceof Ageable && $ai->entity->isBaby()) ? 50 : 1000) == 0){
$idm = $ai->entity->level->level->getBlock($ai->entity->x, $ai->entity->y, $ai->entity->z);
$idb = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y - 1, $ai->entity->z);

View file

@ -58,19 +58,7 @@ class Arrow extends Projectile{
//$this->update();
//TODO i guess? $ticksInGround = 0;
}
public function sendMoveUpdate()
{
$pk = new MoveEntityPacket_PosRot();
$pk->eid = $this->eid;
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->yaw = $this->yaw;
$pk->pitch = $this->pitch;
foreach($this->level->players as $p){ //sending packets directly makes movement less laggy
$p->directDataPacket(clone $pk);
}
}
public function update($now){
$this->lastX = $this->x;
$this->lastY = $this->y;
@ -115,20 +103,13 @@ class Arrow extends Projectile{
$this->lastPitch = $this->pitch = atan2($this->speedY, $v1) * 180 / M_PI;
}
$v16 = $this->level->level->getBlockID($this->xTile, $this->yTile, $this->zTile);
if($v16 > 0){
//TODO this->inGround = true + checks
}
if($this->shake > 0){
--$this->shake;
}
if($this->inGround){
$this->speedZ = $this->speedX = $this->speedY = 0;
[$blockID, $blockMeta] = $this->level->level->getBlock($this->xTile, $this->yTile, $this->zTile);
$this->speedZ = $this->speedX = $this->speedY = 0;
if($blockID == $this->inTile && $blockMeta == $this->inData){
++$this->groundTicks;
@ -159,10 +140,54 @@ class Arrow extends Projectile{
$end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ);
}
$entities = $this->level->getEntitiesInAABB($this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(1, 1, 1));
$bestDist = 0;
$bestEnt = null;
foreach($entities as $eid => $ent){
if($eid != $this->eid && $ent->isPickable() && ($eid != $this->shooterEID || $this->airTicks >= 5)){
$v12 = $ent->boundingBox->expand(0.3, 0.3, 0.3);
$v13 = $v12->calculateIntercept($start, $end);
if($v13 != null){
$dist = $start->distance($v13->hitVector);
if($dist < $bestDist || $bestDist == 0){
$bestEnt = $ent;
}
}
}
}
if($bestEnt != null){
$v4 = MovingObjectPosition::fromEntity($bestEnt);
}
//TODO entity collisions
if($v4 != null){
if($v4->entityHit != null){
//TODO entity hit
$v49 = sqrt($this->speedY*$this->speedY + $this->speedX*$this->speedX + $this->speedZ*$this->speedZ);
$damage = ceil($v49+$v49);
if($this->criticial){
$damage += mt_rand(0, $damage / 2 + 1);
}
if($v4->entityHit->harm($damage, $this->eid)){
//vanilla seems to increase arrow count if $v4->entity is mob
$this->close();
}else{
$this->speedX *= -0.1;
$this->speedY *= -0.1;
$this->speedZ *= -0.1;
$this->yaw += 180;
$this->lastYaw += 180;
$this->airTicks = 0;
}
}else{
$this->xTile = $v4->blockX;
$this->yTile = $v4->blockY;

View file

@ -16,6 +16,10 @@ class FallingSand extends Entity{
$this->gravity = 0.04;
}
public function isPickable(){
return !$this->dead;
}
public function update($now){
if($this->closed) return;

View file

@ -68,6 +68,10 @@ class Minecart extends Vehicle{
$this->yOffset = $this->height / 2;
}
public function isPickable(){
return !$this->dead;
}
public function getDrops(){
return [
[MINECART, 0, 1]

View file

@ -20,6 +20,10 @@ class Painting extends Entity{
];
}
public function isPickable(){
return true;
}
public function createSaveData(){
$data = parent::createSaveData();

View file

@ -16,6 +16,10 @@ class PrimedTNT extends Entity{
//$this->server->schedule(5, [$this, "updateFuse"], [], true);
}
public function isPickable(){
return !$this->dead;
}
public function getMetadata(){
$d = parent::getMetadata();
$d[16]["value"] = (int) $this->data["fuse"];

View file

@ -8,6 +8,10 @@ class TripodCamera extends Entity{
$this->setHealth(PHP_INT_MAX, "generic");
}
public function isPickable(){
return !$this->dead;
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() and $action === InteractPacket::ACTION_HOLD){
//todo