//Checks to see if the closest enemy is in attack range and if they are calls combat or move if they aren't
        public override void CheckAttackRange(List <Unit> uni, List <Building> build)
        {
            units     = uni;
            buildings = build;

            closestUnit     = ClosestEnemy();
            closestBuilding = ClosestEnemyBuilding();

            int enemyType;

            int xDis = 0, yDis = 0;

            int uDistance = 10000, bDistance = 10000;
            int distance;

            if (closestUnit is MeleeUnit)
            {
                MeleeUnit M = (MeleeUnit)closestUnit;
                xDis = Math.Abs((PosX - M.PosX) * (PosX - M.PosX));
                yDis = Math.Abs((PosY - M.PosY) * (PosY - M.PosY));

                uDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0);
            }
            else if (closestUnit is RangedUnit)
            {
                RangedUnit R = (RangedUnit)closestUnit;
                xDis = Math.Abs((PosX - R.PosX) * (PosX - R.PosX));
                yDis = Math.Abs((PosY - R.PosY) * (PosY - R.PosY));

                uDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0);
            }
            else if (closestUnit is WizardUnit)
            {
                WizardUnit W = (WizardUnit)closestUnit;
                xDis = Math.Abs((PosX - W.PosX) * (PosX - W.PosX));
                yDis = Math.Abs((PosY - W.PosY) * (PosY - W.PosY));

                uDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0);
            }

            if (closestBuilding is FactoryBuilding)
            {
                FactoryBuilding FB = (FactoryBuilding)closestBuilding;
                xDis = Math.Abs((PosX - FB.PosX) * (PosX - FB.PosX));
                yDis = Math.Abs((PosY - FB.PosY) * (PosY - FB.PosY));

                bDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0);
            }
            else if (closestBuilding is ResourceBuilding)
            {
                ResourceBuilding RB = (ResourceBuilding)closestBuilding;
                xDis = Math.Abs((PosX - RB.PosX) * (PosX - RB.PosX));
                yDis = Math.Abs((PosY - RB.PosY) * (PosY - RB.PosY));

                bDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0);
            }

            if (units[0] != null)
            {
                if (uDistance < bDistance)
                {
                    distance  = uDistance;
                    enemyType = 0;
                }
                else
                {
                    distance  = bDistance;
                    enemyType = 1;
                }
            }
            else
            {
                distance  = bDistance;
                enemyType = 1;
            }

            //Checks to see if they are below 25% health so they move rather than attacking
            if (Health > MaxHealth * 0.25)
            {
                if (distance <= AttackRange)
                {
                    IsAttacking = true;
                    Combat(enemyType);
                }
                else
                {
                    IsAttacking = false;
                    Move(enemyType);
                }
            }
            else
            {
                Move(enemyType);
            }
        }
        //Changes the x and y position towards the closest enemy or to run away
        public override void Move(int type)
        {
            //Moves towards closest enemey
            if (Health > MaxHealth * 0.25)
            {
                if (type == 0)
                {
                    if (closestUnit is MeleeUnit)
                    {
                        MeleeUnit closestUnitM = (MeleeUnit)closestUnit;

                        if (closestUnitM.PosX > posX && PosX < MapWidth - 1)
                        {
                            posX++;
                        }
                        else if (closestUnitM.PosX < posX && posX > 0)
                        {
                            posX--;
                        }

                        if (closestUnitM.PosY > posY && PosY < MapHeight - 1)
                        {
                            posY++;
                        }
                        else if (closestUnitM.PosY < posY && posY > 0)
                        {
                            posY--;
                        }
                    }
                    else if (closestUnit is RangedUnit)
                    {
                        RangedUnit closestUnitR = (RangedUnit)closestUnit;

                        if (closestUnitR.PosX > posX && PosX < MapWidth - 1)
                        {
                            posX++;
                        }
                        else if (closestUnitR.PosX < posX && posX > 0)
                        {
                            posX--;
                        }

                        if (closestUnitR.PosY > posY && PosY < MapHeight - 1)
                        {
                            posY++;
                        }
                        else if (closestUnitR.PosY < posY && posY > 0)
                        {
                            posY--;
                        }
                    }
                    else if (closestUnit is WizardUnit)
                    {
                        WizardUnit closestUnitW = (WizardUnit)closestUnit;

                        if (closestUnitW.PosX > posX && PosX < MapWidth - 1)
                        {
                            posX++;
                        }
                        else if (closestUnitW.PosX < posX && posX > 0)
                        {
                            posX--;
                        }

                        if (closestUnitW.PosY > posY && PosY < MapHeight - 1)
                        {
                            posY++;
                        }
                        else if (closestUnitW.PosY < posY && posY > 0)
                        {
                            posY--;
                        }
                    }
                }
                else
                {
                    if (closestBuilding is FactoryBuilding)
                    {
                        FactoryBuilding closestBuildingFB = (FactoryBuilding)closestBuilding;

                        if (closestBuildingFB.PosX > posX && PosX < MapHeight - 1)
                        {
                            posX++;
                        }
                        else if (closestBuildingFB.PosX < posX && posX > 0)
                        {
                            posX--;
                        }

                        if (closestBuildingFB.PosY > posY && PosY < MapWidth - 1)
                        {
                            posY++;
                        }
                        else if (closestBuildingFB.PosY < posY && posY > 0)
                        {
                            posY--;
                        }
                    }
                    else if (closestBuilding is ResourceBuilding)
                    {
                        ResourceBuilding closestBuildingRB = (ResourceBuilding)closestBuilding;

                        if (closestBuildingRB.PosX > posX && PosX < MapHeight - 1)
                        {
                            posX++;
                        }
                        else if (closestBuildingRB.PosX < posX && posX > 0)
                        {
                            posX--;
                        }

                        if (closestBuildingRB.PosY > posY && PosY < MapWidth - 1)
                        {
                            posY++;
                        }
                        else if (closestBuildingRB.PosY < posY && posY > 0)
                        {
                            posY--;
                        }
                    }
                }
            }
            else //Moves in random direction to run away
            {
                int direction = r.Next(0, 4);

                if (direction == 0 && PosX < MapHeight - 1)
                {
                    posX++;
                }
                else if (direction == 1 && posX > 0)
                {
                    posX--;
                }
                else if (direction == 2 && posY < MapWidth - 1)
                {
                    posY++;
                }
                else if (direction == 3 && posY > 0)
                {
                    posY--;
                }
            }
        }
