2025-07-25 19:16:58 +08:00
|
|
|
|
using System;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
using System.Collections;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-08-22 20:43:55 +08:00
|
|
|
|
using System.Linq;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
using AI;
|
|
|
|
|
using Base;
|
|
|
|
|
using Data;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
using Prefab;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Serialization;
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-07-21 13:58:58 +08:00
|
|
|
|
namespace Entity
|
|
|
|
|
{
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 表示游戏中的实体类,继承自 MonoBehaviour 并实现 ITick 接口。
|
|
|
|
|
/// </summary>
|
2025-08-07 16:44:43 +08:00
|
|
|
|
public class Entity : MonoBehaviour, ITick
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 动画预制体,用于管理实体的动画逻辑。
|
|
|
|
|
/// </summary>
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public SpriteAnimator animatorPrefab;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 图像预制体,用于管理实体的静态图像显示。
|
|
|
|
|
/// </summary>
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public ImagePrefab imagePrefab;
|
2025-08-07 16:44:43 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
public ProgressBarPrefab healthBarPrefab;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 人工智能行为树,定义实体的行为逻辑。
|
|
|
|
|
/// </summary>
|
2025-07-21 13:58:58 +08:00
|
|
|
|
public AIBase aiTree;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前实体正在执行的任务。
|
|
|
|
|
/// </summary>
|
2025-07-21 13:58:58 +08:00
|
|
|
|
public JobBase currentJob;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 实体的属性定义,包括生命值、攻击力、防御力等。
|
|
|
|
|
/// </summary>
|
2025-08-07 16:44:43 +08:00
|
|
|
|
public AttributesDef attributes = new();
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 实体当前的移动方向。
|
|
|
|
|
/// </summary>
|
2025-07-21 13:58:58 +08:00
|
|
|
|
public Vector3 direction;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 实体的身体部分,用于挂载动画和图像节点。
|
|
|
|
|
/// </summary>
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public GameObject body;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 实体所属的阵营或派系。
|
|
|
|
|
/// </summary>
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public string affiliation;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 表示实体是否可以被选择。
|
|
|
|
|
/// </summary>
|
2025-07-21 13:58:58 +08:00
|
|
|
|
public bool canSelect = true;
|
2025-08-07 16:44:43 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 表示实体是否处于追逐状态(影响移动速度)。
|
|
|
|
|
/// </summary>
|
2025-07-21 13:58:58 +08:00
|
|
|
|
public bool IsChase { set; get; } = true;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 表示实体是否由玩家控制。
|
|
|
|
|
/// </summary>
|
2025-07-21 13:58:58 +08:00
|
|
|
|
public bool PlayerControlled
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
if (value)
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
|
|
|
|
IsChase = true;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
currentJob = null;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
_isPlayerControlled = value;
|
|
|
|
|
}
|
|
|
|
|
get => _isPlayerControlled;
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取实体当前位置。
|
|
|
|
|
/// </summary>
|
2025-08-07 16:44:43 +08:00
|
|
|
|
public Vector3 Position => transform.position;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 表示实体是否已经死亡(生命值小于等于零)。
|
|
|
|
|
/// </summary>
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public bool IsDead => attributes.health <= 0;
|
2025-08-22 20:43:55 +08:00
|
|
|
|
public bool IsShowingHealthBarUI=>hitBarUIShowTimer > 0;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
public bool IsAttacking => attackCoroutine != null;
|
|
|
|
|
|
|
|
|
|
|
2025-07-21 13:58:58 +08:00
|
|
|
|
private bool _isPlayerControlled = false;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
private bool _warning = false;
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 存储不同朝向下的动画节点集合。
|
|
|
|
|
/// </summary>
|
2025-08-22 20:43:55 +08:00
|
|
|
|
public Dictionary<EntityState, Dictionary<Orientation, List<ITick>>> bodyAnimationNode = new();
|
|
|
|
|
private List<ITick> currentAnimatorCache=new ();
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 存储不同朝向下的身体节点对象。
|
|
|
|
|
/// </summary>
|
2025-08-22 20:43:55 +08:00
|
|
|
|
private Dictionary<EntityState, Dictionary<Orientation,GameObject>> bodyNodes = new();
|
2025-08-07 16:44:43 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前实体的朝向。
|
|
|
|
|
/// </summary>
|
2025-07-25 19:16:58 +08:00
|
|
|
|
private Orientation currentOrientation = Orientation.Down;
|
2025-08-22 20:43:55 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前实体的状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
private EntityState currentState = EntityState.Idle;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 攻击动画的持续时间(秒)。
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const float attackAnimationDuration = 0.1f;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 抖动的偏移量。
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const float shakeOffset = 0.5f;
|
|
|
|
|
// 协程引用
|
|
|
|
|
private Coroutine attackCoroutine;
|
|
|
|
|
|
|
|
|
|
protected EntityDef entityDef;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public float hitBarUIShowTime = 5;
|
|
|
|
|
private float hitBarUIShowTimer = 0;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 初始化实体的基本属性和行为树。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="entityDef">实体的定义数据。</param>
|
|
|
|
|
public virtual void Init(EntityDef entityDef)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-19 20:22:10 +08:00
|
|
|
|
attributes = entityDef.attributes.Clone();
|
|
|
|
|
aiTree = Utils.BehaviorTree.ConvertToAIBase(entityDef.behaviorTree);
|
|
|
|
|
affiliation = entityDef.affiliation?.defName;
|
|
|
|
|
InitBody(entityDef.drawingOrder);
|
|
|
|
|
this.entityDef = entityDef;
|
|
|
|
|
|
|
|
|
|
HideHealthBar();
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 初始化实体的身体部分,包括不同朝向下的绘图节点。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="drawingOrder">绘制顺序定义。</param>
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public virtual void InitBody(DrawingOrderDef drawingOrder)
|
|
|
|
|
{
|
2025-08-22 20:43:55 +08:00
|
|
|
|
// 预缓存枚举值(避免每次循环重复调用 Enum.GetValues)
|
|
|
|
|
var states = Enum.GetValues(typeof(EntityState)).Cast<EntityState>().ToArray();
|
|
|
|
|
var orientations = Enum.GetValues(typeof(Orientation)).Cast<Orientation>().ToArray();
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-08-22 20:43:55 +08:00
|
|
|
|
// 预初始化字典结构(减少内层循环的字典检查)
|
|
|
|
|
foreach (var state in states)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-22 20:43:55 +08:00
|
|
|
|
bodyNodes.TryAdd(state, new Dictionary<Orientation, GameObject>());
|
|
|
|
|
bodyAnimationNode.TryAdd(state, new Dictionary<Orientation, List<ITick>>());
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 20:43:55 +08:00
|
|
|
|
// 主初始化逻辑
|
|
|
|
|
foreach (var state in states)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-22 20:43:55 +08:00
|
|
|
|
var stateBodyNodes = bodyNodes[state];
|
|
|
|
|
var stateAnimNodes = bodyAnimationNode[state];
|
|
|
|
|
|
|
|
|
|
foreach (var orientation in orientations)
|
|
|
|
|
{
|
|
|
|
|
// 获取节点定义(避免重复调用)
|
|
|
|
|
var nodeDef = drawingOrder.GetDrawNodeDef(state, orientation, out var original);
|
|
|
|
|
|
|
|
|
|
// 处理空节点定义(直接创建空对象)
|
|
|
|
|
if (nodeDef == null)
|
|
|
|
|
{
|
|
|
|
|
var obj = new GameObject { name = $"{state}_Empty" };
|
|
|
|
|
obj.transform.SetParent(body.transform, false);
|
|
|
|
|
stateBodyNodes[orientation] = obj;
|
|
|
|
|
continue; // 跳过后续动画处理
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理有效节点定义
|
|
|
|
|
GameObject targetObj;
|
|
|
|
|
if (original.HasValue && stateBodyNodes.TryGetValue(original.Value, out var reusedObj))
|
|
|
|
|
{
|
|
|
|
|
targetObj = reusedObj; // 复用已有对象
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
targetObj = InitBodyPart(nodeDef, body); // 创建新对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stateBodyNodes[orientation] = targetObj;
|
|
|
|
|
|
|
|
|
|
// 提取动画组件(安全处理空组件情况)
|
|
|
|
|
var animators = targetObj.GetComponentsInChildren<SpriteAnimator>();
|
|
|
|
|
if (animators.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
stateAnimNodes[orientation] = animators.Cast<ITick>().ToList();
|
|
|
|
|
}
|
|
|
|
|
// 无动画组件时保持 stateAnimNodes[orientation] 为 null(符合原始逻辑)
|
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
2025-08-22 20:43:55 +08:00
|
|
|
|
|
|
|
|
|
// 批量隐藏所有节点(使用字典值集合直接操作)
|
|
|
|
|
foreach (var nodeDict in bodyNodes.Values)
|
|
|
|
|
{
|
|
|
|
|
foreach (var obj in nodeDict.Values)
|
|
|
|
|
{
|
|
|
|
|
obj.SetActive(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetBodyTexture(EntityState.Idle,Orientation.Down); // 激活默认朝向
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
2025-08-22 20:43:55 +08:00
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
2025-08-22 20:43:55 +08:00
|
|
|
|
/// 递归初始化单个绘图节点及其子节点,具有更强的健壮性和错误处理。
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="drawNode">绘图节点定义。</param>
|
|
|
|
|
/// <param name="parent">父节点对象。</param>
|
2025-08-22 20:43:55 +08:00
|
|
|
|
/// <returns>创建的GameObject,如果失败则返回null</returns>
|
|
|
|
|
public virtual GameObject InitBodyPart(DrawNodeDef drawNode, GameObject parent)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-22 20:43:55 +08:00
|
|
|
|
try
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-22 20:43:55 +08:00
|
|
|
|
// 参数验证
|
|
|
|
|
if (drawNode == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("InitBodyPart: drawNode参数为null");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parent == null)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-22 20:43:55 +08:00
|
|
|
|
Debug.LogWarning($"InitBodyPart: 父节点为null (节点名: {drawNode.nodeName})");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GameObject nodeObject = null;
|
|
|
|
|
// 根据纹理数量创建不同类型的节点
|
|
|
|
|
switch (drawNode.animationTextures?.Count ?? 0)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
// 无纹理节点
|
|
|
|
|
nodeObject = new GameObject(drawNode.nodeName);
|
|
|
|
|
nodeObject.transform.SetParent(parent.transform, false);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
// 单纹理节点
|
|
|
|
|
if (imagePrefab == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"InitBodyPart: imagePrefab未设置 (节点名: {drawNode.nodeName})");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
nodeObject = Instantiate(imagePrefab.gameObject, parent.transform);
|
|
|
|
|
var texture =
|
2025-08-22 20:43:55 +08:00
|
|
|
|
Managers.PackagesImageManager.Instance?.GetSprite(drawNode.packID,
|
|
|
|
|
drawNode.animationTextures[0]);
|
|
|
|
|
|
|
|
|
|
if (!texture)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning(
|
|
|
|
|
$"InitBodyPart: 无法获取纹理 (节点名: {drawNode.nodeName}, 纹理ID: {drawNode.animationTextures[0]})");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var imagePrefabCom = nodeObject.GetComponent<ImagePrefab>();
|
|
|
|
|
if (imagePrefabCom != null)
|
|
|
|
|
{
|
|
|
|
|
imagePrefabCom.SetSprite(texture);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"InitBodyPart: 无法获取ImagePrefab组件 (节点名: {drawNode.nodeName})");
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
break;
|
|
|
|
|
|
2025-08-22 20:43:55 +08:00
|
|
|
|
default:
|
|
|
|
|
// 多纹理动画节点
|
|
|
|
|
if (!animatorPrefab)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"InitBodyPart: animatorPrefab未设置 (节点名: {drawNode.nodeName})");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
nodeObject = Instantiate(animatorPrefab.gameObject, parent.transform);
|
|
|
|
|
var animator = nodeObject.GetComponent<SpriteAnimator>();
|
2025-08-22 20:43:55 +08:00
|
|
|
|
|
|
|
|
|
if (animator == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"InitBodyPart: 无法获取SpriteAnimator组件 (节点名: {drawNode.nodeName})");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
animator.SetFPS(drawNode.FPS);
|
|
|
|
|
var animatedSprites = new List<Sprite>();
|
|
|
|
|
foreach (var textureId in drawNode.animationTextures)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var sprite =
|
|
|
|
|
Managers.PackagesImageManager.Instance?.GetSprite(drawNode.packID, textureId);
|
|
|
|
|
if (sprite != null)
|
|
|
|
|
{
|
|
|
|
|
animatedSprites.Add(sprite);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning(
|
|
|
|
|
$"InitBodyPart: 无法获取动画纹理 (节点名: {drawNode.nodeName}, 纹理ID: {textureId})");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError(
|
|
|
|
|
$"InitBodyPart: 加载动画纹理时出错 (节点名: {drawNode.nodeName}, 纹理ID: {textureId}): {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (animatedSprites.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
animator.SetSprites(animatedSprites.ToArray());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"InitBodyPart: 没有有效的动画纹理 (节点名: {drawNode.nodeName})");
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2025-08-22 20:43:55 +08:00
|
|
|
|
|
|
|
|
|
// 设置节点属性
|
|
|
|
|
if (!nodeObject) return nodeObject;
|
|
|
|
|
|
|
|
|
|
nodeObject.transform.localPosition = drawNode.position;
|
|
|
|
|
nodeObject.name = drawNode.nodeName ?? "UnnamedNode";
|
|
|
|
|
|
|
|
|
|
// 递归初始化子节点
|
|
|
|
|
if (drawNode.nodes == null) return nodeObject;
|
|
|
|
|
foreach (var child in drawNode.nodes)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
InitBodyPart(child, nodeObject);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"InitBodyPart: 初始化子节点失败 (父节点: {drawNode.nodeName}): {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nodeObject;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
2025-08-22 20:43:55 +08:00
|
|
|
|
catch (Exception ex)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-22 20:43:55 +08:00
|
|
|
|
Debug.LogError($"InitBodyPart: 初始化节点时发生未处理的异常 (节点名: {drawNode?.nodeName}): {ex}");
|
|
|
|
|
return null;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 更新实体的逻辑,包括玩家控制和自动行为。
|
|
|
|
|
/// </summary>
|
2025-07-21 13:58:58 +08:00
|
|
|
|
public void Tick()
|
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
if (_isPlayerControlled)
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
|
|
|
|
UpdatePlayerControls();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AutoBehave();
|
|
|
|
|
}
|
2025-08-22 20:43:55 +08:00
|
|
|
|
|
|
|
|
|
if (currentAnimatorCache!=null)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-22 20:43:55 +08:00
|
|
|
|
foreach (var animator in currentAnimatorCache)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-22 20:43:55 +08:00
|
|
|
|
animator.Tick();
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-08-22 20:43:55 +08:00
|
|
|
|
|
|
|
|
|
if (IsShowingHealthBarUI)
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
|
|
|
|
hitBarUIShowTimer -= Time.deltaTime;
|
|
|
|
|
if (hitBarUIShowTimer <= 0)
|
|
|
|
|
{
|
|
|
|
|
HideHealthBar();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 尝试攻击目标实体。
|
|
|
|
|
/// </summary>
|
2025-08-07 16:44:43 +08:00
|
|
|
|
public virtual void TryAttack()
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
2025-08-19 20:22:10 +08:00
|
|
|
|
if(!IsAttacking)
|
|
|
|
|
attackCoroutine = StartCoroutine(AttackFlow());
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
2025-08-22 20:43:55 +08:00
|
|
|
|
|
|
|
|
|
public virtual void SetBodyTexture(EntityState state, Orientation orientation)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-22 20:43:55 +08:00
|
|
|
|
if (bodyNodes.TryGetValue(currentState, out var stateNode))
|
|
|
|
|
{
|
|
|
|
|
if (stateNode.TryGetValue(currentOrientation, out var node))
|
|
|
|
|
{
|
|
|
|
|
node.SetActive(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bodyNodes.TryGetValue(state, out var showStateNode))
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
2025-08-22 20:43:55 +08:00
|
|
|
|
if (showStateNode.TryGetValue(orientation, out var showNode))
|
|
|
|
|
{
|
|
|
|
|
showNode.SetActive(true);
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
2025-08-22 20:43:55 +08:00
|
|
|
|
|
|
|
|
|
currentState = state;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
currentOrientation = orientation;
|
2025-08-22 20:43:55 +08:00
|
|
|
|
|
|
|
|
|
if (bodyAnimationNode.TryGetValue(currentState, out var animationNode))
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
2025-08-22 20:43:55 +08:00
|
|
|
|
if (animationNode.TryGetValue(currentOrientation, out var value))
|
|
|
|
|
{
|
|
|
|
|
currentAnimatorCache=value;
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-07-21 13:58:58 +08:00
|
|
|
|
/// <summary>
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// 根据方向尝试移动实体。
|
2025-07-21 13:58:58 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual void TryMove()
|
|
|
|
|
{
|
2025-08-19 20:22:10 +08:00
|
|
|
|
if (IsAttacking)
|
|
|
|
|
return;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
transform.position += direction * (attributes.moveSpeed * Time.deltaTime * (IsChase ? 1 : 0.5f));
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 处理实体受到攻击的逻辑。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="from">攻击来源实体。</param>
|
2025-07-21 13:58:58 +08:00
|
|
|
|
public virtual void OnHit(Entity from)
|
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
var hit = from.attributes.attack - attributes.defense;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
if (hit < 0)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
hit = from.attributes.attack / 100;
|
|
|
|
|
attributes.health -= hit;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
currentJob?.StopJob();
|
|
|
|
|
ShowHealthBar();
|
|
|
|
|
}
|
2025-08-07 16:44:43 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
public void ShowHealthBar()
|
|
|
|
|
{
|
|
|
|
|
if(!healthBarPrefab)
|
|
|
|
|
return;
|
|
|
|
|
healthBarPrefab.gameObject.SetActive(true);
|
|
|
|
|
healthBarPrefab.Progress = (float)attributes.health / entityDef.attributes.health;
|
|
|
|
|
hitBarUIShowTimer=hitBarUIShowTime;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
public void HideHealthBar()
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
2025-08-19 20:22:10 +08:00
|
|
|
|
if(!healthBarPrefab)
|
|
|
|
|
return;
|
|
|
|
|
healthBarPrefab.gameObject.SetActive(false);
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 杀死实体,设置生命值为零。
|
|
|
|
|
/// </summary>
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public virtual void Kill()
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
attributes.health = 0;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置实体的目标位置。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pos">目标位置。</param>
|
|
|
|
|
public virtual void SetTarget(Vector3 pos)
|
|
|
|
|
{
|
|
|
|
|
direction = (pos - transform.position).normalized;
|
|
|
|
|
Orientation ori;
|
|
|
|
|
// 判断方向向量最接近哪个朝向
|
|
|
|
|
if (Mathf.Abs(direction.y) > Mathf.Abs(direction.x))
|
|
|
|
|
{
|
|
|
|
|
// 垂直方向优先
|
|
|
|
|
ori = direction.y > 0 ? Orientation.Up : Orientation.Down;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 水平方向优先
|
|
|
|
|
ori = direction.x > 0 ? Orientation.Right : Orientation.Left;
|
|
|
|
|
}
|
2025-08-22 20:43:55 +08:00
|
|
|
|
|
|
|
|
|
SetBodyTexture(currentState, ori);
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 自动行为逻辑,根据行为树执行任务。
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void AutoBehave()
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
2025-08-07 16:44:43 +08:00
|
|
|
|
if (aiTree == null)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
return;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
if (currentJob == null || !currentJob.Running)
|
|
|
|
|
{
|
|
|
|
|
currentJob = aiTree.GetJob(this);
|
|
|
|
|
if (currentJob == null)
|
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
if (!_warning)
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"{GetType().Name}类型的{name}没有分配到任何工作,给行为树末尾添加等待行为,避免由于没有工作导致无意义的反复查找工作导致性能问题");
|
2025-07-25 19:16:58 +08:00
|
|
|
|
_warning = true;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
currentJob.StartJob(this);
|
|
|
|
|
}
|
2025-08-07 16:44:43 +08:00
|
|
|
|
|
2025-07-21 13:58:58 +08:00
|
|
|
|
currentJob.Update();
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-07-21 13:58:58 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 更新玩家控制的逻辑,处理输入和移动。
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void UpdatePlayerControls()
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
// 检测 Shift 键状态
|
|
|
|
|
var isHoldingShift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
|
|
|
|
|
IsChase = !isHoldingShift; // 按住 Shift 时 IsChase = false,否则 true
|
2025-08-19 20:22:10 +08:00
|
|
|
|
// 获取当前键盘输入状态(2D 移动,只使用 X 和 Y 轴)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
var inputDirection = Vector2.zero;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
// 检测 WASD 或方向键输入
|
2025-07-21 13:58:58 +08:00
|
|
|
|
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
|
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
inputDirection += Vector2.up; // 向上移动(Y 轴正方向)
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
|
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
inputDirection += Vector2.down; // 向下移动(Y 轴负方向)
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
|
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
inputDirection += Vector2.left; // 向左移动(X 轴负方向)
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
|
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
inputDirection += Vector2.right; // 向右移动(X 轴正方向)
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
if (Input.GetMouseButtonDown(0))
|
|
|
|
|
{
|
|
|
|
|
TryAttack();
|
|
|
|
|
}
|
2025-07-21 13:58:58 +08:00
|
|
|
|
// 如果有输入方向,则设置目标位置并尝试移动
|
2025-07-25 19:16:58 +08:00
|
|
|
|
if (inputDirection == Vector2.zero) return;
|
|
|
|
|
// 归一化方向向量,确保对角线移动速度一致
|
|
|
|
|
inputDirection = inputDirection.normalized;
|
|
|
|
|
|
|
|
|
|
// 设置目标位置(2D 移动,Z 轴保持不变)
|
|
|
|
|
var targetPosition = transform.position + new Vector3(inputDirection.x, inputDirection.y, 0);
|
|
|
|
|
|
|
|
|
|
// 调用 SetTarget 方法设置目标位置
|
|
|
|
|
SetTarget(targetPosition);
|
|
|
|
|
|
|
|
|
|
// 调用 TryMove 方法处理实际移动逻辑
|
|
|
|
|
TryMove();
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
// 攻击流程协程
|
|
|
|
|
IEnumerator AttackFlow()
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-19 20:22:10 +08:00
|
|
|
|
// 播放攻击动画并获取动画持续时间
|
|
|
|
|
var animationDuration = PlayAttackAnimation();
|
|
|
|
|
// 等待动画执行完毕
|
|
|
|
|
yield return new WaitForSeconds(animationDuration);
|
|
|
|
|
// 调用检测并攻击敌人的方法
|
|
|
|
|
DetectAndAttackEnemies();
|
|
|
|
|
// 攻击流程结束,清理协程引用
|
|
|
|
|
attackCoroutine = null;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 播放攻击动画。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>开始检测攻击范围内敌人的时间。</returns>
|
|
|
|
|
public float PlayAttackAnimation()
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-19 20:22:10 +08:00
|
|
|
|
// 启动协程来执行攻击动画
|
|
|
|
|
StartCoroutine(ShakeInDirectionCoroutine());
|
|
|
|
|
|
|
|
|
|
// 返回检测敌人的起始时间
|
|
|
|
|
return attackAnimationDuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator ShakeInDirectionCoroutine()
|
|
|
|
|
{
|
|
|
|
|
var originalPosition = transform.position; // 记录原始位置
|
|
|
|
|
transform.position += direction * shakeOffset;
|
|
|
|
|
yield return new WaitForSeconds(attackAnimationDuration);
|
|
|
|
|
transform.position = originalPosition;
|
|
|
|
|
}
|
2025-07-21 13:58:58 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
public void DetectAndAttackEnemies()
|
|
|
|
|
{
|
|
|
|
|
var attackCount = attributes.attackTargetCount;
|
|
|
|
|
// 获取攻击范围内的所有碰撞体
|
|
|
|
|
var hits = Physics2D.OverlapCircleAll(
|
|
|
|
|
transform.position,
|
|
|
|
|
attributes.attackRange,
|
|
|
|
|
LayerMask.GetMask("Entity"));
|
|
|
|
|
|
|
|
|
|
foreach (var hit in hits)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-19 20:22:10 +08:00
|
|
|
|
if (attackCount <= 0) break;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
// 检查是否是自身(额外安全措施)
|
|
|
|
|
if (hit.gameObject == this.gameObject) continue;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
// 获取Entity组件
|
|
|
|
|
var entity = hit.GetComponent<Entity>();
|
|
|
|
|
if (!entity) continue;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
// 执行攻击
|
|
|
|
|
entity.OnHit(this);
|
|
|
|
|
attackCount--;
|
|
|
|
|
}
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|