(client) feat:主要实现实体的行为树和工作类 (#40)

Co-authored-by: zzdxxz <2079238449@qq.com>
Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
2025-07-21 13:58:58 +08:00
committed by TheRedApricot
parent 389376ec47
commit 28ddcda9a0
87 changed files with 9052 additions and 2504 deletions

View File

@ -5,11 +5,24 @@ namespace Data
public class AttributesDef : Define
{
public int health = 10;
public int moveSpeed = 1;
public float moveSpeed = 1;
public int attack = 1;
public int defense = 0;
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
};
}
}
}

View File

@ -34,17 +34,49 @@ namespace Data
base.Init(xmlDef);
var nodes = xmlDef.Elements("DrawNodeDef");
if (nodes.Count() == 0)
var xElements = nodes as XElement[] ?? nodes.ToArray();
if (!xElements.Any())
return false;
foreach (var node in nodes)
{
var drawNode = new DrawNodeDef();
drawNode.Init(node);
drawNodes.Add(drawNode);
}
foreach (var node in xElements)
{
var drawNode = new DrawNodeDef();
drawNode.Init(node);
drawNodes.Add(drawNode);
}
return true;;
}
// 重载 == 运算符
public static bool operator ==(DrawingOrderDef a, DrawingOrderDef b)
{
if (ReferenceEquals(a, b)) return true; // 如果是同一个对象,直接返回 true
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false; // 如果其中一个为 null返回 false
return AreEqual(a, b);
}
// 重载 != 运算符
public static bool operator !=(DrawingOrderDef a, DrawingOrderDef b)
{
return !(a == b);
}
// 判断两个 DrawingOrderDef 是否相等
private static bool AreEqual(DrawingOrderDef a, DrawingOrderDef b)
{
// 比较 drawNodes 的数量
if (a.drawNodes.Count != b.drawNodes.Count)
return false;
// 递归比较每个 DrawNodeDef
for (int i = 0; i < a.drawNodes.Count; i++)
{
if (!DrawNodeDef.AreEqual(a.drawNodes[i], b.drawNodes[i]))
return false;
}
return true;
}
}
public class DrawNodeDef : Define
@ -77,16 +109,16 @@ namespace Data
public Vector2 StringToVector(string vectorDef)
{
// 去掉可能存在的括号和多余的空格
string cleanedInput = vectorDef.Replace("(", "").Replace(")", "").Trim();
var cleanedInput = vectorDef.Replace("(", "").Replace(")", "").Trim();
// 使用正则表达式匹配两个浮点数
Match match = Regex.Match(cleanedInput, @"\s*(-?\d+(\.\d*)?)\s*[, ]\s*(-?\d+(\.\d*)?)\s*");
var match = Regex.Match(cleanedInput, @"\s*(-?\d+(\.\d*)?)\s*[, ]\s*(-?\d+(\.\d*)?)\s*");
if (match.Success)
{
// 提取匹配到的两个浮点数
float x = float.Parse(match.Groups[1].Value);
float y = float.Parse(match.Groups[3].Value);
var x = float.Parse(match.Groups[1].Value);
var y = float.Parse(match.Groups[3].Value);
// 返回 Vector2 对象
return new Vector2(x, y);
@ -96,6 +128,30 @@ namespace Data
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;
}
}

View File

@ -279,8 +279,14 @@ namespace Data
value = reference;
}
}
else if(field.FieldType.IsArray)
{
value = ProcessArrayField(field, element);
}
else
{
value = Convert.ChangeType(element.Value, field.FieldType);
}
field.SetValue(define, value);
}
catch (Exception ex)
@ -289,7 +295,44 @@ namespace Data
}
}
}
private static object ProcessArrayField(FieldInfo field, XElement element)
{
Type elementType = field.FieldType.GetElementType();
if (elementType == null) return null;
var arrayElements = new List<object>();
foreach (var liElement in element.Elements())
{
if (elementType.IsSubclassOf(typeof(Define)))
{
Define 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);
}
else
{
// 基本类型处理
arrayElements.Add(Convert.ChangeType(liElement.Value, elementType));
}
}
// 构建结果数组
Array resultArray = Array.CreateInstance(elementType, arrayElements.Count);
for (int i = 0; i < arrayElements.Count; i++)
{
resultArray.SetValue(arrayElements[i], i);
}
return resultArray;
}
/// <summary>
/// 从 List<c>XDocument</c> 中查找指定根元素名称的文档。
/// </summary>

View File

@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace Data
{
public class PawnDef : Define
{
public AttributesDef attributes;
public string aiController;
public string texturePath = null;
public DrawingOrderDef
@ -11,6 +15,10 @@ namespace Data
drawingOrder_up,
drawingOrder_left,
drawingOrder_right;
public BehaviorTreeDef behaviorTree;
public DrawingOrderDef GetDrawingOrder(Orientation orientation)
{
// 定义一个临时变量用于存储结果
@ -48,4 +56,33 @@ namespace Data
{
}
public class BehaviorTreeDef : Define
{
public BehaviorTreeDef[] childTree;
public string className;
public override bool Init(XElement xmlDef)
{
base.Init(xmlDef);
var nodes = xmlDef.Elements("Node");
var xElements = nodes as XElement[] ?? nodes.ToArray();
if (!xElements.Any())
return false;
className = xmlDef.Attribute("className")?.Value;
List<BehaviorTreeDef> children = new();
foreach (var node in xElements)
{
var childNode = new BehaviorTreeDef();
childNode.Init(node);
children.Add(childNode);
}
childTree = children.ToArray();
return true;
}
}
}