Exemple #3
0
        //Runs all the logic behind the game
        public void GameLogic()
        {
            //Working out if both teams are alive
            int dire    = 0;
            int radiant = 0;

            foreach (ResourceBuilding RB in m.mines)
            {
                if (RB.FactionType == Faction.Dire)
                {
                    dire++;
                }
                else
                {
                    radiant++;
                }
            }

            foreach (FactoryBuilding FB in m.factories)
            {
                if (FB.FactionType == Faction.Dire)
                {
                    dire++;
                }
                else
                {
                    radiant++;
                }
            }

            foreach (MeleeUnit u in m.meleeUnits)
            {
                if (u.FactionType == Faction.Dire)
                {
                    dire++;
                }
                else
                {
                    radiant++;
                }
            }

            foreach (RangedUnit u in m.rangedUnits)
            {
                if (u.FactionType == Faction.Dire)
                {
                    dire++;
                }
                else
                {
                    radiant++;
                }
            }

            if (dire > 0 && radiant > 0)//Checks to see if both teams are still alive
            {
                foreach (ResourceBuilding RB in m.mines)
                {
                    if (RB.FactionType == Faction.Dire)
                    {
                        direResources += RB.GenerateResource();
                    }
                    else if (RB.FactionType == Faction.Radient)
                    {
                        radientResources += RB.GenerateResource();
                    }
                }

                foreach (FactoryBuilding FB in m.factories)
                {
                    Unit u = FB.SpawnUnit();

                    if (FB.FactionType == Faction.Dire && direResources > FB.SpawnCost)
                    {
                        if (m.round % FB.SpawnSpeed == 0)
                        {
                            m.units.Add(u);

                            if (u is MeleeUnit)
                            {
                                MeleeUnit M = (MeleeUnit)u;

                                M.MapHeight = mapHeight;
                                M.MapWidth  = mapWidth;
                                m.meleeUnits.Add(M);
                            }
                            else if (u is RangedUnit)
                            {
                                RangedUnit R = (RangedUnit)u;

                                R.MapHeight = mapHeight;
                                R.MapWidth  = mapWidth;
                                m.rangedUnits.Add(R);
                            }
                            direResources -= FB.SpawnCost;
                        }
                    }
                    else if (FB.FactionType == Faction.Radient && radientResources > FB.SpawnCost)
                    {
                        if (m.round % FB.SpawnSpeed == 0)
                        {
                            m.units.Add(u);

                            if (u is MeleeUnit)
                            {
                                MeleeUnit M = (MeleeUnit)u;

                                m.meleeUnits.Add(M);
                            }
                            else if (u is RangedUnit)
                            {
                                RangedUnit R = (RangedUnit)u;

                                m.rangedUnits.Add(R);
                            }
                            radientResources -= FB.SpawnCost;
                        }
                    }
                }

                foreach (Unit u in m.units)
                {
                    u.CheckAttackRange(m.units, m.buildings);
                }

                m.round++;
                m.PlaceUnits();
                m.PlaceBuildings();
                Placebuttons();
            }
            else
            {
                m.PlaceUnits();
                m.PlaceBuildings();
                Placebuttons();
                GameTick.Enabled = false;

                if (dire > radiant)
                {
                    MessageBox.Show("Dire Wins in " + m.round + " rounds");
                }
                else
                {
                    MessageBox.Show("Radiant Wins in " + m.round + " rounds");
                }
            }

            //Checks to see who has died and needs to be deleted
            for (int i = 0; i < m.rangedUnits.Count; i++)
            {
                if (m.rangedUnits[i].Death())
                {
                    m.map[m.rangedUnits[i].PosX, m.rangedUnits[i].PosY] = "";
                    m.rangedUnits.RemoveAt(i);
                }
            }

            for (int i = 0; i < m.meleeUnits.Count; i++)
            {
                if (m.meleeUnits[i].Death())
                {
                    m.map[m.meleeUnits[i].PosX, m.meleeUnits[i].PosY] = "";
                    m.meleeUnits.RemoveAt(i);
                }
            }

            for (int i = 0; i < m.wizardUnits.Count; i++)
            {
                if (m.wizardUnits[i].Death())
                {
                    m.map[m.wizardUnits[i].PosX, m.wizardUnits[i].PosY] = "";
                    m.wizardUnits.RemoveAt(i);
                }
            }

            for (int i = 0; i < m.units.Count; i++)
            {
                if (m.units[i].Death())
                {
                    if (m.units[i] is MeleeUnit)
                    {
                        MeleeUnit M = (MeleeUnit)m.units[i];
                        m.map[M.PosX, M.PosY] = "";
                    }
                    else if (m.units[i] is RangedUnit)
                    {
                        RangedUnit R = (RangedUnit)m.units[i];
                        m.map[R.PosX, R.PosY] = "";
                    }
                    else if (m.units[i] is WizardUnit)
                    {
                        WizardUnit W = (WizardUnit)m.units[i];
                        m.map[W.PosX, W.PosY] = "";
                    }

                    m.units.RemoveAt(i);
                }
            }

            for (int i = 0; i < m.factories.Count; i++)
            {
                if (m.factories[i].Death())
                {
                    m.map[m.factories[i].PosX, m.factories[i].PosY] = "";
                    m.factories.RemoveAt(i);
                }
            }

            for (int i = 0; i < m.mines.Count; i++)
            {
                if (m.mines[i].Death())
                {
                    m.map[m.mines[i].PosX, m.mines[i].PosY] = "";
                    m.mines.RemoveAt(i);
                }
            }

            for (int i = 0; i < m.buildings.Count; i++)
            {
                if (m.buildings[i].Death())
                {
                    if (m.buildings[i] is FactoryBuilding)
                    {
                        FactoryBuilding FB = (FactoryBuilding)m.buildings[i];
                        m.map[FB.PosX, FB.PosY] = "";
                    }
                    else if (m.buildings[i] is ResourceBuilding)
                    {
                        ResourceBuilding RB = (ResourceBuilding)m.buildings[i];
                        m.map[RB.PosX, RB.PosY] = "";
                    }

                    m.buildings.RemoveAt(i);
                }
            }
        }
