(client) feat:实现摄像机跟踪与移动,实现任意位置生成实体,实现更安全的资源加载方式(指定unity内部加载资源) (#42)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
140
Client/Assets/Scripts/Data/DrawingOrderDef.cs
Normal file
140
Client/Assets/Scripts/Data/DrawingOrderDef.cs
Normal file
@ -0,0 +1,140 @@
|
||||
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 DrawingOrderDef : Define
|
||||
{
|
||||
public DrawNodeDef drawingOrder_down;
|
||||
public DrawNodeDef drawingOrder_up;
|
||||
public DrawNodeDef drawingOrder_left;
|
||||
public DrawNodeDef drawingOrder_right;
|
||||
public string texturePath;
|
||||
public int pixelsPerUnit = 16;
|
||||
|
||||
public DrawNodeDef GetDrawingOrder(Orientation orientation)
|
||||
{
|
||||
// 定义一个临时变量用于存储结果
|
||||
DrawNodeDef 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 DrawNodeDef : Define
|
||||
{
|
||||
public List<DrawNodeDef> children = new();
|
||||
public DrawNodeType drawNodeType = DrawNodeType.Image;
|
||||
public string nodeName;
|
||||
public Vector2 position = new(0, 0);
|
||||
public float FPS = 0.5f;
|
||||
|
||||
public override bool Init(XElement xmlDef)
|
||||
{
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user