//Methods
        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);
            }
            else if (attacker is WizzardUnit)//updated to include wizzard unit
            {
                WizzardUnit wu = (WizzardUnit)attacker;
                Health = Health - (wu.Attack - wu.AttackRange);
            }
            else if (attacker is NeutralTeam) //includes neutral team unit
            {
                NeutralTeam nt = (NeutralTeam)attacker;
                Health = Health - (nt.Attack - nt.AttackRange);
            }

            if (Health <= 0)
            {
                Death(); //Mistakes were made!!!!
            }
        }
Пример #2
0
 public void Generate()
 {
     for (int i = 0; i < numUnits; i++)
     {
         if (rd.Next(0, 4) == 0)//Generate MeleeUnit
         {
             MeleeUnit m = new MeleeUnit(rd.Next(0, 20),
                                         rd.Next(0, 20),
                                         120,
                                         1,
                                         20,
                                         (i % 2 == 0 ? 1 : 0),
                                         "M",
                                         "Ninja");//task 2 unit type
             units.Add(m);
         }
         else if (rd.Next(0, 4) == 1)// Generate RangedUnit
         {
             RangedUnit r = new RangedUnit(rd.Next(0, 20),
                                           rd.Next(0, 20),
                                           100,
                                           1,
                                           20,
                                           5,
                                           (i % 2 == 0 ? 1 : 0),
                                           "R",
                                           "Archer");//task 2 unit type
             units.Add(r);
         }
         else if (rd.Next(0, 4) == 2) //task 3-Generate WizzardUnit
         {
             WizzardUnit w = new WizzardUnit(rd.Next(0, 20),
                                             rd.Next(0, 20),
                                             100,
                                             1,
                                             20,
                                             (i % 2 == 0 ? 1 : 0),
                                             "W",
                                             "Mage");
             units.Add(w);
         }
         else//Generates the neutral unit
         {
             NeutralTeam n = new NeutralTeam(rd.Next(0, 20),
                                             rd.Next(0, 20),
                                             100,
                                             1,
                                             20,
                                             "NW",
                                             "Rouge");
             units.Add(n);
         }
     }
     for (int i = 0; i < numBuildings; i++)
     {
         if (rd.Next(0, 2) == 0)
         {
             ResourceBuilding rb = new ResourceBuilding(rd.Next(0, 20),
                                                        rd.Next(0, 20),
                                                        200,
                                                        (i % 2 == 0 ? 1 : 0),
                                                        "RB");
             buildings.Add(rb);
         }
         else
         {
             FactoryBuilding fb = new FactoryBuilding(rd.Next(0, 20),
                                                      rd.Next(0, 20),
                                                      200,
                                                      (i % 2 == 0 ? 1 : 0),
                                                      "FB");
             buildings.Add(fb);
         }
     }
 }
Пример #3
0
        public void Unit_Click(Object sender, EventArgs e)
        {
            int    x, y;
            Button b = (Button)sender;

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

            foreach (Unit u in units)
            {
                if (u is RangedUnit)
                {
                    RangedUnit ru = (RangedUnit)u;
                    if (ru.XPos == x && ru.YPos == y)
                    {
                        txtInfo.Text = " ";
                        txtInfo.Text = ru.ToString();
                    }
                }
                else if (u is MeleeUnit)
                {
                    MeleeUnit mu = (MeleeUnit)u;
                    if (mu.XPos == x && mu.YPos == y)
                    {
                        txtInfo.Text = " ";
                        txtInfo.Text = mu.ToString();
                    }
                }
                else if (u is WizzardUnit)
                {
                    //Task 3 Wizzard unit
                    WizzardUnit wu = (WizzardUnit)u;
                    if (wu.XPos == x && wu.YPos == y)
                    {
                        txtInfo.Text = "";
                        txtInfo.Text = wu.ToString();
                    }
                }
                else
                {
                    //Neutral team info
                    NeutralTeam nt = (NeutralTeam)u;
                    if (nt.XPos == x && nt.YPos == y)
                    {
                        txtInfo.Text = "";
                        txtInfo.Text = nt.ToString();
                    }
                }
            }
            foreach (Building d in buildings)
            {
                if (d is ResourceBuilding)
                {
                    ResourceBuilding rb = (ResourceBuilding)d;
                    if (rb.XPos == x && rb.YPos == y)
                    {
                        txtInfo.Text = " ";
                        txtInfo.Text = rb.ToString();
                    }
                }
                else if (d is FactoryBuilding)
                {
                    FactoryBuilding fb = (FactoryBuilding)d;
                    if (fb.XPos == x && fb.YPos == y)
                    {
                        txtInfo.Text = " ";
                        txtInfo.Text = fb.ToString();
                    }
                }
            }
        }
