mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 12:09:43 +00:00
entity collisions
This commit is contained in:
parent
2ce333af5e
commit
3b15c26b0c
13 changed files with 104 additions and 34 deletions
|
|
@ -1879,7 +1879,7 @@ class Player{
|
|||
* powerMax is 1, powerMin is 0.1
|
||||
* args: xvel, yvel, zvel, (power+power)*1.5, 1.0
|
||||
*/
|
||||
$e->critical = ($power === 1);
|
||||
$e->critical = ($power == 1);
|
||||
$e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0);
|
||||
$this->server->api->entity->spawnToAll($e);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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){
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -20,6 +20,10 @@ class Painting extends Entity{
|
|||
];
|
||||
}
|
||||
|
||||
public function isPickable(){
|
||||
return true;
|
||||
}
|
||||
|
||||
public function createSaveData(){
|
||||
$data = parent::createSaveData();
|
||||
|
||||
|
|
|
|||
|
|
@ -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"];
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -220,14 +220,19 @@ class AxisAlignedBB{
|
|||
public function isVectorInXY(Vector3 $vector){
|
||||
return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->y >= $this->minY and $vector->y <= $this->maxY;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Vector3 $pos1
|
||||
* @param Vector3 $pos2
|
||||
* @return NULL|MovingObjectPosition
|
||||
*/
|
||||
public function calculateIntercept(Vector3 $pos1, Vector3 $pos2){
|
||||
$v1 = $pos1->getIntermediateWithXValue($pos2, $this->minX);
|
||||
$v2 = $pos1->getIntermediateWithXValue($pos2, $this->maxX);
|
||||
$v3 = $pos1->getIntermediateWithYValue($pos2, $this->minY);
|
||||
$v4 = $pos1->getIntermediateWithYValue($pos2, $this->maxY);
|
||||
$v5 = $pos1->getIntermediateWithZValue($pos2, $this->minZ);
|
||||
$v6 = $pos1->getIntermediateWithZValue($pos2, $this->maxZ);
|
||||
$v1 = $pos1->clipX($pos2, $this->minX);
|
||||
$v2 = $pos1->clipX($pos2, $this->maxX);
|
||||
$v3 = $pos1->clipY($pos2, $this->minY);
|
||||
$v4 = $pos1->clipY($pos2, $this->maxY);
|
||||
$v5 = $pos1->clipZ($pos2, $this->minZ);
|
||||
$v6 = $pos1->clipZ($pos2, $this->maxZ);
|
||||
|
||||
if($v1 !== null and !$this->isVectorInYZ($v1)){
|
||||
$v1 = null;
|
||||
|
|
|
|||
|
|
@ -215,7 +215,7 @@ class Vector3{
|
|||
}
|
||||
}
|
||||
public function getIntermediateWithXValue(Vector3 $v, $y){
|
||||
return $this->clipZ($v, $y);
|
||||
return $this->clipX($v, $y);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -630,7 +630,10 @@ class Level{
|
|||
}
|
||||
return $ents;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AxisAlignedBB $bb
|
||||
* @return Entity[]
|
||||
*/
|
||||
public function getEntitiesInAABB(AxisAlignedBB $bb){
|
||||
$minChunkX = ((int)($bb->minX)) >> 4;
|
||||
$minChunkZ = ((int)($bb->minZ)) >> 4;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue