using System.Collections.Generic;
using System.Linq;
using Continentis.MainGame.Card;
using Continentis.MainGame.UI;
using SLSFramework.General;
using UnityEngine;
namespace Continentis.MainGame.Character
{
public abstract partial class CharacterCombatBuffBase : CharacterBuffBase
{
public CardLogicBase sourceCard;
///
/// 行动计数,每次行动开始时计数-1。
///
public CountSubmodule actionCountSubmodule;
///
/// 回合计数,每回合开始时计数-1。
///
public CountSubmodule roundCountSubmodule;
///
/// 首次行动计数,仅在角色本回合首次行动时计数-1。
///
public CountSubmodule roundFirstActionCountSubmodule;
public UnitedStackSubmodule unitedStackSubmodule;
public IndependentStackSubmodule independentStackSubmodule;
public CoreAttributeSubmodule coreAttributeSubmodule;
public GeneralAttributeSubmodule generalAttributeSubmodule;
public StatusSubmodule statusSubmodule;
}
public partial class CharacterCombatBuffBase
{
public sealed override bool OnBuffApply(out BuffBase existingBuff)
{
throw new System.NotImplementedException("请使用类型约束更强的OnBuffApply方法");
}
public abstract bool OnBuffApply(out CharacterCombatBuffBase existingBuff);
public override void OnAfterFirstApply()
{
statusSubmodule?.AddStatus();
attachedCharacter.combatBuffSubmodule.buffList.Exclude(this).For(buff => buff.eventSubmodule?.onOtherBuffFirstApplied.Invoke(this));
}
public override void OnBuffRemove()
{
RefreshAttributes();
statusSubmodule?.RemoveStatus();
iconSubmodule?.Remove();
}
}
public partial class CharacterCombatBuffBase
{
protected bool FindExistingSameBuff(out T existingBuff) where T : CharacterBuffBase
{
bool result = FindExistingSameBuffs(out List existingBuffs, attachedCharacter.combatBuffSubmodule.buffList);
existingBuff = result ? existingBuffs[0] : null;
return result;
}
protected bool FindExistingSameBuffs(out List existingBuffs) where T : CharacterBuffBase
{
bool result = FindExistingSameBuffs(out existingBuffs, attachedCharacter.combatBuffSubmodule.buffList);
return result;
}
public override void Apply(CharacterBase attachedCharacter, CharacterBase sourceCharacter = null)
{
this.Apply(attachedCharacter, sourceCharacter, null);
}
public void Apply(CharacterBase attachedCharacter, CharacterBase sourceCharacter = null, CardLogicBase sourceCard = null)
{
this.attachedCharacter = attachedCharacter;
this.sourceCharacter = sourceCharacter;
this.sourceCard = sourceCard;
if (OnBuffApply(out CharacterCombatBuffBase existingBuff))
{
this.attachedCharacter.combatBuffSubmodule.buffList.AddByPriority(this);
OnAfterFirstApply();
if (iconSubmodule != null)
{
(attachedCharacter.characterView.hudContainer.enablingHUDs["CharacterBuffCollection"] as HUD_CharacterBuffCollection)
?.AddBuffIcon(this);
}
}
else
{
existingBuff.iconSubmodule?.Update();
}
attachedCharacter.combatBuffSubmodule.buffList.Exclude(this).For(buff => buff.eventSubmodule?.onOtherBuffApplied.Invoke(this));
RefreshAttributes();
iconSubmodule?.Update();
attachedCharacter.deckSubmodule.GetAllCards().ForEach(card => card.RefreshCardAttributes());
}
public override void Remove()
{
OnBuffRemove();
if (iconSubmodule != null)
{
(attachedCharacter.characterView.hudContainer.enablingHUDs["CharacterBuffCollection"] as HUD_CharacterBuffCollection)
?.RemoveBuffIcon(this);
}
this.attachedCharacter.combatBuffSubmodule.buffList.Remove(this);
attachedCharacter.deckSubmodule.GetAllCards().ForEach(card => card.RefreshCardAttributes());
attachedCharacter.combatBuffSubmodule.buffList.Exclude(this).For(buff => buff.eventSubmodule?.onOtherBuffRemoved.Invoke(this));
}
public override void UntriggerRemove()
{
this.attachedCharacter.combatBuffSubmodule.buffList.Remove(this);
attachedCharacter.deckSubmodule.GetAllCards().ForEach(card => card.RefreshCardAttributes());
}
///
/// 检查并处理专注类Buff的添加逻辑。
/// 如果已有相同Buff存在,则返回true,并通过out参数返回该Buff。
/// 如果没有相同Buff存在,则返回false,并在必要时移除优先级最低的专注类Buff以腾出空间。
///
///
///
public bool FocusingCheck(out CharacterCombatBuffBase existingBuff)
{
if (buffType != BuffType.Focusing)
{
Debug.LogError("FocusingCheck只能用于专注类Buff");
existingBuff = null;
return false;
}
// 移除超出上限的专注类Buff
List focusingBuffs =
attachedCharacter.combatBuffSubmodule.buffList.Where(buff => buff.buffType == BuffType.Focusing).ToList();
focusingBuffs.Sort();
int maximumFocusingBuffAmount = attachedCharacter.GetAttribute("MaximumFocusingBuffAmount", 1);
for (int i = maximumFocusingBuffAmount; i < focusingBuffs.Count; i++)
{
focusingBuffs[i].Remove();
}
// 检查是否已有相同Buff存在
if (FindExistingSameBuff(out existingBuff))
{
return true;
}
// 如果没有相同Buff存在但已达上限,则移除优先级最低的专注类Buff(将由新Buff替代)
if(focusingBuffs.Count >= maximumFocusingBuffAmount)
{
CharacterCombatBuffBase lowestPriorityBuff = focusingBuffs[focusingBuffs.Count - 1];
lowestPriorityBuff.Remove();
}
return false;
}
}
public partial class CharacterCombatBuffBase
{
private void RefreshAttributes()
{
if (coreAttributeSubmodule != null)
{
coreAttributeSubmodule.RefreshAllModifiedAttributes();
}
else
{
generalAttributeSubmodule?.RefreshAllModifiedAttributes();
}
}
}
}