Esempio n. 1
0
        private void ResolveBorderBattles()
        {
            List <Battle> newBattles = new List <Battle>();

            foreach (BorderBattle battle in Battles[Turn].Where(x => x.Type == BattleType.BorderBattle))
            {
                foreach (VisualArmy va in battle.Armies.Select(x => x.VisualArmy))
                {
                    Destroy(va.gameObject);
                }

                if (battle.SupportOnly)
                {
                    // Continue the armies to their target when it's just support
                    foreach (Army army in battle.Armies)
                    {
                        TerritoryBattle targetBattle = (TerritoryBattle)Battles[Turn].Where(x => x.Type == BattleType.TerritoryBattle && x.Territories.Contains(army.TargetTerritory)).FirstOrDefault();
                        if (targetBattle != null)
                        {
                            targetBattle.AddArmyFromBorderBattle(ArmyPrefab, army);
                        }
                        else
                        {
                            TerritoryBattle newTerritoryBattle = new TerritoryBattle(ArmyPrefab, Turn, new List <Army>());
                            newTerritoryBattle.AddArmyFromBorderBattle(ArmyPrefab, army);
                            newBattles.Add(newTerritoryBattle);
                        }
                    }
                }

                else
                {
                    // Fight
                    battle.ResolveBattle();

                    // Create new army for winner
                    Territory sourceTerritory      = battle.Territories.First(x => x.Player == battle.Winner);
                    Territory targetTerritory      = battle.Territories.First(x => x.Player == battle.Winner);
                    Player    winner               = battle.Winner;
                    Player    loser                = battle.Players.First(x => x != battle.Winner);
                    Army      armyFromBorderBattle = new Army(Turn, battle.WinnerSourceTerritory, battle.WinnerTargetTerritory, battle.Winner, battle.Losers[0], battle.WinnerTroopsRemaining, battle.IsWaterBattle);

                    // Add winner army to existing territory battle (if exists) or create new territory battle
                    TerritoryBattle targetBattle = (TerritoryBattle)Battles[Turn].Where(x => x.Type == BattleType.TerritoryBattle && x.Territories.Contains(battle.WinnerTargetTerritory)).FirstOrDefault();
                    if (targetBattle != null)
                    {
                        targetBattle.AddArmyFromBorderBattle(ArmyPrefab, armyFromBorderBattle);
                    }
                    else
                    {
                        TerritoryBattle newTerritoryBattle = new TerritoryBattle(ArmyPrefab, Turn, new List <Army>());
                        newTerritoryBattle.AddArmyFromBorderBattle(ArmyPrefab, armyFromBorderBattle);
                        newBattles.Add(newTerritoryBattle);
                    }
                }
            }
            Battles[Turn].AddRange(newBattles);
        }
Esempio n. 2
0
        private void StartCombatPhase()
        {
            Debug.Log("Combat Phase started");
            // Border battles
            List <Army> borderFightArmies = new List <Army>();

            foreach (Army army in TroopMovements[Turn])
            {
                Army mirrorMovement = TroopMovements[Turn].Where(x => x.SourceTerritory == army.TargetTerritory && x.TargetTerritory == army.SourceTerritory && !borderFightArmies.Contains(x)).FirstOrDefault();
                if (mirrorMovement != null)
                {
                    borderFightArmies.Add(army);
                    borderFightArmies.Add(mirrorMovement);
                    BorderBattle borderBattle = new BorderBattle(ArmyPrefab, Turn, new List <Army>()
                    {
                        army, mirrorMovement
                    });
                    Battles[Turn].Add(borderBattle);
                }
            }

            // Territory
            Dictionary <Territory, List <Army> > territoryBattles = new Dictionary <Territory, List <Army> >();

            foreach (Army army in TroopMovements[Turn].Where(x => !borderFightArmies.Contains(x)))
            {
                if (!territoryBattles.ContainsKey(army.TargetTerritory))
                {
                    territoryBattles.Add(army.TargetTerritory, new List <Army>());
                }
                territoryBattles[army.TargetTerritory].Add(army);
            }
            foreach (KeyValuePair <Territory, List <Army> > kvp in territoryBattles)
            {
                Battle territoryBattle = new TerritoryBattle(ArmyPrefab, Turn, kvp.Value);
                Battles[Turn].Add(territoryBattle);
            }

            Invoke(nameof(ResolveBorderBattles), ArmyApproachTime);
            Invoke(nameof(ResolveTerritoryBattles), ArmyApproachTime * 2);
            Invoke(nameof(EndCombatPhase), ArmyApproachTime * 2);

            State = GameState.CombatPhase;
        }