mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 12:09:43 +00:00
entity ai classes, pigs should look around
This commit is contained in:
parent
6f603fcbe9
commit
8d454e5ec9
12 changed files with 206 additions and 38 deletions
46
src/entity/ai/EntityAI.php
Normal file
46
src/entity/ai/EntityAI.php
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
27
src/entity/ai/tasks/TaskBase.php
Normal file
27
src/entity/ai/tasks/TaskBase.php
Normal 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);
|
||||
}
|
||||
|
||||
26
src/entity/ai/tasks/TaskFindPath.php
Normal file
26
src/entity/ai/tasks/TaskFindPath.php
Normal 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?
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
47
src/entity/ai/tasks/TaskLookAround.php
Normal file
47
src/entity/ai/tasks/TaskLookAround.php
Normal 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){
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
26
src/entity/ai/tasks/TaskRandomWalk.php
Normal file
26
src/entity/ai/tasks/TaskRandomWalk.php
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue