Esempio n. 1
0
        //Handling additional distance calculations
        public override (Unit, int) Closest(List <Unit> units)
        {
            int  shortest = 100;
            Unit closest  = this;

            //Closest Unit and Distance
            foreach (Unit u in units)
            {
                if (u is MeleeUnit)
                {
                    MeleeUnit otherMu  = (MeleeUnit)u;
                    int       distance = Math.Abs(this.XPos - otherMu.XPos)
                                         + Math.Abs(this.YPos - otherMu.YPos);
                    if (distance < shortest)
                    {
                        shortest = distance;
                        closest  = otherMu;
                    }
                }
                else if (u is RangedUnit)
                {
                    RangedUnit otherRu  = (RangedUnit)u;
                    int        distance = Math.Abs(this.XPos - otherRu.XPos)
                                          + Math.Abs(this.YPos - otherRu.YPos);
                    if (distance < shortest)
                    {
                        shortest = distance;
                        closest  = otherRu;
                    }
                }
            }
            return(closest, shortest);
        }
Esempio n. 2
0
        //Adds a unit's info to the ToString
        public void Unit_Click(object sender, EventArgs e)
        {
            //Determines the button's position
            int    x, y;
            Button b = (Button)sender;

            x = b.Location.X / 30;
            y = b.Location.Y / 30;

            //Does this for every unit on the map
            foreach (Unit u in units)
            {
                //Determines type of unit
                if (u is RangedUnit)
                {
                    //Calls the ToString method to display the stats
                    RangedUnit ru = (RangedUnit)u;
                    if (ru.XPos == x && ru.YPos == y)
                    {
                        txtInfo.Text = "";
                        txtInfo.Text = ru.ToString();
                    }
                }
                else if (u is MeleeUnit)
                {
                    //Calls the ToString method to display the stats
                    MeleeUnit mu = (MeleeUnit)u;
                    if (mu.XPos == x && mu.YPos == y)
                    {
                        txtInfo.Text = "";
                        txtInfo.Text = mu.ToString();
                    }
                }
            }
        }
Esempio n. 3
0
        //Handles generation of the units
        public void Generate()
        {
            for (int i = 0; i < numUnits; i++)
            {
                if (r.Next(0, 2) == 0) //Generate Melee Unit
                {
                    //creates the unit's stats
                    MeleeUnit m = new MeleeUnit(r.Next(0, 10),
                                                r.Next(0, 10),
                                                100,
                                                1,
                                                20,
                                                (i % 2 == 0 ? 1 : 0),
                                                "M/");
                    units.Add(m);
                }
                else // Generate Ranged Unit
                {
                    //creates the unit's stats
                    RangedUnit ru = new RangedUnit(r.Next(0, 10),
                                                   r.Next(0, 10),
                                                   100,
                                                   1,
                                                   20,
                                                   5,
                                                   (i % 2 == 0 ? 1 : 0),
                                                   "R}");
                    units.Add(ru);
                }
            }

            for (int k = 0; k < numBuildings; k++)
            {
                if (r.Next(0, 2) == 0) //Generate Resource Building
                {
                    //creates the Building's stats
                    ResourceBuilding rb = new ResourceBuilding(r.Next(0, 10),
                                                               r.Next(0, 10),
                                                               150,
                                                               (k % 2 == 0 ? 1 : 0),
                                                               "[G]",
                                                               false);
                    buildings.Add(rb);
                }
                else //Generate Unit Building
                {
                    //creates the Building's stats
                    FactoryBuilding fb = new FactoryBuilding(r.Next(0, 10),
                                                             r.Next(0, 10),
                                                             200,
                                                             (k % 2 == 0 ? 1 : 0),
                                                             "[F]",
                                                             false,
                                                             (r.Next(0, 2) == 1 ? "Melee" : "Ranged"));

                    buildings.Add(fb);
                }
            }
        }
Esempio n. 4
0
        //Handles combat
        public override void Combat(Unit attacker)
        {
            if (attacker is MeleeUnit)
            {
                Health = Health - ((MeleeUnit)attacker).Attack;
            }
            else if (attacker is RangedUnit)
            {
                RangedUnit ru = (RangedUnit)attacker;
                Health = Health - (ru.Attack - ru.AttackRange);
            }

            if (Health <= 0)
            {
                Death();
            }
        }
