mixedcards/Assets/Battle/HandPlayability.cs
2020-09-26 18:24:46 -04:00

65 lines
2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class HandPlayability<BaseCardT> where BaseCardT : BaseCard {
public delegate void HandPlayabilityChangedAction();
public event HandPlayabilityChangedAction OnPlayabilityChanged;
private BattleState battleState;
private EnergyPool energyPool;
private TeamChoice team;
private BattleDeck<BaseCardT> deck;
private Dictionary<BaseCardT, bool> playable = new Dictionary<BaseCardT, bool>();
public HandPlayability(BattleState battleState, EnergyPool energyPool, TeamChoice team,
BattleDeck<BaseCardT> deck) {
this.battleState = battleState;
this.energyPool = energyPool;
this.team = team;
this.deck = deck;
deck.OnHandPileChanged += NotifyHeroHandPileChanged;
}
public void NotifyBattleStateChanged() {
UpdatePlayability();
}
public bool IsPlayable(BaseCardT card) {
bool value;
if (!this.playable.TryGetValue(card, out value)) {
Debug.Log("Card " + card.GetDisplayName() + " not playable because not in hand!");
return false;
}
return value;
}
private void TriggerOnPlayabilityChanged() {
if (this.OnPlayabilityChanged != null) {
this.OnPlayabilityChanged();
}
}
private void NotifyHeroHandPileChanged() {
UpdatePlayability();
}
private void UpdatePlayability() {
this.playable.Clear();
foreach (BaseCardT card in this.deck.GetHandPile()) {
bool canPlay;
if (card.Type == CardType.Emotion) {
canPlay = this.battleState.CanPlayEmotion(this.team);
} else {
canPlay = this.energyPool.CanPayForCard(card);
}
canPlay = canPlay
&& this.battleState.IsTeamsTurn(this.team)
&& card.Owner.CanUseCard(card);
this.playable[card] = canPlay;
}
TriggerOnPlayabilityChanged();
}
}