(client) feat:实现热重载,实现多维度,实现武器,实现掉落物,实现状态UI,实现攻击AI (#44)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
@ -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)
|
||||
{
|
||||
this.condition = (e) => false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.condition = (e) => false; // 如果没有指定条件,则条件始终不满足
|
||||
}
|
||||
return null; // 所有子节点完成时返回 null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
309
Client/Assets/Scripts/AI/ConditionDelegateFactory.cs
Normal file
309
Client/Assets/Scripts/AI/ConditionDelegateFactory.cs
Normal file
@ -0,0 +1,309 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public static class ConditionDelegateFactory
|
||||
{
|
||||
// 正则表达式用于解析函数名和参数
|
||||
private static readonly Regex _methodCallRegex = new Regex(
|
||||
@"^(?<methodName>[a-zA-Z_][a-zA-Z0-9_]*)(?:\((?<args>.*)\))?$",
|
||||
RegexOptions.Compiled
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// 解析条件字符串并创建 Func<Entity.Entity, bool> 委托。
|
||||
/// </summary>
|
||||
/// <param name="conditionString">条件字符串,如 "EntityHealth(20)" 或 "IsTargetAlive"。</param>
|
||||
/// <param name="entityType">实体类型,通常是 typeof(Entity.Entity)。</param>
|
||||
/// <param name="conditionClassType">包含条件静态方法的类类型,如 typeof(ConditionFunctions)。</param>
|
||||
/// <returns>一个 Func<Entity.Entity, bool> 委托。</returns>
|
||||
/// <exception cref="ArgumentException">当条件字符串格式不正确时抛出。</exception>
|
||||
/// <exception cref="MissingMethodException">当找不到匹配的条件方法时抛出。</exception>
|
||||
public static Func<Entity.Entity, bool> CreateConditionDelegate(
|
||||
string conditionString,
|
||||
Type entityType,
|
||||
Type conditionClassType)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(conditionString))
|
||||
throw new ArgumentException("条件字符串不能为空。", nameof(conditionString));
|
||||
if (entityType == null)
|
||||
throw new ArgumentNullException(nameof(entityType));
|
||||
if (conditionClassType == null)
|
||||
throw new ArgumentNullException(nameof(conditionClassType));
|
||||
|
||||
var match = _methodCallRegex.Match(conditionString);
|
||||
if (!match.Success)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"条件字符串 '{conditionString}' 格式不正确。期望格式: MethodName 或 MethodName(arg1, arg2)。");
|
||||
}
|
||||
|
||||
var methodName = match.Groups["methodName"].Value;
|
||||
var argsString = match.Groups["args"].Success ? match.Groups["args"].Value : null;
|
||||
|
||||
// 逻辑修改:支持多参数解析
|
||||
var parsedArgValues = new List<object>(); // 存储解析后的参数值
|
||||
var parsedArgTypes = new List<Type>(); // 存储解析后的参数类型
|
||||
|
||||
// 第一个参数始终是实体类型
|
||||
parsedArgTypes.Add(entityType);
|
||||
|
||||
if (!string.IsNullOrEmpty(argsString))
|
||||
{
|
||||
// 使用辅助方法拆分参数字符串
|
||||
var argStrings = SplitArguments(argsString); // <-- 新增辅助方法调用
|
||||
foreach (var argStr in argStrings)
|
||||
{
|
||||
var arg = ParseLiteral(argStr.Trim());
|
||||
parsedArgValues.Add(arg.value);
|
||||
parsedArgTypes.Add(arg.type);
|
||||
}
|
||||
}
|
||||
|
||||
// 逻辑修改:更健壮的方法查找和参数类型匹配
|
||||
MethodInfo bestMatchMethod = null;
|
||||
object[] finalInvokeArgs = null; // 存储最终用于 Invoke 的参数值(已转换类型)
|
||||
|
||||
// 获取所有静态、公共或非公共的同名方法,并过滤返回类型为 bool 的
|
||||
var methods = conditionClassType
|
||||
.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
|
||||
.Where(m => m.Name == methodName && m.ReturnType == typeof(bool))
|
||||
.ToList();
|
||||
|
||||
foreach (var method in methods)
|
||||
{
|
||||
var parameters = method.GetParameters();
|
||||
|
||||
// 检查参数数量是否匹配
|
||||
if (parameters.Length != parsedArgTypes.Count)
|
||||
continue;
|
||||
|
||||
var paramsMatch = true;
|
||||
var currentInvokeArgs = new object[parameters.Length]; // 临时存储当前方法的参数值
|
||||
|
||||
// 检查第一个参数(实体类型)
|
||||
if (!parameters[0].ParameterType.IsAssignableFrom(entityType))
|
||||
{
|
||||
paramsMatch = false;
|
||||
continue;
|
||||
}
|
||||
// 注意:currentInvokeArgs[0] 在这里不赋值,它将在委托执行时由传入的 entity 填充。
|
||||
|
||||
// 检查后续参数(解析出的额外参数)
|
||||
for (var i = 1; i < parameters.Length; i++)
|
||||
{
|
||||
var methodParamType = parameters[i].ParameterType;
|
||||
var parsedValueType = parsedArgTypes[i];
|
||||
var parsedValue = parsedArgValues[i - 1]; // parsedArgValues 从索引0开始对应第一个额外参数
|
||||
|
||||
// 尝试直接赋值
|
||||
if (methodParamType.IsInstanceOfType(parsedValue))
|
||||
{
|
||||
currentInvokeArgs[i] = parsedValue;
|
||||
}
|
||||
// 尝试类型转换(例如 int 到 long,float 到 double)
|
||||
else if (CanConvert(parsedValue, methodParamType)) // <-- 新增辅助方法调用
|
||||
{
|
||||
try
|
||||
{
|
||||
currentInvokeArgs[i] = Convert.ChangeType(parsedValue, methodParamType);
|
||||
}
|
||||
catch (Exception) // 捕获所有转换异常
|
||||
{
|
||||
paramsMatch = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
paramsMatch = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (paramsMatch)
|
||||
{
|
||||
bestMatchMethod = method;
|
||||
finalInvokeArgs = currentInvokeArgs; // 存储已转换的参数值
|
||||
break; // 找到第一个匹配项即停止,如果需要更复杂的匹配规则(如最具体匹配),则需要进一步处理
|
||||
}
|
||||
}
|
||||
|
||||
if (bestMatchMethod == null)
|
||||
{
|
||||
// 如果没有找到匹配的方法,抛出异常
|
||||
var expectedSignature = $"{methodName}({string.Join(", ", parsedArgTypes.Select(t => t.Name))})";
|
||||
throw new MissingMethodException(
|
||||
$"在类 '{conditionClassType.FullName}' 中未找到名为 '{methodName}' 的静态方法," +
|
||||
$"其签名与期望的 '{expectedSignature}' 且返回类型为 'bool' 不兼容。"
|
||||
);
|
||||
}
|
||||
|
||||
// 如果没有额外参数,直接创建委托以获得最佳性能
|
||||
if (parsedArgValues.Count == 0)
|
||||
{
|
||||
return (Func<Entity.Entity, bool>)Delegate.CreateDelegate(typeof(Func<Entity.Entity, bool>),
|
||||
bestMatchMethod);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 创建一个闭包来绑定解析后的参数
|
||||
// 闭包捕获 finalInvokeArgs,并在运行时填充第一个参数 (entity)
|
||||
return (entity) =>
|
||||
{
|
||||
// 复制一份 finalInvokeArgs,因为 Invoke 会修改数组内容(如果参数是 ref/out)
|
||||
// 并且需要将 entity 放入第一个位置
|
||||
var invokeArgs = new object[finalInvokeArgs.Length];
|
||||
invokeArgs[0] = entity;
|
||||
for (var i = 1; i < finalInvokeArgs.Length; i++)
|
||||
{
|
||||
invokeArgs[i] = finalInvokeArgs[i];
|
||||
}
|
||||
|
||||
return (bool)bestMatchMethod.Invoke(null, invokeArgs);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 辅助方法:拆分参数字符串。
|
||||
/// 这是一个更健壮的实现,能够正确处理包含逗号的带引号字符串和嵌套括号。
|
||||
/// </summary>
|
||||
/// <param name="argsString">待拆分的参数字符串。</param>
|
||||
/// <returns>拆分后的参数列表。</returns>
|
||||
private static IEnumerable<string> SplitArguments(string argsString)
|
||||
{
|
||||
if (string.IsNullOrEmpty(argsString))
|
||||
{
|
||||
return Enumerable.Empty<string>();
|
||||
}
|
||||
|
||||
var arguments = new List<string>();
|
||||
var currentArgument = new StringBuilder();
|
||||
var inQuote = false; // 跟踪是否在双引号内部
|
||||
var parenLevel = 0; // 跟踪括号的嵌套层级
|
||||
|
||||
for (var i = 0; i < argsString.Length; i++)
|
||||
{
|
||||
var c = argsString[i];
|
||||
|
||||
if (c == '"')
|
||||
{
|
||||
inQuote = !inQuote; // 切换引号状态
|
||||
currentArgument.Append(c); // 将引号字符保留在参数中
|
||||
}
|
||||
else if (c == '(')
|
||||
{
|
||||
parenLevel++; // 增加括号层级
|
||||
currentArgument.Append(c);
|
||||
}
|
||||
else if (c == ')')
|
||||
{
|
||||
parenLevel--; // 减少括号层级
|
||||
currentArgument.Append(c);
|
||||
}
|
||||
else if (c == ',' && !inQuote && parenLevel == 0)
|
||||
{
|
||||
// 发现一个顶级的逗号分隔符:不在引号内,也不在任何括号内
|
||||
var arg = currentArgument.ToString().Trim();
|
||||
if (!string.IsNullOrEmpty(arg))
|
||||
{
|
||||
arguments.Add(arg);
|
||||
}
|
||||
currentArgument.Clear(); // 重置,开始收集下一个参数
|
||||
}
|
||||
else
|
||||
{
|
||||
// 其他字符,直接添加到当前参数
|
||||
currentArgument.Append(c);
|
||||
}
|
||||
}
|
||||
|
||||
// 循环结束后,添加最后一个参数(如果有的话)
|
||||
var lastArg = currentArgument.ToString().Trim();
|
||||
if (!string.IsNullOrEmpty(lastArg))
|
||||
{
|
||||
arguments.Add(lastArg);
|
||||
}
|
||||
|
||||
return arguments;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 辅助方法:检查一个值是否可以转换为目标类型。
|
||||
/// </summary>
|
||||
private static bool CanConvert(object value, Type targetType)
|
||||
{
|
||||
// 逻辑修改:新增辅助方法,用于检查类型转换可行性
|
||||
if (value == null) return !targetType.IsValueType || (Nullable.GetUnderlyingType(targetType) != null);
|
||||
if (targetType.IsInstanceOfType(value)) return true;
|
||||
|
||||
try
|
||||
{
|
||||
// 尝试转换,如果成功则表示可转换
|
||||
Convert.ChangeType(value, targetType);
|
||||
return true;
|
||||
}
|
||||
catch (Exception) // 捕获所有可能的转换异常
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析字符串字面量为对应的对象和类型。
|
||||
/// 支持 int, long, float, double, bool, string。
|
||||
/// </summary>
|
||||
/// <param name="literalString">要解析的字面量字符串。</param>
|
||||
/// <returns>包含解析后的值和类型的元组。</returns>
|
||||
private static (object value, Type type) ParseLiteral(string literalString)
|
||||
{
|
||||
// 逻辑修改:增强 ParseLiteral,增加对 long 和 double 的支持
|
||||
// 顺序很重要:先尝试更窄的类型,再尝试更宽的类型,以避免不必要的类型提升。
|
||||
|
||||
// 尝试解析为 int
|
||||
if (int.TryParse(literalString, out var intValue))
|
||||
{
|
||||
return (intValue, typeof(int));
|
||||
}
|
||||
|
||||
// 尝试解析为 long
|
||||
if (long.TryParse(literalString, out var longValue))
|
||||
{
|
||||
return (longValue, typeof(long));
|
||||
}
|
||||
|
||||
// 尝试解析为 float
|
||||
if (float.TryParse(literalString, out var floatValue))
|
||||
{
|
||||
return (floatValue, typeof(float));
|
||||
}
|
||||
|
||||
// 尝试解析为 double
|
||||
if (double.TryParse(literalString, out var doubleValue))
|
||||
{
|
||||
return (doubleValue, typeof(double));
|
||||
}
|
||||
|
||||
// 尝试解析为 bool
|
||||
if (bool.TryParse(literalString, out var boolValue))
|
||||
{
|
||||
return (boolValue, typeof(bool));
|
||||
}
|
||||
|
||||
// 尝试解析为 string (如果被双引号包围)
|
||||
if (literalString.StartsWith("\"") && literalString.EndsWith("\"") && literalString.Length > 1)
|
||||
{
|
||||
return (literalString.Substring(1, literalString.Length - 2), typeof(string));
|
||||
}
|
||||
|
||||
// 默认作为字符串处理
|
||||
return (literalString, typeof(string));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57f4a853e270411f89c13a3b385ab47d
|
||||
timeCreated: 1756024020
|
21
Client/Assets/Scripts/AI/ConditionFunctions.cs
Normal file
21
Client/Assets/Scripts/AI/ConditionFunctions.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using Data;
|
||||
using Managers;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public static class ConditionFunctions
|
||||
{
|
||||
public static bool EntityHealth(Entity.Entity entity, int minHealth)
|
||||
{
|
||||
return entity.attributes.health >= minHealth;
|
||||
}
|
||||
|
||||
|
||||
public static bool HasEnemyInSight(Entity.Entity entity)
|
||||
{
|
||||
return Managers.EntityManage.Instance.ExistsHostile(entity.currentDimensionId,entity.entityPrefab);
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/AI/ConditionFunctions.cs.meta
Normal file
3
Client/Assets/Scripts/AI/ConditionFunctions.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c81bab8c57104b6ba1fddfee22bc6c7c
|
||||
timeCreated: 1756023349
|
@ -1,5 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using Base;
|
||||
using Data;
|
||||
using Managers;
|
||||
using Prefab;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
@ -19,6 +21,7 @@ namespace AI
|
||||
{
|
||||
entity = target;
|
||||
}
|
||||
|
||||
public bool Update()
|
||||
{
|
||||
if (!Running)
|
||||
@ -27,11 +30,13 @@ namespace AI
|
||||
timeoutTicks--;
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual void StopJob()
|
||||
{
|
||||
timeoutTicks = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public class WanderJob : JobBase
|
||||
{
|
||||
public override void StartJob(Entity.Entity target)
|
||||
@ -55,6 +60,7 @@ namespace AI
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class IdleJob : JobBase
|
||||
{
|
||||
override public void StartJob(Entity.Entity target)
|
||||
@ -62,10 +68,12 @@ namespace AI
|
||||
base.StartJob(target);
|
||||
timeoutTicks = 500;
|
||||
}
|
||||
|
||||
protected override void UpdateJob()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class MoveJob : JobBase
|
||||
{
|
||||
protected override void UpdateJob()
|
||||
@ -74,222 +82,203 @@ namespace AI
|
||||
}
|
||||
}
|
||||
|
||||
public class TrackPlayerJob : JobBase
|
||||
public class AttackJob : JobBase
|
||||
{
|
||||
private EntityPrefab currentTarget; // 当前追踪的目标玩家
|
||||
private LinkedList<EntityPrefab> players; // 玩家实体列表
|
||||
private Entity.Entity attackTarget;
|
||||
|
||||
public override void StartJob(Entity.Entity target)
|
||||
// StartJob 方法:用于初始化任务,寻找初始攻击目标
|
||||
override public void StartJob(Entity.Entity performerEntityContext) // 参数名更明确,通常是发起任务的实体
|
||||
{
|
||||
base.StartJob(target);
|
||||
UpdateTarget();
|
||||
base.StartJob(performerEntityContext);
|
||||
// 1. 任务执行者自身有效性检查
|
||||
if (entity == null)
|
||||
{
|
||||
StopJob(); // 调用StopJob来结束任务
|
||||
return;
|
||||
}
|
||||
|
||||
attackTarget = FindNewHostileTarget();
|
||||
|
||||
if (attackTarget == null)
|
||||
{
|
||||
StopJob(); // 调用StopJob来结束任务
|
||||
}
|
||||
}
|
||||
|
||||
protected override void UpdateJob()
|
||||
{
|
||||
if (!currentTarget || currentTarget.entity.IsDead)
|
||||
// 1. 任务执行者的基本检查
|
||||
if (entity == null || entity.IsDead)
|
||||
{
|
||||
// 如果当前目标无效,则重新查找最近的玩家
|
||||
UpdateTarget();
|
||||
StopJob();
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentTarget)
|
||||
if (attackTarget == null || attackTarget.IsDead)
|
||||
{
|
||||
attackTarget = FindNewHostileTarget(); // 尝试寻找新的攻击目标
|
||||
if (attackTarget == null)
|
||||
{
|
||||
StopJob();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var weapon = entity.GetCurrentWeapon();
|
||||
var attackRange = 0f;
|
||||
if (weapon != null)
|
||||
{
|
||||
attackRange = weapon.Attributes.attackRange;
|
||||
}
|
||||
|
||||
var distanceSq = (entity.Position - attackTarget.Position).sqrMagnitude;
|
||||
var effectiveAttackRangeSq = attackRange * attackRange; // 将攻击范围也平方
|
||||
|
||||
entity.SetTarget(attackTarget.Position);
|
||||
|
||||
if (weapon != null && distanceSq <= effectiveAttackRangeSq)
|
||||
{
|
||||
entity.TryAttack();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
var targetPosition = new Vector3(currentTarget.Position.x, currentTarget.Position.y, 0);
|
||||
entity.SetTarget(targetPosition);
|
||||
entity.TryMove();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateTarget()
|
||||
/// <summary>
|
||||
/// 查找执行实体最近的敌对目标。
|
||||
/// </summary>
|
||||
/// <returns>找到的敌对实体,如果没有则返回null。</returns>
|
||||
private Entity.Entity FindNewHostileTarget()
|
||||
{
|
||||
players = Managers.EntityManage.Instance.FindEntitiesByFaction("Player");
|
||||
if (!entity) return null;
|
||||
return EntityManage.Instance.FindNearestEntityByRelation(
|
||||
entity.currentDimensionId, // 搜索维度ID
|
||||
entity.entityPrefab, // 执行实体的Prefab ID,用于关系判断
|
||||
Relation.Hostile)?.entity; // 寻找敌对关系的目标
|
||||
}
|
||||
}
|
||||
|
||||
if (players == null || players.Count == 0)
|
||||
public class AdvancedAttackJob : JobBase
|
||||
{
|
||||
private Entity.Entity attackTarget;
|
||||
|
||||
// 常量:用于配置远程AI行为的风筝参数
|
||||
private const float KITING_THRESHOLD_MULTIPLIER = 0.5f; // 当目标距离小于 (攻击范围 * 此乘数) 时,远程单位开始尝试远离
|
||||
|
||||
private const float KITING_BUFFER_DISTANCE = 5.0f; // 当远程单位远离时,目标点会是当前位置向反方向偏移此距离
|
||||
|
||||
// StartJob 方法:用于初始化任务,寻找初始攻击目标
|
||||
override public void StartJob(Entity.Entity performerEntityContext)
|
||||
{
|
||||
base.StartJob(performerEntityContext);
|
||||
if (entity == null)
|
||||
{
|
||||
currentTarget = null;
|
||||
StopJob();
|
||||
return;
|
||||
}
|
||||
currentTarget = GetNearestPlayer(players);
|
||||
}
|
||||
|
||||
private EntityPrefab GetNearestPlayer(LinkedList<EntityPrefab> players)
|
||||
{
|
||||
EntityPrefab nearestPlayer = null;
|
||||
float minDistance = float.MaxValue;
|
||||
|
||||
foreach (var player in players)
|
||||
attackTarget = FindNewHostileTarget();
|
||||
if (attackTarget == null)
|
||||
{
|
||||
if (player.entity.IsDead) continue; // 跳过无效玩家
|
||||
|
||||
float distance = Vector3.Distance(
|
||||
new Vector3(player.Position.x, player.Position.y, 0),
|
||||
new Vector3(entity.Position.x, entity.Position.y, 0)
|
||||
);
|
||||
|
||||
if (distance < minDistance)
|
||||
{
|
||||
minDistance = distance;
|
||||
nearestPlayer = player;
|
||||
}
|
||||
StopJob();
|
||||
}
|
||||
|
||||
return nearestPlayer;
|
||||
}
|
||||
}
|
||||
public class AttackPlayerJob : JobBase
|
||||
{
|
||||
private EntityPrefab player;
|
||||
|
||||
protected override void UpdateJob()
|
||||
{
|
||||
if (!player || !IsPlayerInRange())
|
||||
// 1. 任务执行者的基本检查
|
||||
if (entity == null || entity.IsDead)
|
||||
{
|
||||
StopJob(); // 如果玩家不在范围内,停止攻击工作
|
||||
return;
|
||||
}
|
||||
entity.TryAttack();
|
||||
}
|
||||
|
||||
private bool IsPlayerInRange()
|
||||
{
|
||||
float distance = Vector3.Distance(
|
||||
new Vector3(player.Position.x, player.Position.y, 0),
|
||||
new Vector3(entity.Position.x, entity.Position.y, 0)
|
||||
);
|
||||
|
||||
return distance <= entity.attributes.attackRange;
|
||||
}
|
||||
|
||||
public override void StartJob(Entity.Entity target)
|
||||
{
|
||||
base.StartJob(target);
|
||||
|
||||
// 查找最近的玩家作为目标
|
||||
LinkedList<EntityPrefab> players = Managers.EntityManage.Instance.FindEntitiesByFaction("Player");
|
||||
player = GetNearestPlayer(players);
|
||||
}
|
||||
|
||||
private EntityPrefab GetNearestPlayer(LinkedList<EntityPrefab> players)
|
||||
{
|
||||
EntityPrefab nearestPlayer = null;
|
||||
float minDistance = float.MaxValue;
|
||||
|
||||
foreach (var player in players)
|
||||
{
|
||||
if (!IsPlayerValid(player)) continue;
|
||||
|
||||
float distance = Vector3.Distance(
|
||||
new Vector3(player.Position.x, player.Position.y, 0),
|
||||
new Vector3(entity.Position.x, entity.Position.y, 0)
|
||||
);
|
||||
|
||||
if (distance < minDistance)
|
||||
{
|
||||
minDistance = distance;
|
||||
nearestPlayer = player;
|
||||
}
|
||||
}
|
||||
|
||||
return nearestPlayer;
|
||||
}
|
||||
|
||||
private bool IsPlayerValid(EntityPrefab player)
|
||||
{
|
||||
return player && !player.entity.IsDead;
|
||||
}
|
||||
}
|
||||
public class RangedAttackJob : JobBase
|
||||
{
|
||||
private EntityPrefab player;
|
||||
|
||||
protected override void UpdateJob()
|
||||
{
|
||||
if (!player || !IsPlayerValid(player))
|
||||
{
|
||||
StopJob(); // 如果当前目标无效,停止工作
|
||||
StopJob();
|
||||
return;
|
||||
}
|
||||
|
||||
float distance = Vector3.Distance(
|
||||
new Vector3(player.Position.x, player.Position.y, 0),
|
||||
new Vector3(entity.Position.x, entity.Position.y, 0)
|
||||
);
|
||||
|
||||
if (distance <= entity.attributes.attackRange)
|
||||
// 2. 攻击目标检查
|
||||
if (attackTarget == null || attackTarget.IsDead)
|
||||
{
|
||||
// 如果在攻击范围内,进行攻击
|
||||
entity.TryAttack();
|
||||
}
|
||||
else if (distance > entity.attributes.attackRange && distance < MaxTrackDistance)
|
||||
{
|
||||
// 如果距离过远,靠近目标
|
||||
MoveTowardsPlayer();
|
||||
}
|
||||
else if (distance < entity.attributes.attackRange)
|
||||
{
|
||||
// 如果距离过近,远离目标
|
||||
MoveAwayFromPlayer();
|
||||
}
|
||||
}
|
||||
|
||||
private void MoveTowardsPlayer()
|
||||
{
|
||||
Vector3 targetPosition = new Vector3(player.Position.x, player.Position.y, 0);
|
||||
entity.SetTarget(targetPosition);
|
||||
entity.TryMove();
|
||||
}
|
||||
|
||||
private void MoveAwayFromPlayer()
|
||||
{
|
||||
Vector3 direction = entity.Position - player.Position;
|
||||
direction.Normalize();
|
||||
Vector3 targetPosition = entity.Position + direction * RetreatDistance;
|
||||
|
||||
entity.SetTarget(targetPosition);
|
||||
entity.TryMove();
|
||||
}
|
||||
|
||||
public override void StartJob(Entity.Entity target)
|
||||
{
|
||||
base.StartJob(target);
|
||||
|
||||
// 查找最近的玩家作为目标
|
||||
var players = Managers.EntityManage.Instance.FindEntitiesByFaction("Player");
|
||||
player = GetNearestPlayer(players);
|
||||
}
|
||||
|
||||
private EntityPrefab GetNearestPlayer(LinkedList<EntityPrefab> players)
|
||||
{
|
||||
EntityPrefab nearestPlayer = null;
|
||||
float minDistance = float.MaxValue;
|
||||
|
||||
foreach (var player in players)
|
||||
{
|
||||
if (!IsPlayerValid(player)) continue;
|
||||
|
||||
float distance = Vector3.Distance(
|
||||
new Vector3(player.Position.x, player.Position.y, 0),
|
||||
new Vector3(entity.Position.x, entity.Position.y, 0)
|
||||
);
|
||||
|
||||
if (distance < minDistance)
|
||||
attackTarget = FindNewHostileTarget(); // 尝试寻找新的攻击目标
|
||||
if (attackTarget == null)
|
||||
{
|
||||
minDistance = distance;
|
||||
nearestPlayer = player;
|
||||
StopJob();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return nearestPlayer;
|
||||
// 获取武器和其属性
|
||||
var weapon = entity.GetCurrentWeapon();
|
||||
var attackRange = 0f;
|
||||
var isRangedWeapon = false; // 标识是否为远程武器
|
||||
if (weapon != null)
|
||||
{
|
||||
if (weapon.Attributes != null)
|
||||
{
|
||||
attackRange = weapon.Attributes.attackRange;
|
||||
}
|
||||
|
||||
// 使用 WeaponType 来判断武器类型,更明确
|
||||
isRangedWeapon = weapon.Type == WeaponType.Ranged;
|
||||
}
|
||||
|
||||
var distanceSq = (entity.Position - attackTarget.Position).sqrMagnitude;
|
||||
var effectiveAttackRangeSq = attackRange * attackRange; // 将攻击范围平方
|
||||
// ---- 核心AI行为决策 ( AdvancedAttackJob 的智能之处) ----
|
||||
if (isRangedWeapon)
|
||||
{
|
||||
// 远程单位的风筝(Kiting)逻辑
|
||||
var kitingDistance = attackRange * KITING_THRESHOLD_MULTIPLIER;
|
||||
var kitingThresholdSq = kitingDistance * kitingDistance; // 过近距离的平方
|
||||
// 1. 如果目标过于接近 (小于风筝阈值),尝试远离
|
||||
if (distanceSq < kitingThresholdSq)
|
||||
{
|
||||
// 计算一个远离目标的点作为移动目标
|
||||
var directionAway = (entity.Position - attackTarget.Position).normalized;
|
||||
var fleePosition = entity.Position + directionAway * KITING_BUFFER_DISTANCE;
|
||||
entity.SetTarget(fleePosition); // 设置远离点为新的移动目标
|
||||
entity.TryMove(); // 优先执行移动操作以拉开距离
|
||||
// 在此状态下不进行攻击,专注于 reposition
|
||||
}
|
||||
// 2. 如果目标在最佳攻击范围内 (即在风筝阈值和有效攻击范围之间),则停止移动并攻击
|
||||
else if (distanceSq <= effectiveAttackRangeSq)
|
||||
{
|
||||
entity.SetTarget(entity.Position); // 设定目标为当前位置,使其停止移动,专注于攻击
|
||||
entity.TryAttack();
|
||||
}
|
||||
// 3. 如果目标太远 (超出有效攻击范围),则移动靠近目标
|
||||
else
|
||||
{
|
||||
entity.SetTarget(attackTarget.Position); // 设置目标位置为移动目标
|
||||
entity.TryMove();
|
||||
}
|
||||
}
|
||||
else // 近战单位或没有武器的单位
|
||||
{
|
||||
entity.SetTarget(attackTarget.Position);
|
||||
if (weapon != null && distanceSq <= effectiveAttackRangeSq)
|
||||
{
|
||||
entity.TryAttack();
|
||||
}
|
||||
else
|
||||
{
|
||||
entity.TryMove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsPlayerValid(EntityPrefab player)
|
||||
/// <summary>
|
||||
/// 查找执行实体最近的敌对目标。
|
||||
/// </summary>
|
||||
/// <returns>找到的敌对实体,如果没有则返回null。</returns>
|
||||
private Entity.Entity FindNewHostileTarget()
|
||||
{
|
||||
return player && !player.entity.IsDead;
|
||||
if (!entity) return null;
|
||||
return EntityManage.Instance.FindNearestEntityByRelation(
|
||||
entity.currentDimensionId,
|
||||
entity.entityPrefab,
|
||||
Relation.Hostile)?.entity;
|
||||
}
|
||||
|
||||
private const float MaxTrackDistance = 20f; // 最大追踪距离
|
||||
private const float RetreatDistance = 3f; // 后退距离
|
||||
}
|
||||
|
||||
}
|
@ -8,13 +8,7 @@ namespace AI
|
||||
}
|
||||
}
|
||||
|
||||
public class JobGiver_Enemies : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return new TrackPlayerJob();
|
||||
}
|
||||
}
|
||||
|
||||
public class JobGiver_RandomWander : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
@ -30,4 +24,18 @@ namespace AI
|
||||
}
|
||||
}
|
||||
|
||||
public class JobGiver_AttackJob : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return Managers.EntityManage.Instance.ExistsHostile(target.currentDimensionId, target.entityPrefab) ? new AttackJob() : null;
|
||||
}
|
||||
}
|
||||
public class JobGiver_AdvancedAttackJob : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return Managers.EntityManage.Instance.ExistsHostile(target.currentDimensionId, target.entityPrefab) ? new AdvancedAttackJob() : null;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user