Example #1
0
        ///
        ///
        ///
        ///

        void UpdateUnits()
        {
            foreach (string faction in factions)
            {
                string[] ignoreFactions = GetFactionsToIgnore(faction);

                foreach (Unit unit in manager.Units)
                {
                    //ignore this unit if it is destroyed
                    if (unit.IsDestroyed)
                    {
                        unit.CheckHide();
                        continue;
                    }

                    //unit has to wait a number of rounds equal to speed before it can take action
                    if (round % unit.Speed != 0)
                    {
                        continue;
                    }

                    Target closestTarget = manager.GetClosestTarget(unit, ignoreFactions);

                    if (faction == WIZARDS)
                    {
                        //wizards only target units
                        closestTarget = manager.GetClosestTarget(unit, ignoreFactions, true, false);
                    }

                    if (closestTarget == null)
                    {
                        //if a unit has no target it means the game has ended
                        gameOver = true;
                        winning  = unit.Faction;

                        map.UpdateMap(manager);
                        return;
                    }

                    double healthPercentage = unit.Health / (double)unit.MaxHealth;
                    bool   isWizard         = unit is WizardUnit;

                    //if not a wizard, run away if health is below 25%
                    if (healthPercentage <= 0.25 && !isWizard)
                    { //if health is 25 percent and not wizard, RUN AWAY
                        unit.Run();
                    }
                    //if a wizard and health is below 50%, run away
                    else if (healthPercentage <= 0.5 && isWizard)
                    { //if health 50 and is wizard, RUN BOY RUUUUUUUUN
                        unit.Run();
                    }
                    //if target is in range and this unit is not a wizard, attack closest target
                    else if (unit.IsInRange(closestTarget) && !isWizard)
                    {
                        if (unit.Attack(closestTarget))
                        {
                            //if killed unit, add to resource pool of all faction resource buildings
                            AddToResourcePoolByFaction(unit.Faction); //pass faction name, adds 1 resource to each building of
                            //cont. that faction
                        }
                    }
                    //if target is in range and  this unit is a wizard, attack all targets in range
                    else if (unit.IsInRange(closestTarget) && isWizard)
                    { //finds which units fall in wizard's square, so that he knows
                        //cont. if he can attack them (finds all possible targets, if it is not destroyed and x y cords are gucci
                        //find all units close to the target we are attacking
                        foreach (Target targetInArea in manager.GetTargetsInArea(closestTarget, ignoreFactions, true, false))
                        {
                            unit.Attack(targetInArea);
                        }
                    }
                    //if none of the above just move the unit
                    else
                    {
                        unit.Move(closestTarget); //if nothing else, just move, whilst staying in bounds
                    }

                    StayInBounds(unit, map.width, map.height);
                }
            }
        }