Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
158 lines
4.9 KiB
C#
158 lines
4.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text.RegularExpressions;
|
||
using System.Xml.Linq;
|
||
using UnityEditor.ShaderGraph.Internal;
|
||
using UnityEngine;
|
||
|
||
namespace Data
|
||
{
|
||
public enum Orientation
|
||
{
|
||
Down,
|
||
Left,
|
||
Right,
|
||
Up
|
||
}
|
||
|
||
public enum DrawNodeType
|
||
{
|
||
Image,
|
||
Animation
|
||
}
|
||
|
||
public class CharacterDef : PawnDef
|
||
{
|
||
}
|
||
|
||
public class DrawingOrderDef : Define
|
||
{
|
||
public List<DrawNodeDef> drawNodes = new();
|
||
public override bool Init(XElement xmlDef)
|
||
{
|
||
base.Init(xmlDef);
|
||
|
||
var nodes = xmlDef.Elements("DrawNodeDef");
|
||
var xElements = nodes as XElement[] ?? nodes.ToArray();
|
||
if (!xElements.Any())
|
||
return false;
|
||
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
|
||
{
|
||
public List<DrawNodeDef> children = new();
|
||
public DrawNodeType drawNodeType = DrawNodeType.Image;
|
||
public string nodeName;
|
||
public Vector2 position = new(0, 0);
|
||
public float FPS = 1;
|
||
|
||
public override bool Init(XElement xmlDef)
|
||
{
|
||
base.Init(xmlDef);
|
||
|
||
nodeName = xmlDef.Attribute("name")?.Value;
|
||
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("DrawNodeDef"))
|
||
{
|
||
var child = new DrawNodeDef();
|
||
child.Init(childNode);
|
||
children.Add(child);
|
||
}
|
||
return true;
|
||
}
|
||
public Vector2 StringToVector(string vectorDef)
|
||
{
|
||
// 去掉可能存在的括号和多余的空格
|
||
var cleanedInput = vectorDef.Replace("(", "").Replace(")", "").Trim();
|
||
|
||
// 使用正则表达式匹配两个浮点数
|
||
var match = Regex.Match(cleanedInput, @"\s*(-?\d+(\.\d*)?)\s*[, ]\s*(-?\d+(\.\d*)?)\s*");
|
||
|
||
if (match.Success)
|
||
{
|
||
// 提取匹配到的两个浮点数
|
||
var x = float.Parse(match.Groups[1].Value);
|
||
var y = float.Parse(match.Groups[3].Value);
|
||
|
||
// 返回 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;
|
||
}
|
||
}
|
||
|
||
|
||
} |