(client) feat:图像加载失败时加载默认图像,生成实体身体结构
This commit is contained in:
@ -28,55 +28,43 @@ namespace Data
|
||||
|
||||
public class DrawingOrderDef : Define
|
||||
{
|
||||
public List<DrawNodeDef> drawNodes = new();
|
||||
public DrawNodeDef drawingOrder_down;
|
||||
public DrawNodeDef drawingOrder_up;
|
||||
public DrawNodeDef drawingOrder_left;
|
||||
public DrawNodeDef drawingOrder_right;
|
||||
public string texturePath;
|
||||
public override bool Init(XElement xmlDef)
|
||||
|
||||
public DrawNodeDef GetDrawingOrder(Orientation orientation)
|
||||
{
|
||||
base.Init(xmlDef);
|
||||
// 定义一个临时变量用于存储结果
|
||||
DrawNodeDef result = null;
|
||||
|
||||
var nodes = xmlDef.Elements("DrawNodeDef");
|
||||
var xElements = nodes as XElement[] ?? nodes.ToArray();
|
||||
if (!xElements.Any())
|
||||
return false;
|
||||
foreach (var node in xElements)
|
||||
// 根据传入的 Orientation 获取对应的 DrawingOrderDef
|
||||
switch (orientation)
|
||||
{
|
||||
var drawNode = new DrawNodeDef();
|
||||
drawNode.Init(node);
|
||||
drawNodes.Add(drawNode);
|
||||
}
|
||||
texturePath= xmlDef.Element("texturePath")?.Value;
|
||||
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;
|
||||
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.");
|
||||
}
|
||||
|
||||
return true;
|
||||
// 如果当前方向的结果为空,则尝试用 drawingOrder_down 填充
|
||||
if (result == null) result = drawingOrder_down;
|
||||
|
||||
// 如果 drawingOrder_down 仍然为空,则尝试用其他非空方向填充
|
||||
if (result == null) result = drawingOrder_up ?? drawingOrder_left ?? drawingOrder_right;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@ -92,7 +80,7 @@ namespace Data
|
||||
{
|
||||
base.Init(xmlDef);
|
||||
|
||||
nodeName = xmlDef.Attribute("name")?.Value;
|
||||
nodeName = xmlDef.Attribute("name")?.Value??"noName";
|
||||
drawNodeType = Enum.TryParse(xmlDef.Attribute("type")?.Value, true, out DrawNodeType typeResult)
|
||||
? typeResult
|
||||
: DrawNodeType.Image;
|
||||
|
@ -8,48 +8,12 @@ namespace Data
|
||||
public class PawnDef : Define
|
||||
{
|
||||
public AttributesDef attributes;
|
||||
public DrawingOrderDef
|
||||
drawingOrder_down,
|
||||
drawingOrder_up,
|
||||
drawingOrder_left,
|
||||
drawingOrder_right;
|
||||
public DrawingOrderDef drawingOrder;
|
||||
|
||||
public BehaviorTreeDef behaviorTree;
|
||||
public string affiliation;
|
||||
|
||||
|
||||
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 MonsterDef:PawnDef
|
||||
{
|
||||
|
@ -44,9 +44,61 @@ namespace Entity
|
||||
attributes = pawnDef.attributes.Clone();
|
||||
aiTree = ConvertToAIBase(pawnDef.behaviorTree);
|
||||
affiliation=pawnDef.affiliation;
|
||||
//定义的tag有限,不用这个了
|
||||
// if(!string.IsNullOrEmpty(affiliation))
|
||||
// gameObject.CompareTag(affiliation);
|
||||
InitBody(pawnDef.drawingOrder);
|
||||
}
|
||||
|
||||
public virtual void InitBody(DrawingOrderDef drawingOrder)
|
||||
{
|
||||
// 定义方向枚举和对应的 GetDrawingOrder 调用
|
||||
Orientation[] orientations = { Orientation.Down, Orientation.Up, Orientation.Left, Orientation.Right };
|
||||
|
||||
foreach (var orientation in orientations)
|
||||
{
|
||||
// 获取当前方向的绘图节点
|
||||
var drawNode = drawingOrder.GetDrawingOrder(orientation);
|
||||
|
||||
if (drawNode == null) continue;
|
||||
var directionRoot = new GameObject(orientation.ToString());
|
||||
directionRoot.transform.SetParent(body.transform, false);
|
||||
|
||||
InitBodyPart(drawNode, directionRoot);
|
||||
}
|
||||
}
|
||||
|
||||
// 递归初始化单个绘图节点及其子节点
|
||||
public virtual void InitBodyPart(DrawNodeDef drawNode, GameObject parent)
|
||||
{
|
||||
if(drawNode==null) return;
|
||||
// 创建新的 GameObject 表示当前节点
|
||||
var nodeObject = new GameObject(drawNode.nodeName);
|
||||
|
||||
// 设置节点的父对象
|
||||
nodeObject.transform.SetParent(parent.transform, false);
|
||||
|
||||
// 设置节点的位置
|
||||
nodeObject.transform.localPosition = new Vector3(drawNode.position.x, drawNode.position.y, 0);
|
||||
|
||||
// 根据节点类型设置其他特性(例如动画或图片)
|
||||
switch (drawNode.drawNodeType)
|
||||
{
|
||||
case DrawNodeType.Image:
|
||||
var spriteRenderer = nodeObject.AddComponent<SpriteRenderer>();
|
||||
spriteRenderer.color = Color.white; // 默认颜色
|
||||
break;
|
||||
|
||||
case DrawNodeType.Animation:
|
||||
var animator = nodeObject.AddComponent<Animator>();
|
||||
animator.runtimeAnimatorController = null; // 需要手动设置动画控制器
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
// 递归初始化子节点
|
||||
foreach (var child in drawNode.children)
|
||||
{
|
||||
InitBodyPart(child, nodeObject);
|
||||
}
|
||||
}
|
||||
public void Tick()
|
||||
{
|
||||
|
151
Client/Assets/Scripts/Entity/SpriteAnimator.cs
Normal file
151
Client/Assets/Scripts/Entity/SpriteAnimator.cs
Normal file
@ -0,0 +1,151 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class SpriteAnimator : MonoBehaviour
|
||||
{
|
||||
// 引用 SpriteRenderer 组件
|
||||
[SerializeField] private SpriteRenderer spriteRenderer;
|
||||
|
||||
// 精灵列表
|
||||
[SerializeField] private Sprite[] sprites;
|
||||
|
||||
// 动画帧率 (Frames Per Second)
|
||||
[SerializeField] private float fps = 10f;
|
||||
|
||||
// 是否暂停动画
|
||||
[SerializeField] private bool isPaused = false;
|
||||
|
||||
// 暂停时显示的静态精灵索引 (-1 表示不显示静态精灵)
|
||||
[SerializeField] private int staticSpriteIndex = -1;
|
||||
|
||||
// 当前帧索引
|
||||
private int currentFrameIndex = 0;
|
||||
|
||||
// 帧间隔时间
|
||||
private float frameInterval;
|
||||
|
||||
// 计时器
|
||||
private float timer;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// 初始化帧间隔时间
|
||||
frameInterval = 1f / fps;
|
||||
|
||||
// 如果指定了静态精灵索引,则直接显示静态精灵
|
||||
if (staticSpriteIndex >= 0 && staticSpriteIndex < sprites.Length)
|
||||
{
|
||||
ShowStaticSprite(staticSpriteIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 否则从第一个精灵开始
|
||||
UpdateSprite();
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// 如果暂停并且没有设置静态精灵,则不更新
|
||||
if (isPaused && staticSpriteIndex == -1) return;
|
||||
|
||||
// 如果暂停并设置了静态精灵,则直接显示静态精灵
|
||||
if (isPaused && staticSpriteIndex >= 0)
|
||||
{
|
||||
ShowStaticSprite(staticSpriteIndex);
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新计时器
|
||||
timer += Time.deltaTime;
|
||||
|
||||
// 如果达到下一帧的时间间隔
|
||||
if (timer >= frameInterval)
|
||||
{
|
||||
timer -= frameInterval; // 重置计时器
|
||||
UpdateSprite(); // 更新精灵
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新当前显示的精灵
|
||||
/// </summary>
|
||||
private void UpdateSprite()
|
||||
{
|
||||
if (sprites.Length == 0) return; // 如果没有精灵,则退出
|
||||
|
||||
// 设置当前帧的精灵
|
||||
spriteRenderer.sprite = sprites[currentFrameIndex];
|
||||
|
||||
// 循环播放:更新到下一帧
|
||||
currentFrameIndex = (currentFrameIndex + 1) % sprites.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示静态精灵
|
||||
/// </summary>
|
||||
/// <param name="index">静态精灵的索引</param>
|
||||
private void ShowStaticSprite(int index)
|
||||
{
|
||||
if (index < 0 || index >= sprites.Length)
|
||||
{
|
||||
Debug.LogWarning("静态精灵索引超出范围!");
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示指定的静态精灵
|
||||
spriteRenderer.sprite = sprites[index];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始播放动画
|
||||
/// </summary>
|
||||
public void PlayAnimation()
|
||||
{
|
||||
isPaused = false; // 取消暂停
|
||||
staticSpriteIndex = -1; // 清除静态精灵索引
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 暂停动画并显示静态精灵
|
||||
/// </summary>
|
||||
/// <param name="index">静态精灵的索引</param>
|
||||
public void PauseAnimationWithStaticSprite(int index)
|
||||
{
|
||||
if (index < 0 || index >= sprites.Length)
|
||||
{
|
||||
Debug.LogWarning("静态精灵索引超出范围!");
|
||||
return;
|
||||
}
|
||||
|
||||
isPaused = true; // 暂停动画
|
||||
staticSpriteIndex = index; // 设置静态精灵索引
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 暂停动画但不显示静态精灵
|
||||
/// </summary>
|
||||
public void PauseAnimation()
|
||||
{
|
||||
isPaused = true; // 暂停动画
|
||||
staticSpriteIndex = -1; // 不显示静态精灵
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置帧率
|
||||
/// </summary>
|
||||
/// <param name="newFps">新的帧率</param>
|
||||
public void SetFPS(float newFps)
|
||||
{
|
||||
if (newFps <= 0)
|
||||
{
|
||||
Debug.LogWarning("帧率必须大于 0!");
|
||||
return;
|
||||
}
|
||||
|
||||
fps = newFps;
|
||||
frameInterval = 1f / fps; // 更新帧间隔时间
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Entity/SpriteAnimator.cs.meta
Normal file
3
Client/Assets/Scripts/Entity/SpriteAnimator.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 713a1742950a45d8b4be258a82321e45
|
||||
timeCreated: 1753282330
|
@ -7,6 +7,8 @@ namespace Managers
|
||||
{
|
||||
public class PackagesImageManager : Utils.Singleton<PackagesImageManager>
|
||||
{
|
||||
public Sprite defaultSprite;
|
||||
|
||||
public Dictionary<string, Dictionary<string, Texture2D>> packagesImages = new();
|
||||
public Dictionary<string, Dictionary<string, Sprite>> sprites = new();
|
||||
|
||||
@ -14,7 +16,9 @@ namespace Managers
|
||||
{
|
||||
if (packagesImages.Count > 0)
|
||||
return;
|
||||
|
||||
defaultSprite = Resources.Load<Sprite>("Default/DefaultImage");
|
||||
if(defaultSprite)
|
||||
Debug.Log("加载成功");
|
||||
var imageDef = Managers.DefineManager.Instance.QueryDefinesByType<ImageDef>();
|
||||
foreach (var ima in imageDef)
|
||||
{
|
||||
@ -113,14 +117,13 @@ namespace Managers
|
||||
return sprite;
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (sprites.TryGetValue(packID, out var dict))
|
||||
{
|
||||
if (!sprites.TryGetValue(packID, out var dict)) return null;
|
||||
dict.TryGetValue(name, out var sprite);
|
||||
return sprite;
|
||||
if (dict.TryGetValue(name, out var sprite))
|
||||
return sprite;
|
||||
}
|
||||
|
||||
return null;
|
||||
return defaultSprite;
|
||||
}
|
||||
|
||||
public Sprite GetSprite(string packID, string name, int index)
|
||||
|
Reference in New Issue
Block a user