/// <summary> /// Runs in a state loop until either the battle is lost or won. /// It will cycle thought each units turn from friendly unit to enemy unit. /// If a unit is missing or dead it will skip that unit and go to the next. /// </summary> /// <returns>Not sure.</returns> private IEnumerator Battle() { bool ongoingBattle = true; while (ongoingBattle) { CheckForUnitsWithDot(); switch (state) { case BattleState.START: SetupBattle(); yield return(new WaitForSeconds(2f)); state = BattleState.FRIENDLY1TURN; break; case BattleState.FRIENDLY1TURN: unitsTurn = friendlyUnit1; BattleHUD.SetBattleStationToDark(enemy3BattleStation); BattleHUD.SetBattleStationToLight(friendly1BattleStation); BattleHUD.showPortrait(unitsTurn); if (!unitsTurn.isDead) { yield return(FriendlyTurn()); } if (IsAllEnemiesDead()) { state = BattleState.WON; } else { state = BattleState.ENEMY1TURN; } break; case BattleState.ENEMY1TURN: unitsTurn = enemyUnit1; BattleHUD.SetBattleStationToDark(friendly1BattleStation); BattleHUD.SetBattleStationToLight(enemy1BattleStation); BattleHUD.showPortrait(unitsTurn); if (!unitsTurn.isDead) { yield return(DoEnemyAI()); } if (IsAllFriendlyDead()) { state = BattleState.LOST; } else { state = BattleState.FRIENDLY2TURN; } break; case BattleState.FRIENDLY2TURN: unitsTurn = friendlyUnit2; BattleHUD.SetBattleStationToDark(enemy1BattleStation); BattleHUD.SetBattleStationToLight(friendly2BattleStation); BattleHUD.showPortrait(unitsTurn); if (!unitsTurn.isDead) { yield return(FriendlyTurn()); } if (IsAllEnemiesDead()) { state = BattleState.WON; } else { state = BattleState.ENEMY2TURN; } break; case BattleState.ENEMY2TURN: unitsTurn = enemyUnit2; BattleHUD.SetBattleStationToDark(friendly2BattleStation); BattleHUD.SetBattleStationToLight(enemy2BattleStation); BattleHUD.showPortrait(unitsTurn); if (!unitsTurn.isDead) { yield return(DoEnemyAI()); } if (IsAllFriendlyDead()) { state = BattleState.LOST; } else { state = BattleState.FRIENDLY3TURN; } break; case BattleState.FRIENDLY3TURN: unitsTurn = friendlyUnit3; BattleHUD.SetBattleStationToDark(enemy2BattleStation); BattleHUD.SetBattleStationToLight(friendly3BattleStation); BattleHUD.showPortrait(unitsTurn); if (!unitsTurn.isDead) { yield return(FriendlyTurn()); } if (IsAllEnemiesDead()) { state = BattleState.WON; } else { state = BattleState.ENEMY3TURN; } break; case BattleState.ENEMY3TURN: unitsTurn = enemyUnit3; BattleHUD.SetBattleStationToDark(friendly3BattleStation); BattleHUD.SetBattleStationToLight(enemy3BattleStation); BattleHUD.showPortrait(unitsTurn); if (!unitsTurn.isDead) { yield return(DoEnemyAI()); } if (IsAllFriendlyDead()) { state = BattleState.LOST; } else { state = BattleState.FRIENDLY1TURN; } break; case BattleState.LOST: EnableDisableAttackButtons(false); EnableDisableTargetButtons(false); ongoingBattle = false; EndBattle(); break; case BattleState.WON: EnableDisableAttackButtons(false); EnableDisableTargetButtons(false); ongoingBattle = false; EndBattle(); break; default: ongoingBattle = false; break; } } }