using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Xml.Linq; using UnityEngine; namespace Data { public enum Orientation { Down, Left, Right, Up } public enum DrawNodeType { Image, Animation } public class CharacterDef : Define { public string texturePath = null; public DrawingOrderDef drawingOrder_down, drawingOrder_up, drawingOrder_left, drawingOrder_right; public DrawingOrderDef GetDrawingOrder(Orientation orientation) { // 定义一个临时变量用于存储结果 DrawingOrderDef result = null; // 根据传入的 Orientation 获取对应的 DrawingOrderDef switch (orientation) { case Orientation.Down: result = drawingOrder_down; break; case Orientation.Up: result = drawingOrder_up; break; case Orientation.Left: result = drawingOrder_left; break; case Orientation.Right: result = drawingOrder_right; break; default: throw new ArgumentException("Invalid orientation value."); } // 如果当前方向的结果为空,则尝试用 drawingOrder_down 填充 if (result == null) result = drawingOrder_down; // 如果 drawingOrder_down 仍然为空,则尝试用其他非空方向填充 if (result == null) result = drawingOrder_up ?? drawingOrder_left ?? drawingOrder_right; return result; } } public class DrawingOrderDef : Define { public List drawNodes = new(); public override bool Init(XElement xmlDef) { base.Init(xmlDef); foreach (var node in xmlDef.Elements("DrawNodeDef")) { var drawNode = new DrawNodeDef(); drawNode.Init(node); drawNodes.Add(drawNode); } return true; } } public class DrawNodeDef : Define { public List 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) { // 去掉可能存在的括号和多余的空格 string cleanedInput = vectorDef.Replace("(", "").Replace(")", "").Trim(); // 使用正则表达式匹配两个浮点数 Match 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); // 返回 Vector2 对象 return new Vector2(x, y); } else { return Vector2.zero; } } } }