attackEntity method was added

This commit is contained in:
GameHerobrine 2023-01-23 11:38:40 +03:00
parent 89ee29a30a
commit 45b36cb034
4 changed files with 50 additions and 40 deletions

View file

@ -189,6 +189,10 @@ class Entity extends Position
return in_array($this->type, func_get_args()); return in_array($this->type, func_get_args());
} }
public function attackEntity($entity){
}
public function addVelocity($vX, $vY = 0, $vZ = 0) public function addVelocity($vX, $vY = 0, $vZ = 0)
{ {
if($vX instanceof Vector3){ if($vX instanceof Vector3){

View file

@ -1,11 +1,15 @@
<?php <?php
class Monster extends Creature{ class Monster extends Creature{
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data); parent::__construct($level, $eid, $class, $type, $data);
$this->ai->addTask(new TaskAttackPlayer()); $this->ai->addTask(new TaskAttackPlayer());
} }
public function getAttackDamage(){ public function getAttackDamage(){
return 2; return 2;
} }
public function attackEntity($entity){
$entity->harm($this->getAttackDamage(), $this->eid);
}
} }

View file

@ -4,36 +4,36 @@
*/ */
class TaskAttackPlayer extends TaskTempt class TaskAttackPlayer extends TaskTempt
{ {
public $attackTime = 0; public $attackTime = 0;
public function onStart(EntityAI $ai) public function onStart(EntityAI $ai)
{ {
$this->selfCounter = 1; $this->selfCounter = 1;
} }
public function onUpdate(EntityAI $ai) 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()){ if(($this->target instanceof Entity && !$this->target->isPlayer()) || $this->target->distanceSquared($ai->entity) > 100 || $this->target->level->getName() != $ai->entity->level->getName()){
$this->reset(); $this->reset();
return; return;
} }
$ai->mobController->moveTo($this->target->x, floor($ai->entity->y), $this->target->z); $ai->mobController->moveTo($this->target->x, floor($ai->entity->y), $this->target->z);
$ai->mobController->lookOn($this->target); $ai->mobController->lookOn($this->target);
if($ai->entity->boundingBox->intersectsWith($this->target->boundingBox) && $this->attackTime <= 0){ if($ai->entity->boundingBox->intersectsWith($this->target->boundingBox) && $this->attackTime <= 0){
$this->attackTime = 20; $ai->entity->attackEntity($this->target);
$this->target->harm($ai->entity->getAttackDamage(), $ai->entity->eid); $this->attackTime = 20;
} }
--$this->attackTime; --$this->attackTime;
} }
public function canBeExecuted(EntityAI $ai) public function canBeExecuted(EntityAI $ai)
{ {
$target = $this->findTarget($ai->entity, 10); $target = $this->findTarget($ai->entity, 10);
if($target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer()){ if($target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer()){
$this->target = $target; $this->target = $target;
return true; return true;
} }
return false; return false;
} }
} }

View file

@ -22,6 +22,8 @@ class Creeper extends Monster{
$this->setState($v === null ? !$this->getState() : $v); $this->setState($v === null ? !$this->getState() : $v);
} }
/** /**
* @return boolean * @return boolean
*/ */
@ -74,4 +76,4 @@ class Creeper extends Monster{
[GUNPOWDER, 0, mt_rand(0,2)] [GUNPOWDER, 0, mt_rand(0,2)]
]; ];
} }
} }