Пример #4
0
        public void Display(GroupBox groupBox)
        {
            groupBox.Controls.Clear();

            foreach (Unit u in units)
            {
                if (u is MeleeUnit)
                {
                    MeleeUnit mu = (MeleeUnit)u;
                    Button    b  = new Button();
                    b.Size     = new Size(20, 20);
                    b.Location = new Point(mu.XPos * 20, mu.YPos * 20);
                    b.Text     = mu.Symbol;
                    if (mu.Faction == 0)
                    {
                        b.ForeColor = Color.Red;
                    }
                    else
                    {
                        b.ForeColor = Color.Blue;
                    }
                    b.Click += Unit_Click;
                    groupBox.Controls.Add(b);
                }
                else if (u is RangedUnit)
                {
                    RangedUnit ru = (RangedUnit)u;
                    Button     b  = new Button();
                    b.Size     = new Size(20, 20);
                    b.Location = new Point(ru.XPos * 20, ru.YPos * 20);
                    b.Text     = ru.Symbol;
                    if (ru.Faction == 0)
                    {
                        b.ForeColor = Color.Red;
                    }
                    else
                    {
                        b.ForeColor = Color.Blue;
                    }
                    b.Click += Unit_Click;
                    groupBox.Controls.Add(b);
                }
                else if (u is WizzardUnit)
                {
                    //Task 3 WizzardUnit
                    WizzardUnit wu = (WizzardUnit)u;
                    Button      b  = new Button();
                    b.Size     = new Size(20, 20);
                    b.Location = new Point(wu.XPos * 20, wu.YPos * 20);
                    b.Text     = wu.Symbol;
                    if (wu.Faction == 0)
                    {
                        b.ForeColor = Color.Red;
                    }
                    else
                    {
                        b.ForeColor = Color.Blue;
                    }
                    b.Click += Unit_Click;
                    groupBox.Controls.Add(b);
                }
                else
                {
                    //Displays neutral team
                    NeutralTeam nt = (NeutralTeam)u;
                    Button      b  = new Button();
                    b.Size      = new Size(20, 20);
                    b.Location  = new Point(nt.XPos * 20, nt.YPos * 20);
                    b.Text      = nt.Symbol;
                    b.ForeColor = Color.Green;
                    b.Click    += Unit_Click;
                    groupBox.Controls.Add(b);
                }
            }
            foreach (Building d in buildings)
            {
                if (d is ResourceBuilding)
                {
                    ResourceBuilding rb = (ResourceBuilding)d;
                    Button           b  = new Button();
                    b.Size     = new Size(50, 50);
                    b.Location = new Point(rb.XPos * 20, rb.YPos * 20);
                    b.Text     = rb.Symbol;
                    if (rb.Faction == 0)
                    {
                        b.ForeColor = Color.Red;
                    }
                    else
                    {
                        b.ForeColor = Color.Blue;
                    }
                    b.Click += Unit_Click;
                    groupBox.Controls.Add(b);
                }
                else
                {
                    FactoryBuilding fb = (FactoryBuilding)d;
                    Button          b  = new Button();
                    b.Size     = new Size(50, 50);
                    b.Location = new Point(fb.XPos * 20, fb.YPos * 20);
                    b.Text     = fb.Symbol;
                    if (fb.Faction == 0)
                    {
                        b.ForeColor = Color.Red;
                    }
                    else
                    {
                        b.ForeColor = Color.Blue;
                    }
                    b.Click += Unit_Click;
                    groupBox.Controls.Add(b);
                }
            }
        }
