mirror of
http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite.git
synced 2025-11-20 13:57:12 +08:00
(client) feat:健康给予,路径优化,结算界面,商店界面 (#60)
Co-authored-by: m0_75251201 <m0_75251201@noreply.gitcode.com> Reviewed-on: http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite/pulls/60
This commit is contained in:
@@ -1,39 +1,67 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AI;
|
||||
using Base;
|
||||
using Data;
|
||||
using Item;
|
||||
using Managers;
|
||||
using Prefab;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Parsing;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
using Utils;
|
||||
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示游戏中的实体类,继承自 MonoBehaviour 并实现 ITick 接口。
|
||||
/// 表示游戏中的实体类,继承自 MonoBehaviour 并实现 ITick 接口。
|
||||
/// </summary>
|
||||
public class Entity : MonoBehaviour, ITick
|
||||
{
|
||||
private const float TAN_50_DEGREES = 1.19175356925f; // Mathf.Tan(50f * Mathf.Deg2Rad)
|
||||
private const float TargetReachThresholdSquared = 0.001f;
|
||||
public ProgressBarPrefab healthBarPrefab;
|
||||
public EntityPrefab entityPrefab;
|
||||
public EntityDef entityDef;
|
||||
|
||||
/// <summary>
|
||||
/// 手上拿着显示的贴图
|
||||
/// 手上拿着显示的贴图
|
||||
/// </summary>
|
||||
public GameObject weaponAnimator;
|
||||
|
||||
public SpriteAnimator weaponItem;
|
||||
public SpriteAnimator weaponAttackAnimation;
|
||||
|
||||
|
||||
public Vector3 attackDirection;
|
||||
|
||||
/// <summary>
|
||||
/// 人工智能行为树,定义实体的行为逻辑。
|
||||
/// 实体的身体部分,用于挂载动画和图像节点。
|
||||
/// </summary>
|
||||
public BehaviorTreeBase BehaviorTreeTree { get; private set; }
|
||||
public GameObject body;
|
||||
|
||||
/// <summary>
|
||||
/// 实体所属的阵营或派系。
|
||||
/// </summary>
|
||||
public string affiliation;
|
||||
|
||||
/// <summary>
|
||||
/// 表示实体是否可以被选择。
|
||||
/// </summary>
|
||||
public bool canSelect = true;
|
||||
|
||||
|
||||
public string currentDimensionId;
|
||||
|
||||
public AudioSource Audio;
|
||||
|
||||
public Vector3 moveTarget;
|
||||
|
||||
|
||||
[SerializeField] private float _hitBarUIShowTime = 5;
|
||||
|
||||
private readonly List<(Func<Entity, bool>, string)> _conditionalEvents = new();
|
||||
private float _attackDetectionTime;
|
||||
private float _attackTimer;
|
||||
|
||||
// /// <summary>
|
||||
// /// 当前实体正在执行的任务。
|
||||
@@ -41,20 +69,48 @@ namespace Entity
|
||||
// public JobBase currentJob;
|
||||
|
||||
private Attributes _attribute;
|
||||
private Attributes _defAttributes;
|
||||
|
||||
private Vector3 _direction;
|
||||
private float _hitBarUIShowTimer;
|
||||
protected int _walkingTimer;
|
||||
|
||||
|
||||
private bool _warning;
|
||||
|
||||
/// <summary>
|
||||
/// 实体的属性定义,包括生命值、攻击力、防御力等。
|
||||
/// 存储不同朝向下的身体节点对象。
|
||||
/// </summary>
|
||||
protected Dictionary<EntityState, Dictionary<Orientation, GameObject>> bodyNodes = new();
|
||||
|
||||
private WeaponResource currentAttackWeapon;
|
||||
public EntityDef entityDef;
|
||||
|
||||
/// <summary>
|
||||
/// 人工智能行为树,定义实体的行为逻辑。
|
||||
/// </summary>
|
||||
public BehaviorTreeBase BehaviorTreeTree { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实体的属性定义,包括生命值、攻击力、防御力等。
|
||||
/// </summary>
|
||||
public virtual Attributes AttributesNow
|
||||
{
|
||||
get { return _attribute ??= new Attributes(baseAttributes); }
|
||||
protected set => _attribute = value;
|
||||
get { return _attribute ??= new Attributes(BaseAttributes); }
|
||||
protected set
|
||||
{
|
||||
if (_attribute != null)
|
||||
_attribute = Attributes.Min(_attribute, value);
|
||||
else
|
||||
_attribute = value;
|
||||
gameObject.SetActive(!IsDead);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public virtual Attributes baseAttributes => defAttributes;
|
||||
private Attributes _defAttributes;
|
||||
public virtual Attributes defAttributes
|
||||
public virtual Attributes BaseAttributes => DefAttributes;
|
||||
|
||||
public virtual Attributes DefAttributes
|
||||
{
|
||||
get
|
||||
{
|
||||
@@ -62,53 +118,51 @@ namespace Entity
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 _direction;
|
||||
/// <summary>
|
||||
/// 实体当前的移动方向。
|
||||
/// </summary>
|
||||
public Vector3 Direction
|
||||
{
|
||||
get => _direction;
|
||||
set
|
||||
{
|
||||
// 确保方向向量是单位向量
|
||||
_direction = Mathf.Approximately(value.sqrMagnitude, 1) ? value : value.normalized;
|
||||
Orientation ori;
|
||||
if (Mathf.Abs(Direction.y) > Mathf.Abs(Direction.x))
|
||||
|
||||
Orientation newOrientation;
|
||||
// 获取方向向量的绝对X和Y值,用于比较
|
||||
var absX = Mathf.Abs(Direction.x);
|
||||
var absY = Mathf.Abs(Direction.y);
|
||||
// 检查当前方向,并根据切换角度决定新的方向
|
||||
// 这里体现了滞后(Hysteresis)逻辑:
|
||||
// 从上下切换到左右时,45°切换(左右优先)
|
||||
if (CurrentOrientation is Orientation.Up or Orientation.Down)
|
||||
{
|
||||
ori = Direction.y > 0 ? Orientation.Up : Orientation.Down;
|
||||
// 如果X的绝对值大于Y的绝对值(与X轴夹角小于45度),则切换到左右
|
||||
if (absX > absY)
|
||||
newOrientation = Direction.x > 0 ? Orientation.Right : Orientation.Left;
|
||||
else
|
||||
// 否则(与X轴夹角大于等于45度),保持上下
|
||||
newOrientation = Direction.y > 0 ? Orientation.Up : Orientation.Down;
|
||||
}
|
||||
else
|
||||
// 修改部分二:替换原 else 分支中的角度计算,使用高效的XY分量比较
|
||||
else // 当前方向是 Orientation.Left or Orientation.Right
|
||||
{
|
||||
ori = Direction.x > 0 ? Orientation.Right : Orientation.Left;
|
||||
// 从左右切换到上下时,需要更强的垂直分量。
|
||||
// 原始逻辑要求角度 >= 50度或 <= 130度(对于'Up')
|
||||
// 这等同于 absY / absX >= tan(50度)。
|
||||
// 使用 absY >= absX * TAN_50_DEGREES 进行高效比较,并避免除零问题。
|
||||
if (absY >= absX * TAN_50_DEGREES)
|
||||
newOrientation = Direction.y > 0 ? Orientation.Up : Orientation.Down;
|
||||
else // 垂直分量不足,保持左右
|
||||
newOrientation = Direction.x > 0 ? Orientation.Right : Orientation.Left;
|
||||
}
|
||||
SetBodyTexture(CurrentState, ori);
|
||||
|
||||
SetBodyTexture(CurrentState, newOrientation);
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 attackDirection;
|
||||
|
||||
/// <summary>
|
||||
/// 实体的身体部分,用于挂载动画和图像节点。
|
||||
/// </summary>
|
||||
public GameObject body;
|
||||
|
||||
/// <summary>
|
||||
/// 实体所属的阵营或派系。
|
||||
/// </summary>
|
||||
public string affiliation;
|
||||
|
||||
/// <summary>
|
||||
/// 表示实体是否可以被选择。
|
||||
/// </summary>
|
||||
public bool canSelect = true;
|
||||
|
||||
|
||||
public string currentDimensionId;
|
||||
|
||||
public Vector2 Size => entityPrefab.outline.GetColliderSize();
|
||||
|
||||
/// <summary>
|
||||
/// 表示实体是否由玩家控制。
|
||||
/// 表示实体是否由玩家控制。
|
||||
/// </summary>
|
||||
public bool PlayerControlled
|
||||
{
|
||||
@@ -118,12 +172,10 @@ namespace Entity
|
||||
{
|
||||
BehaviorTreeTree?.Reset();
|
||||
if (Program.Instance.FocusedEntity && Program.Instance.FocusedEntity != this)
|
||||
{
|
||||
Program.Instance.FocusedEntity.PlayerControlled = false;
|
||||
}
|
||||
|
||||
Program.Instance.SetFocusedEntity(this);
|
||||
gameObject.tag="Player";
|
||||
gameObject.tag = "Player";
|
||||
}
|
||||
else if (Program.Instance.FocusedEntity == this)
|
||||
{
|
||||
@@ -138,94 +190,108 @@ namespace Entity
|
||||
public bool IsWalking => _walkingTimer > 0;
|
||||
|
||||
/// <summary>
|
||||
/// 获取实体当前位置。
|
||||
/// 获取实体当前位置。
|
||||
/// </summary>
|
||||
public Vector3 Position => transform.position;
|
||||
|
||||
/// <summary>
|
||||
/// 表示实体是否已经死亡(生命值小于等于零)。
|
||||
/// 表示实体是否已经死亡(生命值小于等于零)。
|
||||
/// </summary>
|
||||
public bool IsDead => AttributesNow.health <= 0;
|
||||
|
||||
public bool IsShowingHealthBarUI => _hitBarUIShowTimer > 0;
|
||||
public bool IsAttacking => _attackTimer > 0;
|
||||
private float _attackTimer;
|
||||
private float _attackDetectionTime;
|
||||
private WeaponResource currentAttackWeapon;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 当实体死亡时触发的事件。
|
||||
/// 只在实体首次进入死亡状态时触发一次。
|
||||
/// </summary>
|
||||
public event Action<Entity> OnEntityDied;
|
||||
|
||||
private bool _warning;
|
||||
|
||||
/// <summary>
|
||||
/// 存储不同朝向下的身体节点对象。
|
||||
/// </summary>
|
||||
protected Dictionary<EntityState, Dictionary<Orientation, GameObject>> bodyNodes = new();
|
||||
public float _attackingAnimationEndTime;
|
||||
|
||||
/// <summary>
|
||||
/// 当前实体的朝向。
|
||||
/// 当前实体的朝向。
|
||||
/// </summary>
|
||||
public Orientation CurrentOrientation { get; private set; } = Orientation.Up;
|
||||
|
||||
/// <summary>
|
||||
/// 当前实体的状态
|
||||
/// 当前实体的状态
|
||||
/// </summary>
|
||||
public EntityState CurrentState { get; private set; } = EntityState.Idle;
|
||||
|
||||
public AudioSource Audio;
|
||||
|
||||
public Vector3 moveTarget;
|
||||
private const float TargetReachThresholdSquared = 0.001f;
|
||||
public virtual bool OnTargetPoint
|
||||
{
|
||||
get
|
||||
{
|
||||
var offset = Position - moveTarget;
|
||||
var distanceSquared = offset.sqrMagnitude;
|
||||
var distanceSquared = offset.sqrMagnitude;
|
||||
return distanceSquared <= TargetReachThresholdSquared;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
[SerializeField] private float _hitBarUIShowTime = 5;
|
||||
private float _hitBarUIShowTimer;
|
||||
protected int _walkingTimer;
|
||||
|
||||
private List<(Func<Entity, bool>,string)> _conditionalEvents=new();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化实体的基本属性和行为树。
|
||||
/// 更新实体的逻辑,包括玩家控制和自动行为。
|
||||
/// </summary>
|
||||
public virtual void Tick()
|
||||
{
|
||||
//行走动画切换
|
||||
if (_walkingTimer > 0)
|
||||
{
|
||||
_walkingTimer -= 1;
|
||||
if (_walkingTimer <= 0) SetBodyTexture(EntityState.Idle, CurrentOrientation);
|
||||
}
|
||||
|
||||
//行为控制
|
||||
if (PlayerControlled)
|
||||
UpdatePlayerControls();
|
||||
else
|
||||
AutoBehave();
|
||||
|
||||
//血条显示
|
||||
if (IsShowingHealthBarUI)
|
||||
{
|
||||
_hitBarUIShowTimer -= Time.deltaTime;
|
||||
if (_hitBarUIShowTimer <= 0) HideHealthBar();
|
||||
}
|
||||
|
||||
//攻击控制
|
||||
if (_attackTimer > 0)
|
||||
{
|
||||
_attackTimer -= Time.deltaTime;
|
||||
if (currentAttackWeapon != null && _attackTimer <= _attackDetectionTime)
|
||||
{
|
||||
if (currentAttackWeapon.Type == WeaponType.Melee)
|
||||
ExecuteMeleeAttack(currentAttackWeapon);
|
||||
else
|
||||
ExecuteRangedAttack(currentAttackWeapon);
|
||||
|
||||
currentAttackWeapon = null;
|
||||
}
|
||||
|
||||
if (_attackTimer <= _attackingAnimationEndTime)
|
||||
SetBodyTexture(EntityState.Idle, CurrentOrientation);
|
||||
}
|
||||
|
||||
//条件事件控制
|
||||
foreach (var conditionalEvent in _conditionalEvents)
|
||||
if (conditionalEvent.Item1(this))
|
||||
EventManager.Instance.Action(conditionalEvent.Item2, this);
|
||||
}
|
||||
|
||||
|
||||
public event Action<Entity> OnEntityDiedEvent;
|
||||
public event Action<Entity> OnAttackEvent;
|
||||
public event Action<Entity> OnHitEvent;
|
||||
public event Action<Entity, Entity> OnDealHitEvent;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化实体的基本属性和行为树。
|
||||
/// </summary>
|
||||
/// <param name="entityDefine">实体的定义数据。</param>
|
||||
public virtual void Init(EntityDef entityDefine)
|
||||
{
|
||||
AttributesNow = new Attributes(entityDefine.attributes);
|
||||
entityDef = entityDefine;
|
||||
|
||||
AttributesNow = BaseAttributes;
|
||||
|
||||
affiliation = entityDefine.affiliation?.defName;
|
||||
InitBody(entityDefine.drawingOrder);
|
||||
entityDef = entityDefine;
|
||||
|
||||
|
||||
BehaviorTreeTree = BehaviorTreeUtils.ConvertToAIBase(entityDefine.behaviorTree);
|
||||
BehaviorTreeTree?.Init(entityDefine.behaviorTree, this);
|
||||
HideHealthBar();
|
||||
@@ -236,28 +302,24 @@ namespace Entity
|
||||
protected virtual void InitWeaponAnimator()
|
||||
{
|
||||
if (weaponAnimator && weaponAnimator.transform.childCount > 0)
|
||||
{
|
||||
foreach (Transform child in weaponAnimator.transform)
|
||||
{
|
||||
foreach (Transform child in weaponAnimator.transform)
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
var currentWeapon = GetCurrentWeapon();
|
||||
if (currentWeapon?.AttackAnimation == null)
|
||||
if (currentWeapon?.AttackAnimation == null || !currentWeapon.SelfWeaponDef.showInHand)
|
||||
return;
|
||||
weaponItem=GameObjectCreate.SpriteAnimator(currentWeapon.Icon.ToArray(), weaponAnimator.transform);
|
||||
weaponItem = AnimationUtils.SpriteAnimator(currentWeapon.Icon.ToArray(), weaponAnimator.transform);
|
||||
weaponItem.SetFPS(currentWeapon.FPS);
|
||||
weaponItem.gameObject.SetActive(true);
|
||||
|
||||
weaponAttackAnimation = GameObjectCreate.SpriteAnimator(currentWeapon.AttackAnimation.ToArray(),
|
||||
|
||||
weaponAttackAnimation = AnimationUtils.SpriteAnimator(currentWeapon.AttackAnimation.ToArray(),
|
||||
weaponAnimator.transform);
|
||||
weaponAttackAnimation.SetFPS(currentWeapon.FPS);
|
||||
weaponAttackAnimation.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化实体的身体部分,包括不同朝向下的绘图节点。
|
||||
/// 初始化实体的身体部分,包括不同朝向下的绘图节点。
|
||||
/// </summary>
|
||||
/// <param name="drawingOrder">绘制顺序定义。</param>
|
||||
protected virtual void InitBody(DrawingOrderDef drawingOrder)
|
||||
@@ -265,13 +327,10 @@ namespace Entity
|
||||
// 预缓存枚举值(避免每次循环重复调用 Enum.GetValues)
|
||||
var states = Enum.GetValues(typeof(EntityState)).Cast<EntityState>().ToList();
|
||||
states.Remove(EntityState.Death);
|
||||
|
||||
|
||||
var orientations = Enum.GetValues(typeof(Orientation)).Cast<Orientation>().ToArray();
|
||||
// 预初始化字典结构(减少内层循环的字典检查)
|
||||
foreach (var state in states)
|
||||
{
|
||||
bodyNodes.TryAdd(state, new Dictionary<Orientation, GameObject>());
|
||||
}
|
||||
foreach (var state in states) bodyNodes.TryAdd(state, new Dictionary<Orientation, GameObject>());
|
||||
|
||||
// 主初始化逻辑
|
||||
foreach (var state in states)
|
||||
@@ -286,7 +345,7 @@ namespace Entity
|
||||
{
|
||||
if (PackagesImageManager.Instance.defaultSprite)
|
||||
{
|
||||
targetObj = Instantiate(GameObjectCreate.ImagePrefab.gameObject, body.transform);
|
||||
targetObj = Instantiate(AnimationUtils.ImagePrefab.gameObject, body.transform);
|
||||
targetObj.name = $"{state}_{orientation}_Default";
|
||||
targetObj.transform.localPosition = Vector3.zero;
|
||||
var imagePrefabCom = targetObj.GetComponent<ImagePrefab>();
|
||||
@@ -312,13 +371,9 @@ namespace Entity
|
||||
{
|
||||
// 处理有效节点定义
|
||||
if (original.HasValue && stateBodyNodes.TryGetValue(original.Value, out var reusedObj))
|
||||
{
|
||||
targetObj = reusedObj; // 复用已有对象
|
||||
}
|
||||
else
|
||||
{
|
||||
targetObj = GameObjectCreate.InitBodyPart(nodeDef, body); // 创建新对象
|
||||
}
|
||||
targetObj = AnimationUtils.InitBodyPart(nodeDef, body); // 创建新对象
|
||||
}
|
||||
|
||||
if (targetObj)
|
||||
@@ -336,100 +391,28 @@ namespace Entity
|
||||
|
||||
// 批量隐藏所有节点(使用字典值集合直接操作)
|
||||
foreach (var nodeDict in bodyNodes.Values)
|
||||
{
|
||||
foreach (var obj in nodeDict.Values)
|
||||
{
|
||||
obj.SetActive(false);
|
||||
}
|
||||
}
|
||||
foreach (var obj in nodeDict.Values)
|
||||
obj.SetActive(false);
|
||||
|
||||
SetBodyTexture(EntityState.Idle, Orientation.Down); // 激活默认朝向
|
||||
}
|
||||
|
||||
|
||||
protected void InitConditionalEvents(ConditionalEvent[] conditionalEvents)
|
||||
{
|
||||
if(conditionalEvents==null)return;
|
||||
if (conditionalEvents == null) return;
|
||||
_conditionalEvents.Clear();
|
||||
foreach (var conditionalEvent in conditionalEvents)
|
||||
{
|
||||
var condition = ConditionDelegateFactory.CreateConditionDelegate(conditionalEvent.parameter,
|
||||
typeof(Entity), typeof(ConditionFunctions));
|
||||
if(condition==null)continue;
|
||||
if (condition == null) continue;
|
||||
_conditionalEvents.Add((condition, conditionalEvent.defName));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 更新实体的逻辑,包括玩家控制和自动行为。
|
||||
/// </summary>
|
||||
public virtual void Tick()
|
||||
{
|
||||
//行走动画切换
|
||||
if (_walkingTimer > 0)
|
||||
{
|
||||
_walkingTimer -= 1;
|
||||
if (_walkingTimer <= 0)
|
||||
{
|
||||
SetBodyTexture(EntityState.Idle, CurrentOrientation);
|
||||
}
|
||||
}
|
||||
//行为控制
|
||||
if (PlayerControlled)
|
||||
{
|
||||
UpdatePlayerControls();
|
||||
}
|
||||
else
|
||||
{
|
||||
AutoBehave();
|
||||
}
|
||||
//血条显示
|
||||
if (IsShowingHealthBarUI)
|
||||
{
|
||||
_hitBarUIShowTimer -= Time.deltaTime;
|
||||
if (_hitBarUIShowTimer <= 0)
|
||||
{
|
||||
HideHealthBar();
|
||||
}
|
||||
}
|
||||
//攻击控制
|
||||
if (_attackTimer > 0)
|
||||
{
|
||||
_attackTimer -= Time.deltaTime;
|
||||
if (currentAttackWeapon != null && _attackTimer <= _attackDetectionTime)
|
||||
{
|
||||
if (currentAttackWeapon.Type == WeaponType.Melee)
|
||||
{
|
||||
ExecuteMeleeAttack(currentAttackWeapon);
|
||||
}
|
||||
else
|
||||
{
|
||||
ExecuteRangedAttack(currentAttackWeapon);
|
||||
}
|
||||
|
||||
currentAttackWeapon = null;
|
||||
}
|
||||
|
||||
if (_attackTimer <= 0)
|
||||
{
|
||||
|
||||
SetBodyTexture(EntityState.Idle, CurrentOrientation);
|
||||
}
|
||||
}
|
||||
//条件事件控制
|
||||
foreach (var conditionalEvent in _conditionalEvents)
|
||||
{
|
||||
if (conditionalEvent.Item1(this))
|
||||
{
|
||||
EventManager.Instance.Action(conditionalEvent.Item2, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 尝试攻击目标实体。
|
||||
/// 尝试攻击目标实体。
|
||||
/// </summary>
|
||||
public virtual bool TryAttack() // 使用override允许子类重写
|
||||
{
|
||||
@@ -437,11 +420,8 @@ namespace Entity
|
||||
|
||||
// 尝试获取当前武器
|
||||
var currentWeapon = GetCurrentWeapon();
|
||||
|
||||
if (currentWeapon == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (currentWeapon == null) return false;
|
||||
|
||||
StartAttack(currentWeapon);
|
||||
return true;
|
||||
@@ -450,15 +430,15 @@ namespace Entity
|
||||
private void StartAttack(WeaponResource weaponResource)
|
||||
{
|
||||
_attackTimer = weaponResource.AttackCooldown;
|
||||
_attackDetectionTime =Mathf.Max(0,weaponResource.AttackCooldown- weaponResource.AttackDetectionTime);
|
||||
_attackDetectionTime = Mathf.Max(0, weaponResource.AttackCooldown - weaponResource.AttackDetectionTime);
|
||||
_attackingAnimationEndTime = MathF.Max(0, weaponResource.AttackCooldown - weaponResource.AttackAnimationTime);
|
||||
currentAttackWeapon = weaponResource;
|
||||
|
||||
|
||||
if (weaponResource.AttackAnimationTime > 0)
|
||||
{
|
||||
TemporaryAnimationManager.Instance.GenerateTemporaryAnimation(
|
||||
weaponResource.AttackAnimation.ToArray(), Position,transform, weaponResource.AttackAnimationTime,
|
||||
weaponResource.AttackAnimation.ToArray(), Position, transform, weaponResource.AttackAnimationTime,
|
||||
weaponResource.FPS);
|
||||
}
|
||||
|
||||
if (weaponResource.UseEntityAttackAnimation)
|
||||
{
|
||||
SetBodyTexture(
|
||||
@@ -468,11 +448,10 @@ namespace Entity
|
||||
else
|
||||
{
|
||||
HideCurrentBodyTexture();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void SetBodyTexture(EntityState state, Orientation orientation)
|
||||
{
|
||||
if (state == CurrentState && orientation == CurrentOrientation)
|
||||
@@ -481,12 +460,8 @@ namespace Entity
|
||||
if (IsAttacking && currentAttackWeapon is { UseEntityAttackAnimation: false })
|
||||
return;
|
||||
if (bodyNodes.TryGetValue(state, out var showStateNode))
|
||||
{
|
||||
if (showStateNode.TryGetValue(orientation, out var showNode))
|
||||
{
|
||||
showNode.SetActive(true);
|
||||
}
|
||||
}
|
||||
|
||||
CurrentState = state;
|
||||
CurrentOrientation = orientation;
|
||||
@@ -495,21 +470,15 @@ namespace Entity
|
||||
public void HideCurrentBodyTexture()
|
||||
{
|
||||
if (!bodyNodes.TryGetValue(CurrentState, out var stateNode)) return;
|
||||
if (stateNode.TryGetValue(CurrentOrientation, out var node))
|
||||
{
|
||||
node.SetActive(false);
|
||||
}
|
||||
if (stateNode.TryGetValue(CurrentOrientation, out var node)) node.SetActive(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据方向尝试移动实体,并避免超调目标点。
|
||||
/// 根据方向尝试移动实体,并避免超调目标点。
|
||||
/// </summary>
|
||||
public virtual void TryMove()
|
||||
{
|
||||
if (OnTargetPoint)
|
||||
{
|
||||
return; // 已到达目标点,无需进一步移动。
|
||||
}
|
||||
if (OnTargetPoint || AttributesNow.moveSpeed <= 0) return; // 已到达目标点,无需进一步移动。
|
||||
|
||||
var currentPosition = Position; // 假设 Position 是获取当前位置的属性
|
||||
var targetDestination = moveTarget; // 假设 moveTarget 是目标位置字段
|
||||
@@ -539,9 +508,9 @@ namespace Entity
|
||||
newPosition = currentPosition + Direction * maxMoveDistance;
|
||||
willReachTargetThisFrame = false;
|
||||
}
|
||||
|
||||
|
||||
transform.position = newPosition;
|
||||
|
||||
|
||||
// 如果已到达目标,则不设置行走动画和重置计时器
|
||||
if (willReachTargetThisFrame) return;
|
||||
SetBodyTexture(EntityState.Walking, CurrentOrientation); // 假设 SetBodyTexture 是设置动画的方法
|
||||
@@ -554,39 +523,68 @@ namespace Entity
|
||||
SetBodyTexture(EntityState.Walking, CurrentOrientation);
|
||||
_walkingTimer = 2;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理实体受到攻击的逻辑。
|
||||
/// 处理实体受到攻击的逻辑。
|
||||
/// </summary>
|
||||
/// <param name="from">攻击来源实体。</param>
|
||||
public virtual void OnHit(Entity from)
|
||||
{
|
||||
// MODIFIED: 收到攻击时触发回调
|
||||
if (IsDead) // 如果已经死亡,则不再处理伤害 nor 触发事件
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var hit = from.AttributesNow.attack - AttributesNow.defense;
|
||||
if (hit < 0)
|
||||
hit = from.AttributesNow.attack / 100;
|
||||
// 确保伤害不为负,最小为0
|
||||
hit += (int)from.AttributesNow.totalDamageTakenAbsoluteOffset;
|
||||
hit = (int)(hit * (1 + from.AttributesNow.totalDamageTakenPercentOffset));
|
||||
OnHit(hit);
|
||||
}
|
||||
|
||||
public virtual void OnHit(int hit, bool showAnimation = true)
|
||||
{
|
||||
hit += (int)AttributesNow.totalDamageTakenAbsoluteOffset;
|
||||
hit = (int)(hit * (1 + AttributesNow.totalDamageTakenPercentOffset));
|
||||
hit = Mathf.Max(0, hit);
|
||||
AttributesNow.health -= hit;
|
||||
var wasFatal = IsDead; // 检查这次攻击是否导致实体死亡
|
||||
|
||||
BehaviorTreeTree?.Reset();
|
||||
if (wasFatal)
|
||||
{
|
||||
OnEntityDied?.Invoke(this);
|
||||
}
|
||||
OnHitCallback();
|
||||
if (IsDead)
|
||||
OnDiedCallback();
|
||||
|
||||
if (Setting.Instance.CurrentSettings.showHealthBarByHit)
|
||||
{
|
||||
ShowHealthBar();
|
||||
}
|
||||
if (Setting.Instance.CurrentSettings.showHealthBarByHit && showAnimation) ShowHealthBar();
|
||||
|
||||
if (Setting.Instance.CurrentSettings.showHitNumber)
|
||||
if (Setting.Instance.CurrentSettings.showHitNumber && showAnimation)
|
||||
{
|
||||
TemporaryAnimationManager.Instance.GenerateTemporaryAnimation(hit.ToString(), Position);
|
||||
var textObj = TemporaryAnimationManager.Instance.GenerateTemporaryAnimation(hit.ToString(), Position,
|
||||
0.5f, 1, null,
|
||||
(f => MathF.Log(f * 100 + 1)));
|
||||
textObj.text.fontSize = 5;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnHitCallback()
|
||||
{
|
||||
OnHitEvent?.Invoke(this);
|
||||
if (entityDef?.eventDef?.onHitEvents != null)
|
||||
{
|
||||
foreach (var onHitEvent in entityDef.eventDef.onHitEvents)
|
||||
{
|
||||
EventManager.Instance.Action(onHitEvent, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDiedCallback()
|
||||
{
|
||||
OnEntityDiedEvent?.Invoke(this);
|
||||
if (entityDef?.eventDef?.onDeathEvents != null)
|
||||
{
|
||||
foreach (var eventDef in entityDef.eventDef.onDeathEvents)
|
||||
{
|
||||
EventManager.Instance.Action(eventDef, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -607,93 +605,80 @@ namespace Entity
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 杀死实体,设置生命值为零。
|
||||
/// 杀死实体,设置生命值为零。
|
||||
/// </summary>
|
||||
public virtual void Kill()
|
||||
{
|
||||
if (IsDead)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (IsDead) return;
|
||||
|
||||
AttributesNow.health = 0; // 直接设置生命值为0
|
||||
BehaviorTreeTree?.Reset();
|
||||
// 触发 OnEntityDied 事件
|
||||
OnEntityDied?.Invoke(this);
|
||||
ShowHealthBar();
|
||||
OnDiedCallback();
|
||||
}
|
||||
|
||||
public void OnDealHit(Entity target)
|
||||
{
|
||||
OnDealHitEvent?.Invoke(this, target);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置实体的目标位置。
|
||||
/// 设置实体的目标位置。
|
||||
/// </summary>
|
||||
/// <param name="pos">目标位置。</param>
|
||||
public virtual void SetTarget(Vector3 pos)
|
||||
public virtual bool SetTarget(Vector3 pos)
|
||||
{
|
||||
Direction = (pos - transform.position).normalized;
|
||||
moveTarget = pos;
|
||||
if (!PlayerControlled)
|
||||
{
|
||||
attackDirection=Direction;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自动行为逻辑,根据行为树执行任务。
|
||||
/// 自动行为逻辑,根据行为树执行任务。
|
||||
/// </summary>
|
||||
protected virtual void AutoBehave()
|
||||
{
|
||||
var result=BehaviorTreeTree?.Tick();
|
||||
var result = BehaviorTreeTree?.Tick();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 更新玩家控制的逻辑,处理输入和移动。
|
||||
/// 更新玩家控制的逻辑,处理输入和移动。
|
||||
/// </summary>
|
||||
protected virtual void UpdatePlayerControls()
|
||||
{
|
||||
if (Input.GetMouseButton(0))
|
||||
if (EntityManager.Instance.ExistsHostileInSightRange(currentDimensionId, entityPrefab,
|
||||
AttributesNow.attackRange))
|
||||
{
|
||||
var mousePos = MousePosition.GetWorldPosition();
|
||||
attackDirection = new Vector3(mousePos.x, mousePos.y) - Position;
|
||||
if (weaponItem)
|
||||
attackDirection =
|
||||
EntityManager.Instance
|
||||
.FindNearestEntityByRelation(currentDimensionId, entityPrefab, Relation.Hostile).Position -
|
||||
Position;
|
||||
if (weaponItem && weaponItem.gameObject.activeInHierarchy)
|
||||
{
|
||||
var angle= RotateTool.RotateTransformToDirection(weaponItem.transform, attackDirection);
|
||||
weaponItem.Flip(false,angle is > 90 or <= -90);
|
||||
var angle = RotateTool.RotateTransformToDirection(weaponItem.transform, attackDirection);
|
||||
weaponItem.Flip(false, angle is > 90 or <= -90);
|
||||
}
|
||||
|
||||
TryAttack();
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.V))
|
||||
{
|
||||
weaponItem.gameObject.SetActive(!weaponItem.gameObject.activeSelf);
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.V)) weaponItem.gameObject.SetActive(!weaponItem.gameObject.activeSelf);
|
||||
|
||||
// 获取当前键盘输入状态(2D 移动,只使用 X 和 Y 轴)
|
||||
var inputDirection = Vector2.zero;
|
||||
|
||||
// 检测 WASD 或方向键输入
|
||||
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
|
||||
{
|
||||
inputDirection += Vector2.up; // 向上移动(Y 轴正方向)
|
||||
}
|
||||
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) inputDirection += Vector2.up; // 向上移动(Y 轴正方向)
|
||||
|
||||
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
|
||||
{
|
||||
inputDirection += Vector2.down; // 向下移动(Y 轴负方向)
|
||||
}
|
||||
|
||||
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
|
||||
{
|
||||
inputDirection += Vector2.left; // 向左移动(X 轴负方向)
|
||||
}
|
||||
|
||||
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
|
||||
{
|
||||
inputDirection += Vector2.right; // 向右移动(X 轴正方向)
|
||||
}
|
||||
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
TryAttack();
|
||||
}
|
||||
|
||||
// 如果有输入方向,则设置目标位置并尝试移动
|
||||
if (inputDirection == Vector2.zero) return;
|
||||
// 归一化方向向量,确保对角线移动速度一致
|
||||
@@ -701,37 +686,24 @@ namespace Entity
|
||||
|
||||
Direction = new Vector3(inputDirection.x, inputDirection.y, 0);
|
||||
Move();
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void ExecuteMeleeAttack(WeaponResource weapon)
|
||||
{
|
||||
if (weapon.Attributes == null)
|
||||
{
|
||||
Debug.LogWarning($"武器 {weapon.DefName} 没有定义Attributes,无法执行近战攻击。");
|
||||
return;
|
||||
}
|
||||
|
||||
protected virtual void ExecuteMeleeAttack(WeaponResource weapon)
|
||||
{
|
||||
var attackRange = weapon.Attributes.attackRange;
|
||||
var attackTargetCount = weapon.Attributes.attackTargetCount;
|
||||
|
||||
var hits = Physics2D.OverlapCircleAll(
|
||||
transform.position,
|
||||
attackRange,
|
||||
LayerMask.GetMask("Entity"));
|
||||
var hits = EntityManager.FindEntity<LivingEntity>(Position, attackRange);
|
||||
|
||||
foreach (var hit in hits)
|
||||
{
|
||||
if (attackTargetCount <= 0) break; // 已达到最大攻击目标数
|
||||
if (hit.gameObject == gameObject) continue; // 不攻击自己
|
||||
|
||||
var entity = hit.GetComponent<Entity>();
|
||||
if (entity != null && entity.affiliation != affiliation) // 确保是敌对实体
|
||||
{
|
||||
entity.OnHit(this); // 攻击时将自身作为攻击来源
|
||||
attackTargetCount--;
|
||||
}
|
||||
var relation = AffiliationManager.Instance.GetRelation(affiliation, hit.affiliation);
|
||||
if (!Setting.Instance.CurrentSettings.friendlyFire && relation != Relation.Hostile) continue;
|
||||
hit.OnHit(this);
|
||||
attackTargetCount--;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -744,9 +716,7 @@ namespace Entity
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取子弹方向。这里使用实体当前的移动方向作为子弹发射方向
|
||||
// 更复杂的逻辑可能根据鼠标位置、目标位置等确定
|
||||
var bulletDirection = attackDirection; // 实体当前的朝向
|
||||
var bulletDirection = attackDirection;
|
||||
|
||||
// 如果没有明确的方向,给一个默认值以防万一
|
||||
if (bulletDirection == Vector3.zero) bulletDirection = Vector3.down;
|
||||
@@ -754,19 +724,15 @@ namespace Entity
|
||||
|
||||
// 假设 EntityManage.Instance.GenerateBulletEntity 方法存在
|
||||
// (需要一个 EntityManage 单例来实现子弹生成)
|
||||
if (EntityManager.Instance != null && Program.Instance != null)
|
||||
{
|
||||
if (EntityManager.Instance && Program.Instance != null)
|
||||
EntityManager.Instance.GenerateBulletEntity(
|
||||
Program.Instance.FocusedDimensionId,
|
||||
weapon.Bullet,
|
||||
transform.position, // 子弹的生成位置
|
||||
bulletDirection, // 子弹的初始方向
|
||||
this); // 子弹的发射者
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("EntityManage.Instance 或 Program.Instance 为空,无法生成子弹。请确保它们已正确初始化。");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -779,10 +745,12 @@ namespace Entity
|
||||
{
|
||||
var eventDefs = entityDef?.eventDef?.GetEventsByType(type);
|
||||
if (eventDefs == null || eventDefs.Length == 0) return;
|
||||
foreach (var eventDef in eventDefs)
|
||||
{
|
||||
EventManager.Instance.Action(eventDef,this);
|
||||
}
|
||||
foreach (var eventDef in eventDefs) EventManager.Instance.Action(eventDef, this);
|
||||
}
|
||||
|
||||
public void OffsetAttribute(AttributesOffsetDef offsetDef)
|
||||
{
|
||||
AttributesNow = AttributesNow.GetModifiedAttributes(offsetDef);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user