bool CheckTeamEnterBattle(BattleTeam team, bool otherTeamInBattle = false) { bool enteredBattle = team.TryEnterBattle(); if (enteredBattle) { team.PlayAnimation(Animations.FaceUp); } else { team.PlayAnimation(otherTeamInBattle ? Animations.WaitingForBattle : Animations.ShowTeam); } return(enteredBattle); }
IEnumerator BattleCr() { //TODO LIST // Alternate ShowTeam and something else when not in battle? // Stop show team has soon as not idle // // Wait for both teams to enter battle // PlaySound(_Sounds.NewPlayer); bool team1EnterBattle = _team1.Dice.Any(d => d.IsBattling); bool team2EnterBattle = _team2.Dice.Any(d => d.IsBattling); while ((!team1EnterBattle) || (!team2EnterBattle)) { yield return(null); team1EnterBattle = team1EnterBattle || CheckTeamEnterBattle(_team1, team2EnterBattle); team2EnterBattle = team2EnterBattle || CheckTeamEnterBattle(_team2, team1EnterBattle); } PlaySound(_Sounds.NewPlayer); Debug.Log("<color=red>Battle started!</color>"); // Show faces for a little while yield return(new WaitForSecondsRealtime(3)); // And then stop animations _team1.PlayAnimation(Animations.None); _team2.PlayAnimation(Animations.None); yield return(new WaitForSecondsRealtime(1)); // // Pair dice for battle // BattleDie GetHighestValue(IEnumerable <BattleDie> dice) { var dieType = dice.FirstOrDefault(d => d.Die.dieType == Die.DieType.TwentySided)?.Die.dieType ?? Die.DieType.SixSided; return(dice.Where(d => d.Die.dieType == dieType).OrderByDescending(d => d.Value).FirstOrDefault()); } int battleScore = 0; int numPairs = Mathf.Min(_team1.Dice.Count, _team2.Dice.Count); var pairs = new List <System.ValueTuple <BattleDie, BattleDie> >(numPairs); for (int i = 0; i < numPairs; ++i) { // Find new pair (goes with D20 first) var die1 = GetHighestValue(_team1.Dice.Except(pairs.Select(p => p.Item1))); var die2 = GetHighestValue(_team2.Dice.Except(pairs.Select(p => p.Item2))); if ((die1 == null) || (die2 == null)) { break; } // Keep pair (they might be of different type) pairs.Add((die1, die2)); Debug.Log($"<color=green>Duel between {die1} ({die1.Die.dieType}) and {die2} ({die2.Die.dieType})</color>"); die1.PlayAnimation(Animations.Duel); die2.PlayAnimation(Animations.Duel); PlaySound(_Sounds.DuelStarted); yield return(new WaitForSecondsRealtime(2)); // Find winner var winner = die1.Value > die2.Value ? die1 : (die2.Value > die1.Value ? die2 : null); if (winner != null) { var looser = winner == die1 ? die2 : die1; looser.PlayAnimation(Animations.DuelLoose); winner.PlayAnimation(Animations.DuelWin); battleScore += winner == die1 ? 1 : -1; } else { die1.PlayAnimation(Animations.DuelDraw); die2.PlayAnimation(Animations.DuelDraw); } yield return(new WaitForSecondsRealtime(2)); } // // Battle results // // Stop animations _team1.PlayAnimation(Animations.None); _team2.PlayAnimation(Animations.None); yield return(new WaitForSecondsRealtime(1)); if (pairs.Count >= numPairs) { if (battleScore != 0) { var winner = battleScore > 0 ? _team1 : _team2; var looser = winner == _team1 ? _team2 : _team1; Debug.Log($"<color=green>Winner: {winner.Name}</color>"); looser.PlayAnimation(Animations.TeamLoose); winner.PlayAnimation(Animations.TeamWin); } else { Debug.Log("<color=green>Battle draw</color>"); _team1.PlayAnimation(Animations.TeamDraw); _team2.PlayAnimation(Animations.TeamDraw); } PlaySound(_Sounds.GameCompleted); yield return(new WaitForSecondsRealtime(5)); } _battleCr = null; Debug.Log("<color=red>Battle finished!</color>"); }