From 45b91ed3522da9544c0d0658d01e080175bd78c7 Mon Sep 17 00:00:00 2001 From: henriquenakashima Date: Thu, 24 Sep 2020 23:00:36 -0400 Subject: [PATCH] Mark cards that are not playable --- Assets/Battle/BattleController.cs | 32 +++++++++++---- Assets/Battle/BattlePlayerControls.cs | 9 +++-- Assets/Battle/BattleState.cs | 21 ++++++++++ Assets/Battle/BattleState.cs.meta | 11 +++++ Assets/Battle/HandControls.cs | 29 ++++++++++++-- Assets/Battle/HandPlayability.cs | 58 +++++++++++++++++++++++++++ Assets/Battle/HandPlayability.cs.meta | 11 +++++ Assets/Battle/UI/CardUI.cs | 4 ++ TODO.txt | 1 + 9 files changed, 162 insertions(+), 14 deletions(-) create mode 100644 Assets/Battle/BattleState.cs create mode 100644 Assets/Battle/BattleState.cs.meta create mode 100644 Assets/Battle/HandPlayability.cs create mode 100644 Assets/Battle/HandPlayability.cs.meta diff --git a/Assets/Battle/BattleController.cs b/Assets/Battle/BattleController.cs index ae9ff56..c654245 100644 --- a/Assets/Battle/BattleController.cs +++ b/Assets/Battle/BattleController.cs @@ -27,11 +27,15 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe private Dictionary monsterSprites; private Dictionary monsterUIs; - private bool heroTurn; + private BattleState state; private bool heroPassedTurn; private EnergyPool heroEnergy; private EnergyPool monsterEnergy; private InsightLevel insightLevel; + private HandPlayability heroHandPlayability; + + private delegate void BattleStateChangedAction(); + private event BattleStateChangedAction OnBattleStateChanged; void Awake() { HeroId[] heroIds = new HeroId[3] { @@ -93,6 +97,8 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe this.heroCardDatabase = new HeroCardDatabase(); this.monsterCardDatabase = new MonsterCardDatabase(); + this.state = new BattleState(); + this.heroEnergy = new EnergyPool(); this.heroEnergyUI.Init(this.heroEnergy); this.heroEnergy.OnEnergyChanged += this.heroEnergyUI.NotifyEnergyChanged; @@ -169,14 +175,18 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe this.monsterCardDatabase, mCardIds.Cast().ToArray(), mCardOwners); this.playArea = new PlayArea(); + this.heroHandPlayability = new HandPlayability( + this.state, this.heroEnergy, TeamChoice.Heroes, this.heroDeck); this.battlePlayerControls.Init( - this.heroDeck, this.monsterDeck, this.playArea, this.insightLevel); + this.heroDeck, this.monsterDeck, this.playArea, this.insightLevel, + this.heroHandPlayability); this.battlePlayerControls.OnCardUsed += this.NotifyHeroCardUsed; this.battlePlayerControls.OnTurnPassed += this.NotifyHeroTurnPassed; this.insightLevel.OnChanged += this.battlePlayerControls.NotifyInsightLevelChanged; this.heroDeck.OnHandPileChanged += this.battlePlayerControls.NotifyHeroHandPileChanged; this.monsterDeck.OnHandPileChanged += this.battlePlayerControls.NotifyMonsterHandPileChanged; + this.OnBattleStateChanged += this.heroHandPlayability.NotifyBattleStateChanged; } private void PositionSprite(TeamChoice team, GameObject sprite, int i) { @@ -214,7 +224,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe } private IEnumerator RunBattleRound() { - this.heroTurn = true; + this.state.HeroTurn = true; this.damageTriggers.RemoveAll(t => t.Expiration == TriggerExpiration.StartOfNextHeroTurn); @@ -225,12 +235,13 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe yield return RunHeroTurn(); - this.heroTurn = false; + this.state.HeroTurn = false; yield return RunMonsterTurn(); } private IEnumerator RunHeroTurn() { // Start of hero turn + OnBattleStateChanged(); // TODO: Play emotions from hand if (this.heroEnergy.MaxEnergy < 9) { @@ -239,6 +250,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe foreach (BattleHero hero in this.heroes) { hero.ResetBlock(); } + OnBattleStateChanged(); // Heroes act until they pass while (!this.heroPassedTurn) { @@ -251,9 +263,11 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe foreach (BattleHero hero in this.heroes) { hero.TickStatuses(); } + OnBattleStateChanged(); } private IEnumerator RunMonsterTurn() { + OnBattleStateChanged(); yield return new WaitForSeconds(0.5f); // Start of monster turn @@ -263,6 +277,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe foreach (BattleMonster monster in this.monsters) { monster.ResetBlock(); } + OnBattleStateChanged(); // Monsters act until they pass List monsterHand = this.monsterDeck.GetHandPile(); @@ -270,6 +285,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe MonsterCard monsterCard = monsterHand[i]; if (CanUseMonsterCard(monsterCard)) { yield return UseMonsterCard(monsterCard); + OnBattleStateChanged(); } } @@ -278,6 +294,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe foreach (BattleMonster monster in this.monsters) { monster.TickStatuses(); } + OnBattleStateChanged(); } public void NotifyHeroCardUsed(HeroCard card) { @@ -298,6 +315,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe } yield return new WaitForSeconds(0.5f); + OnBattleStateChanged(); CheckState(); } @@ -316,17 +334,17 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe } public void NotifyHeroTurnPassed() { - if (this.heroTurn) { + if (this.state.HeroTurn) { this.heroPassedTurn = true; } } public bool CanUseHeroCard(HeroCard card) { - return this.heroTurn && card.Owner.CanUseCard(card) && this.heroEnergy.CanPayForCard(card); + return this.heroHandPlayability.IsPlayable(card); } public bool CanUseMonsterCard(MonsterCard card) { - return !this.heroTurn && card.Owner.CanUseCard(card) && this.monsterEnergy.CanPayForCard(card); + return !this.state.HeroTurn && card.Owner.CanUseCard(card) && this.monsterEnergy.CanPayForCard(card); } public BattleDeck GetHeroDeck() { diff --git a/Assets/Battle/BattlePlayerControls.cs b/Assets/Battle/BattlePlayerControls.cs index 94b41e6..1556e08 100644 --- a/Assets/Battle/BattlePlayerControls.cs +++ b/Assets/Battle/BattlePlayerControls.cs @@ -28,15 +28,18 @@ public class BattlePlayerControls : MonoBehaviour public void Init( BattleDeck heroDeck, BattleDeck monsterDeck, PlayArea playArea, - InsightLevel insightLevel) { + InsightLevel insightLevel, HandPlayability heroHandPlayability) { this.heroDeck = heroDeck; this.monsterDeck = monsterDeck; this.playArea = playArea; this.insightLevel = insightLevel; - this.heroHandControls = new HandControls(this.uiCanvas, this.cardPrefab, HandControlsPosition.Bottom, int.MaxValue); + this.heroHandControls = new HandControls( + this.uiCanvas, this.cardPrefab, HandControlsPosition.Bottom, TeamChoice.Heroes, + int.MaxValue, heroHandPlayability); this.heroHandControls.OnCardClicked += OnHeroCardClicked; - this.monsterHandControls = new HandControls(this.uiCanvas, this.cardPrefab, HandControlsPosition.Top, 1); + this.monsterHandControls = new HandControls( + this.uiCanvas, this.cardPrefab, HandControlsPosition.Top, TeamChoice.Monsters, 1, null); this.monsterHandControls.OnCardClicked += OnMonsterCardClicked; this.cardsInPlayUIs = new Dictionary(); diff --git a/Assets/Battle/BattleState.cs b/Assets/Battle/BattleState.cs new file mode 100644 index 0000000..1503084 --- /dev/null +++ b/Assets/Battle/BattleState.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class BattleState +{ +// public delegate void BattleStateChangedAction(); +// public event BattleStateChangedAction OnBattleStateChanged; + private bool heroTurn; + + public bool IsTeamsTurn(TeamChoice team) { + if (team == TeamChoice.Heroes) { + return this.HeroTurn; + } else { + return !this.HeroTurn; + } + } + + public bool HeroTurn {get => this.heroTurn; set => this.heroTurn = value;} +} diff --git a/Assets/Battle/BattleState.cs.meta b/Assets/Battle/BattleState.cs.meta new file mode 100644 index 0000000..787373e --- /dev/null +++ b/Assets/Battle/BattleState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0a5826793e0ac0241a996d98d1a4826d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Battle/HandControls.cs b/Assets/Battle/HandControls.cs index 270e102..e8dc628 100644 --- a/Assets/Battle/HandControls.cs +++ b/Assets/Battle/HandControls.cs @@ -20,19 +20,27 @@ public class HandControls where BaseCardT : BaseCard private List hand; private List cardsInHandUIs; + private TeamChoice team; private int insightLevel; + private HandPlayability playability; public HandControls(GameObject uiCanvas, GameObject cardPrefab, HandControlsPosition position, - int insightLevel) { + TeamChoice team, int insightLevel, HandPlayability playability) { this.uiCanvas = uiCanvas; this.cardPrefab = cardPrefab; this.position = position; + this.team = team; this.insightLevel = insightLevel; + this.playability = playability; + + if (playability != null) { + playability.OnPlayabilityChanged += UpdateCardPlayability; + } } public void SetHand(List hand) { this.hand = hand; - DrawHand(); + RenderHand(); } private Vector3 CalculateHandCardPosition(int index, int count) { @@ -51,11 +59,11 @@ public class HandControls where BaseCardT : BaseCard get => this.insightLevel; set { this.insightLevel = value; - DrawHand(); + RenderHand(); } } - private void DrawHand() { + private void RenderHand() { if (this.hand == null) { return; } @@ -81,5 +89,18 @@ public class HandControls where BaseCardT : BaseCard int cardIndex = i; cardUI.GetComponent