static bool initialised = false; // tells the class if variables are initialised before trying to use them

        // method runs every round and perfirms the nessesary actions that are needed to be done every round
        public static void Round()
        {
            if (initialised == false)
            {
                map         = new Map(numOfEnemies);
                initialised = true;
            }
            // Unit round actions ***************************************************************************************************************************
            foreach (ButtonUnit b in map.unitButton)
            {
                Unit u = b.Unit;
                if (u.Health > 0)  // checks if the unit is alive or not
                {
                    Unit unit;
                    if (u.GetType() == typeof(MeeleeUnit)) //  gets the type of unit
                    {
                        unit = u as MeeleeUnit;
                    }
                    else
                    {
                        unit = u as RangedUnit;
                    }

                    Unit enemy = unit.FindClosestUnit(map.unitButton);
                    if (enemy != null) // checks the type of unit and if there is a unit
                    {
                        if (enemy.GetType() == typeof(MeeleeUnit))
                        {
                            enemy = enemy as MeeleeUnit;
                        }
                        else
                        {
                            enemy = enemy as RangedUnit;
                        }
                    }

                    if (!unit.DestroyUnit()) // checks if the unit is alive
                    {
                        if (unit.Health / unit.MaxHealth >= 0.25)
                        {
                            if (unit.IsInRange(enemy)) // checks if the unit is in range of the enemy
                            {
                                try
                                {
                                    unit.IsAttacking = true;
                                    enemy.Combat(u.Attack);
                                    enemy.DestroyUnit();
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex);
                                }
                            }
                            else
                            {
                                try
                                {
                                    unit.IsAttacking = false;
                                    unit.Move(unit.DirectionOfEnemy(enemy), 1);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex);
                                }
                            }
                        }
                        else
                        {
                            unit.IsAttacking = false;
                            Random r = new Random();
                            u.Move((Direction)r.Next(), 1);
                        }
                    }
                    unit.DestroyUnit();
                }
            }

            // building round actions ***************************************************************************************************************************
            foreach (ButtonBuilding b in map.buildingButton)
            {
                try
                {
                    Building building = b.Building;
                    if (building.GetType() == typeof(ResourceBuilding))
                    {
                        ResourceBuilding rb = building as ResourceBuilding;
                        rb.GenerateResources();
                    }
                    else if (building.GetType() == typeof(FactoryBuilding))
                    {
                        FactoryBuilding fb = building as FactoryBuilding;
                        if (rounds % fb.ProductionSpeed == 0)
                        {
                            map.AddUnit(fb.CreateUnit());
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
            map.UpDatePosition();
            rounds++;
            Program.UI.RoundUpdate(rounds);
        }
Example #2
0
        static bool initialised = false; // tells the class if variables are initialised before trying to use them

        // method runs every round and perfirms the nessesary actions that are needed to be done every round
        public static void Round()
        {
            if (initialised == false)
            {
                SetMapSize(20, 20);
                initialised = true;
            }
            // Unit round actions ***************************************************************************************************************************
            foreach (ButtonUnit b in map.unitButton)
            {
                Unit u = b.Unit;
                if (u.Health > 0)  // checks if the unit is alive or not
                {
                    Unit unit;
                    if (u.GetType() == typeof(MeeleeUnit)) //  gets the type of unit
                    {
                        unit = u as MeeleeUnit;
                    }
                    else if (u.GetType() == typeof(RangedUnit))
                    {
                        unit = u as RangedUnit;
                    }
                    else
                    {
                        unit = u as WizzardUnit;
                    }

                    Unit enemy = unit.FindClosestUnit(map.unitButton);
                    if (enemy != null) // checks the type of unit and if there is a unit

                    {
                        if (!unit.DestroyUnit()) // checks if the unit is alive
                        {
                            if (unit.Health / unit.MaxHealth >= 0.25)
                            {
                                if (unit.IsInRange(enemy)) // checks if the unit is in range of the enemy
                                {
                                    try
                                    {
                                        if (u.GetType() == typeof(WizzardUnit))
                                        {
                                            unit.IsAttacking = true;
                                            enemy.Combat(Convert.ToDouble(u.Attack));
                                            enemy.DestroyUnit();
                                        }
                                        else
                                        {
                                            // allows the wizard to do AOE (area oif effect) damage
                                            for (int i = unit.XPos - 1; i <= unit.XPos + 1; i++)
                                            {
                                                for (int ii = unit.YPos - 1; ii <= unit.YPos + 1; ii++)
                                                {
                                                    try
                                                    {
                                                        Unit targets;
                                                        if (u.GetType() == typeof(MeeleeUnit)) //  gets the type of unit
                                                        {
                                                            targets = map.map[i, ii] as MeeleeUnit;
                                                        }
                                                        else
                                                        {
                                                            targets = map.map[i, ii] as RangedUnit;
                                                        }
                                                        targets.Combat(Convert.ToDouble(unit.Attack));
                                                    } catch (Exception ex)
                                                    {
                                                        Console.WriteLine(ex);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex);
                                    }
                                }
                                else
                                {
                                    try
                                    {
                                        unit.IsAttacking = false;
                                        unit.Move(unit.DirectionOfEnemy(enemy), 1);
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex);
                                    }
                                }
                            }
                            else
                            {
                                unit.IsAttacking = false;
                                Random r = new Random();
                                u.Move((Direction)r.Next(), 1);
                            }
                        }
                    }
                    unit.DestroyUnit();
                }
            }

            // building round actions ***************************************************************************************************************************
            foreach (ButtonBuilding b in map.buildingButton)
            {
                try
                {
                    Building building = b.Building;
                    if (building.GetType() == typeof(ResourceBuilding))
                    {
                        ResourceBuilding rb = building as ResourceBuilding;
                        rb.GenerateResources();
                    }
                    else if (building.GetType() == typeof(FactoryBuilding))
                    {
                        FactoryBuilding fb = building as FactoryBuilding;
                        if (rounds % fb.ProductionSpeed == 0)
                        {
                            map.AddUnit(fb.CreateUnit());
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
            map.UpDatePosition();
            rounds++;
            Program.UI.RoundUpdate(rounds);
        }