mirror of
http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite.git
synced 2025-11-20 15:27:13 +08:00
(client) feat:添加基地界面到游玩界面的过程,添加存档管理,技能树变得可用 (#58)
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/58
This commit is contained in:
@@ -33,19 +33,19 @@ namespace Entity
|
||||
/// <summary>
|
||||
/// 人工智能行为树,定义实体的行为逻辑。
|
||||
/// </summary>
|
||||
public AIBase aiTree;
|
||||
public BehaviorTreeBase BehaviorTreeTree { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前实体正在执行的任务。
|
||||
/// </summary>
|
||||
public JobBase currentJob;
|
||||
// /// <summary>
|
||||
// /// 当前实体正在执行的任务。
|
||||
// /// </summary>
|
||||
// public JobBase currentJob;
|
||||
|
||||
private Attributes _attribute;
|
||||
|
||||
/// <summary>
|
||||
/// 实体的属性定义,包括生命值、攻击力、防御力等。
|
||||
/// </summary>
|
||||
public virtual Attributes attributes
|
||||
public virtual Attributes AttributesNow
|
||||
{
|
||||
get { return _attribute ??= new Attributes(baseAttributes); }
|
||||
protected set => _attribute = value;
|
||||
@@ -62,10 +62,28 @@ namespace Entity
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 _direction;
|
||||
/// <summary>
|
||||
/// 实体当前的移动方向。
|
||||
/// </summary>
|
||||
public Vector3 direction;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 attackDirection;
|
||||
|
||||
@@ -87,7 +105,7 @@ namespace Entity
|
||||
|
||||
public string currentDimensionId;
|
||||
|
||||
|
||||
public Vector2 Size => entityPrefab.outline.GetColliderSize();
|
||||
|
||||
/// <summary>
|
||||
/// 表示实体是否由玩家控制。
|
||||
@@ -98,7 +116,7 @@ namespace Entity
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
currentJob = null;
|
||||
BehaviorTreeTree?.Reset();
|
||||
if (Program.Instance.FocusedEntity && Program.Instance.FocusedEntity != this)
|
||||
{
|
||||
Program.Instance.FocusedEntity.PlayerControlled = false;
|
||||
@@ -127,7 +145,7 @@ namespace Entity
|
||||
/// <summary>
|
||||
/// 表示实体是否已经死亡(生命值小于等于零)。
|
||||
/// </summary>
|
||||
public bool IsDead => attributes.health <= 0;
|
||||
public bool IsDead => AttributesNow.health <= 0;
|
||||
|
||||
public bool IsShowingHealthBarUI => _hitBarUIShowTimer > 0;
|
||||
public bool IsAttacking => _attackTimer > 0;
|
||||
@@ -135,12 +153,7 @@ namespace Entity
|
||||
private float _attackDetectionTime;
|
||||
private WeaponResource currentAttackWeapon;
|
||||
|
||||
/// <summary>
|
||||
/// 当实体受到伤害时触发的事件。
|
||||
/// 可以订阅此事件来响应实体的生命值变化,例如更新UI或播放受击特效。
|
||||
/// </summary>
|
||||
public event Action<EntityHitEventArgs> OnEntityHit;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 当实体死亡时触发的事件。
|
||||
/// 只在实体首次进入死亡状态时触发一次。
|
||||
@@ -157,7 +170,7 @@ namespace Entity
|
||||
/// <summary>
|
||||
/// 当前实体的朝向。
|
||||
/// </summary>
|
||||
public Orientation CurrentOrientation { get; private set; } = Orientation.Down;
|
||||
public Orientation CurrentOrientation { get; private set; } = Orientation.Up;
|
||||
|
||||
/// <summary>
|
||||
/// 当前实体的状态
|
||||
@@ -165,7 +178,18 @@ namespace Entity
|
||||
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;
|
||||
return distanceSquared <= TargetReachThresholdSquared;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -186,25 +210,27 @@ namespace Entity
|
||||
|
||||
[SerializeField] private float _hitBarUIShowTime = 5;
|
||||
private float _hitBarUIShowTimer;
|
||||
private int _walkingTimer;
|
||||
protected int _walkingTimer;
|
||||
|
||||
private List<(Func<Entity, bool>,string)> _conditionalEvents=new();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化实体的基本属性和行为树。
|
||||
/// </summary>
|
||||
/// <param name="entityDef">实体的定义数据。</param>
|
||||
public virtual void Init(EntityDef entityDef)
|
||||
/// <param name="entityDefine">实体的定义数据。</param>
|
||||
public virtual void Init(EntityDef entityDefine)
|
||||
{
|
||||
attributes = new Attributes(entityDef.attributes);
|
||||
aiTree = BehaviorTree.ConvertToAIBase(entityDef.behaviorTree);
|
||||
affiliation = entityDef.affiliation?.defName;
|
||||
InitBody(entityDef.drawingOrder);
|
||||
this.entityDef = entityDef;
|
||||
AttributesNow = new Attributes(entityDefine.attributes);
|
||||
|
||||
affiliation = entityDefine.affiliation?.defName;
|
||||
InitBody(entityDefine.drawingOrder);
|
||||
entityDef = entityDefine;
|
||||
|
||||
BehaviorTreeTree = BehaviorTreeUtils.ConvertToAIBase(entityDefine.behaviorTree);
|
||||
BehaviorTreeTree?.Init(entityDefine.behaviorTree, this);
|
||||
HideHealthBar();
|
||||
InitWeaponAnimator();
|
||||
InitConditionalEvents(entityDef.eventDef?.GetAllConditionalEvents());
|
||||
InitConditionalEvents(entityDefine.eventDef?.GetAllConditionalEvents());
|
||||
}
|
||||
|
||||
protected virtual void InitWeaponAnimator()
|
||||
@@ -316,7 +342,6 @@ namespace Entity
|
||||
obj.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
SetBodyTexture(EntityState.Idle, Orientation.Down); // 激活默认朝向
|
||||
}
|
||||
|
||||
@@ -406,21 +431,20 @@ namespace Entity
|
||||
/// <summary>
|
||||
/// 尝试攻击目标实体。
|
||||
/// </summary>
|
||||
public virtual void TryAttack() // 使用override允许子类重写
|
||||
public virtual bool TryAttack() // 使用override允许子类重写
|
||||
{
|
||||
if (IsAttacking || IsDead) return; // 死亡时无法攻击
|
||||
if (IsAttacking || IsDead) return false; // 死亡时无法攻击
|
||||
|
||||
// 尝试获取当前武器
|
||||
var currentWeapon = GetCurrentWeapon();
|
||||
|
||||
// 如果没有武器,可以选择进行徒手攻击或者直接返回
|
||||
// 暂时设定为:如果没有武器,则不进行攻击
|
||||
|
||||
if (currentWeapon == null)
|
||||
{
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
StartAttack(currentWeapon);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void StartAttack(WeaponResource weaponResource)
|
||||
@@ -451,6 +475,8 @@ namespace Entity
|
||||
|
||||
public void SetBodyTexture(EntityState state, Orientation orientation)
|
||||
{
|
||||
if (state == CurrentState && orientation == CurrentOrientation)
|
||||
return;
|
||||
HideCurrentBodyTexture();
|
||||
if (IsAttacking && currentAttackWeapon is { UseEntityAttackAnimation: false })
|
||||
return;
|
||||
@@ -476,17 +502,58 @@ namespace Entity
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据方向尝试移动实体。
|
||||
/// 根据方向尝试移动实体,并避免超调目标点。
|
||||
/// </summary>
|
||||
public virtual void TryMove()
|
||||
{
|
||||
// if (IsAttacking)
|
||||
// return;
|
||||
transform.position += direction * (attributes.moveSpeed * Time.deltaTime);
|
||||
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);
|
||||
SetBodyTexture(EntityState.Walking, CurrentOrientation);
|
||||
_walkingTimer = 2;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理实体受到攻击的逻辑。
|
||||
/// </summary>
|
||||
@@ -498,22 +565,17 @@ namespace Entity
|
||||
{
|
||||
return;
|
||||
}
|
||||
var hit = from.attributes.attack - attributes.defense;
|
||||
var hit = from.AttributesNow.attack - AttributesNow.defense;
|
||||
if (hit < 0)
|
||||
hit = from.attributes.attack / 100;
|
||||
hit = from.AttributesNow.attack / 100;
|
||||
// 确保伤害不为负,最小为0
|
||||
hit = Mathf.Max(0, hit);
|
||||
attributes.health -= hit;
|
||||
AttributesNow.health -= hit;
|
||||
var wasFatal = IsDead; // 检查这次攻击是否导致实体死亡
|
||||
// 触发 OnEntityHit 事件
|
||||
OnEntityHit?.Invoke(new EntityHitEventArgs(
|
||||
this, from, hit, attributes.health, entityDef.attributes.health, wasFatal));
|
||||
currentJob?.StopJob();
|
||||
|
||||
BehaviorTreeTree?.Reset();
|
||||
if (wasFatal)
|
||||
{
|
||||
// 如果是首次死亡,则触发 OnEntityDied 事件
|
||||
// MODIFIED: 停止所有活动,包括当前工作
|
||||
currentJob = null; // 清除当前工作
|
||||
OnEntityDied?.Invoke(this);
|
||||
}
|
||||
|
||||
@@ -533,7 +595,7 @@ namespace Entity
|
||||
if (!healthBarPrefab)
|
||||
return;
|
||||
healthBarPrefab.gameObject.SetActive(true);
|
||||
healthBarPrefab.Progress = (float)attributes.health / entityDef.attributes.health;
|
||||
healthBarPrefab.Progress = (float)AttributesNow.health / entityDef.attributes.health;
|
||||
_hitBarUIShowTimer = _hitBarUIShowTime;
|
||||
}
|
||||
|
||||
@@ -554,10 +616,8 @@ namespace Entity
|
||||
return;
|
||||
}
|
||||
|
||||
attributes.health = 0; // 直接设置生命值为0
|
||||
// MODIFIED: 停止所有活动,包括当前工作
|
||||
currentJob?.StopJob();
|
||||
currentJob = null; // 清除当前工作
|
||||
AttributesNow.health = 0; // 直接设置生命值为0
|
||||
BehaviorTreeTree?.Reset();
|
||||
// 触发 OnEntityDied 事件
|
||||
OnEntityDied?.Invoke(this);
|
||||
ShowHealthBar();
|
||||
@@ -569,24 +629,11 @@ namespace Entity
|
||||
/// <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;
|
||||
}
|
||||
|
||||
SetBodyTexture(CurrentState, ori);
|
||||
Direction = (pos - transform.position).normalized;
|
||||
moveTarget = pos;
|
||||
if (!PlayerControlled)
|
||||
{
|
||||
attackDirection=direction;
|
||||
attackDirection=Direction;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -595,26 +642,7 @@ namespace Entity
|
||||
/// </summary>
|
||||
protected virtual void AutoBehave()
|
||||
{
|
||||
if (aiTree == null)
|
||||
return;
|
||||
if (currentJob == null || !currentJob.Running)
|
||||
{
|
||||
currentJob = aiTree.GetJob(this);
|
||||
if (currentJob == null)
|
||||
{
|
||||
if (!_warning)
|
||||
{
|
||||
Debug.LogWarning($"{GetType().Name}类型的{name}没有分配到任何工作,给行为树末尾添加等待行为,避免由于没有工作导致无意义的反复查找工作导致性能问题");
|
||||
_warning = true;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
currentJob.StartJob(this);
|
||||
}
|
||||
|
||||
currentJob.Update();
|
||||
var result=BehaviorTreeTree?.Tick();
|
||||
}
|
||||
|
||||
|
||||
@@ -626,11 +654,14 @@ namespace Entity
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
var mousePos = MousePosition.GetWorldPosition();
|
||||
attackDirection = new Vector3(mousePos.x,mousePos.y) - Position;
|
||||
attackDirection = new Vector3(mousePos.x, mousePos.y) - Position;
|
||||
if (weaponItem)
|
||||
RotateTool.RotateTransformToDirection(weaponItem.transform, attackDirection);
|
||||
{
|
||||
var angle= RotateTool.RotateTransformToDirection(weaponItem.transform, attackDirection);
|
||||
weaponItem.Flip(false,angle is > 90 or <= -90);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.V))
|
||||
{
|
||||
weaponItem.gameObject.SetActive(!weaponItem.gameObject.activeSelf);
|
||||
@@ -668,14 +699,8 @@ namespace Entity
|
||||
// 归一化方向向量,确保对角线移动速度一致
|
||||
inputDirection = inputDirection.normalized;
|
||||
|
||||
// 设置目标位置(2D 移动,Z 轴保持不变)
|
||||
var targetPosition = transform.position + new Vector3(inputDirection.x, inputDirection.y, 0);
|
||||
|
||||
// 调用 SetTarget 方法设置目标位置
|
||||
SetTarget(targetPosition);
|
||||
|
||||
// 调用 TryMove 方法处理实际移动逻辑
|
||||
TryMove();
|
||||
Direction = new Vector3(inputDirection.x, inputDirection.y, 0);
|
||||
Move();
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user