(client)feat:实现动画组件和图片组件预制体
This commit is contained in:
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using AI;
|
||||
using Base;
|
||||
using Data;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
@ -10,6 +11,9 @@ namespace Entity
|
||||
{
|
||||
public class Entity:MonoBehaviour,ITick
|
||||
{
|
||||
public SpriteAnimator animatorPrefab;
|
||||
public ImagePrefab imagePrefab;
|
||||
|
||||
public AIBase aiTree;
|
||||
public JobBase currentJob;
|
||||
public AttributesDef attributes;
|
||||
@ -38,6 +42,8 @@ namespace Entity
|
||||
private bool _isPlayerControlled = false;
|
||||
private bool _warning = false;
|
||||
|
||||
private Dictionary<Orientation,List<ITick>> bodyNode=new();
|
||||
private Orientation currentOrientation=Orientation.Down;
|
||||
|
||||
public virtual void Init(PawnDef pawnDef)
|
||||
{
|
||||
@ -54,6 +60,8 @@ namespace Entity
|
||||
|
||||
foreach (var orientation in orientations)
|
||||
{
|
||||
currentOrientation=orientation;
|
||||
bodyNode[orientation]=new();
|
||||
// 获取当前方向的绘图节点
|
||||
var drawNode = drawingOrder.GetDrawingOrder(orientation);
|
||||
|
||||
@ -63,32 +71,26 @@ namespace Entity
|
||||
|
||||
InitBodyPart(drawNode, directionRoot);
|
||||
}
|
||||
currentOrientation=Orientation.Down;
|
||||
}
|
||||
|
||||
// 递归初始化单个绘图节点及其子节点
|
||||
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);
|
||||
|
||||
// 根据节点类型设置其他特性(例如动画或图片)
|
||||
GameObject nodeObject;
|
||||
switch (drawNode.drawNodeType)
|
||||
{
|
||||
case DrawNodeType.Image:
|
||||
var spriteRenderer = nodeObject.AddComponent<SpriteRenderer>();
|
||||
spriteRenderer.color = Color.white; // 默认颜色
|
||||
nodeObject = Instantiate(imagePrefab.gameObject);
|
||||
break;
|
||||
|
||||
case DrawNodeType.Animation:
|
||||
var animator = nodeObject.AddComponent<Animator>();
|
||||
animator.runtimeAnimatorController = null; // 需要手动设置动画控制器
|
||||
nodeObject = Instantiate(animatorPrefab.gameObject);
|
||||
ITick tick = nodeObject.GetComponent<SpriteAnimator>();
|
||||
if (tick != null)
|
||||
bodyNode[currentOrientation].Add(tick);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
@ -110,6 +112,14 @@ namespace Entity
|
||||
{
|
||||
AutoBehave();
|
||||
}
|
||||
|
||||
foreach (var bodyPart in bodyNode.Values)
|
||||
{
|
||||
foreach (var tick in bodyPart)
|
||||
{
|
||||
tick.Tick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void TryAttck()
|
||||
|
@ -1,151 +0,0 @@
|
||||
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; // 更新帧间隔时间
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 713a1742950a45d8b4be258a82321e45
|
||||
timeCreated: 1753282330
|
Reference in New Issue
Block a user