(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
}
}
}

View 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 到 longfloat 到 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));
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 57f4a853e270411f89c13a3b385ab47d
timeCreated: 1756024020

View File

@ -0,0 +1,19 @@
using System;
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 IsPlayer(Entity.Entity entity)
{
return entity.PlayerControlled;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c81bab8c57104b6ba1fddfee22bc6c7c
timeCreated: 1756023349

View File

@ -11,8 +11,8 @@ namespace Base
[System.Serializable]
public class GameSettings
{
public float progressStepDuration = 1f;
public float exitAnimationDuration = 2f;
public float progressStepDuration = 0.5f;
public float exitAnimationDuration = 1f;
public bool developerMode = false;
public bool friendlyFire = false;
public float globalVolume = 1.0f;

View File

@ -11,17 +11,31 @@ using Object = System.Object;
namespace Data
{
/// <summary>
/// 表示模组包的基本信息
/// </summary>
public class PackAbout
{
public string name;
public string description;
public string author;
public string version;
public string packID;
public string Name { get; private set; }
public string Description { get; private set; }
public string Author { get; private set; }
public string Version { get; private set; }
public string PackID { get; private set; }
public string[] necessary;
public string[] after;
public string[] before;
public string[] Necessary { get; private set; }
public string[] After { get; private set; }
public string[] Before { get; private set; }
private const string RootElementName = "About";
private const string ElementName_Name = "name";
private const string ElementName_Description = "description";
private const string ElementName_Author = "author";
private const string ElementName_Version = "version";
private const string ElementName_PackID = "packID";
private const string ElementName_Sort = "sort";
private const string ElementName_Before = "before";
private const string ElementName_After = "after";
private const string ElementName_Necessary = "necessary";
/// <summary>
/// 使用静态方法从 XML 文档创建 PackAbout 实例。
@ -30,28 +44,28 @@ namespace Data
/// <returns>初始化的 PackAbout 实例。</returns>
public static PackAbout FromXDocument(XDocument doc)
{
var aboutElement = doc.Element("About");
if (aboutElement == null) throw new ArgumentException("XML 文档无效,根节点为空或不是 'About'。");
var aboutElement = doc.Element(RootElementName);
if (aboutElement == null) throw new ArgumentException($"XML 文档无效,根节点为空或不是 '{RootElementName}'。");
PackAbout result = new();
result.name = aboutElement.Element("name")?.Value ?? "Unknown";
result.description = aboutElement.Element("description")?.Value ?? "Unknown";
result.author = aboutElement.Element("author")?.Value ?? "Unknown";
result.version = aboutElement.Element("version")?.Value ?? "Unknown";
result.packID = aboutElement.Element("packID")?.Value ?? "Unknown";
result.Name = aboutElement.Element(ElementName_Name)?.Value ?? "Unknown";
result.Description = aboutElement.Element(ElementName_Description)?.Value ?? "Unknown";
result.Author = aboutElement.Element(ElementName_Author)?.Value ?? "Unknown";
result.Version = aboutElement.Element(ElementName_Version)?.Value ?? "Unknown";
result.PackID = aboutElement.Element(ElementName_PackID)?.Value ?? "Unknown";
var sortElement = aboutElement.Element("sort");
var sortElement = aboutElement.Element(ElementName_Sort);
if (sortElement != null)
{
result.before = GetElementValues(sortElement.Element("before"));
result.after = GetElementValues(sortElement.Element("after"));
result.necessary = GetElementValues(sortElement.Element("necessary"));
result.Before = GetElementValues(sortElement.Element(ElementName_Before));
result.After = GetElementValues(sortElement.Element(ElementName_After));
result.Necessary = GetElementValues(sortElement.Element(ElementName_Necessary));
}
else
{
result.before = Array.Empty<string>();
result.after = Array.Empty<string>();
result.necessary = Array.Empty<string>();
result.Before = Array.Empty<string>();
result.After = Array.Empty<string>();
result.Necessary = Array.Empty<string>();
}
return result;
@ -81,30 +95,56 @@ namespace Data
var sb = new StringBuilder();
// 基础字段(单行)
sb.AppendLine($"{"Name:",labelWidth}{name,valueWidth}");
sb.AppendLine($"{"Description:",labelWidth}{description,valueWidth}");
sb.AppendLine($"{"Author:",labelWidth}{author,valueWidth}");
sb.AppendLine($"{"Version:",labelWidth}{version,valueWidth}");
sb.AppendLine($"{"PackID:",labelWidth}{packID,valueWidth}");
sb.AppendLine($"{"Name:",labelWidth}{Name,valueWidth}");
sb.AppendLine($"{"Description:",labelWidth}{Description,valueWidth}");
sb.AppendLine($"{"Author:",labelWidth}{Author,valueWidth}");
sb.AppendLine($"{"Version:",labelWidth}{Version,valueWidth}");
sb.AppendLine($"{"PackID:",labelWidth}{PackID,valueWidth}");
// 数组字段(多行,每项缩进)
sb.AppendLine(
$"{"Necessary:",labelWidth}{string.Join(", ", necessary ?? Array.Empty<string>()),valueWidth}");
sb.AppendLine($"{"After:",labelWidth}{string.Join(", ", after ?? Array.Empty<string>()),valueWidth}");
sb.AppendLine($"{"Before:",labelWidth}{string.Join(", ", before ?? Array.Empty<string>()),valueWidth}");
$"{"Necessary:",labelWidth}{string.Join(", ", Necessary ?? Array.Empty<string>()),valueWidth}");
sb.AppendLine($"{"After:",labelWidth}{string.Join(", ", After ?? Array.Empty<string>()),valueWidth}");
sb.AppendLine($"{"Before:",labelWidth}{string.Join(", ", Before ?? Array.Empty<string>()),valueWidth}");
return sb.ToString();
}
}
/// <summary>
/// 表示一个模组包的定义集合
/// </summary>
public class DefinePack
{
private const string CoreNamespace = "Data.";
// 优化点7将魔法字符串转换为常量
private const string RootElementName_About = "About";
private const string RootElementName_Define = "Define";
// 优化点1和2反射缓存和改进的类型查找
private static readonly Dictionary<string, Type> _typeCache = new();
private static readonly Dictionary<Type, ConstructorInfo> _constructorCache = new();
private static readonly Dictionary<Type, FieldInfo[]> _fieldCache = new();
private static readonly List<Assembly> _assembliesToSearch = new(); // 缓存要搜索的程序集
static DefinePack()
{
// 优化点2一次性初始化要搜索的程序集。
// 为简单起见,我们将扫描所有当前加载的程序集。
// 在实际游戏中,您可能希望显式添加特定的程序集
// 如 Assembly.Load("YourGameLogicAssembly") 或按名称过滤。
_assembliesToSearch.AddRange(AppDomain.CurrentDomain.GetAssemblies());
// 可选:过滤或添加特定程序集:
// _assembliesToSearch.Add(Assembly.GetExecutingAssembly()); // 添加当前程序集
// _assembliesToSearch.Add(Assembly.Load("AnotherAssemblyContainingDefines"));
}
/// <summary>
/// define类别及其定义
/// </summary>
public Dictionary<string, List<Define>> defines=new();
public Dictionary<string, List<Define>> defines = new();
public PackAbout packAbout;
public string packID;
@ -114,15 +154,17 @@ namespace Data
{
get
{
return packAbout.name;
// 优化点5Name属性的空值安全性
return packAbout?.Name ?? "Unnamed Pack"; // 使用 PackAbout.Name 属性
}
}
public bool LoadPack(string packPath)
{
packRootPath=System.IO.Path.GetFullPath(packPath);;
packRootPath = System.IO.Path.GetFullPath(packPath);
var packDatas = ConfigProcessor.LoadXmlFromPath(packPath);
var aboutXmls = FindDocumentsWithRootName(packDatas, "About");
// 优化点7使用常量
var aboutXmls = FindDocumentsWithRootName(packDatas, RootElementName_About);
if (aboutXmls == null || aboutXmls.Count < 1)
{
Debug.LogError("包缺少配置文件,加载跳过");
@ -131,10 +173,13 @@ namespace Data
var aboutXml = aboutXmls[0];
packAbout = PackAbout.FromXDocument(aboutXml);
packID = packAbout.packID;
if (aboutXmls.Count > 1) Debug.LogWarning($"{packAbout.name}包拥有多个配置文件,系统选择了加载序的第一个,请避免这种情况");
// 优化点3使用 PackAbout.PackID 属性
packID = packAbout.PackID;
// 优化点3使用 PackAbout.Name 属性
if (aboutXmls.Count > 1) Debug.LogWarning($"{packAbout.Name}包拥有多个配置文件,系统选择了加载序的第一个,请避免这种情况");
var defineXmls = FindDocumentsWithRootName(packDatas, "Define");
// 优化点7使用常量
var defineXmls = FindDocumentsWithRootName(packDatas, RootElementName_Define);
// Debug.Log($"Define文件数量{defineXmls.Count}");
foreach (var defineXml in defineXmls) LoadDefines(defineXml);
return true;
@ -143,7 +188,8 @@ namespace Data
private void LoadDefines(XDocument defineDoc)
{
var rootElement = defineDoc.Root;
if (rootElement == null || rootElement.Name != "Define")
// 优化点7使用常量
if (rootElement == null || rootElement.Name != RootElementName_Define)
return;
foreach (var element in rootElement.Elements())
@ -151,7 +197,7 @@ namespace Data
var className = element.Name.ToString();
if (string.IsNullOrEmpty(className))
continue;
var def = LoadDefineClass(element,element.Name.ToString());
var def = LoadDefineClass(element, className);
if (def == null)
continue;
def.packID = packID;
@ -160,6 +206,7 @@ namespace Data
defines[className].Add(def);
}
}
/// <summary>
/// 根据指定的 XML 元素 (<paramref name="defineDoc"/>) 和类名 (<paramref name="className"/>),
/// 动态加载并初始化一个继承自 <see cref="Define"/> 的类实例。
@ -178,44 +225,47 @@ namespace Data
/// 如果类存在且满足条件,则尝试调用其 <see cref="Define.Init(XElement)"/> 方法进行初始化。
/// 如果初始化失败,则使用默认初始化方法 (<see cref="DefaultInitDefine(Define, XElement, Type)"/>)。
/// </remarks>
public static Define LoadDefineClass(XElement defineDoc,string className)
public static Define LoadDefineClass(XElement defineDoc, string className)
{
var assembly = Assembly.GetExecutingAssembly();
Type type;
if (!className.Contains('.'))
// 优化点1和2反射缓存和改进的类型查找
if (!_typeCache.TryGetValue(className, out Type type))
{
// 尝试拼接默认命名空间
// 首先尝试使用 CoreNamespace
var fullClassName = CoreNamespace + className;
type = assembly.GetType(fullClassName);
type = _assembliesToSearch.Select(a => a.GetType(fullClassName)).FirstOrDefault(t => t != null);
// 如果拼接命名空间后仍找不到,尝试直接查找(可能全局命名空间下的类
if (type == null) type = assembly.GetType(className);
}
else
{
// 直接查找
type = assembly.GetType(className);
// 如果未找到,尝试不使用 CoreNamespace(可能全局命名空间中或已经是完全限定名
if (type == null)
{
type = _assembliesToSearch.Select(a => a.GetType(className)).FirstOrDefault(t => t != null);
}
if (type == null)
{
Debug.LogError($"未定义的类型: {className}");
return null;
}
_typeCache[className] = type;
}
if (type == null)
// 优化点1构造函数缓存
if (!_constructorCache.TryGetValue(type, out ConstructorInfo constructor))
{
Debug.LogError($"未定义的类型: {className}");
return null;
constructor = type.GetConstructor(Type.EmptyTypes);
if (constructor == null)
{
Debug.LogError($"{className} 必须包含无参构造函数");
return null;
}
_constructorCache[type] = constructor;
}
var constructor = type.GetConstructor(Type.EmptyTypes);
if (constructor == null)
{
Debug.LogError($"{className} 必须包含无参构造函数");
return null;
}
// 3. 创建实例
object instance;
try
{
instance = Activator.CreateInstance(type);
instance = constructor.Invoke(null); // 使用缓存的构造函数
}
catch (Exception ex)
{
@ -231,10 +281,11 @@ namespace Data
}
if (define.Init(defineDoc)) return define;
DefaultInitDefine(define,defineDoc, type);
DefaultInitDefine(define, defineDoc, type);
return define;
}
/// <summary>
/// 初始化指定的 <paramref name="define"/> 对象,根据 <paramref name="defineDoc"/> 中的 XML 元素内容,
/// 将对应的字段值赋给 <paramref name="define"/> 对象。
@ -251,9 +302,14 @@ namespace Data
/// 如果找到匹配的子元素,则将其值转换为字段的类型并赋值给字段。
/// 如果字段类型继承自 <see cref="Define"/>,则递归调用 <see cref="LoadDefineClass(XElement, string)"/> 方法进行加载。
/// </remarks>
public static void DefaultInitDefine(Define define,XElement defineDoc,Type defineType)
public static void DefaultInitDefine(Define define, XElement defineDoc, Type defineType)
{
var fields = defineType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
// 优化点1FieldInfo 缓存
if (!_fieldCache.TryGetValue(defineType, out FieldInfo[] fields))
{
fields = defineType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
_fieldCache[defineType] = fields;
}
// 遍历字段并尝试从 XElement 中赋值
foreach (var field in fields)
@ -263,115 +319,98 @@ namespace Data
if (element != null)
try
{
Object value;
if (IsFieldTypeInheritedFrom(field, typeof(Define)))
{
if (element.HasElements)
{
value = LoadDefineClass(element, field.FieldType.Name);
}
else
{
var reference = (Define)Activator.CreateInstance(field.FieldType);
reference.isReferene = true;
reference.description=field.FieldType.Name;
reference.label = field.Name;
reference.defName = element.Value;
value = reference;
}
}
else if(field.FieldType.IsArray||typeof(IList).IsAssignableFrom(field.FieldType))
{
value = ProcessArrayField(field, element);
}
else if (field.FieldType.IsEnum)
{
value = Enum.Parse(field.FieldType, element.Value);
}
else
{
value = Convert.ChangeType(element.Value, field.FieldType);
}
// 优化点4重构 ProcessArrayField 并引入通用转换辅助方法
object value = ConvertXElementValueToType(element, field.FieldType);
field.SetValue(define, value);
}
catch (Exception ex)
{
Debug.LogWarning($"Error setting field ,field name:{field.Name}; value: {element.Value}; error: {ex.Message}");
Debug.LogWarning($"设置字段时出错,字段名:{field.Name}; : {element.Value}; 错误: {ex.Message}");
}
}
}
private static object ProcessArrayField(FieldInfo field, XElement element)
// 优化点4新的辅助方法用于将 XElement 值转换为特定类型
private static object ConvertXElementValueToType(XElement element, Type targetType)
{
// 获取集合元素的类型
Type elementType = field.FieldType.IsArray
? field.FieldType.GetElementType()
: field.FieldType.GetGenericArguments()[0];
if (elementType == null) return null;
var arrayElements = new List<object>();
// 遍历 XML 元素中的子元素
foreach (var liElement in element.Elements())
if (IsTypeInheritedFrom(targetType, typeof(Define)))
{
if (elementType.IsSubclassOf(typeof(Define)))
if (element.HasElements)
{
// 如果是 Define 类型或其子类
var nestedDefine = (Define)Activator.CreateInstance(elementType);
DefaultInitDefine(nestedDefine, liElement, elementType);
arrayElements.Add(nestedDefine);
}
else if (elementType.IsArray || typeof(IList).IsAssignableFrom(elementType))
{
// 嵌套数组处理(递归调用)
var pseudoField = new { FieldType = elementType };
var nestedArray = ProcessArrayField(
pseudoField.GetType().GetField("FieldType"),
liElement
);
arrayElements.Add(nestedArray);
}
else if (elementType.IsEnum)
{
// 枚举类型处理
arrayElements.Add(Enum.Parse(elementType, liElement.Value));
return LoadDefineClass(element, targetType.Name);
}
else
{
// 基本类型处理
arrayElements.Add(Convert.ChangeType(liElement.Value, elementType));
// 引用另一个 Define
var reference = (Define)Activator.CreateInstance(targetType);
reference.isReferene = true;
reference.description = targetType.Name;
reference.label = element.Name.LocalName; // 使用元素的名称作为标签
reference.defName = element.Value;
return reference;
}
}
// 根据目标字段的类型构建结果
if (field.FieldType.IsArray)
else if (targetType.IsArray || typeof(IList).IsAssignableFrom(targetType))
{
return ProcessArrayField(targetType, element);
}
else if (targetType.IsEnum)
{
return Enum.Parse(targetType, element.Value);
}
else
{
return Convert.ChangeType(element.Value, targetType);
}
}
// 优化点4修改 ProcessArrayField 以直接接受 Type
private static object ProcessArrayField(Type fieldType, XElement element)
{
// 获取集合的元素类型
Type elementType = fieldType.IsArray
? fieldType.GetElementType()
: fieldType.GetGenericArguments().FirstOrDefault(); // 使用 FirstOrDefault 以确保安全
if (elementType == null)
{
Debug.LogWarning($"无法确定类型为 {fieldType.Name} 的集合字段的元素类型");
return null;
}
var arrayElements = new List<object>();
// 遍历 XML 子元素
foreach (var liElement in element.Elements())
{
// 对每个项目使用新的辅助方法
arrayElements.Add(ConvertXElementValueToType(liElement, elementType));
}
// 根据目标字段的类型构造结果
if (fieldType.IsArray)
{
// 如果目标字段是数组类型
var resultArray = Array.CreateInstance(elementType, arrayElements.Count);
for (var i = 0; i < arrayElements.Count; i++)
{
resultArray.SetValue(arrayElements[i], i);
}
return resultArray;
}
else if (typeof(IList).IsAssignableFrom(field.FieldType))
else if (typeof(IList).IsAssignableFrom(fieldType))
{
// 如果目标字段是泛型集合类型(如 List<T>
var listType = typeof(List<>).MakeGenericType(elementType);
var resultList = (IList)Activator.CreateInstance(listType);
foreach (var item in arrayElements)
{
resultList.Add(item);
}
return resultList;
}
return null;
}
/// <summary>
/// 从 List<c>XDocument</c> 中查找指定根元素名称的文档。
@ -381,7 +420,7 @@ namespace Data
/// <returns>符合条件的 XML 文档列表。</returns>
public static List<XDocument> FindDocumentsWithRootName(List<XDocument> xmlDocuments, string rootName)
{
// Using LINQ to Objects for a more concise solution
// 使用 LINQ to Objects 实现更简洁的解决方案
var result = xmlDocuments
.Where(doc => doc.Root != null && doc.Root.Name.LocalName == rootName)
.ToList();
@ -403,7 +442,8 @@ namespace Data
// PackAbout 对象
sb.AppendLine("=== PackAbout ===");
sb.AppendLine(packAbout?.ToString() ?? "N/A"); // 调用 PackAboutToString()
// 优化点3使用 PackAbout.ToString()
sb.AppendLine(packAbout?.ToString() ?? "N/A");
sb.AppendLine();
// 字典字段defines
@ -417,28 +457,38 @@ namespace Data
sb.AppendLine(); // 每个类别后空一行
}
else
sb.AppendLine("No defines found.");
sb.AppendLine("未找到定义。");
return sb.ToString();
}
/// <summary>
/// 检查字段的类型是否继承自指定的类
/// 检查字段的类型是否继承自指定的类 (严格派生,不包括基类本身)
/// </summary>
/// <param name="field">字段信息</param>
/// <param name="baseType">要检查的基类类型</param>
/// <returns>如果字段的类型是基类或其派生类,则返回 true</returns>
/// <returns>如果字段的类型是基类的严格派生类,则返回 true</returns>
// 优化点6为 IsFieldTypeInheritedFrom 进行语义澄清
public static bool IsFieldTypeInheritedFrom(FieldInfo field, Type baseType)
{
// 获取字段的类型
var fieldType = field.FieldType;
// 如果字段的类型为 null 或不是基类的派生类,则返回 false
if (!baseType.IsAssignableFrom(fieldType))
return false;
// 如果字段的类型直接是基类或其派生类,则返回 true
return fieldType != baseType && baseType.IsAssignableFrom(fieldType);
// 严格派生:不包括基类本身
return fieldType != null && fieldType != baseType && baseType.IsAssignableFrom(fieldType);
}
/// <summary>
/// 检查一个类型是否继承自指定的基类 (严格派生,不包括基类本身)
/// </summary>
/// <param name="type">要检查的类型</param>
/// <param name="baseType">要检查的基类类型</param>
/// <returns>如果类型是基类的严格派生类,则返回 true</returns>
// 在 ConvertXElementValueToType 中使用的新类型检查辅助方法
public static bool IsTypeInheritedFrom(Type type, Type baseType)
{
return type != null && type != baseType && baseType.IsAssignableFrom(type);
}
}
}

