Files
Gen_Hack-and-Slash-Roguelit…/Client/Assets/Scripts/Data/CharacterDefine.cs

102 lines
2.8 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");
if (nodes.Count() == 0)
return false;
foreach (var node in nodes)
{
var drawNode = new DrawNodeDef();
drawNode.Init(node);
drawNodes.Add(drawNode);
}
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)
{
// 去掉可能存在的括号和多余的空格
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;
}
}
}
}