(client)feat:实现动画组件和图片组件预制体

This commit is contained in:
m0_75251201
2025-07-24 13:19:27 +08:00
parent 179123f660
commit 8471f00b64
13 changed files with 605 additions and 313 deletions

View File

@ -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()