using System;
using Cielonos.MainGame.Characters;
using SLSUtilities.General;
using UnityEngine;
namespace Cielonos.MainGame.Inventory.Collections
{
///
/// 记忆外骨骼 / Memory Exoskeleton
/// 记录并调校玩家的动作模式:每击杀一个敌人,获得1%攻击速度加成(叠层)。
/// 切换主武器时,外骨骼需要重新适配,叠层归零。
///
public class MemoryExoskeleton : PassiveEquipmentBase
{
private const string EventKey = "MemoryExoskeleton";
private int _currentStacks;
public override void OnObtained()
{
base.OnObtained();
_currentStacks = 0;
Action onFinishAttack = OnFinishAttack;
player.eventSm.onFinishAttack.Add(EventKey, onFinishAttack.ToPrioritized());
Action onMainWeaponChanged = OnMainWeaponChanged;
player.eventSm.onMainWeaponChanged.Add(EventKey, onMainWeaponChanged.ToPrioritized());
}
public override void OnDiscarded()
{
player.eventSm.onMainWeaponChanged.Remove(EventKey);
player.eventSm.onFinishAttack.Remove(EventKey);
// 清除已施加的攻击速度加成
if (_currentStacks > 0)
{
_currentStacks = 0;
ApplyAttackSpeed(0f);
}
base.OnDiscarded();
}
///
/// 攻击结算完成时检查是否击杀目标,是则叠加攻击速度。
///
private void OnFinishAttack(AttackAreaBase attackArea, CharacterBase target, Attack.Result result)
{
if (!result.causedDeath) return;
float attackSpeedPerKill = passiveAttributeSm.GetItemAttribute("AttackSpeedPerKill");
float maxAttackSpeed = passiveAttributeSm.GetItemAttribute("MaxAttackSpeed");
_currentStacks++;
float newAttackSpeed = _currentStacks * attackSpeedPerKill;
newAttackSpeed = Mathf.Min(newAttackSpeed, maxAttackSpeed);
ApplyAttackSpeed(newAttackSpeed);
}
///
/// 切换主武器时,外骨骼需要重新适配当前武器,叠层归零。
///
private void OnMainWeaponChanged(MainWeaponBase newWeapon)
{
if (_currentStacks <= 0) return;
_currentStacks = 0;
ApplyAttackSpeed(0f);
MainGameBaseCollection.Instance.InfoText()?.Spawn(player.centerPosition, "Exo Reset");
}
///
/// 将攻击速度加成写入属性系统并刷新。
///
private void ApplyAttackSpeed(float bonus)
{
if (bonus > 0f)
{
passiveAttributeSm.charAttrNumericChange[CharacterAttribute.AttackSpeed] = bonus;
}
else
{
passiveAttributeSm.charAttrNumericChange.Remove(CharacterAttribute.AttackSpeed);
}
player.attributeSm.RefreshAttribute(CharacterAttribute.AttackSpeed);
}
}
}