103 lines
2.6 KiB
C#
103 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace AI
|
|
{
|
|
public abstract class AIBase
|
|
{
|
|
public List<AIBase> children = new();
|
|
|
|
public abstract JobBase GetJob(Entity.Entity target);
|
|
}
|
|
|
|
public class Selector : AIBase
|
|
{
|
|
public override JobBase GetJob(Entity.Entity target)
|
|
{
|
|
foreach (var aiBase in children)
|
|
{
|
|
var job = aiBase.GetJob(target);
|
|
if (job != null)
|
|
return job;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
public class ConditionalAI : Selector
|
|
{
|
|
// 条件函数,返回 true 表示满足条件
|
|
private Func<Entity.Entity, bool> condition;
|
|
|
|
// 构造函数,传入条件函数
|
|
public ConditionalAI(Func<Entity.Entity, bool> conditionFunc)
|
|
{
|
|
condition = conditionFunc;
|
|
}
|
|
|
|
public override JobBase GetJob(Entity.Entity target)
|
|
{
|
|
// 检查条件是否满足
|
|
if (condition != null && condition(target))
|
|
{
|
|
// 如果条件满足,继续查找子节点的任务
|
|
return base.GetJob(target);
|
|
}
|
|
|
|
// 条件不满足,直接返回 null
|
|
return null;
|
|
}
|
|
}
|
|
public class SequentialAI : Selector
|
|
{
|
|
private int currentIndex = 0; // 当前正在尝试的子节点索引
|
|
|
|
public override JobBase GetJob(Entity.Entity target)
|
|
{
|
|
// 如果当前索引超出了子节点范围,重置索引并返回 null
|
|
if (currentIndex >= children.Count)
|
|
{
|
|
ResetIndex();
|
|
return null;
|
|
}
|
|
|
|
// 获取当前子节点的任务
|
|
var currentChild = children[currentIndex];
|
|
var job = currentChild.GetJob(target);
|
|
|
|
// 移动到下一个子节点
|
|
currentIndex++;
|
|
|
|
return job;
|
|
}
|
|
|
|
// 重置当前索引(用于重新开始遍历)
|
|
private void ResetIndex()
|
|
{
|
|
currentIndex = 0;
|
|
}
|
|
}
|
|
public class ContinuousMove : AIBase
|
|
{
|
|
public override JobBase GetJob(Entity.Entity target)
|
|
{
|
|
return new MoveJob();
|
|
}
|
|
}
|
|
|
|
public class TrackPlayer : AIBase
|
|
{
|
|
public override JobBase GetJob(Entity.Entity target)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
public class RandomWander : AIBase
|
|
{
|
|
public override JobBase GetJob(Entity.Entity target)
|
|
{
|
|
return new WanderJob();
|
|
}
|
|
}
|
|
|
|
} |