From 45b36cb03426f80c84ef87c37d06bfef53315cd1 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 23 Jan 2023 11:38:40 +0300 Subject: [PATCH] attackEntity method was added --- src/entity/Entity.php | 4 ++ src/entity/Monster.php | 20 +++++--- src/entity/ai/tasks/TaskAttackPlayer.php | 62 ++++++++++++------------ src/entity/monster/Creeper.php | 4 +- 4 files changed, 50 insertions(+), 40 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 78ca87ba..38f97871 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -189,6 +189,10 @@ class Entity extends Position return in_array($this->type, func_get_args()); } + public function attackEntity($entity){ + + } + public function addVelocity($vX, $vY = 0, $vZ = 0) { if($vX instanceof Vector3){ diff --git a/src/entity/Monster.php b/src/entity/Monster.php index 30615f02..969de944 100644 --- a/src/entity/Monster.php +++ b/src/entity/Monster.php @@ -1,11 +1,15 @@ ai->addTask(new TaskAttackPlayer()); - } - - public function getAttackDamage(){ - return 2; - } + public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ + parent::__construct($level, $eid, $class, $type, $data); + $this->ai->addTask(new TaskAttackPlayer()); + } + + public function getAttackDamage(){ + return 2; + } + + public function attackEntity($entity){ + $entity->harm($this->getAttackDamage(), $this->eid); + } } diff --git a/src/entity/ai/tasks/TaskAttackPlayer.php b/src/entity/ai/tasks/TaskAttackPlayer.php index b6acb81a..deb9d66b 100644 --- a/src/entity/ai/tasks/TaskAttackPlayer.php +++ b/src/entity/ai/tasks/TaskAttackPlayer.php @@ -4,36 +4,36 @@ */ class TaskAttackPlayer extends TaskTempt { - public $attackTime = 0; - public function onStart(EntityAI $ai) - { - $this->selfCounter = 1; - } - public function onUpdate(EntityAI $ai) - { - if(($this->target instanceof Entity && !$this->target->isPlayer()) || $this->target->distanceSquared($ai->entity) > 100 || $this->target->level->getName() != $ai->entity->level->getName()){ - $this->reset(); - return; - } - - $ai->mobController->moveTo($this->target->x, floor($ai->entity->y), $this->target->z); - $ai->mobController->lookOn($this->target); - if($ai->entity->boundingBox->intersectsWith($this->target->boundingBox) && $this->attackTime <= 0){ - $this->attackTime = 20; - $this->target->harm($ai->entity->getAttackDamage(), $ai->entity->eid); - } - --$this->attackTime; - - } - public function canBeExecuted(EntityAI $ai) - { - $target = $this->findTarget($ai->entity, 10); - if($target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer()){ - $this->target = $target; - return true; - } - - return false; - } + public $attackTime = 0; + public function onStart(EntityAI $ai) + { + $this->selfCounter = 1; + } + public function onUpdate(EntityAI $ai) + { + if(($this->target instanceof Entity && !$this->target->isPlayer()) || $this->target->distanceSquared($ai->entity) > 100 || $this->target->level->getName() != $ai->entity->level->getName()){ + $this->reset(); + return; + } + + $ai->mobController->moveTo($this->target->x, floor($ai->entity->y), $this->target->z); + $ai->mobController->lookOn($this->target); + if($ai->entity->boundingBox->intersectsWith($this->target->boundingBox) && $this->attackTime <= 0){ + $ai->entity->attackEntity($this->target); + $this->attackTime = 20; + } + --$this->attackTime; + + } + public function canBeExecuted(EntityAI $ai) + { + $target = $this->findTarget($ai->entity, 10); + if($target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer()){ + $this->target = $target; + return true; + } + + return false; + } } diff --git a/src/entity/monster/Creeper.php b/src/entity/monster/Creeper.php index a89f687b..ca83b02a 100644 --- a/src/entity/monster/Creeper.php +++ b/src/entity/monster/Creeper.php @@ -22,6 +22,8 @@ class Creeper extends Monster{ $this->setState($v === null ? !$this->getState() : $v); } + + /** * @return boolean */ @@ -74,4 +76,4 @@ class Creeper extends Monster{ [GUNPOWDER, 0, mt_rand(0,2)] ]; } -} \ No newline at end of file +}