View File

@ -84,11 +84,15 @@ namespace Entity
{
IsChase = true;
currentJob = null;
if(Program.Instance.focusedEntity)
// 逻辑修改只有当存在一个不同的焦点实体时才将其PlayerControlled设为false
if (Program.Instance.focusedEntity && Program.Instance.focusedEntity != this)
{
Program.Instance.focusedEntity.PlayerControlled = false;
}
Program.Instance.focusedEntity = this;
}
else if (PlayerControlled)
// 逻辑修改:确保只有当自身是焦点实体时才取消焦点,避免不必要的逻辑执行
else if (Program.Instance.focusedEntity == this)
{
Program.Instance.focusedEntity = null;
}
@ -96,6 +100,7 @@ namespace Entity
get => Program.Instance.focusedEntity == this;
}
public bool IsWalking => _walkingTimer > 0;
/// <summary>

View File

@ -0,0 +1,9 @@
using UnityEngine;
namespace Map
{
public class Dimension:MonoBehaviour
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1cf2b22a0f934c5b852b5cdd588d82fa
timeCreated: 1756014704

View File

@ -10,13 +10,17 @@ namespace Map
{
public class DoubleMap : MonoBehaviour
{
public List<List<int>> mapData = new();
private List<List<int>> mapData = new();
public Tilemap textureLevel;
public Vector2Int dataOffset = Vector2Int.zero; // 数据起始点偏移变量
// public Vector2Int dataOffset = Vector2Int.zero; // 数据起始点偏移变量 - 已删除
private void Start()
{
Managers.DefineManager.Instance.Init();
Managers.PackagesImageManager.Instance.Init();
Managers.TileManager.Instance.Init();
var size = 100;
InitializeData(size, size);
for (var i = 0; i < size; i++)
@ -44,9 +48,11 @@ namespace Map
{
column.Add(defaultValue);
}
mapData.Add(column);
}
}
// 设置指定数据坐标的瓦片值并刷新相关瓦片
public void SetTile(int dataX, int dataY, int value)
{
@ -100,8 +106,8 @@ namespace Map
if (mapData.Count == 0 || mapData[0].Count == 0) return;
// 计算瓦片地图的有效范围(考虑偏移)
var startX = dataOffset.x;
var startY = dataOffset.y;
var startX = 0; // dataOffset.x 已删除
var startY = 0; // dataOffset.y 已删除
var endX = startX + mapData.Count - 1;
var endY = startY + mapData[0].Count - 1;
@ -119,8 +125,8 @@ namespace Map
private void UpdateTileAtTilemapPosition(int tileX, int tileY)
{
// 计算对应的数据坐标(考虑偏移)
var dataX = tileX - dataOffset.x;
var dataY = tileY - dataOffset.y;
var dataX = tileX; // - dataOffset.x 已删除
var dataY = tileY; // - dataOffset.y 已删除
// 获取四个角的数据坐标
var topLeftX = dataX;
@ -181,4 +187,5 @@ namespace Map
return null;
}
}
}

View File

@ -6,24 +6,6 @@ namespace Map
{
public class MapGenerator:MonoBehaviour
{
public DoubleMap baseLevel;
public Tilemap buildLevel;
public Tilemap plantLevel;
public bool CanPassThrough(int x, int y)
{
return GetTilePassCost(x, y) < 1;
}
public float GetTilePassCost(int x, int y)
{
return 0;
}
public float GetTileBulletCover(int x, int y)
{
return 0;
}
}
}

View File

@ -17,6 +17,7 @@ namespace Utils
if (behaviorTreeDef == null)
return null;
var aiBase = CreateAIBaseInstance(behaviorTreeDef.className);
aiBase.Init(behaviorTreeDef);
if (behaviorTreeDef.childTree != null)
{
foreach (var child in behaviorTreeDef.childTree)