entity ai classes, pigs should look around

This commit is contained in:
GameHerobrine 2022-10-20 19:30:04 +03:00
parent 6f603fcbe9
commit 8d454e5ec9
12 changed files with 206 additions and 38 deletions

View file

@ -0,0 +1,46 @@
<?php
class EntityAI
{
/**
* @var Living
*/
public $entity;
/**
* @var TaskBase[]
*/
protected $tasks;
public $lastTask;
public function __construct($entity){
$this->entity = $entity;
$this->tasks = [];
}
/**
* Add a task for entity
* @param TaskBase $task
*/
public function addTask(TaskBase $task){
console("[DEBUG] Adding new task...",true, true, 2);
$this->tasks[] = $task;
}
public function updateTasks(){
foreach($this->tasks as $t){
if($t->wasExecuted($this)){
$t->onUpdate($this);
continue;
}
if($t->shouldBeExecuted($this)){
$t->onStart($this);
$t->onUpdate($this);
$this->lastTask = $this;
}
}
}
}

View file

@ -0,0 +1,27 @@
<?php
abstract class TaskBase
{
/**
* @param EntityAI $ai
* @return bool
*/
abstract function shouldBeExecuted(EntityAI $ai);
/**
* @param EntityAI $ai
* @return bool
*/
abstract function wasExecuted(EntityAI $ai);
/**
* @param EntityAI $ai
*/
abstract function onStart(EntityAI $ai);
/**
* @param EntityAI $ai
*/
abstract function onUpdate(EntityAI $ai);
}

View file

@ -0,0 +1,26 @@
<?php
class TaskFindPath extends TaskBase
{
public function shouldBeExecuted(EntityAI $ai)
{
return !$ai->entity->pathFound && $ai->entity->target instanceof Entity && $ai->entity->moveTime <= 0;
}
public function onUpdate(EntityAI $ai)
{
console("find path!");
}
public function wasExecuted(EntityAI $ai)
{
return $ai->entity->pathFound;
}
public function onStart(EntityAI $ai)
{
//todo?
}
}

View file

@ -0,0 +1,47 @@
<?php
class TaskLookAround extends TaskBase
{
private $finYaw = 0;
public function shouldBeExecuted(EntityAI $ai)
{
return !$ai->entity->isMoving() && $ai->entity->lookTime <= 0 && mt_rand(0,30) == 1 && $ai->entity->idleTime <= 0;
}
public function wasExecuted(EntityAI $ai){
return $ai->entity->lookTime > 0;
}
public function onStart(EntityAI $ai){
$ai->entity->lookTime = 120;
$this->finYaw = $ai->entity->yaw;
}
public function onUpdate(EntityAI $ai)
{
//90 - 75 = -45deg rot
//60 - 30 = 90deg rot
//30 - 15 = -45deg rot
if($ai->entity->lookTime === 1){
$ai->entity->idleTime = mt_rand(40, 200);
}
if(Utils::in_range($ai->entity->lookTime, 105, 120) || Utils::in_range($ai->entity->lookTime, 15, 30)){
$ai->entity->yaw -= 3;
}
if(Utils::in_range($ai->entity->lookTime, 50, 80)){
$ai->entity->yaw += 3;
}
$pk = new RotateHeadPacket();
$pk->eid = $ai->entity->eid;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
}
public function testLook($pos){
}
}

View file

@ -0,0 +1,26 @@
<?php
class TaskRandomWalk extends TaskBase
{
public function shouldBeExecuted(EntityAI $ai)
{
return !$ai->entity->pathFound && $ai->entity->moveTime <= 0 && $ai->entity->lookTime <= 0 && !($ai->entity->target instanceof Entity) && mt_rand(0,3) == 1;
}
public function onUpdate(EntityAI $ai)
{
console("random walk");
}
public function wasExecuted(EntityAI $ai)
{
return $ai->entity->moveTime > 0;
}
public function onStart(EntityAI $ai)
{
$ai->entity->moveTime = 100;
}
}