/// <summary> /// Called when a unit is to enter/attack this tower. /// See documenation for full explaination and metrics. /// If unit is friendly, stationed units is increased. /// Raises ChangedFaction and AttackedByUnit events. /// </summary> /// <param name="unit">Unit.</param> public void UnitEntered(UnitBehavior unit) { if (unit.Faction == Faction) { // Friendly units are transfered to this towers group unit.TransferGroup(stationedGroup); unit.gameObject.SetActive(false); } else { // Hostile units damage this towers stationed unit group int strength = FactionController.GetAttackStrengthForFaction(unit.Faction); stationedGroup.Damage(strength); // Change tower's faction if last unit was killed if (stationedGroup.Empty) { TowerController.ConvertTowerToFaction(this, unit.Faction); if (ChangedFaction != null) { ChangedFaction(Faction); SetGraphic(); } } unit.ImpactKill(); if (AttackedByUnit != null) { AttackedByUnit(unit); } } }
/// <summary> /// Returns the Number of Units Needed to successfully defend a tower /// </summary> public static int NumUnitsNeededToDefend(TowerBehavior defendingTower, int attackingFaction, int numAttackingUnits) { int strength = FactionController.GetAttackStrengthForFaction(attackingFaction); int unitsNeeded = 0; int endurance = defendingTower.StationedGroup.Endurance; for (int i = 0; i < numAttackingUnits; i++) { endurance -= strength; if (endurance <= 0) { unitsNeeded++; endurance += defendingTower.CurrentStats.Endurance; } } return(unitsNeeded + 1); }
/// <summary> /// Returns True if the attacking Units will take over a tower /// </summary> public static bool SimulateAttack(int numUnitsAttacking, TowerBehavior destination, int AttackingFaction, out int UnitsLeft) { UnitsLeft = numUnitsAttacking; //how many stationUnits does the destination have int defendingUnits = destination.StationedUnits; //what is the current endurance of the destination tower int currentEndurance = destination.StationedGroup.Endurance; //strength of soldiers int strength = FactionController.GetAttackStrengthForFaction(AttackingFaction); //Simulate attacking for (int i = 0; i < numUnitsAttacking; i++) { currentEndurance -= strength; UnitsLeft--; if (currentEndurance <= 0) { currentEndurance += destination.CurrentStats.Endurance; defendingUnits--; } //if the player is out of units stop the loop if (defendingUnits <= 0) { break; } } //if the there are defending Units leftover, we have failed the attack if (defendingUnits > 0) { return(false); } //we have succeeded the attack in defending Units is 0 or less return(true); }