Esempio n. 5
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 <= mu.MaxHealth * 0.25) // Running Away
                    {
                        mu.Move(r.Next(0, 4));
                    }
                    else
                    {
                        (Unit closest, int distanceTo) = mu.Closest(map.Units); //determines a closest unit

                        //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) //if ranged then attack from range
                            {
                                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 if (map.Units[i] is RangedUnit)
                {
                    RangedUnit ru = (RangedUnit)map.Units[i];
                    if (ru.Health <= ru.MaxHealth * 0.25) //Run away
                    {
                        ru.Move(r.Next(0, 4));
                    }
                    else
                    {
                        (Unit closest, int distanceTo) = ru.Closest(map.Units);

                        //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);
                                }
                            }
                        }
                    }
                }
            }
            map.Display(grpMap); //Display update
            round++;             //Update round
        }
 //Method for the buildings to generate units
 public Unit BuildUnit(int factions)
 {
     //Determines Faction Type
     if (faction == 1)
     {
         //Determines what type of unit to generate
         if (unitType == "Melee")
         {
             //Creates the unit's
             MeleeUnit m = new MeleeUnit(xPos,
                                         yPos--,
                                         100,
                                         1,
                                         20,
                                         1,
                                         "M/");
             //Returns unit
             return(m);
         }
         //Does the same as abow except for ranged units
         else if (unitType == "Ranged")
         {
             RangedUnit ru = new RangedUnit(xPos,
                                            yPos--,
                                            100,
                                            1,
                                            20,
                                            5,
                                            1,
                                            "R}");
             return(ru);
         }
     }
     //Does the same as above except for the other team
     else if (faction == 0)
     {
         if (unitType == "Melee")
         {
             MeleeUnit m = new MeleeUnit(xPos,
                                         yPos--,
                                         100,
                                         1,
                                         20,
                                         0,
                                         "M/");
             return(m);
         }
         else if (unitType == "Ranged")
         {
             RangedUnit ru = new RangedUnit(xPos,
                                            yPos--,
                                            100,
                                            1,
                                            20,
                                            5,
                                            0,
                                            "R}");
             return(ru);
         }
     }
     //Return default
     return(null);
 }
Esempio n. 7
0
        //Displays the units onto the form
        public void Display(GroupBox groupBox)
        {
            //Clears form to prevent multiple instances of buttons
            groupBox.Controls.Clear();


            //Adding Units
            foreach (Unit u in units)
            {
                Button b = new Button();
                //Determines what type of unit to create
                if (u is MeleeUnit)
                {
                    //Creates the physical properties of the unit
                    MeleeUnit mu = (MeleeUnit)u;
                    b.Size     = new Size(30, 30);
                    b.Location = new Point(mu.XPos * 30, mu.YPos * 30);
                    b.Text     = mu.Symbol;
                    if (mu.Faction == 0)
                    {
                        b.ForeColor = Color.HotPink;
                    }
                    else
                    {
                        b.ForeColor = Color.Blue;
                    }
                }
                else
                {
                    //Creates the physical properties of the unit
                    RangedUnit ru = (RangedUnit)u;
                    b.Size     = new Size(30, 30);
                    b.Location = new Point(ru.XPos * 30, ru.YPos * 30);
                    b.Text     = ru.Symbol;
                    if (ru.Faction == 0)
                    {
                        b.ForeColor = Color.HotPink;
                    }
                    else
                    {
                        b.ForeColor = Color.Blue;
                    }
                }
                //adds the unit's stats to the Unit_Click method
                b.Click += Unit_Click;
                //Adds the units to the groupbox
                groupBox.Controls.Add(b);
            }

            //Adding Buildings
            foreach (Building bud in buildings)
            {
                Button b = new Button();
                //Determines what type of building to create
                if (bud is ResourceBuilding)
                {
                    //Creates its physical properties
                    ResourceBuilding rb = (ResourceBuilding)bud;

                    b.Size     = new Size(30, 30);
                    b.Location = new Point(rb.XPos * 30, rb.YPos * 30);
                    b.Text     = rb.Symbol;
                    if (rb.Faction == 0)
                    {
                        b.ForeColor = Color.HotPink;
                    }
                    else
                    {
                        b.ForeColor = Color.Blue;
                    }
                }
                else
                {
                    FactoryBuilding fb = (FactoryBuilding)bud;

                    b.Size     = new Size(30, 30);
                    b.Location = new Point(fb.XPos * 30, fb.YPos * 30);
                    b.Text     = fb.Symbol;
                    if (fb.Faction == 0)
                    {
                        b.ForeColor = Color.HotPink;
                    }
                    else
                    {
                        b.ForeColor = Color.Blue;
                    }
                }
                //Adds the stats to the Building_Click method
                b.Click += Building_Click;
                //Adds the buildings to the groupbox
                groupBox.Controls.Add(b);
            }
        }