(client) feat:实现行为树条件节点的实际功能

This commit is contained in:
m0_75251201
2025-08-24 17:15:52 +08:00
parent da93368f02
commit 797cf69f75
17 changed files with 937 additions and 752 deletions

View File

@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using Data;
using UnityEngine;
namespace AI
@ -9,6 +12,10 @@ namespace AI
public List<AIBase> children = new();
public abstract JobBase GetJob(Entity.Entity target);
public virtual void Init(BehaviorTreeDef def)
{
}
}
public class ThinkNode_Selector : AIBase
@ -28,13 +35,7 @@ namespace AI
{
// 条件函数,返回 true 表示满足条件
private Func<Entity.Entity, bool> condition;
// 构造函数,传入条件函数
public ThinkNode_Conditional(Func<Entity.Entity, bool> conditionFunc)
{
condition = conditionFunc;
}
public override JobBase GetJob(Entity.Entity target)
{
// 检查条件是否满足
@ -47,19 +48,32 @@ namespace AI
// 条件不满足,直接返回 null
return null;
}
}
public class ThinkNode_Sequence : AIBase
{
public override JobBase GetJob(Entity.Entity target)
public override void Init(BehaviorTreeDef def)
{
foreach (var aiBase in children)
base.Init(def); // 调用基类的Init方法
if (!string.IsNullOrEmpty(def.value))
{
var job = aiBase.GetJob(target);
if (job == null)
return null; // 如果某个子节点返回 null则整个序列失败
try
{
// 使用 ConditionDelegateFactory 来解析 def.value 并创建条件委托
this.condition = ConditionDelegateFactory.CreateConditionDelegate(
def.value,
typeof(Entity.Entity),
typeof(ConditionFunctions) // 指定查找条件函数的类
);
}
catch (Exception ex)
{
this.condition = (e) => false;
}
}
else
{
this.condition = (e) => false; // 如果没有指定条件,则条件始终不满足
}
return null; // 所有子节点完成时返回 null
}
}
}