(client) feat:添加行为树和工作类
This commit is contained in:
63
Client/Assets/Scripts/AI/AIBase.cs
Normal file
63
Client/Assets/Scripts/AI/AIBase.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public abstract class AIBase
|
||||
{
|
||||
public List<AIBase> children = new();
|
||||
|
||||
public virtual JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
foreach (var aiBase in children)
|
||||
{
|
||||
var job = aiBase.GetJob(target);
|
||||
if (job != null)
|
||||
return job;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class ContinuousMove : AIBase
|
||||
{
|
||||
override public JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class TrackPlayer : AIBase
|
||||
{
|
||||
|
||||
}
|
||||
public class WanderNode : AIBase
|
||||
{
|
||||
}
|
||||
public class ConditionalAI : AIBase
|
||||
{
|
||||
// 条件函数,返回 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;
|
||||
}
|
||||
}
|
||||
}
|
38
Client/Assets/Scripts/AI/JobBase.cs
Normal file
38
Client/Assets/Scripts/AI/JobBase.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using Base;
|
||||
using Unity.VisualScripting;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public abstract class JobBase
|
||||
{
|
||||
public Entity.Entity entity;
|
||||
private int timeoutTicks = 1000;
|
||||
public bool Running=>timeoutTicks > 0;
|
||||
|
||||
public virtual void StartJob(Entity.Entity target)
|
||||
{
|
||||
entity = target;
|
||||
}
|
||||
|
||||
protected abstract void UpdateJob();
|
||||
|
||||
public bool Update()
|
||||
{
|
||||
if(!Running)
|
||||
return false;
|
||||
UpdateJob();
|
||||
timeoutTicks--;
|
||||
if (timeoutTicks <= 0)
|
||||
{
|
||||
StopJob();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public virtual void StopJob()
|
||||
{
|
||||
timeoutTicks = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -11,5 +11,18 @@ namespace Data
|
||||
public int attackSpeed = 2;
|
||||
public int attackRange = 3;
|
||||
public int attackTargetCount = 1;
|
||||
public AttributesDef Clone()
|
||||
{
|
||||
return new AttributesDef
|
||||
{
|
||||
health = this.health,
|
||||
moveSpeed = this.moveSpeed,
|
||||
attack = this.attack,
|
||||
defense = this.defense,
|
||||
attackSpeed = this.attackSpeed,
|
||||
attackRange = this.attackRange,
|
||||
attackTargetCount = this.attackTargetCount
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ namespace Data
|
||||
{
|
||||
public class PawnDef : Define
|
||||
{
|
||||
public AttributesDef attributes;
|
||||
public string aiController;
|
||||
public string texturePath = null;
|
||||
public DrawingOrderDef
|
||||
@ -15,7 +16,7 @@ namespace Data
|
||||
drawingOrder_left,
|
||||
drawingOrder_right;
|
||||
|
||||
public string[] behaviorTree;
|
||||
public BehaviorTreeDef behaviorTree;
|
||||
|
||||
|
||||
public DrawingOrderDef GetDrawingOrder(Orientation orientation)
|
||||
|
@ -1,35 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public abstract class AIBase
|
||||
{
|
||||
public virtual bool Run(Entity target)
|
||||
{
|
||||
foreach (var aiBase in children)
|
||||
{
|
||||
if (aiBase.Run(target))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
public List<AIBase> children=new();
|
||||
}
|
||||
|
||||
public class ContinuousMove : AIBase
|
||||
{
|
||||
override public bool Run(Entity target)
|
||||
{
|
||||
target.gameObject.transform.position +=
|
||||
Time.deltaTime * target.runtimeAttributes.moveSpeed * target.direction;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class TrackPlayer : AIBase
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -1,22 +1,42 @@
|
||||
using System.Collections.Generic;
|
||||
using AI;
|
||||
using Base;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public abstract class Entity:MonoBehaviour,ITick
|
||||
{
|
||||
public string name;
|
||||
public bool playerControlled = false;
|
||||
public List<AIBase> aiTree=new();
|
||||
public Data.AttributesDef runtimeAttributes;
|
||||
public AIBase aiTree;
|
||||
public JobBase currentJob;
|
||||
public AttributesDef runtimeAttributes;
|
||||
public Vector3 direction;
|
||||
|
||||
private const int WarningInterval = 5000;
|
||||
private int warningTicks = 0;
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
foreach (var aiBase in aiTree)
|
||||
if (currentJob == null || !currentJob.Running)
|
||||
{
|
||||
if (aiBase.Run(this))
|
||||
break;
|
||||
currentJob = aiTree.GetJob(this);
|
||||
if (currentJob == null)
|
||||
{
|
||||
if (warningTicks<=0)
|
||||
{
|
||||
Debug.LogWarning($"{GetType().Name}类型的{name}没有分配到任何工作,给行为树末尾添加等待行为,避免由于没有工作导致无意义的反复查找工作导致性能问题");
|
||||
warningTicks += WarningInterval;
|
||||
}
|
||||
|
||||
warningTicks--;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
currentJob.Update();
|
||||
}
|
||||
|
||||
public virtual void TryAttck()
|
||||
|
@ -1,3 +1,4 @@
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Managers
|
||||
@ -5,11 +6,17 @@ namespace Managers
|
||||
public class Generator:MonoBehaviour
|
||||
{
|
||||
public GameObject entityLevel;
|
||||
|
||||
public EntityPrefab entityPrefab;
|
||||
|
||||
public void GenerateEntity(Data.PawnDef pawnDef, Vector3 pos)
|
||||
{
|
||||
if (entityPrefab == null || pawnDef == null)
|
||||
return;
|
||||
|
||||
GameObject entity = Instantiate(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
// entity.name = pawnDef.name;
|
||||
var entityComponent = entity.GetComponent<EntityPrefab>();
|
||||
entityComponent?.Init(pawnDef);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,71 @@
|
||||
using System;
|
||||
using AI;
|
||||
using Data;
|
||||
using Entity;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
public class EntityPrefab:MonoBehaviour
|
||||
public class EntityPrefab : MonoBehaviour
|
||||
{
|
||||
|
||||
public Entity.Entity entity;
|
||||
public void Init(Data.PawnDef pawnDef)
|
||||
{
|
||||
entity.runtimeAttributes = pawnDef.attributes.Clone();
|
||||
entity.aiTree = ConvertToAIBase(pawnDef.behaviorTree);
|
||||
}
|
||||
public static AIBase ConvertToAIBase(BehaviorTreeDef behaviorTreeDef)
|
||||
{
|
||||
if (behaviorTreeDef == null)
|
||||
return null;
|
||||
AIBase aiBase = CreateAIBaseInstance(behaviorTreeDef.className);
|
||||
if (behaviorTreeDef.childTree != null)
|
||||
{
|
||||
foreach (var child in behaviorTreeDef.childTree)
|
||||
{
|
||||
if (child != null)
|
||||
{
|
||||
aiBase.children.Add(ConvertToAIBase(child));
|
||||
}
|
||||
}
|
||||
}
|
||||
return aiBase;
|
||||
}
|
||||
|
||||
// 使用反射根据 className 创建具体的 AIBase 子类实例
|
||||
private static AIBase CreateAIBaseInstance(string className)
|
||||
{
|
||||
if (string.IsNullOrEmpty(className))
|
||||
throw new ArgumentException("className 不能为空");
|
||||
|
||||
// 定义可能的命名空间列表
|
||||
var possibleNamespaces = new[] { "AI"};
|
||||
|
||||
foreach (var ns in possibleNamespaces)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取当前程序集
|
||||
var assembly = typeof(AIBase).Assembly;
|
||||
|
||||
// 尝试查找类型
|
||||
var type = assembly.GetType($"{ns}.{className}");
|
||||
|
||||
if (type != null && typeof(AIBase).IsAssignableFrom(type))
|
||||
{
|
||||
// 如果找到合适的类型,则创建实例并返回
|
||||
return (AIBase)Activator.CreateInstance(type);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 忽略单个命名空间的错误,继续尝试下一个命名空间
|
||||
}
|
||||
}
|
||||
|
||||
// 如果所有命名空间都未找到对应的类型,抛出异常
|
||||
throw new InvalidOperationException($"无法找到类型 {className} 或该类型不是 AIBase 的子类");
|
||||
}
|
||||
}
|
||||
}
|
131
Client/Assets/Scripts/Utils/Resolver.cs
Normal file
131
Client/Assets/Scripts/Utils/Resolver.cs
Normal file
@ -0,0 +1,131 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
public static class Resolver
|
||||
{
|
||||
/// <summary>
|
||||
/// 将字符串表达式解析为一个谓词函数,该函数可以用于筛选实体对象。
|
||||
/// </summary>
|
||||
/// <param name="expression">表示条件的字符串表达式。格式示例:"entity.Id > 10" 或 "entity.Name == 'John'"。</param>
|
||||
/// <returns>返回一个 Func<Entity.Entity, bool> 类型的委托,表示解析后的谓词函数。</returns>
|
||||
/// <exception cref="FormatException">当输入表达式的格式不正确时抛出此异常。</exception>
|
||||
/// <exception cref="NotSupportedException">当表达式中包含不支持的操作符或数据类型时抛出此异常。</exception>
|
||||
/// <remarks>
|
||||
/// 表达式的格式必须符合以下规则:
|
||||
/// - 表达式由三部分组成:属性路径、操作符和值,用空格分隔。
|
||||
/// - 属性路径格式为 "entity.PropertyName",其中 PropertyName 是实体类中的一个公共属性或字段。
|
||||
/// - 操作符可以是以下之一:">", "<", ">=", "<=", "==", "!="。
|
||||
/// - 值的类型必须与属性的类型匹配,并且支持以下类型:string, int, long, float, double, decimal, bool, DateTime, Guid 和枚举类型。
|
||||
///
|
||||
/// 注意事项:
|
||||
/// - 字符串值需要用单引号或双引号括起来,例如 'John' 或 "John"。
|
||||
/// - 对于可为空类型(Nullable),会自动处理其底层类型的转换。
|
||||
/// - 字符串比较默认使用不区分大小写的 Equals 方法。
|
||||
/// </remarks>
|
||||
public static Func<Entity.Entity, bool> ParsePredicate(string expression)
|
||||
{
|
||||
// 格式示例:"entity.Id > 10" 或 "entity.Name == 'John'"
|
||||
var parts = expression.Split(new[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length != 3)
|
||||
throw new FormatException(
|
||||
"Invalid expression format. Expected format: 'entity.Property Operator Value'");
|
||||
|
||||
// 解析属性和操作符
|
||||
var propPath = parts[0].Split('.')[1]; // "Id" 或 "Name"
|
||||
var op = parts[1]; // ">", "==" 等
|
||||
|
||||
// 创建表达式参数
|
||||
var param = Expression.Parameter(typeof(Entity.Entity), "entity");
|
||||
|
||||
// 获取属性访问表达式
|
||||
var propAccess = propPath.Split('.')
|
||||
.Aggregate<string, Expression>(param, Expression.PropertyOrField);
|
||||
|
||||
// 获取属性类型
|
||||
var propType = propAccess.Type;
|
||||
|
||||
// 解析值并转换为适当类型
|
||||
object value;
|
||||
var valueStr = parts[2].Trim();
|
||||
|
||||
try
|
||||
{
|
||||
if (propType == typeof(string))
|
||||
// 处理字符串值(去除引号)
|
||||
value = valueStr.Trim('\'', '"');
|
||||
else if (propType == typeof(int))
|
||||
value = int.Parse(valueStr);
|
||||
else if (propType == typeof(long))
|
||||
value = long.Parse(valueStr);
|
||||
else if (propType == typeof(float))
|
||||
value = float.Parse(valueStr);
|
||||
else if (propType == typeof(double))
|
||||
value = double.Parse(valueStr);
|
||||
else if (propType == typeof(decimal))
|
||||
value = decimal.Parse(valueStr);
|
||||
else if (propType == typeof(bool))
|
||||
value = bool.Parse(valueStr);
|
||||
else if (propType == typeof(DateTime))
|
||||
value = DateTime.Parse(valueStr);
|
||||
else if (propType == typeof(Guid))
|
||||
value = Guid.Parse(valueStr);
|
||||
else if (propType.IsEnum)
|
||||
value = Enum.Parse(propType, valueStr);
|
||||
else
|
||||
throw new NotSupportedException($"Type {propType.Name} is not supported");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new FormatException($"Failed to parse value '{valueStr}' for type {propType.Name}", ex);
|
||||
}
|
||||
|
||||
// 创建常量表达式(确保类型匹配)
|
||||
var constant = Expression.Constant(value, propType);
|
||||
|
||||
// 处理可为空类型的情况
|
||||
if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
||||
{
|
||||
var underlyingType = Nullable.GetUnderlyingType(propType);
|
||||
propAccess = Expression.Property(propAccess, "Value");
|
||||
constant = Expression.Constant(Convert.ChangeType(value, underlyingType), underlyingType);
|
||||
}
|
||||
|
||||
// 创建比较表达式
|
||||
Expression comparison;
|
||||
if (propType == typeof(string) && (op == "==" || op == "!="))
|
||||
{
|
||||
// 字符串特殊处理:使用Equals方法进行不区分大小写的比较
|
||||
var equalsMethod =
|
||||
typeof(string).GetMethod("Equals", new[] { typeof(string), typeof(StringComparison) });
|
||||
var methodCall = Expression.Call(
|
||||
propAccess,
|
||||
equalsMethod,
|
||||
constant,
|
||||
Expression.Constant(StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
comparison = op == "==" ? methodCall : Expression.Not(methodCall);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 其他类型使用标准二元运算符
|
||||
comparison = op switch
|
||||
{
|
||||
">" => Expression.GreaterThan(propAccess, constant),
|
||||
"<" => Expression.LessThan(propAccess, constant),
|
||||
">=" => Expression.GreaterThanOrEqual(propAccess, constant),
|
||||
"<=" => Expression.LessThanOrEqual(propAccess, constant),
|
||||
"==" => Expression.Equal(propAccess, constant),
|
||||
"!=" => Expression.NotEqual(propAccess, constant),
|
||||
_ => throw new NotSupportedException($"Operator {op} not supported")
|
||||
};
|
||||
}
|
||||
|
||||
// 编译为委托
|
||||
var lambda = Expression.Lambda<Func<Entity.Entity, bool>>(comparison, param);
|
||||
return lambda.Compile();
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Utils/Resolver.cs.meta
Normal file
3
Client/Assets/Scripts/Utils/Resolver.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: acd36958b991488f92c4b9a8b903c0be
|
||||
timeCreated: 1753012536
|
@ -25,6 +25,9 @@
|
||||
<texture>
|
||||
<path>Resources\Item\YellowBullet.png</path>
|
||||
</texture>
|
||||
<behaviorTree>
|
||||
<Node className="ContinuousMove"/>
|
||||
</behaviorTree>
|
||||
</BulletDef>
|
||||
|
||||
<ImageDef>
|
||||
|
Reference in New Issue
Block a user