77 lines
2.1 KiB
C#
77 lines
2.1 KiB
C#
using Data;
|
|
using Parsing;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace AI
|
|
{
|
|
public abstract class AIBase
|
|
{
|
|
public List<AIBase> children = new();
|
|
|
|
public abstract JobBase GetJob(Entity.Entity target);
|
|
|
|
public virtual void Init(BehaviorTreeDef def)
|
|
{
|
|
}
|
|
}
|
|
|
|
public class ThinkNode_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 ThinkNode_Conditional : ThinkNode_Selector
|
|
{
|
|
// 条件函数,返回 true 表示满足条件
|
|
private Func<Entity.Entity, bool> condition;
|
|
|
|
public override JobBase GetJob(Entity.Entity target)
|
|
{
|
|
// 检查条件是否满足
|
|
if (condition != null && condition(target))
|
|
{
|
|
// 如果条件满足,继续查找子节点的任务
|
|
return base.GetJob(target);
|
|
}
|
|
|
|
// 条件不满足,直接返回 null
|
|
return null;
|
|
}
|
|
|
|
public override void Init(BehaviorTreeDef def)
|
|
{
|
|
base.Init(def); // 调用基类的Init方法
|
|
|
|
if (!string.IsNullOrEmpty(def.value))
|
|
{
|
|
try
|
|
{
|
|
// 使用 ConditionDelegateFactory 来解析 def.value 并创建条件委托
|
|
this.condition = ConditionDelegateFactory.CreateConditionDelegate(
|
|
def.value,
|
|
typeof(Entity.Entity),
|
|
typeof(ConditionFunctions) // 指定查找条件函数的类
|
|
);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
this.condition = (e) => false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.condition = (e) => false; // 如果没有指定条件,则条件始终不满足
|
|
}
|
|
}
|
|
}
|
|
|
|
} |