多轮审查和修复
This commit is contained in:
@@ -1,28 +1,105 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BaseGames.Player
|
||||
{
|
||||
/// <summary>
|
||||
/// 武器管理器(Phase 1 实现)。
|
||||
/// 架构 05_PlayerModule §7:ActiveWeapon(WeaponSO),OnWeaponChanged 事件。
|
||||
/// ⚠️ 无 Equip() 方法,无 WeaponInstance 类。
|
||||
/// Phase 2 §2.3:接入 FormController.OnFormChanged。
|
||||
/// 武器管理器。
|
||||
/// 监听 FormController.OnFormChanged,依据当前形态切换 ActiveWeapon。
|
||||
/// 支持护符 Override:护符调用 SetOverride() 临时替换特定形态的武器。
|
||||
/// 架构 05_PlayerModule §7。
|
||||
/// </summary>
|
||||
public class WeaponManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private WeaponSO _startingWeapon;
|
||||
[SerializeField] private FormController _formController;
|
||||
[SerializeField] private WeaponSO _startingWeapon; // 无 FormController 时的回退武器
|
||||
|
||||
public WeaponSO ActiveWeapon { get; private set; }
|
||||
public event Action<WeaponSO> OnWeaponChanged;
|
||||
|
||||
// 护符注入的武器覆盖:Key = FormSO.formId,Value = 替换武器(架构 05 §7)
|
||||
private readonly Dictionary<string, WeaponSO> _overrides = new();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (_formController != null && _formController.CurrentForm != null)
|
||||
ApplyWeapon(_formController.CurrentForm);
|
||||
else if (_startingWeapon != null)
|
||||
SetDirectWeapon(_startingWeapon);
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (_formController != null)
|
||||
_formController.OnFormChanged += HandleFormChanged;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (_formController != null)
|
||||
_formController.OnFormChanged -= HandleFormChanged;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (_startingWeapon != null)
|
||||
// 若 FormController 在 Awake 时 CurrentForm 尚未初始化,在 Start 重试
|
||||
if (ActiveWeapon == null && _formController != null && _formController.CurrentForm != null)
|
||||
ApplyWeapon(_formController.CurrentForm);
|
||||
else if (ActiveWeapon == null && _startingWeapon != null)
|
||||
SetDirectWeapon(_startingWeapon);
|
||||
}
|
||||
|
||||
// ── 内部切换 ───────────────────────────────────────────────────────────
|
||||
|
||||
private void HandleFormChanged() => ApplyWeapon(_formController.CurrentForm);
|
||||
|
||||
private void ApplyWeapon(FormSO form)
|
||||
{
|
||||
if (form == null) return;
|
||||
WeaponSO next = _overrides.TryGetValue(form.formId, out var ov) ? ov : form.defaultWeapon;
|
||||
if (next == ActiveWeapon) return;
|
||||
SetDirectWeapon(next);
|
||||
}
|
||||
|
||||
private void SetDirectWeapon(WeaponSO weapon)
|
||||
{
|
||||
ActiveWeapon = weapon;
|
||||
OnWeaponChanged?.Invoke(weapon);
|
||||
}
|
||||
|
||||
// ── 护符 Override API(由 WeaponOverrideEffect 调用,架构 05 §7)──────
|
||||
|
||||
/// <summary>为指定形态设置武器覆盖。formId 为空 = 覆盖所有形态。</summary>
|
||||
public void SetOverride(string formId, WeaponSO weapon)
|
||||
{
|
||||
if (string.IsNullOrEmpty(formId))
|
||||
{
|
||||
ActiveWeapon = _startingWeapon;
|
||||
OnWeaponChanged?.Invoke(ActiveWeapon);
|
||||
if (_formController != null)
|
||||
foreach (var f in _formController.AllForms)
|
||||
_overrides[f.formId] = weapon;
|
||||
}
|
||||
else
|
||||
{
|
||||
_overrides[formId] = weapon;
|
||||
}
|
||||
if (_formController?.CurrentForm != null)
|
||||
ApplyWeapon(_formController.CurrentForm);
|
||||
}
|
||||
|
||||
/// <summary>移除覆盖,恢复默认武器。formId 为空 = 移除所有形态覆盖。</summary>
|
||||
public void ClearOverride(string formId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(formId))
|
||||
{
|
||||
_overrides.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
_overrides.Remove(formId);
|
||||
}
|
||||
if (_formController?.CurrentForm != null)
|
||||
ApplyWeapon(_formController.CurrentForm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user