Exemple #4
0
        //Places the buttons on the form and puts the units in the buttons
        public void Placebuttons()
        {
            gbMap.Controls.Clear();

            Size btnSize = new Size(30, 30);

            for (int x = 0; x < mapWidth; x++)
            {
                for (int y = 0; y < mapHeight; y++)
                {
                    if (m.map[x, y] == "R")//If the string map representation has an 'R' for a ranged unit
                    {
                        Button btn = new Button();

                        btn.Size     = btnSize;
                        btn.Location = new Point(x * 30, y * 30);

                        if (m.unitMap[x, y] is RangedUnit)
                        {
                            RangedUnit R = (RangedUnit)m.unitMap[x, y];
                            btn.Text = R.Symbol;
                            if (R.FactionType == Faction.Dire)
                            {
                                btn.BackColor = Color.Red;
                            }
                            else if (R.FactionType == Faction.Radient)
                            {
                                btn.BackColor = Color.Green;
                            }
                            btn.Name   = m.unitMap[x, y].ToString();
                            btn.Click += MyButtonClick;
                            gbMap.Controls.Add(btn);
                        }
                    }
                    else if (m.map[x, y] == "M")//If the string map representation has an 'M' for a melee unit
                    {
                        Button btn = new Button();

                        btn.Size     = btnSize;
                        btn.Location = new Point(x * 30, y * 30);

                        if (m.unitMap[x, y] is MeleeUnit)
                        {
                            MeleeUnit M = (MeleeUnit)m.unitMap[x, y];
                            btn.Text = M.Symbol;
                            if (M.FactionType == Faction.Dire)
                            {
                                btn.BackColor = Color.Red;
                            }
                            else if (M.FactionType == Faction.Radient)
                            {
                                btn.BackColor = Color.Green;
                            }
                            btn.Name   = m.unitMap[x, y].ToString();
                            btn.Click += MyButtonClick;
                            gbMap.Controls.Add(btn);
                        }
                    }
                    else if (m.map[x, y] == "W")//If the string map representation has an 'W' for a wizard unit
                    {
                        Button btn = new Button();

                        btn.Size     = btnSize;
                        btn.Location = new Point(x * 30, y * 30);

                        if (m.unitMap[x, y] is WizardUnit)
                        {
                            WizardUnit W = (WizardUnit)m.unitMap[x, y];
                            btn.Text = W.Symbol;

                            btn.BackColor = Color.Turquoise;

                            btn.Name   = m.unitMap[x, y].ToString();
                            btn.Click += MyButtonClick;
                            gbMap.Controls.Add(btn);
                        }
                    }
                    else if (m.map[x, y] == "FB")//If the string map representation has an 'FB' for a factory building
                    {
                        Button btn = new Button();

                        btn.Size     = btnSize;
                        btn.Location = new Point(x * 30, y * 30);

                        if (m.buildingMap[x, y] is FactoryBuilding)
                        {
                            FactoryBuilding FB = (FactoryBuilding)m.buildingMap[x, y];
                            btn.Text = FB.Symbol;
                            if (FB.FactionType == Faction.Dire)
                            {
                                btn.BackColor = Color.Red;
                            }
                            else if (FB.FactionType == Faction.Radient)
                            {
                                btn.BackColor = Color.Green;
                            }
                            btn.Name   = m.buildingMap[x, y].ToString();
                            btn.Click += MyButtonClick;
                            gbMap.Controls.Add(btn);
                        }
                    }
                    else if (m.map[x, y] == "RB")//If the string map representation has an 'RB' for a resource building
                    {
                        Button btn = new Button();

                        btn.Size     = btnSize;
                        btn.Location = new Point(x * 30, y * 30);

                        if (m.buildingMap[x, y] is ResourceBuilding)
                        {
                            ResourceBuilding RB = (ResourceBuilding)m.buildingMap[x, y];
                            btn.Text = RB.Symbol;
                            if (RB.FactionType == Faction.Dire)
                            {
                                btn.BackColor = Color.Red;
                            }
                            else if (RB.FactionType == Faction.Radient)
                            {
                                btn.BackColor = Color.Green;
                            }
                            btn.Name   = m.buildingMap[x, y].ToString();
                            btn.Click += MyButtonClick;
                            gbMap.Controls.Add(btn);
                        }
                    }
                }
            }
        }
