多轮审查和修复

This commit is contained in:
2026-05-12 15:34:08 +08:00
parent f55d2a57c3
commit ebbbb7332e
805 changed files with 838724 additions and 1905 deletions

View File

@@ -1,29 +1,67 @@
using UnityEngine;
using BaseGames.Core;
using BaseGames.Core.Events;
namespace BaseGames.Enemies
{
/// <summary>
/// 敌人运行时数值组件(架构 07_EnemyModule §2
/// 由 EnemyBase.Awake() 通过 Initialize(EnemyStatsSO) 注入配置。
/// 同时订阅难度变更事件,支持游戏进行中切换难度(架构 19 §5
/// </summary>
public class EnemyStats : MonoBehaviour
{
private EnemyStatsSO _config;
[SerializeField] private DifficultyChangedEventChannel _onDifficultyChanged;
public int MaxHP { get; private set; }
public int CurrentHP { get; private set; }
public int Defense { get; private set; }
public float AttackCooldownTimer { get; private set; }
/// <summary>每帧由 EnemyBase 更新(读取玩家位置后写入)。</summary>
public float DistanceToPlayer { get; set; }
/// <summary>
/// 每帧由 EnemyBase 更新sqrMagnitude避免 sqrt 开销)。
/// 使用方请与 range*range 比较,而非直接与 range 比较。
/// </summary>
public float SqrDistanceToPlayer { get; set; }
private readonly CompositeDisposable _subs = new();
private void OnEnable()
{
_onDifficultyChanged?.Subscribe(HandleDifficultyChanged).AddTo(_subs);
}
private void OnDisable()
{
_subs.Clear();
}
public void Initialize(EnemyStatsSO so)
{
_config = so;
MaxHP = so.MaxHP;
CurrentHP = so.MaxHP;
Defense = so.Defense;
_config = so;
Defense = so.Defense; // Defense 不随难度缩放(架构 19 §5
ApplyHPScaler();
CurrentHP = MaxHP;
}
/// <summary>难度变更时重算 HP保持 HP 比例,架构 19 §5。</summary>
private void HandleDifficultyChanged(DifficultyLevel _)
{
if (_config == null) return;
float hpRatio = MaxHP > 0 ? (float)CurrentHP / MaxHP : 1f;
ApplyHPScaler();
CurrentHP = Mathf.Clamp(Mathf.RoundToInt(MaxHP * hpRatio), 1, MaxHP);
}
private void ApplyHPScaler()
{
if (_config == null) return;
var scaler = ServiceLocator.GetOrDefault<IDifficultyService>()?.CurrentScaler;
MaxHP = scaler != null
? Mathf.Max(1, Mathf.RoundToInt(_config.MaxHP * scaler.EnemyHPMultiplier))
: _config.MaxHP;
}
public void TakeDamage(int amount)
@@ -39,7 +77,7 @@ namespace BaseGames.Enemies
public void ResetAttackCooldown()
{
AttackCooldownTimer = _config != null ? _config.AttackCooldown : 1f;
AttackCooldownTimer = _config.AttackCooldown;
}
}
}