/// <summary> /// returns true if attacker is winner /// </summary> BattleResult attack(Army defender) { Army attacker = this; int initialAttackerSize = attacker.getSize(); int initialDefenderSize = defender.getSize(); bool attackerWon; BattleResult result; string attackerBonus; var attackerModifier = Army.modifierStrenght.getModifier(attacker, out attackerBonus); string defenderBonus; var defenderModifier = Army.modifierStrenght.getModifier(defender, out defenderBonus); if (attacker.getStrenght(attackerModifier) > defender.getStrenght(defenderModifier)) { attackerWon = true; float winnerLossUnConverted; if (attacker.getStrenght(attackerModifier) > 0f) { winnerLossUnConverted = defender.getStrenght(defenderModifier) * defender.getStrenght(defenderModifier) / attacker.getStrenght(attackerModifier); } else { winnerLossUnConverted = 0f; } int attackerLoss = attacker.takeLossUnconverted(winnerLossUnConverted, defender.owner); int loserLoss = defender.takeLoss(defender.getSize(), attacker.owner); result = new BattleResult(attacker.getOwner(), defender.getOwner(), initialAttackerSize, attackerLoss , initialDefenderSize, loserLoss, attacker.destination, attackerWon, attackerBonus, defenderBonus); } else if (attacker.getStrenght(attackerModifier) == defender.getStrenght(defenderModifier)) { attacker.takeLoss(attacker.getSize(), defender.owner); defender.takeLoss(defender.getSize(), attacker.owner); var r = new BattleResult(attacker.getOwner(), defender.getOwner(), attacker.getSize(), attacker.getSize(), defender.getSize(), defender.getSize(), attacker.destination, false, attackerBonus, defenderBonus); return(r); } else //defender.getStrenght() > attacker.getStrenght() { attackerWon = false; float winnerLossUnConverted; if (defender.getStrenght(defenderModifier) > 0f) { winnerLossUnConverted = attacker.getStrenght(attackerModifier) * attacker.getStrenght(attackerModifier) / (defender.getStrenght(defenderModifier)); } else { winnerLossUnConverted = 0f; } int defenderLoss = defender.takeLossUnconverted(winnerLossUnConverted, attacker.owner); int attackerLoss = attacker.takeLoss(attacker.getSize(), defender.owner); result = new BattleResult(attacker.getOwner(), defender.getOwner(), initialAttackerSize, attackerLoss , initialDefenderSize, defenderLoss, attacker.destination, attackerWon, attackerBonus, defenderBonus); } return(result); }
public static Unit Create(Army army) { var unitObject = GameObject.Instantiate(LinksManager.Get.UnitPrefab, LinksManager.Get.ArmiesHolder.transform); unitObject.name = army.FullName; //(World.GetAllProvinces().Count() + UnityEngine.Random.Range(0, 2000)).ToString(); //army.getOwner()+"'s " unitObject.transform.position = army.Position; var unit = unitObject.GetComponent <Unit>(); unit.Province = army.Province; //unit.Country = army.getOwner().Country; unit.SetUnitShield(army.getOwner().Flag); unit.UpdateUnitShieldText(army.Province); return(unit); }
/// <summary> /// returns true if attacker is winner /// </summary> private BattleResult attack(Army defender) { Army attacker = this; int initialAttackerSize = attacker.getSize(); int initialDefenderSize = defender.getSize(); bool attackerWon; BattleResult result; string attackerBonus; var attackerModifier = modifierStrenght.getModifier(attacker, out attackerBonus); string defenderBonus; var defenderModifier = modifierStrenght.getModifier(defender, out defenderBonus); if (attacker.getStrenght(attackerModifier) > defender.getStrenght(defenderModifier)) { attackerWon = true; float winnerLossUnConverted; if (attacker.getStrenght(attackerModifier) > 0f) { winnerLossUnConverted = defender.getStrenght(defenderModifier) * defender.getStrenght(defenderModifier) / attacker.getStrenght(attackerModifier); } else { winnerLossUnConverted = 0f; } int attackerLoss = attacker.takeLossUnconverted(winnerLossUnConverted, defender.owner); int loserLoss = defender.takeLoss(defender.getSize(), attacker.owner); result = new BattleResult(attacker.getOwner(), defender.getOwner(), initialAttackerSize, attackerLoss , initialDefenderSize, loserLoss, attacker.Province, attackerWon, attackerBonus, defenderBonus); } else if (attacker.getStrenght(attackerModifier) == defender.getStrenght(defenderModifier)) { attacker.takeLoss(attacker.getSize(), defender.owner); defender.takeLoss(defender.getSize(), attacker.owner); result = new BattleResult(attacker.getOwner(), defender.getOwner(), attacker.getSize(), attacker.getSize(), defender.getSize(), defender.getSize(), attacker.Province, false, attackerBonus, defenderBonus); } else // defender win { attackerWon = false; float winnerLossUnConverted; if (defender.getStrenght(defenderModifier) > 0f) { winnerLossUnConverted = attacker.getStrenght(attackerModifier) * attacker.getStrenght(attackerModifier) / (defender.getStrenght(defenderModifier)); } else { winnerLossUnConverted = 0f; } int defenderLoss = defender.takeLossUnconverted(winnerLossUnConverted, attacker.owner); int attackerLoss = attacker.takeLoss(attacker.getSize(), defender.owner); result = new BattleResult(attacker.getOwner(), defender.getOwner(), initialAttackerSize, attackerLoss , initialDefenderSize, defenderLoss, attacker.Province, attackerWon, attackerBonus, defenderBonus); } Game.provincesToRedrawArmies.Add(defender.Province); if (attacker.getSize() == 0) { attacker.owner.KillArmy(attacker); } if (defender.getSize() == 0) { defender.owner.KillArmy(defender); } return(result); }