Exemple #5
0
        //Places the buttons on the form and puts the units in the buttons
        public void Placebuttons()
        {
            gbMap.Controls.Clear();

            Size btnSize = new Size(30, 30);

            for (int x = 0; x < 20; x++)
            {
                for (int y = 0; y < 20; y++)
                {
                    Button btn = new Button();

                    btn.Size     = btnSize;
                    btn.Location = new Point(x * 30, y * 30);

                    if (m.map[x, y] == "R")
                    {
                        if (m.unitMap[x, y] is RangedUnit)
                        {
                            RangedUnit R = (RangedUnit)m.unitMap[x, y];
                            btn.Text = R.Symbol;
                            if (R.FactionType == Faction.Dire)
                            {
                                btn.BackColor = Color.Red;
                            }
                            else
                            {
                                btn.BackColor = Color.Green;
                            }
                            btn.Name   = m.unitMap[x, y].ToString();
                            btn.Click += MyButtonClick;
                            gbMap.Controls.Add(btn);
                        }
                    }
                    else if (m.map[x, y] == "M")
                    {
                        if (m.unitMap[x, y] is MeleeUnit)
                        {
                            MeleeUnit M = (MeleeUnit)m.unitMap[x, y];
                            btn.Text = M.Symbol;
                            if (M.FactionType == Faction.Dire)
                            {
                                btn.BackColor = Color.Red;
                            }
                            else
                            {
                                btn.BackColor = Color.Green;
                            }
                            btn.Name   = m.unitMap[x, y].ToString();
                            btn.Click += MyButtonClick;
                            gbMap.Controls.Add(btn);
                        }
                    }
                    else if (m.map[x, y] == "FB")
                    {
                        if (m.buildingMap[x, y] is FactoryBuilding)
                        {
                            FactoryBuilding FB = (FactoryBuilding)m.buildingMap[x, y];
                            btn.Text = FB.Symbol;
                            if (FB.FactionType == Faction.Dire)
                            {
                                btn.BackColor = Color.Red;
                            }
                            else
                            {
                                btn.BackColor = Color.Green;
                            }
                            btn.Name   = m.buildingMap[x, y].ToString();
                            btn.Click += MyButtonClick;
                            gbMap.Controls.Add(btn);
                        }
                    }
                    else if (m.map[x, y] == "RB")
                    {
                        if (m.buildingMap[x, y] is ResourceBuilding)
                        {
                            ResourceBuilding RB = (ResourceBuilding)m.buildingMap[x, y];
                            btn.Text = RB.Symbol;
                            if (RB.FactionType == Faction.Dire)
                            {
                                btn.BackColor = Color.Red;
                            }
                            else
                            {
                                btn.BackColor = Color.Green;
                            }
                            btn.Name   = m.buildingMap[x, y].ToString();
                            btn.Click += MyButtonClick;
                            gbMap.Controls.Add(btn);
                        }
                    }
                }
            }
        }