From 1fa7697503ef0e5a9d33c7c78104e206e808b387 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 5 Jul 2023 11:44:26 +0300 Subject: [PATCH] arrows will attack entities --- src/API/PlayerAPI.php | 17 +++++++++-------- src/Player.php | 7 ++++--- src/entity/Entity.php | 4 ++++ src/entity/Living.php | 4 +++- src/entity/object/Arrow.php | 35 +++++++++++++++++++++++++++++++++-- src/world/Level.php | 8 +++++++- src/world/MobSpawner.php | 6 +----- 7 files changed, 61 insertions(+), 20 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 63dfb710..c26b5e75 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -61,13 +61,15 @@ class PlayerAPI{ if(is_numeric($data["cause"])){ $e = $this->server->api->entity->get($data["cause"]); if($e instanceof Entity){ - switch($e->class){ - case ENTITY_PLAYER: - $message = " was killed by " . $e->name; - break; - default: - $message = " was killed by {$e->getName()}"; - break; + if($e instanceof Arrow){ + if($e->shotByEntity && isset($this->server->api->entity->entities[$e->shooterEID]) && $this->server->api->entity->entities[$e->shooterEID] instanceof Entity){ + $message = " was shot by {$this->server->api->entity->entities[$e->shooterEID]->name}"; + }else{ + $message = " was shot"; + } + + }else{ + $message = "was killed by {$e->name}"; } } }else{ @@ -106,7 +108,6 @@ class PlayerAPI{ } $this->server->api->chat->broadcast($data["player"]->username . $message); return true; - break; } } diff --git a/src/Player.php b/src/Player.php index c00af045..bbf857d6 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1729,7 +1729,8 @@ class Player{ $e->speedX = -sin(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI); $e->speedZ = cos(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI); $e->speedY = -sin(($e->pitch / 180) * M_PI); - + $e->shooterEID = $this->entity->eid; + $e->shotByEntity = true; /** * Max usage: 72000ticks * initalPower = 72000 - (72000 - usedCtr) @@ -1747,8 +1748,8 @@ class Player{ //CANCEL but i am too lazy $power = 0.1; } - //console("Arrow sent! power: $power"); - $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); //TODO bow power + $e->critical = ($power === 1); + $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); $this->server->api->entity->spawnToAll($e); } } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 53d01a03..78bbceef 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -777,6 +777,10 @@ class Entity extends Position } } + public function canBeShot(){ + return $this->isPlayer(); + } + public function updateFallState($fallTick){ if($this->onGround && $this->fallDistance > 0){ $this->fall(); diff --git a/src/entity/Living.php b/src/entity/Living.php index ecb8fc93..d02d38f4 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -33,7 +33,9 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ unset($this->pathFollower->entity); unset($this->ai->entity); } - + public function canBeShot(){ + return true; + } public function collideHandler(){ $this->level->applyCallbackToNearbyEntities($this, [$this, "applyCollision"], 2); //TODO radiuses } diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index 69279a20..1185715d 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -2,9 +2,20 @@ class Arrow extends Projectile{ const TYPE = OBJECT_ARROW; - function __construct(Level $level, $eid, $class, $type = 0, $data = []){ + + public $criticial = false; + public $shooterEID = 0; + public $shotByEntity; + public $airTicks = 0; + public $groundTicks = 0; + function __construct(Level $level, $eid, $class, $type = 0, $data = [], $shooter = false){ parent::__construct($level, $eid, $class, $type, $data); $this->gravity = 0.05; + $this->setSize(0.5, 0.5); + $this->setName("Arrow"); + $this->shooterEID = $shooter; + $this->shotByEntity = $shooter instanceof Entity; + $this->airTicks = $this->groundTicks = 0; //$this->server->schedule(1210, array($this, "update")); //Despawn } @@ -50,13 +61,17 @@ class Arrow extends Projectile{ $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(){ //parent::update(); + if($this->closed) { + $this->close(); + return; + } + $this->needsUpdate = true; if($this->speedX != 0 or $this->speedY != 0 or $this->speedZ != 0){ @@ -67,14 +82,30 @@ class Arrow extends Projectile{ $this->x += $this->speedX; $this->y += $this->speedY; $this->z += $this->speedZ; + /**oh no, entity collision**/ + ++$this->airTicks; //TODO onGround state $this->speedX *= 0.99; $this->speedY *= 0.99; $this->speedZ *= 0.99; $this->speedY -= $this->gravity; + $bbexp = $this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(0, 0.2, 0); $this->sendMotion(); $this->updatePosition(); + + foreach($this->level->entityList as $e){ + if($e instanceof Entity && $e->eid !== $this->eid && !$e->closed && $e->canBeShot() && $e->boundingBox->intersectsWith($bbexp)){ + if($this->shotByEntity && $this->shooterEID === $e->eid && $this->airTicks < 5) continue; + $dmg = ceil(sqrt($this->speedX * $this->speedX + $this->speedY * $this->speedY + $this->speedZ * $this->speedZ) * 2); + if($this->criticial){ + $dmg += mt_rand(0, (int)($dmg/2+1)); + } + $e->harm($dmg, $this->eid); + $this->closed = true; + return; + } + } } public function spawn($player){ diff --git a/src/world/Level.php b/src/world/Level.php index c929a88e..960eaa00 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -11,7 +11,7 @@ class Level{ */ public $entityList; - public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner; + public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner, $totalMobsAmount = 0; private $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime; public static $randomUpdateBlocks = [ @@ -325,11 +325,17 @@ class Level{ } } } + $this->totalMobsAmount = 0; foreach($this->entityList as $k => $e){ if(!($e instanceof Entity)){ unset($this->entityList[$k]); continue; } + + if($e->type === ENTITY_MOB && !$e->isPlayer()){ + ++$this->totalMobsAmount; + } + if($e->needsUpdate){ $e->update(); } diff --git a/src/world/MobSpawner.php b/src/world/MobSpawner.php index bdb878b4..a0691ac4 100644 --- a/src/world/MobSpawner.php +++ b/src/world/MobSpawner.php @@ -11,11 +11,7 @@ class MobSpawner{ } public function countEntities(){ - $ents = 0; - foreach($this->level->entityList as $e){ - if(!$e->isPlayer() && $e->class === ENTITY_MOB) ++$ents; - } - return $ents; + return $this->level->totalMobsAmount; } public function handle(){