Beispiel #1
0
        //Updates the map to display the units' movement, combat and deaths
        public void Update()
        {
            for (int i = 0; i < map.Units.Count; i++)
            {
                if (map.Units[i] is MeleeUnit)
                {
                    MeleeUnit mu = (MeleeUnit)map.Units[i];
                    if (mu.Health == 0)
                    {
                        mu.Move(-1);
                    }
                    else if (mu.Health <= mu.MaxHealth * 0.25) // Running Away
                    {
                        mu.Move(r.Next(0, 4));
                    }
                    else
                    {
                        (Unit closest, int distanceTo) = mu.Closest(map.Units);
                        (Building buildingclosest, int distanceToBuilding) = mu.BuildingClosest(map.Buildings);

                        if (distanceTo > distanceToBuilding)
                        {
                            //Check In Range
                            if (distanceTo <= mu.AttackRange)
                            {
                                mu.IsAttacking = true;
                                mu.Combat(closest);
                            }
                            else //Move Towards
                            {
                                if (closest is MeleeUnit)
                                {
                                    MeleeUnit closestMu = (MeleeUnit)closest;
                                    if (mu.XPos > closestMu.XPos) //North
                                    {
                                        mu.Move(0);
                                    }
                                    else if (mu.XPos < closestMu.XPos) //South
                                    {
                                        mu.Move(2);
                                    }
                                    else if (mu.YPos > closestMu.YPos) //West
                                    {
                                        mu.Move(3);
                                    }
                                    else if (mu.YPos < closestMu.YPos) //East
                                    {
                                        mu.Move(1);
                                    }
                                }
                                else if (closest is RangedUnit)
                                {
                                    RangedUnit closestRu = (RangedUnit)closest;
                                    if (mu.XPos > closestRu.XPos) //North
                                    {
                                        mu.Move(0);
                                    }
                                    else if (mu.XPos < closestRu.XPos) //South
                                    {
                                        mu.Move(2);
                                    }
                                    else if (mu.YPos > closestRu.YPos) //West
                                    {
                                        mu.Move(3);
                                    }
                                    else if (mu.YPos < closestRu.YPos) //East
                                    {
                                        mu.Move(1);
                                    }
                                }
                            }
                        }
                        else
                        {
                            //Check In Range
                            if (distanceTo <= mu.AttackRange)
                            {
                                mu.IsAttacking = true;
                                mu.Combat(closest);
                            }
                            else //Move Towards
                            {
                                if (buildingclosest is FactoryBuilding)
                                {
                                    FactoryBuilding closestMu = (FactoryBuilding)buildingclosest;
                                    if (mu.XPos > closestMu.XPos) //North
                                    {
                                        mu.Move(0);
                                    }
                                    else if (mu.XPos < closestMu.XPos) //South
                                    {
                                        mu.Move(2);
                                    }
                                    else if (mu.YPos > closestMu.YPos) //West
                                    {
                                        mu.Move(3);
                                    }
                                    else if (mu.YPos < closestMu.YPos) //East
                                    {
                                        mu.Move(1);
                                    }
                                }
                                else if (buildingclosest is ResourceBuilding)
                                {
                                    ResourceBuilding closestRu = (ResourceBuilding)buildingclosest;
                                    if (mu.XPos > closestRu.XPos) //North
                                    {
                                        mu.Move(0);
                                    }
                                    else if (mu.XPos < closestRu.XPos) //South
                                    {
                                        mu.Move(2);
                                    }
                                    else if (mu.YPos > closestRu.YPos) //West
                                    {
                                        mu.Move(3);
                                    }
                                    else if (mu.YPos < closestRu.YPos) //East
                                    {
                                        mu.Move(1);
                                    }
                                }
                            }
                        }
                    }
                }
                else if (map.Units[i] is RangedUnit)
                {
                    RangedUnit ru = (RangedUnit)map.Units[i];
                    if (ru.Health == 0)
                    {
                        ru.Move(-1);
                    }
                    else if (ru.Health <= ru.MaxHealth * 0.25)
                    {
                        ru.Move(r.Next(0, 4));
                    }
                    else
                    {
                        (Unit closest, int distanceTo) = ru.Closest(map.Units);
                        (Building buildingclosest, int distanceToBuilding) = ru.BuildingClosest(map.Buildings);

                        if (distanceTo > distanceToBuilding)
                        {
                            //Check In Range
                            if (distanceTo <= ru.AttackRange)
                            {
                                ru.IsAttacking = true;
                                ru.Combat(closest);
                            }
                            else //Move Towards
                            {
                                if (closest is MeleeUnit)
                                {
                                    MeleeUnit closestMu = (MeleeUnit)closest;
                                    if (ru.XPos > closestMu.XPos) //North
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestMu.XPos) //South
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestMu.YPos) //West
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestMu.YPos) //East
                                    {
                                        ru.Move(1);
                                    }
                                }
                                else if (closest is RangedUnit)
                                {
                                    RangedUnit closestRu = (RangedUnit)closest;
                                    if (ru.XPos > closestRu.XPos) //North
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestRu.XPos) //South
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestRu.YPos) //West
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestRu.YPos) //East
                                    {
                                        ru.Move(1);
                                    }
                                }
                            }
                        }
                        else
                        {
                            //Check In Range
                            if (distanceTo <= ru.AttackRange)
                            {
                                ru.IsAttacking = true;
                                ru.Combat(closest);
                            }
                            else //Move Towards
                            {
                                if (buildingclosest is FactoryBuilding)
                                {
                                    FactoryBuilding closestMu = (FactoryBuilding)buildingclosest;
                                    if (ru.XPos > closestMu.XPos) //North
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestMu.XPos) //South
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestMu.YPos) //West
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestMu.YPos) //East
                                    {
                                        ru.Move(1);
                                    }
                                }
                                else if (buildingclosest is ResourceBuilding)
                                {
                                    ResourceBuilding closestRu = (ResourceBuilding)buildingclosest;
                                    if (ru.XPos > closestRu.XPos) //North
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestRu.XPos) //South
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestRu.YPos) //West
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestRu.YPos) //East
                                    {
                                        ru.Move(1);
                                    }
                                }
                            }
                        }
                    }
                }
                else if (map.Units[i] is WizardUnit)
                {
                    WizardUnit wu = (WizardUnit)map.Units[i];
                    if (wu.Health == 0)
                    {
                        wu.Move(-1);
                    }
                    else if (wu.Health <= wu.MaxHealth * 0.5) // Running Away
                    {
                        wu.Move(r.Next(0, 4));
                    }
                    else
                    {
                        (Unit closest, int distanceTo) = wu.Closest(map.Units);

                        //Check In Range
                        if (distanceTo <= wu.AttackRange)
                        {
                            wu.IsAttacking = true;
                            wu.Combat(closest);
                        }
                        else //Move Towards
                        {
                            if (closest is MeleeUnit)
                            {
                                MeleeUnit closestMu = (MeleeUnit)closest;
                                if (wu.XPos > closestMu.XPos) //North
                                {
                                    wu.Move(0);
                                }
                                else if (wu.XPos < closestMu.XPos) //South
                                {
                                    wu.Move(2);
                                }
                                else if (wu.YPos > closestMu.YPos) //West
                                {
                                    wu.Move(3);
                                }
                                else if (wu.YPos < closestMu.YPos) //East
                                {
                                    wu.Move(1);
                                }
                            }
                            else if (closest is RangedUnit)
                            {
                                RangedUnit closestRu = (RangedUnit)closest;
                                if (wu.XPos > closestRu.XPos) //North
                                {
                                    wu.Move(0);
                                }
                                else if (wu.XPos < closestRu.XPos) //South
                                {
                                    wu.Move(2);
                                }
                                else if (wu.YPos > closestRu.YPos) //West
                                {
                                    wu.Move(3);
                                }
                                else if (wu.YPos < closestRu.YPos) //East
                                {
                                    wu.Move(1);
                                }
                            }
                        }
                    }
                }
            }
            map.Display(grpMap);
            round++;
        }