(client) feat:实现热重载,实现多维度,实现武器,实现掉落物,实现状态UI,实现攻击AI (#44)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
@ -11,18 +11,5 @@ 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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -20,11 +20,12 @@ namespace Data
|
||||
value = xmlDef.Attribute("value")?.Value;
|
||||
|
||||
var nodes = xmlDef.Elements("Node");
|
||||
if (!nodes.Any())
|
||||
var xElements = nodes as XElement[] ?? nodes.ToArray();
|
||||
if (!xElements.Any())
|
||||
return true; // 没有子节点也是有效的
|
||||
|
||||
List<BehaviorTreeDef> children = new();
|
||||
foreach (var node in nodes)
|
||||
foreach (var node in xElements)
|
||||
{
|
||||
var childNode = new BehaviorTreeDef();
|
||||
if (!childNode.Init(node))
|
||||
|
@ -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;
|
||||
// 优化点5:Name属性的空值安全性
|
||||
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 var 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 var 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);
|
||||
// 优化点1:FieldInfo 缓存
|
||||
if (!_fieldCache.TryGetValue(defineType, out var fields))
|
||||
{
|
||||
fields = defineType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
_fieldCache[defineType] = fields;
|
||||
}
|
||||
|
||||
// 遍历字段并尝试从 XElement 中赋值
|
||||
foreach (var field in fields)
|
||||
@ -263,82 +319,99 @@ 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 并引入通用转换辅助方法
|
||||
var 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)
|
||||
{
|
||||
var elementType = field.FieldType.GetElementType();
|
||||
if (elementType == null) return null;
|
||||
|
||||
var arrayElements = new List<object>();
|
||||
foreach (var liElement in element.Elements())
|
||||
if (IsTypeInheritedFrom(targetType, typeof(Define)))
|
||||
{
|
||||
if (elementType.IsSubclassOf(typeof(Define)))
|
||||
if (element.HasElements)
|
||||
{
|
||||
var nestedDefine = (Define)Activator.CreateInstance(elementType);
|
||||
DefaultInitDefine(nestedDefine, liElement, elementType);
|
||||
arrayElements.Add(nestedDefine);
|
||||
}
|
||||
else if (elementType.IsArray) // 嵌套数组处理
|
||||
{
|
||||
// 递归处理嵌套数组
|
||||
var nestedArray = ProcessArrayField(
|
||||
new { FieldType = elementType }.GetType().GetField("FieldType"),
|
||||
liElement
|
||||
);
|
||||
arrayElements.Add(nestedArray);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 构建结果数组
|
||||
var resultArray = Array.CreateInstance(elementType, arrayElements.Count);
|
||||
for (var i = 0; i < arrayElements.Count; i++)
|
||||
else if (targetType.IsArray || typeof(IList).IsAssignableFrom(targetType))
|
||||
{
|
||||
resultArray.SetValue(arrayElements[i], i);
|
||||
return ProcessArrayField(targetType, element);
|
||||
}
|
||||
else if (targetType.IsEnum)
|
||||
{
|
||||
return Enum.Parse(targetType, element.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Convert.ChangeType(element.Value, targetType);
|
||||
}
|
||||
return resultArray;
|
||||
}
|
||||
|
||||
// 优化点4:修改 ProcessArrayField 以直接接受 Type
|
||||
private static object ProcessArrayField(Type fieldType, XElement element)
|
||||
{
|
||||
// 获取集合的元素类型
|
||||
var 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(fieldType))
|
||||
{
|
||||
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> 中查找指定根元素名称的文档。
|
||||
/// </summary>
|
||||
@ -347,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();
|
||||
@ -369,7 +442,8 @@ namespace Data
|
||||
|
||||
// PackAbout 对象
|
||||
sb.AppendLine("=== PackAbout ===");
|
||||
sb.AppendLine(packAbout?.ToString() ?? "N/A"); // 调用 PackAbout 的 ToString()
|
||||
// 优化点3:使用 PackAbout.ToString()
|
||||
sb.AppendLine(packAbout?.ToString() ?? "N/A");
|
||||
sb.AppendLine();
|
||||
|
||||
// 字典字段(defines)
|
||||
@ -383,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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -14,84 +14,221 @@ namespace Data
|
||||
Up
|
||||
}
|
||||
|
||||
public enum DrawNodeType
|
||||
public enum EntityState
|
||||
{
|
||||
Image,
|
||||
Animation
|
||||
Idle,
|
||||
Walking,
|
||||
MeleeAttack,
|
||||
RangedAttack,
|
||||
}
|
||||
|
||||
public class DrawingOrderDef : Define
|
||||
{
|
||||
public DrawNodeDef drawingOrder_down;
|
||||
public DrawNodeDef drawingOrder_up;
|
||||
public DrawNodeDef drawingOrder_left;
|
||||
public DrawNodeDef drawingOrder_right;
|
||||
public string texturePath;
|
||||
public float pixelsPerUnit = 16;
|
||||
|
||||
public DrawNodeDef GetDrawingOrder(Orientation orientation, out Orientation sourceOrientation)
|
||||
public DrawNodeDef idle_down;
|
||||
public DrawNodeDef idle_up;
|
||||
public DrawNodeDef idle_left;
|
||||
public DrawNodeDef idle_right;
|
||||
|
||||
public DrawNodeDef walk_down;
|
||||
public DrawNodeDef walk_up;
|
||||
public DrawNodeDef walk_left;
|
||||
public DrawNodeDef walk_right;
|
||||
|
||||
public DrawNodeDef meleeAttack_down;
|
||||
public DrawNodeDef meleeAttack_up;
|
||||
public DrawNodeDef meleeAttack_left;
|
||||
public DrawNodeDef meleeAttack_right;
|
||||
|
||||
public DrawNodeDef rangedAttack_down;
|
||||
public DrawNodeDef rangedAttack_up;
|
||||
public DrawNodeDef rangedAttack_left;
|
||||
public DrawNodeDef rangedAttack_right;
|
||||
|
||||
public DrawNodeDef GetDrawNodeDef(EntityState state, Orientation orientation,
|
||||
out Orientation? fallbackOrientation)
|
||||
{
|
||||
// 定义一个临时变量用于存储结果
|
||||
DrawNodeDef result = null;
|
||||
fallbackOrientation = null;
|
||||
|
||||
// 初始化 sourceOrientation 为默认值
|
||||
sourceOrientation = Orientation.Down;
|
||||
// 根据状态和方向获取对应的DrawNodeDef
|
||||
var result = GetDrawNodeDefInternal(state, orientation);
|
||||
|
||||
// 根据传入的 Orientation 获取对应的 DrawingOrderDef
|
||||
if (result != null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// 如果找不到,按照规则查找替补
|
||||
switch (orientation)
|
||||
{
|
||||
case Orientation.Down:
|
||||
result = drawingOrder_down;
|
||||
sourceOrientation = Orientation.Down;
|
||||
break;
|
||||
case Orientation.Up:
|
||||
result = drawingOrder_up;
|
||||
sourceOrientation = Orientation.Up;
|
||||
// 上方向优先找下方向
|
||||
result = GetDrawNodeDefInternal(state, Orientation.Down);
|
||||
if (result != null)
|
||||
{
|
||||
fallbackOrientation = Orientation.Down;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 其次找左右方向
|
||||
result = GetDrawNodeDefInternal(state, Orientation.Left);
|
||||
if (result != null)
|
||||
{
|
||||
fallbackOrientation = Orientation.Left;
|
||||
return result;
|
||||
}
|
||||
|
||||
result = GetDrawNodeDefInternal(state, Orientation.Right);
|
||||
if (result != null)
|
||||
{
|
||||
fallbackOrientation = Orientation.Right;
|
||||
return result;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case Orientation.Down:
|
||||
// 下方向优先找上方向
|
||||
result = GetDrawNodeDefInternal(state, Orientation.Up);
|
||||
if (result != null)
|
||||
{
|
||||
fallbackOrientation = Orientation.Up;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 其次找左右方向
|
||||
result = GetDrawNodeDefInternal(state, Orientation.Left);
|
||||
if (result != null)
|
||||
{
|
||||
fallbackOrientation = Orientation.Left;
|
||||
return result;
|
||||
}
|
||||
|
||||
result = GetDrawNodeDefInternal(state, Orientation.Right);
|
||||
if (result != null)
|
||||
{
|
||||
fallbackOrientation = Orientation.Right;
|
||||
return result;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case Orientation.Left:
|
||||
result = drawingOrder_left;
|
||||
sourceOrientation = Orientation.Left;
|
||||
// 左方向优先找右方向
|
||||
result = GetDrawNodeDefInternal(state, Orientation.Right);
|
||||
if (result != null)
|
||||
{
|
||||
fallbackOrientation = Orientation.Right;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 其次找上下方向
|
||||
result = GetDrawNodeDefInternal(state, Orientation.Up);
|
||||
if (result != null)
|
||||
{
|
||||
fallbackOrientation = Orientation.Up;
|
||||
return result;
|
||||
}
|
||||
|
||||
result = GetDrawNodeDefInternal(state, Orientation.Down);
|
||||
if (result != null)
|
||||
{
|
||||
fallbackOrientation = Orientation.Down;
|
||||
return result;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case Orientation.Right:
|
||||
result = drawingOrder_right;
|
||||
sourceOrientation = Orientation.Right;
|
||||
// 右方向优先找左方向
|
||||
result = GetDrawNodeDefInternal(state, Orientation.Left);
|
||||
if (result != null)
|
||||
{
|
||||
fallbackOrientation = Orientation.Left;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 其次找上下方向
|
||||
result = GetDrawNodeDefInternal(state, Orientation.Up);
|
||||
if (result != null)
|
||||
{
|
||||
fallbackOrientation = Orientation.Up;
|
||||
return result;
|
||||
}
|
||||
|
||||
result = GetDrawNodeDefInternal(state, Orientation.Down);
|
||||
if (result != null)
|
||||
{
|
||||
fallbackOrientation = Orientation.Down;
|
||||
return result;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Invalid orientation value.");
|
||||
throw new ArgumentOutOfRangeException(nameof(orientation), orientation, null);
|
||||
}
|
||||
|
||||
// 如果当前方向的结果为空,则尝试用 drawingOrder_down 填充
|
||||
if (result == null)
|
||||
// 如果所有替补都找不到,返回null
|
||||
return null;
|
||||
}
|
||||
|
||||
private DrawNodeDef GetDrawNodeDefInternal(EntityState state, Orientation orientation)
|
||||
{
|
||||
// 根据状态和方向获取对应的DrawNodeDef
|
||||
switch (state)
|
||||
{
|
||||
result = drawingOrder_down;
|
||||
sourceOrientation = Orientation.Down; // 更新 sourceOrientation
|
||||
case EntityState.Idle:
|
||||
switch (orientation)
|
||||
{
|
||||
case Orientation.Down: return idle_down;
|
||||
case Orientation.Up: return idle_up;
|
||||
case Orientation.Left: return idle_left;
|
||||
case Orientation.Right: return idle_right;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case EntityState.Walking:
|
||||
switch (orientation)
|
||||
{
|
||||
case Orientation.Down: return walk_down;
|
||||
case Orientation.Up: return walk_up;
|
||||
case Orientation.Left: return walk_left;
|
||||
case Orientation.Right: return walk_right;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case EntityState.MeleeAttack:
|
||||
switch (orientation)
|
||||
{
|
||||
case Orientation.Down: return meleeAttack_down;
|
||||
case Orientation.Up: return meleeAttack_up;
|
||||
case Orientation.Left: return meleeAttack_left;
|
||||
case Orientation.Right: return meleeAttack_right;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case EntityState.RangedAttack:
|
||||
switch (orientation)
|
||||
{
|
||||
case Orientation.Down: return rangedAttack_down;
|
||||
case Orientation.Up: return rangedAttack_up;
|
||||
case Orientation.Left: return rangedAttack_left;
|
||||
case Orientation.Right: return rangedAttack_right;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// 如果 drawingOrder_down 仍然为空,则尝试用其他非空方向填充
|
||||
if (result != null) return result;
|
||||
if (drawingOrder_up != null)
|
||||
{
|
||||
result = drawingOrder_up;
|
||||
sourceOrientation = Orientation.Up; // 更新 sourceOrientation
|
||||
}
|
||||
else if (drawingOrder_left != null)
|
||||
{
|
||||
result = drawingOrder_left;
|
||||
sourceOrientation = Orientation.Left; // 更新 sourceOrientation
|
||||
}
|
||||
else if (drawingOrder_right != null)
|
||||
{
|
||||
result = drawingOrder_right;
|
||||
sourceOrientation = Orientation.Right; // 更新 sourceOrientation
|
||||
}
|
||||
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class DrawNodeDef : Define
|
||||
{
|
||||
public List<DrawNodeDef> children = new();
|
||||
public DrawNodeType drawNodeType = DrawNodeType.Image;
|
||||
public List<string> textures = new();
|
||||
public List<DrawNodeDef> nodes = new();
|
||||
public string nodeName;
|
||||
public Vector2 position = new(0, 0);
|
||||
public float FPS = 0.5f;
|
||||
@ -101,19 +238,9 @@ namespace Data
|
||||
base.Init(xmlDef);
|
||||
|
||||
nodeName = xmlDef.Attribute("name")?.Value??"noName";
|
||||
drawNodeType = Enum.TryParse(xmlDef.Attribute("type")?.Value, true, out DrawNodeType typeResult)
|
||||
? typeResult
|
||||
: DrawNodeType.Image;
|
||||
|
||||
position = StringToVector(xmlDef.Attribute("position")?.Value ?? "(0 0)");
|
||||
FPS = float.TryParse(xmlDef.Attribute("FPS")?.Value, out float result) ? result : 1.0f;
|
||||
foreach (var childNode in xmlDef.Elements())
|
||||
{
|
||||
var child = new DrawNodeDef();
|
||||
child.Init(childNode);
|
||||
children.Add(child);
|
||||
}
|
||||
return true;
|
||||
FPS = float.TryParse(xmlDef.Attribute("FPS")?.Value, out var result) ? result : 1.0f;
|
||||
return false;
|
||||
}
|
||||
public Vector2 StringToVector(string vectorDef)
|
||||
{
|
||||
@ -132,34 +259,37 @@ namespace Data
|
||||
// 返回 Vector2 对象
|
||||
return new Vector2(x, y);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Vector2.zero;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断两个 DrawNodeDef 是否相等
|
||||
public static bool AreEqual(DrawNodeDef a, DrawNodeDef b)
|
||||
{
|
||||
if (ReferenceEquals(a, b)) return true; // 如果是同一个对象,直接返回 true
|
||||
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false; // 如果其中一个为 null,返回 false
|
||||
// 比较基本属性
|
||||
if (a.drawNodeType != b.drawNodeType ||
|
||||
a.nodeName != b.nodeName ||
|
||||
a.position != b.position ||
|
||||
Math.Abs(a.FPS - b.FPS) > 0.001f) // 浮点数比较需要考虑精度
|
||||
return false;
|
||||
// 比较 children 的数量
|
||||
if (a.children.Count != b.children.Count)
|
||||
return false;
|
||||
// 递归比较每个子节点
|
||||
for (var i = 0; i < a.children.Count; i++)
|
||||
{
|
||||
if (!AreEqual(a.children[i], b.children[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return Vector2.zero;
|
||||
}
|
||||
/// <summary>
|
||||
/// 计算动画执行一个周期的总时间(包括子对象)。
|
||||
/// 如果自身没有纹理,自身动画时间为0。
|
||||
/// 总周期取自身动画时间和所有子动画周期中的最大值。
|
||||
/// </summary>
|
||||
/// <returns>动画执行一个周期的总时间(秒)。</returns>
|
||||
public float GetAnimationCycleDuration()
|
||||
{
|
||||
if (FPS < 0.01)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
float ownDuration = 0f;
|
||||
// 计算当前节点自身的动画周期时间
|
||||
// 由于 Init 方法已经处理了 FPS 的校验,FPS 保证大于 0
|
||||
if (textures.Count > 0)
|
||||
{
|
||||
ownDuration = textures.Count / FPS;
|
||||
}
|
||||
// 递归计算所有子节点的动画周期,并取其中最长的
|
||||
float maxChildDuration = 0f;
|
||||
foreach (var childNode in nodes)
|
||||
{
|
||||
float childDuration = childNode.GetAnimationCycleDuration();
|
||||
maxChildDuration = Math.Max(maxChildDuration, childDuration);
|
||||
}
|
||||
// 整个 DrawNodeDef 的动画周期是自身动画周期和所有子动画周期中的最大值
|
||||
return Math.Max(ownDuration, maxChildDuration);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,15 +7,15 @@ namespace Data
|
||||
{
|
||||
public class EntityDef : Define
|
||||
{
|
||||
public AttributesDef attributes;
|
||||
public AttributesDef attributes = new();
|
||||
public DrawingOrderDef drawingOrder;
|
||||
|
||||
public BehaviorTreeDef behaviorTree;
|
||||
public AffiliationDef affiliation;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
21
Client/Assets/Scripts/Data/EventDef.cs
Normal file
21
Client/Assets/Scripts/Data/EventDef.cs
Normal file
@ -0,0 +1,21 @@
|
||||
namespace Data
|
||||
{
|
||||
public enum EventType
|
||||
{
|
||||
None,
|
||||
SpawnCharacter,
|
||||
SpawnBuilding,
|
||||
SpawnBullet,
|
||||
SpawnPickup,
|
||||
SpawnDefaultEntity,
|
||||
}
|
||||
public class EventDef : Define
|
||||
{
|
||||
public EventType eventType = EventType.None;
|
||||
public EntityDef entityDef_Character; // 用于 EventType.SpawnCharacter
|
||||
public BuildingDef entityDef_Building; // 用于 EventType.SpawnBuilding
|
||||
public BulletDef entityDef_Bullet; // 用于 EventType.SpawnBullet
|
||||
public ItemDef entityDef_Pickup; // 用于 EventType.SpawnPickup
|
||||
public EventDef() { }
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Data/EventDef.cs.meta
Normal file
3
Client/Assets/Scripts/Data/EventDef.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 70b8a820a04c4cafad4c9b91b9d3b01d
|
||||
timeCreated: 1756267922
|
@ -8,16 +8,13 @@ namespace Data
|
||||
Epic,
|
||||
Legendary
|
||||
}
|
||||
public class ItemDef:Define
|
||||
public class ItemDef : Define
|
||||
{
|
||||
public ImageDef texture;
|
||||
public float FPS = 0;
|
||||
public string[] textures;
|
||||
public ItemRarity rarity = ItemRarity.Common;
|
||||
public int maxStack = 1; // 最大堆叠数量,默认为1
|
||||
public int maxStack = 10; // 最大堆叠数量,默认为10
|
||||
public bool ssEquippable = false; // 是否可装备
|
||||
}
|
||||
|
||||
public class WeaponDef : ItemDef
|
||||
{
|
||||
public AttributesDef attributes;
|
||||
}
|
||||
}
|
@ -2,6 +2,6 @@ namespace Data
|
||||
{
|
||||
public class MonsterDef:EntityDef
|
||||
{
|
||||
|
||||
public WeaponDef weapon;
|
||||
}
|
||||
}
|
21
Client/Assets/Scripts/Data/WeaponDef.cs
Normal file
21
Client/Assets/Scripts/Data/WeaponDef.cs
Normal file
@ -0,0 +1,21 @@
|
||||
namespace Data
|
||||
{
|
||||
public enum WeaponType
|
||||
{
|
||||
Melee, // 近战武器
|
||||
Ranged // 远程武器
|
||||
}
|
||||
public class WeaponDef : ItemDef
|
||||
{
|
||||
public WeaponType type = WeaponType.Melee;
|
||||
public AttributesDef attributes;
|
||||
public BulletDef bullet;
|
||||
public DrawNodeDef attackAnimation;
|
||||
public float attackDetectionTime = 0;
|
||||
public WeaponDef() // 构造函数,用于设置武器的默认属性
|
||||
{
|
||||
maxStack = 1; // 武器默认最大堆叠为1
|
||||
ssEquippable = true; // 武器默认可装备
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Data/WeaponDef.cs.meta
Normal file
3
Client/Assets/Scripts/Data/WeaponDef.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3ed90cba91ce41e1ae443cf44a76c932
|
||||
timeCreated: 1756193931
|
Reference in New Issue
Block a user