diff --git a/Assets/Battle/BattleController.cs b/Assets/Battle/BattleController.cs index a7c461f..e6c2848 100644 --- a/Assets/Battle/BattleController.cs +++ b/Assets/Battle/BattleController.cs @@ -30,8 +30,11 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe private HeroInfoUI[] monsterUIs; private bool heroTurn; + private bool heroPassedTurn; private int maxEnergy; private int energy; + private int maxMEnergy; + private int mEnergy; void Awake() { this.heroCardDatabase = new HeroCardDatabase(); @@ -113,7 +116,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe // Start is called before the first frame update void Start() { - StartHeroTurn(); + StartCoroutine(RunBattleRounds()); } // Update is called once per frame @@ -121,37 +124,66 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe { } - private void StartHeroTurn() { + private IEnumerator RunBattleRounds() { + while (true) { + yield return RunBattleRound(); + } + } + + private IEnumerator RunBattleRound() { this.heroTurn = true; this.damageTriggers.RemoveAll(t => t.Expiration == TriggerExpiration.StartOfNextHeroTurn); this.energy = this.maxEnergy; - foreach (BattleHero hero in this.heroes) { - hero.ResetBlock(); - } + this.mEnergy = this.maxMEnergy; this.heroDeck.DrawHand(); - this.battlePlayerControls.NotifyHandPileChanged(); - + this.monsterDeck.DrawHand(); + + yield return RunHeroTurn(); + + this.heroTurn = false; + yield return RunMonsterTurn(); + } + + private IEnumerator RunHeroTurn() { + // Start of hero turn if (this.maxEnergy < 9) { this.maxEnergy++; this.energy++; } + foreach (BattleHero hero in this.heroes) { + hero.ResetBlock(); + } + + // Heroes act until they pas + while (!this.heroPassedTurn) { + yield return null; + } + this.heroPassedTurn = false; + + // End of hero turn + this.heroDeck.DiscardHand(); } - private void StartEnemyTurn() { - this.heroTurn = false; - - this.heroDeck.DiscardHand(); - + private IEnumerator RunMonsterTurn() { + // Start of monster turn + if (this.maxMEnergy < 9) { + this.maxMEnergy++; + this.mEnergy++; + } foreach (BattleMonster monster in this.monsters) { monster.ResetBlock(); } - MonsterCard card = this.monsterCardDatabase.CreateCard((int) MCardId.VampireFangs, this.monsters[0]); - StartCoroutine(UseMonsterCard(card)); - - StartHeroTurn(); + // Start of monster turn + List monsterHand = this.monsterDeck.GetHandPile(); + for (int i = 0; i < monsterHand.Count; i++) { + MonsterCard monsterCard = monsterHand[i]; + if (CanUseMonsterCard(monsterCard)) { + yield return UseMonsterCard(monsterCard); + } + } } public void NotifyHeroCardUsed(HeroCard card) { @@ -163,7 +195,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe public IEnumerator UseHeroCard(HeroCard card) { yield return card.Use(this, battlePlayerControls); - PayCardCost(card); + PayHeroCardCost(card); this.heroDeck.RemoveCardFromHand(card); if (card.IsPermanent()) { this.playArea.AddPlayedHeroPermanent(card); @@ -174,12 +206,18 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe public IEnumerator UseMonsterCard(MonsterCard card) { yield return card.Use(this, this); - // TODO: Manage monster deck. + PayMonsterCardCost(card); + this.monsterDeck.RemoveCardFromHand(card); + if (card.IsPermanent()) { + this.playArea.AddPlayedMonsterPermanent(card); + } else { + this.monsterDeck.MoveCardToDiscard(card); + } } public void NotifyHeroTurnPassed() { if (this.heroTurn) { - StartEnemyTurn(); + this.heroPassedTurn = true; } } @@ -187,10 +225,18 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe return this.heroTurn && this.energy >= card.GetCost(); } - private void PayCardCost(HeroCard card) { + public bool CanUseMonsterCard(MonsterCard card) { + return !this.heroTurn && this.mEnergy >= card.GetCost(); + } + + private void PayHeroCardCost(HeroCard card) { this.energy -= card.GetCost(); } + private void PayMonsterCardCost(MonsterCard card) { + this.mEnergy -= card.GetCost(); + } + public int GetEnergy() { return this.energy; } diff --git a/Assets/Battle/BattlePlayerControls.cs b/Assets/Battle/BattlePlayerControls.cs index 444d5c0..b8f82e6 100644 --- a/Assets/Battle/BattlePlayerControls.cs +++ b/Assets/Battle/BattlePlayerControls.cs @@ -6,6 +6,8 @@ using UnityEngine.UI; public class BattlePlayerControls : MonoBehaviour { + // TODO: Draw enemy permanents + public BattleController battleController; public GameObject uiCanvas; public GameObject cardPrefab; diff --git a/Assets/Battle/PlayArea.cs b/Assets/Battle/PlayArea.cs index aa3eb0a..38db77a 100644 --- a/Assets/Battle/PlayArea.cs +++ b/Assets/Battle/PlayArea.cs @@ -9,9 +9,11 @@ public class PlayArea public event PlayAreaChangedAction OnPlayAreaChanged; private List heroPermanents; + private List monsterPermanents; public PlayArea() { this.heroPermanents = new List(); + this.monsterPermanents = new List(); } public List GetHeroPermanents() { @@ -28,6 +30,16 @@ public class PlayArea TriggerPlayAreaChanged(); } + public void AddPlayedMonsterPermanent(BaseCard permanent) { + this.monsterPermanents.Add(permanent); + TriggerPlayAreaChanged(); + } + + public void RemoveMonsterPermanent(BaseCard permanent) { + this.monsterPermanents.Remove(permanent); + TriggerPlayAreaChanged(); + } + private void TriggerPlayAreaChanged() { if (OnPlayAreaChanged != null) OnPlayAreaChanged(); } diff --git a/TODO.txt b/TODO.txt index a953cd0..889c22d 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,3 +1,2 @@ -- Control flow of battle with state machine or coroutines - Make battle end - Do not allow dead heroes to act \ No newline at end of file