Пример #5
0
 public void Update()
 {
     foreach (Building d in map.Buildings)
     {
         if (d is FactoryBuilding)
         {
             FactoryBuilding fb = (FactoryBuilding)d;
             if (fb.ProductionSpeed % Round == 0)
             {
                 map.Units.Add(fb.Spawn());
             }
         }
     }
     for (int i = 0; i < map.Units.Count; i++)
     {
         if (map.Units[i] is MeleeUnit)
         {
             MeleeUnit mu = (MeleeUnit)map.Units[i];
             mu.Health = 10;                       // tests code
             if (mu.Health <= mu.MaxHealth * 0.25) // Escape!!!( Running away)
             {
                 mu.Move(r.Next(0, 4));
             }
             else
             {
                 int  shortest = 100;
                 Unit closest  = mu;
                 foreach (Unit u in map.Units)
                 {
                     if (u is MeleeUnit)
                     {
                         MeleeUnit otherMu  = (MeleeUnit)u;
                         int       distance = Math.Abs(mu.XPos - otherMu.XPos)
                                              + Math.Abs(mu.YPos - otherMu.YPos);
                         if (distance < shortest)
                         {
                             shortest = distance;
                             closest  = otherMu;
                         }
                     }
                 }
                 //Check In Range
                 if (shortest <= 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.XPos > closestMu.YPos)//West
                         {
                             mu.Move(3);
                         }
                         else if (mu.XPos < closestMu.YPos)//East
                         {
                             mu.Move(1);
                         }
                     }
                 }
             }
         }
         if (map.Units[i] is RangedUnit)
         {
             RangedUnit ru = (RangedUnit)map.Units[i];
             // ru.Health = 10;// tests code
             if (ru.Health <= ru.MaxHealth * 0.25)// Escape!!!( Running away)
             {
                 ru.Move(r.Next(0, 4));
             }
             else
             {
                 int  shortest = 100;
                 Unit closest  = ru;
                 foreach (Unit u in map.Units)
                 {
                     if (u is RangedUnit)
                     {
                         RangedUnit otherRu  = (RangedUnit)u;
                         int        distance = Math.Abs(ru.XPos - otherRu.XPos)
                                               + Math.Abs(ru.YPos - otherRu.YPos);
                         if (distance < shortest)
                         {
                             shortest = distance;
                             closest  = otherRu;
                         }
                     }
                 }
                 //Check In Range
                 if (shortest <= ru.AttackRange)
                 {
                     ru.IsAttacking = true;
                     ru.Combat(closest);
                 }
                 else // Move towards
                 {
                     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.XPos > closestRu.YPos)//West
                         {
                             ru.Move(3);
                         }
                         else if (ru.XPos < closestRu.YPos)//East
                         {
                             ru.Move(1);
                         }
                     }
                 }
             }
         }
         if (map.Units[i] is WizzardUnit)//task 3 new wizzard unit
         {
             WizzardUnit wu = (WizzardUnit)map.Units[i];
             if (wu.Health <= wu.MaxHealth * 0.50)// Escape!!!( Running away)
             {
                 wu.Move(r.Next(0, 4));
             }
             else
             {
                 int  shortest = 100;
                 Unit closest  = wu;
                 foreach (Unit u in map.Units)
                 {
                     if (u is WizzardUnit)
                     {
                         WizzardUnit otherWu  = (WizzardUnit)u;
                         int         distance = Math.Abs(wu.XPos - otherWu.XPos)
                                                + Math.Abs(wu.YPos - otherWu.YPos);
                         if (distance < shortest)
                         {
                             shortest = distance;
                             closest  = otherWu;
                         }
                     }
                 }
                 //Check In Range
                 if (shortest <= wu.AttackRange)
                 {
                     wu.IsAttacking = true;
                     wu.Combat(closest);
                 }
                 else // Move towards
                 {
                     if (closest is WizzardUnit)
                     {
                         WizzardUnit closestWu = (WizzardUnit)closest;
                         if (wu.XPos > closestWu.XPos)//North
                         {
                             wu.Move(0);
                         }
                         else if (wu.XPos < closestWu.XPos)//South
                         {
                             wu.Move(2);
                         }
                         else if (wu.XPos > closestWu.YPos)//West
                         {
                             wu.Move(3);
                         }
                         else if (wu.XPos < closestWu.YPos)//East
                         {
                             wu.Move(1);
                         }
                     }
                 }
             }
         }
         if (map.Units[i] is NeutralTeam)//Neutral
         {
             NeutralTeam nt = (NeutralTeam)map.Units[i];
             if (nt.Health <= nt.MaxHealth * 0.50)// Escape!!!( Running away)
             {
                 nt.Move(r.Next(0, 4));
             }
             else
             {
                 int  shortest = 100;
                 Unit closest  = nt;
                 foreach (Unit u in map.Units)
                 {
                     if (u is NeutralTeam)
                     {
                         NeutralTeam otherNt  = (NeutralTeam)u;
                         int         distance = Math.Abs(nt.XPos - otherNt.XPos)
                                                + Math.Abs(nt.YPos - otherNt.YPos);
                         if (distance < shortest)
                         {
                             shortest = distance;
                             closest  = otherNt;
                         }
                     }
                 }
                 //Check In Range
                 if (shortest <= nt.AttackRange)
                 {
                     nt.IsAttacking = true;
                     nt.Combat(closest);
                 }
                 else // Move towards
                 {
                     if (closest is WizzardUnit)
                     {
                         WizzardUnit closestWu = (WizzardUnit)closest;
                         if (nt.XPos > closestWu.XPos)//North
                         {
                             nt.Move(0);
                         }
                         else if (nt.XPos < closestWu.XPos)//South
                         {
                             nt.Move(2);
                         }
                         else if (nt.XPos > closestWu.YPos)//West
                         {
                             nt.Move(3);
                         }
                         else if (nt.XPos < closestWu.YPos)//East
                         {
                             nt.Move(1);
                         }
                     }
                 }
             }
         }
     }
     map.Display(gBoxMap);
     round++;
 }
