//function is called when the UnitsMoved event is fired private void TowerHasAttack(MovedUnitsInfo attackInfo) { //check to see if the tower being attacked is the AI and the attacking tower is the player if (attackInfo.FromFaction != FactionController.OtherFaction1 && attackInfo.ToFaction == FactionController.OtherFaction1) { //Check if the enemy units can even reach this tower, if (SimulateAttackUtil.DistanceCheck(attackInfo.From, attackInfo.To) == false) { return; } //Check if the enemy units can even take over the tower int EnemyUnitsLeft; if (SimulateAttackUtil.SimulateAttack(attackInfo.NumberOfUnits, attackInfo.To, attackInfo.FromFaction, out EnemyUnitsLeft) == false) { return; } //random check to see if the Ai will defend it float rand = Random.Range(0f, 100f); if (rand <= AIGameStateManager.PercentChanceAIWontDefend) { return; } //if we make it through the check, then Defend Defend(attackInfo, EnemyUnitsLeft); } }
// Replaced MoveUnits /// <summary> /// Sets a unit group's unit's to move toward a destination tower from an origin tower. /// </summary> /// <param name="group">Unit group to move.</param> /// <param name="origin">Tower were group should move from.</param> /// <param name="destination">Tower were group will move toward.</param> public static void MoveGroupFromTo(UnitGroup group, TowerBehavior origin, TowerBehavior destination) { origin.stationedGroup.SubtractUnits(group.UnitCount); group.MoveUnits(destination); var info = new MovedUnitsInfo(origin, destination, group.UnitCount); if (UnitsMoved != null) { UnitsMoved(info); } }
private void Defend(MovedUnitsInfo info, int enemyUnitsLeft) { //if none of the towers are able to make the distance to the AI. List <AIBehavior> friendlyTowers = closestFriendlyTowers(info.To); //Check if we even have towers to use to defend if (friendlyTowers == null || friendlyTowers.Count == 0) { return; } Dictionary <AIBehavior, int> helpDefending = new Dictionary <AIBehavior, int>(); int totalDefendingUnits = info.To.StationedUnits; int numUnitsNeededToDefend = SimulateAttackUtil.NumUnitsNeededToDefend(info.To, info.FromFaction, info.NumberOfUnits); for (int i = 0; i < (maxNumberTowersCanDefend <= friendlyTowers.Count ? maxNumberTowersCanDefend : friendlyTowers.Count); i++) { int additionalUnits = friendlyTowers[0].myTower.StationedUnits; //even though this has some extra steps for if condition is equal //but figured it would be better than writing out another if statement if (totalDefendingUnits + additionalUnits >= numUnitsNeededToDefend) { additionalUnits -= ((totalDefendingUnits + additionalUnits) - numUnitsNeededToDefend); helpDefending.Add(friendlyTowers[i], additionalUnits); totalDefendingUnits += additionalUnits; break; } else { helpDefending.Add(friendlyTowers[i], additionalUnits); totalDefendingUnits += additionalUnits; } } if (totalDefendingUnits >= numUnitsNeededToDefend && helpDefending.Count > 0) { foreach (KeyValuePair <AIBehavior, int> pair in helpDefending) { pair.Key.SetMyTimer(AIController.GetTimer()); //find the percent of Soldiers we are attacking float percent = AIConstants.RoundPercentToClosestOption((float)pair.Value / (float)pair.Key.myTower.StationedUnits); //we don't really need to do anything with the bool from this, it will defend as intended or nothing will happen //we don't need to do anything if the start attack fails pair.Key.StartAttack(info.To, percent, 1f); } } }
private void TowerBehavior_UnitsMoved(MovedUnitsInfo obj) { //Debug.Log("Units Moved From " + obj.From.Index + " to " + obj.To.Index); }
void OnUnitsMoved(MovedUnitsInfo info) { UIController.DisableHelpUI(); }