2025-07-21 13:58:58 +08:00
|
|
|
|
using AI;
|
|
|
|
|
|
using Base;
|
|
|
|
|
|
using Data;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
using Item;
|
|
|
|
|
|
using Managers;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
using Prefab;
|
2025-08-28 16:20:24 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2025-09-28 15:02:57 +08:00
|
|
|
|
using Parsing;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
using UnityEngine;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
using Utils;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
|
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
|
|
|
|
public ProgressBarPrefab healthBarPrefab;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
public EntityPrefab entityPrefab;
|
|
|
|
|
|
public EntityDef entityDef;
|
2025-09-03 19:59:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 手上拿着显示的贴图
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public GameObject weaponAnimator;
|
|
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
public SpriteAnimator weaponItem;
|
|
|
|
|
|
public SpriteAnimator weaponAttackAnimation;
|
2025-09-28 15:02:57 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 人工智能行为树,定义实体的行为逻辑。
|
|
|
|
|
|
/// </summary>
|
2025-10-03 00:31:34 +08:00
|
|
|
|
public BehaviorTreeBase BehaviorTreeTree { get; private set; }
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-10-03 00:31:34 +08:00
|
|
|
|
// /// <summary>
|
|
|
|
|
|
// /// 当前实体正在执行的任务。
|
|
|
|
|
|
// /// </summary>
|
|
|
|
|
|
// public JobBase currentJob;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
private Attributes _attribute;
|
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 实体的属性定义,包括生命值、攻击力、防御力等。
|
|
|
|
|
|
/// </summary>
|
2025-10-03 00:31:34 +08:00
|
|
|
|
public virtual Attributes AttributesNow
|
2025-09-19 08:26:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
get { return _attribute ??= new Attributes(baseAttributes); }
|
|
|
|
|
|
protected set => _attribute = value;
|
|
|
|
|
|
}
|
2025-09-03 19:59:22 +08:00
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
|
|
|
|
|
|
public virtual Attributes baseAttributes => defAttributes;
|
|
|
|
|
|
private Attributes _defAttributes;
|
|
|
|
|
|
public virtual Attributes defAttributes
|
2025-09-03 19:59:22 +08:00
|
|
|
|
{
|
2025-09-19 08:26:54 +08:00
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return _defAttributes ??= entityDef == null ? new Attributes() : new Attributes(entityDef.attributes);
|
|
|
|
|
|
}
|
2025-09-03 19:59:22 +08:00
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-10-03 00:31:34 +08:00
|
|
|
|
private Vector3 _direction;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 实体当前的移动方向。
|
|
|
|
|
|
/// </summary>
|
2025-10-03 00:31:34 +08:00
|
|
|
|
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))
|
|
|
|
|
|
{
|
|
|
|
|
|
ori = Direction.y > 0 ? Orientation.Up : Orientation.Down;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
ori = Direction.x > 0 ? Orientation.Right : Orientation.Left;
|
|
|
|
|
|
}
|
|
|
|
|
|
SetBodyTexture(CurrentState, ori);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
public Vector3 attackDirection;
|
|
|
|
|
|
|
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-09-19 08:26:54 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
public string currentDimensionId;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
2025-10-03 00:31:34 +08:00
|
|
|
|
public Vector2 Size => entityPrefab.outline.GetColliderSize();
|
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
|
|
|
|
{
|
2025-10-03 00:31:34 +08:00
|
|
|
|
BehaviorTreeTree?.Reset();
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (Program.Instance.FocusedEntity && Program.Instance.FocusedEntity != this)
|
|
|
|
|
|
{
|
|
|
|
|
|
Program.Instance.FocusedEntity.PlayerControlled = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Program.Instance.SetFocusedEntity(this);
|
2025-09-28 15:02:57 +08:00
|
|
|
|
gameObject.tag="Player";
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (Program.Instance.FocusedEntity == this)
|
|
|
|
|
|
{
|
|
|
|
|
|
Program.Instance.SetFocusedEntity(null);
|
2025-09-28 15:02:57 +08:00
|
|
|
|
gameObject.tag = "Untagged";
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
get => Program.Instance.FocusedEntity == this;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
public bool IsWalking => _walkingTimer > 0;
|
|
|
|
|
|
|
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-10-03 00:31:34 +08:00
|
|
|
|
public bool IsDead => AttributesNow.health <= 0;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
public bool IsShowingHealthBarUI => _hitBarUIShowTimer > 0;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
public bool IsAttacking => _attackTimer > 0;
|
2025-09-28 15:02:57 +08:00
|
|
|
|
private float _attackTimer;
|
|
|
|
|
|
private float _attackDetectionTime;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
private WeaponResource currentAttackWeapon;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当实体死亡时触发的事件。
|
|
|
|
|
|
/// 只在实体首次进入死亡状态时触发一次。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public event Action<Entity> OnEntityDied;
|
|
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
private bool _warning;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 存储不同朝向下的身体节点对象。
|
|
|
|
|
|
/// </summary>
|
2025-08-27 19:56:49 +08:00
|
|
|
|
protected 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-10-03 00:31:34 +08:00
|
|
|
|
public Orientation CurrentOrientation { get; private set; } = Orientation.Up;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// 当前实体的状态
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// </summary>
|
2025-09-28 15:02:57 +08:00
|
|
|
|
public EntityState CurrentState { get; private set; } = EntityState.Idle;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
public AudioSource Audio;
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
public Vector3 moveTarget;
|
|
|
|
|
|
private const float TargetReachThresholdSquared = 0.001f;
|
|
|
|
|
|
public virtual bool OnTargetPoint
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
var offset = Position - moveTarget;
|
|
|
|
|
|
var distanceSquared = offset.sqrMagnitude;
|
|
|
|
|
|
return distanceSquared <= TargetReachThresholdSquared;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-28 15:02:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
[SerializeField] private float _hitBarUIShowTime = 5;
|
2025-09-28 15:02:57 +08:00
|
|
|
|
private float _hitBarUIShowTimer;
|
2025-10-03 00:31:34 +08:00
|
|
|
|
protected int _walkingTimer;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
private List<(Func<Entity, bool>,string)> _conditionalEvents=new();
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化实体的基本属性和行为树。
|
|
|
|
|
|
/// </summary>
|
2025-10-03 00:31:34 +08:00
|
|
|
|
/// <param name="entityDefine">实体的定义数据。</param>
|
|
|
|
|
|
public virtual void Init(EntityDef entityDefine)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-10-03 00:31:34 +08:00
|
|
|
|
AttributesNow = new Attributes(entityDefine.attributes);
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
2025-10-03 00:31:34 +08:00
|
|
|
|
affiliation = entityDefine.affiliation?.defName;
|
|
|
|
|
|
InitBody(entityDefine.drawingOrder);
|
|
|
|
|
|
entityDef = entityDefine;
|
|
|
|
|
|
|
|
|
|
|
|
BehaviorTreeTree = BehaviorTreeUtils.ConvertToAIBase(entityDefine.behaviorTree);
|
|
|
|
|
|
BehaviorTreeTree?.Init(entityDefine.behaviorTree, this);
|
2025-08-19 20:22:10 +08:00
|
|
|
|
HideHealthBar();
|
2025-09-03 19:59:22 +08:00
|
|
|
|
InitWeaponAnimator();
|
2025-10-03 00:31:34 +08:00
|
|
|
|
InitConditionalEvents(entityDefine.eventDef?.GetAllConditionalEvents());
|
2025-09-03 19:59:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void InitWeaponAnimator()
|
|
|
|
|
|
{
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (weaponAnimator && weaponAnimator.transform.childCount > 0)
|
2025-09-03 19:59:22 +08:00
|
|
|
|
{
|
2025-09-19 08:26:54 +08:00
|
|
|
|
foreach (Transform child in weaponAnimator.transform)
|
|
|
|
|
|
{
|
|
|
|
|
|
Destroy(child.gameObject);
|
|
|
|
|
|
}
|
2025-09-03 19:59:22 +08:00
|
|
|
|
}
|
2025-09-19 08:26:54 +08:00
|
|
|
|
|
|
|
|
|
|
var currentWeapon = GetCurrentWeapon();
|
|
|
|
|
|
if (currentWeapon?.AttackAnimation == null)
|
2025-09-03 19:59:22 +08:00
|
|
|
|
return;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
weaponItem=GameObjectCreate.SpriteAnimator(currentWeapon.Icon.ToArray(), weaponAnimator.transform);
|
|
|
|
|
|
weaponItem.SetFPS(currentWeapon.FPS);
|
|
|
|
|
|
weaponItem.gameObject.SetActive(true);
|
2025-09-03 19:59:22 +08:00
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
weaponAttackAnimation = GameObjectCreate.SpriteAnimator(currentWeapon.AttackAnimation.ToArray(),
|
|
|
|
|
|
weaponAnimator.transform);
|
|
|
|
|
|
weaponAttackAnimation.SetFPS(currentWeapon.FPS);
|
|
|
|
|
|
weaponAttackAnimation.gameObject.SetActive(false);
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化实体的身体部分,包括不同朝向下的绘图节点。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="drawingOrder">绘制顺序定义。</param>
|
2025-09-03 19:59:22 +08:00
|
|
|
|
protected virtual void InitBody(DrawingOrderDef drawingOrder)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
// 预缓存枚举值(避免每次循环重复调用 Enum.GetValues)
|
2025-09-28 15:02:57 +08:00
|
|
|
|
var states = Enum.GetValues(typeof(EntityState)).Cast<EntityState>().ToList();
|
|
|
|
|
|
states.Remove(EntityState.Death);
|
|
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
var orientations = Enum.GetValues(typeof(Orientation)).Cast<Orientation>().ToArray();
|
|
|
|
|
|
// 预初始化字典结构(减少内层循环的字典检查)
|
|
|
|
|
|
foreach (var state in states)
|
|
|
|
|
|
{
|
|
|
|
|
|
bodyNodes.TryAdd(state, new Dictionary<Orientation, GameObject>());
|
|
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
// 主初始化逻辑
|
|
|
|
|
|
foreach (var state in states)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
var stateBodyNodes = bodyNodes[state];
|
|
|
|
|
|
foreach (var orientation in orientations)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 获取节点定义(避免重复调用)
|
|
|
|
|
|
var nodeDef = drawingOrder.GetDrawNodeDef(state, orientation, out var original);
|
2025-09-19 08:26:54 +08:00
|
|
|
|
GameObject targetObj;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (nodeDef == null)
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
if (PackagesImageManager.Instance.defaultSprite)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
targetObj = Instantiate(GameObjectCreate.ImagePrefab.gameObject, body.transform);
|
2025-08-27 19:56:49 +08:00
|
|
|
|
targetObj.name = $"{state}_{orientation}_Default";
|
2025-08-28 16:20:24 +08:00
|
|
|
|
targetObj.transform.localPosition = Vector3.zero;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
var imagePrefabCom = targetObj.GetComponent<ImagePrefab>();
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (imagePrefabCom)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
imagePrefabCom.SetSprite(PackagesImageManager.Instance.defaultSprite);
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning(
|
|
|
|
|
|
$"InitBody: 默认ImagePrefab中无法获取ImagePrefab组件!状态: {state}, 朝向: {orientation}");
|
|
|
|
|
|
targetObj = new GameObject { name = $"{state}_{orientation}_Empty" };
|
|
|
|
|
|
targetObj.transform.SetParent(body.transform, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
targetObj = new GameObject { name = $"{state}_{orientation}_Empty" };
|
|
|
|
|
|
targetObj.transform.SetParent(body.transform, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 处理有效节点定义
|
|
|
|
|
|
if (original.HasValue && stateBodyNodes.TryGetValue(original.Value, out var reusedObj))
|
|
|
|
|
|
{
|
|
|
|
|
|
targetObj = reusedObj; // 复用已有对象
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-19 08:26:54 +08:00
|
|
|
|
targetObj = GameObjectCreate.InitBodyPart(nodeDef, body); // 创建新对象
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (targetObj)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
stateBodyNodes[orientation] = targetObj;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError($"InitBody: 无法为状态 {state}, 朝向 {orientation} 创建或找到有效的GameObject。");
|
|
|
|
|
|
stateBodyNodes[orientation] = new GameObject($"ErrorNode_{state}_{orientation}"); // 提供一个错误占位符
|
|
|
|
|
|
stateBodyNodes[orientation].transform.SetParent(body.transform, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
// 批量隐藏所有节点(使用字典值集合直接操作)
|
|
|
|
|
|
foreach (var nodeDict in bodyNodes.Values)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
foreach (var obj in nodeDict.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
obj.SetActive(false);
|
|
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
SetBodyTexture(EntityState.Idle, Orientation.Down); // 激活默认朝向
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
protected void InitConditionalEvents(ConditionalEvent[] conditionalEvents)
|
|
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
_conditionalEvents.Add((condition, conditionalEvent.defName));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新实体的逻辑,包括玩家控制和自动行为。
|
|
|
|
|
|
/// </summary>
|
2025-09-03 19:59:22 +08:00
|
|
|
|
public virtual void Tick()
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
//行走动画切换
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (_walkingTimer > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
_walkingTimer -= 1;
|
|
|
|
|
|
if (_walkingTimer <= 0)
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
SetBodyTexture(EntityState.Idle, CurrentOrientation);
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-28 15:02:57 +08:00
|
|
|
|
//行为控制
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (PlayerControlled)
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
|
|
|
|
|
UpdatePlayerControls();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
AutoBehave();
|
|
|
|
|
|
}
|
2025-09-28 15:02:57 +08:00
|
|
|
|
//血条显示
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (IsShowingHealthBarUI)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-09-19 08:26:54 +08:00
|
|
|
|
_hitBarUIShowTimer -= Time.deltaTime;
|
|
|
|
|
|
if (_hitBarUIShowTimer <= 0)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-09-19 08:26:54 +08:00
|
|
|
|
HideHealthBar();
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-28 15:02:57 +08:00
|
|
|
|
//攻击控制
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (_attackTimer > 0)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-09-19 08:26:54 +08:00
|
|
|
|
_attackTimer -= Time.deltaTime;
|
|
|
|
|
|
if (currentAttackWeapon != null && _attackTimer <= _attackDetectionTime)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (currentAttackWeapon.Type == WeaponType.Melee)
|
|
|
|
|
|
{
|
|
|
|
|
|
ExecuteMeleeAttack(currentAttackWeapon);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
ExecuteRangedAttack(currentAttackWeapon);
|
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
currentAttackWeapon = null;
|
2025-09-03 19:59:22 +08:00
|
|
|
|
}
|
2025-09-19 08:26:54 +08:00
|
|
|
|
|
|
|
|
|
|
if (_attackTimer <= 0)
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
2025-09-19 08:26:54 +08:00
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
SetBodyTexture(EntityState.Idle, CurrentOrientation);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//条件事件控制
|
|
|
|
|
|
foreach (var conditionalEvent in _conditionalEvents)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (conditionalEvent.Item1(this))
|
|
|
|
|
|
{
|
|
|
|
|
|
EventManager.Instance.Action(conditionalEvent.Item2, this);
|
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>
|
2025-10-03 00:31:34 +08:00
|
|
|
|
public virtual bool TryAttack() // 使用override允许子类重写
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
2025-10-03 00:31:34 +08:00
|
|
|
|
if (IsAttacking || IsDead) return false; // 死亡时无法攻击
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 尝试获取当前武器
|
2025-09-03 19:59:22 +08:00
|
|
|
|
var currentWeapon = GetCurrentWeapon();
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (currentWeapon == null)
|
|
|
|
|
|
{
|
2025-10-03 00:31:34 +08:00
|
|
|
|
return false;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
StartAttack(currentWeapon);
|
2025-10-03 00:31:34 +08:00
|
|
|
|
return true;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
private void StartAttack(WeaponResource weaponResource)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-09-19 08:26:54 +08:00
|
|
|
|
_attackTimer = weaponResource.AttackCooldown;
|
|
|
|
|
|
_attackDetectionTime =Mathf.Max(0,weaponResource.AttackCooldown- weaponResource.AttackDetectionTime);
|
|
|
|
|
|
currentAttackWeapon = weaponResource;
|
|
|
|
|
|
|
|
|
|
|
|
if (weaponResource.AttackAnimationTime > 0)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-09-19 08:26:54 +08:00
|
|
|
|
TemporaryAnimationManager.Instance.GenerateTemporaryAnimation(
|
|
|
|
|
|
weaponResource.AttackAnimation.ToArray(), Position,transform, weaponResource.AttackAnimationTime,
|
|
|
|
|
|
weaponResource.FPS);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (weaponResource.UseEntityAttackAnimation)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetBodyTexture(
|
|
|
|
|
|
weaponResource.Type == WeaponType.Melee ? EntityState.MeleeAttack : EntityState.RangedAttack,
|
2025-09-28 15:02:57 +08:00
|
|
|
|
CurrentOrientation);
|
2025-09-19 08:26:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
HideCurrentBodyTexture();
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void SetBodyTexture(EntityState state, Orientation orientation)
|
|
|
|
|
|
{
|
2025-10-03 00:31:34 +08:00
|
|
|
|
if (state == CurrentState && orientation == CurrentOrientation)
|
|
|
|
|
|
return;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
HideCurrentBodyTexture();
|
|
|
|
|
|
if (IsAttacking && currentAttackWeapon is { UseEntityAttackAnimation: false })
|
|
|
|
|
|
return;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (bodyNodes.TryGetValue(state, out var showStateNode))
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (showStateNode.TryGetValue(orientation, out var showNode))
|
|
|
|
|
|
{
|
|
|
|
|
|
showNode.SetActive(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
CurrentState = state;
|
|
|
|
|
|
CurrentOrientation = orientation;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
public void HideCurrentBodyTexture()
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
if (!bodyNodes.TryGetValue(CurrentState, out var stateNode)) return;
|
|
|
|
|
|
if (stateNode.TryGetValue(CurrentOrientation, out var node))
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
2025-09-19 08:26:54 +08:00
|
|
|
|
node.SetActive(false);
|
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-10-03 00:31:34 +08:00
|
|
|
|
/// 根据方向尝试移动实体,并避免超调目标点。
|
2025-07-21 13:58:58 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual void TryMove()
|
|
|
|
|
|
{
|
2025-10-03 00:31:34 +08:00
|
|
|
|
if (OnTargetPoint)
|
|
|
|
|
|
{
|
|
|
|
|
|
return; // 已到达目标点,无需进一步移动。
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var currentPosition = Position; // 假设 Position 是获取当前位置的属性
|
|
|
|
|
|
var targetDestination = moveTarget; // 假设 moveTarget 是目标位置字段
|
|
|
|
|
|
var directionToTarget = targetDestination - currentPosition;
|
|
|
|
|
|
// 逻辑修改1:计算平方距离,避免开方。
|
|
|
|
|
|
var sqrDistanceToTarget = directionToTarget.sqrMagnitude;
|
|
|
|
|
|
// 逻辑修改2:将到达目标点的判断改为使用平方距离,并包含等于阈值的情况,避免开方。
|
|
|
|
|
|
if (sqrDistanceToTarget <= TargetReachThresholdSquared)
|
|
|
|
|
|
{
|
|
|
|
|
|
transform.position = targetDestination; // 假设 transform 是 Transform 组件
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var maxMoveDistance = AttributesNow.moveSpeed * Time.deltaTime; // 假设 AttributesNow 是包含 moveSpeed 的属性
|
|
|
|
|
|
// 逻辑修改4:计算最大移动距离的平方,用于后续与平方距离的比较,避免开方。
|
|
|
|
|
|
var maxMoveDistanceSquared = maxMoveDistance * maxMoveDistance;
|
|
|
|
|
|
Vector3 newPosition;
|
|
|
|
|
|
bool willReachTargetThisFrame;
|
|
|
|
|
|
// 逻辑修改5:直接比较平方距离来确定是否能到达目标,优化了之前的Mathf.Min和复杂判断。
|
|
|
|
|
|
if (maxMoveDistanceSquared >= sqrDistanceToTarget)
|
|
|
|
|
|
{
|
|
|
|
|
|
newPosition = targetDestination; // 精确捕捉到目标点
|
|
|
|
|
|
willReachTargetThisFrame = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
newPosition = currentPosition + Direction * maxMoveDistance;
|
|
|
|
|
|
willReachTargetThisFrame = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
transform.position = newPosition;
|
|
|
|
|
|
|
|
|
|
|
|
// 如果已到达目标,则不设置行走动画和重置计时器
|
|
|
|
|
|
if (willReachTargetThisFrame) return;
|
|
|
|
|
|
SetBodyTexture(EntityState.Walking, CurrentOrientation); // 假设 SetBodyTexture 是设置动画的方法
|
|
|
|
|
|
_walkingTimer = 2; // 假设 _walkingTimer 是一个计时器
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void Move()
|
|
|
|
|
|
{
|
|
|
|
|
|
transform.position += Direction * (AttributesNow.moveSpeed * Time.deltaTime);
|
2025-09-28 15:02:57 +08:00
|
|
|
|
SetBodyTexture(EntityState.Walking, CurrentOrientation);
|
2025-08-27 19:56:49 +08:00
|
|
|
|
_walkingTimer = 2;
|
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-08-27 19:56:49 +08:00
|
|
|
|
// MODIFIED: 收到攻击时触发回调
|
|
|
|
|
|
if (IsDead) // 如果已经死亡,则不再处理伤害 nor 触发事件
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-10-03 00:31:34 +08:00
|
|
|
|
var hit = from.AttributesNow.attack - AttributesNow.defense;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
if (hit < 0)
|
2025-10-03 00:31:34 +08:00
|
|
|
|
hit = from.AttributesNow.attack / 100;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
// 确保伤害不为负,最小为0
|
|
|
|
|
|
hit = Mathf.Max(0, hit);
|
2025-10-03 00:31:34 +08:00
|
|
|
|
AttributesNow.health -= hit;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
var wasFatal = IsDead; // 检查这次攻击是否导致实体死亡
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
BehaviorTreeTree?.Reset();
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (wasFatal)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnEntityDied?.Invoke(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (Setting.Instance.CurrentSettings.showHealthBarByHit)
|
|
|
|
|
|
{
|
|
|
|
|
|
ShowHealthBar();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Setting.Instance.CurrentSettings.showHitNumber)
|
|
|
|
|
|
{
|
|
|
|
|
|
TemporaryAnimationManager.Instance.GenerateTemporaryAnimation(hit.ToString(), Position);
|
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
2025-08-07 16:44:43 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
public void ShowHealthBar()
|
|
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (!healthBarPrefab)
|
2025-08-19 20:22:10 +08:00
|
|
|
|
return;
|
|
|
|
|
|
healthBarPrefab.gameObject.SetActive(true);
|
2025-10-03 00:31:34 +08:00
|
|
|
|
healthBarPrefab.Progress = (float)AttributesNow.health / entityDef.attributes.health;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
_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-27 19:56:49 +08:00
|
|
|
|
if (!healthBarPrefab)
|
2025-08-19 20:22:10 +08:00
|
|
|
|
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-08-27 19:56:49 +08:00
|
|
|
|
if (IsDead)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 00:31:34 +08:00
|
|
|
|
AttributesNow.health = 0; // 直接设置生命值为0
|
|
|
|
|
|
BehaviorTreeTree?.Reset();
|
2025-08-27 19:56:49 +08:00
|
|
|
|
// 触发 OnEntityDied 事件
|
|
|
|
|
|
OnEntityDied?.Invoke(this);
|
|
|
|
|
|
ShowHealthBar();
|
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)
|
|
|
|
|
|
{
|
2025-10-03 00:31:34 +08:00
|
|
|
|
Direction = (pos - transform.position).normalized;
|
|
|
|
|
|
moveTarget = pos;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (!PlayerControlled)
|
|
|
|
|
|
{
|
2025-10-03 00:31:34 +08:00
|
|
|
|
attackDirection=Direction;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 自动行为逻辑,根据行为树执行任务。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected virtual void AutoBehave()
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
2025-10-03 00:31:34 +08:00
|
|
|
|
var result=BehaviorTreeTree?.Tick();
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
2025-08-27 19:56:49 +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-09-19 08:26:54 +08:00
|
|
|
|
if (Input.GetMouseButton(0))
|
|
|
|
|
|
{
|
|
|
|
|
|
var mousePos = MousePosition.GetWorldPosition();
|
2025-10-03 00:31:34 +08:00
|
|
|
|
attackDirection = new Vector3(mousePos.x, mousePos.y) - Position;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (weaponItem)
|
2025-10-03 00:31:34 +08:00
|
|
|
|
{
|
|
|
|
|
|
var angle= RotateTool.RotateTransformToDirection(weaponItem.transform, attackDirection);
|
|
|
|
|
|
weaponItem.Flip(false,angle is > 90 or <= -90);
|
|
|
|
|
|
}
|
2025-09-19 08:26:54 +08:00
|
|
|
|
}
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (Input.GetKeyDown(KeyCode.V))
|
|
|
|
|
|
{
|
|
|
|
|
|
weaponItem.gameObject.SetActive(!weaponItem.gameObject.activeSelf);
|
|
|
|
|
|
}
|
2025-08-27 19:56:49 +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
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
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
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
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
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
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-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
if (Input.GetMouseButton(0))
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
2025-10-03 00:31:34 +08:00
|
|
|
|
Direction = new Vector3(inputDirection.x, inputDirection.y, 0);
|
|
|
|
|
|
Move();
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
2025-07-21 13:58:58 +08:00
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
private void ExecuteMeleeAttack(WeaponResource weapon)
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (weapon.Attributes == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning($"武器 {weapon.DefName} 没有定义Attributes,无法执行近战攻击。");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var attackRange = weapon.Attributes.attackRange;
|
|
|
|
|
|
var attackTargetCount = weapon.Attributes.attackTargetCount;
|
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
var hits = Physics2D.OverlapCircleAll(
|
2025-08-27 19:56:49 +08:00
|
|
|
|
transform.position,
|
|
|
|
|
|
attackRange,
|
2025-08-19 20:22:10 +08:00
|
|
|
|
LayerMask.GetMask("Entity"));
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var hit in hits)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (attackTargetCount <= 0) break; // 已达到最大攻击目标数
|
|
|
|
|
|
if (hit.gameObject == gameObject) continue; // 不攻击自己
|
2025-07-21 13:58:58 +08:00
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
var entity = hit.GetComponent<Entity>();
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (entity != null && entity.affiliation != affiliation) // 确保是敌对实体
|
|
|
|
|
|
{
|
|
|
|
|
|
entity.OnHit(this); // 攻击时将自身作为攻击来源
|
|
|
|
|
|
attackTargetCount--;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NEW: 辅助方法用于执行远程攻击
|
|
|
|
|
|
private void ExecuteRangedAttack(WeaponResource weapon)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (weapon.Bullet == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning($"远程武器 {weapon.DefName} 没有定义Bullet,无法发射子弹。");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取子弹方向。这里使用实体当前的移动方向作为子弹发射方向
|
|
|
|
|
|
// 更复杂的逻辑可能根据鼠标位置、目标位置等确定
|
2025-09-19 08:26:54 +08:00
|
|
|
|
var bulletDirection = attackDirection; // 实体当前的朝向
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果没有明确的方向,给一个默认值以防万一
|
|
|
|
|
|
if (bulletDirection == Vector3.zero) bulletDirection = Vector3.down;
|
|
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
// 假设 EntityManage.Instance.GenerateBulletEntity 方法存在
|
|
|
|
|
|
// (需要一个 EntityManage 单例来实现子弹生成)
|
2025-09-28 15:02:57 +08:00
|
|
|
|
if (EntityManager.Instance != null && Program.Instance != null)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
EntityManager.Instance.GenerateBulletEntity(
|
2025-08-27 19:56:49 +08:00
|
|
|
|
Program.Instance.FocusedDimensionId,
|
|
|
|
|
|
weapon.Bullet,
|
|
|
|
|
|
transform.position, // 子弹的生成位置
|
|
|
|
|
|
bulletDirection, // 子弹的初始方向
|
|
|
|
|
|
this); // 子弹的发射者
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("EntityManage.Instance 或 Program.Instance 为空,无法生成子弹。请确保它们已正确初始化。");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public virtual WeaponResource GetCurrentWeapon()
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
2025-09-28 15:02:57 +08:00
|
|
|
|
|
|
|
|
|
|
public void ExecuteEvent(EntityEventType type)
|
|
|
|
|
|
{
|
|
|
|
|
|
var eventDefs = entityDef?.eventDef?.GetEventsByType(type);
|
|
|
|
|
|
if (eventDefs == null || eventDefs.Length == 0) return;
|
|
|
|
|
|
foreach (var eventDef in eventDefs)
|
|
|
|
|
|
{
|
|
|
|
|
|
EventManager.Instance.Action(eventDef,this);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|