Пример #6
0
        public int DistanceTo(Unit a, Unit b)
        {
            int distance = 0;

            if (a is MeleeUnit && b is MeleeUnit)
            {
                MeleeUnit start = (MeleeUnit)a;
                MeleeUnit end   = (MeleeUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            else if (a is RangedUnit && b is MeleeUnit)
            {
                RangedUnit start = (RangedUnit)a;
                MeleeUnit  end   = (MeleeUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            else if (a is RangedUnit && b is RangedUnit)
            {
                RangedUnit start = (RangedUnit)a;
                RangedUnit end   = (RangedUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            else if (a is MeleeUnit && b is RangedUnit)
            {
                MeleeUnit  start = (MeleeUnit)a;
                RangedUnit end   = (RangedUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            //Task 3 new wizzard unit
            else if (a is WizzardUnit && b is MeleeUnit)
            {
                WizzardUnit start = (WizzardUnit)a;
                MeleeUnit   end   = (MeleeUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            else if (a is WizzardUnit && b is RangedUnit)
            {
                WizzardUnit start = (WizzardUnit)a;
                RangedUnit  end   = (RangedUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            else if (a is MeleeUnit && b is WizzardUnit)
            {
                MeleeUnit   start = (MeleeUnit)a;
                WizzardUnit end   = (WizzardUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            else if (a is RangedUnit && b is WizzardUnit)
            {
                RangedUnit  start = (RangedUnit)a;
                WizzardUnit end   = (WizzardUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            //Neutral Team units
            else if (a is NeutralTeam && b is MeleeUnit)
            {
                NeutralTeam start = (NeutralTeam)a;
                MeleeUnit   end   = (MeleeUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            else if (a is NeutralTeam && b is RangedUnit)
            {
                NeutralTeam start = (NeutralTeam)a;
                RangedUnit  end   = (RangedUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            else if (a is MeleeUnit && b is NeutralTeam)
            {
                MeleeUnit   start = (MeleeUnit)a;
                NeutralTeam end   = (NeutralTeam)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            else if (a is RangedUnit && b is NeutralTeam)
            {
                RangedUnit  start = (RangedUnit)a;
                NeutralTeam end   = (NeutralTeam)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            return(distance);
        }