Example #1
0
        public void Building_Click(object sender, EventArgs e)
        {
            int    x, y;
            Button myButt = (Button)sender;

            x = myButt.Location.X / 20;
            y = myButt.Location.Y / 20;
            foreach (Building b in builds)
            {
                if (b is ResourceBuilding)
                {
                    ResourceBuilding rb = (ResourceBuilding)b;
                    if (rb.PosX == x && rb.PosY == y)
                    {
                        txtInfo.Text = "";
                        txtInfo.Text = rb.ToString();
                    }
                }
                else if (b is FactoryBuilding)
                {
                    FactoryBuilding fb = (FactoryBuilding)b;
                    if (fb.PosX == x && fb.PosY == y)
                    {
                        txtInfo.Text = "";
                        txtInfo.Text = fb.ToString();
                    }
                }
            }
        }
Example #2
0
 public void Generate()
 {
     for (int i = 0; i < numUnits; i++)
     {
         if (r.Next(0, 2) == 0)
         {
             MeleeUnit m = new MeleeUnit(r.Next(0, 20), r.Next(0, 20), "Soldier", 100, 1, 20, i % 2 == 0 ? 1 : 0, "M");
             units.Add(m);
         }
         else
         {
             RangedUnit ru = new RangedUnit(r.Next(0, 20), r.Next(0, 20), "Archer", 100, 1, 20, 5, i % 2 == 0 ? 1 : 0, "R");
             units.Add(ru);
         }
     }
     for (int j = 0; j < numBuilds; j++)
     {
         if (r.Next(0, 2) == 0)
         {
             if (r.Next(0, 2) == 0)
             {
                 FactoryBuilding fb = new FactoryBuilding(r.Next(0, 20), r.Next(0, 20), 0, 200, 5, j % 2 == 0 ? 1 : 0, "FB");
                 builds.Add(fb);
             }
             else
             {
                 FactoryBuilding fb = new FactoryBuilding(r.Next(0, 20), r.Next(0, 20), 1, 200, 5, j % 2 == 0 ? 1 : 0, "FB");
                 builds.Add(fb);
             }
         }
         else
         {
             ResourceBuilding rb = new ResourceBuilding(r.Next(0, 20), r.Next(0, 20), "Swords", 200, 2, 60, j % 2 == 0 ? 1 : 0);
             builds.Add(rb);
         }
     }
 }
Example #3
0
 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)
             {
                 mu.Move(r.Next(0, 4));
             }
             else
             {
                 (Unit closest, int distanceTo) = mu.Closest(map.Units);
                 if (distanceTo <= mu.AttackRange)
                 {
                     mu.IsAttacking = true;
                     mu.Combat(closest);
                 }
                 else
                 {
                     if (closest is MeleeUnit)
                     {
                         MeleeUnit closestMu = (MeleeUnit)closest;
                         if (mu.XPos > closestMu.XPos)
                         {
                             mu.Move(0);
                         }
                         else if (mu.YPos < closestMu.YPos)
                         {
                             mu.Move(1);
                         }
                         else if (mu.XPos < closestMu.XPos)
                         {
                             mu.Move(2);
                         }
                         else if (mu.YPos > closestMu.YPos)
                         {
                             mu.Move(3);
                         }
                     }
                     else if (closest is RangedUnit)
                     {
                         RangedUnit closestRu = (RangedUnit)closest;
                         if (mu.XPos > closestRu.XPos)
                         {
                             mu.Move(0);
                         }
                         else if (mu.YPos < closestRu.YPos)
                         {
                             mu.Move(1);
                         }
                         else if (mu.XPos < closestRu.XPos)
                         {
                             mu.Move(2);
                         }
                         else if (mu.YPos > closestRu.YPos)
                         {
                             mu.Move(3);
                         }
                     }
                 }
             }
         }
         else if (map.Units[i] is RangedUnit)
         {
             RangedUnit ru = (RangedUnit)map.Units[i];
             (Unit closest, int distanceTo) = ru.Closest(map.Units);
             if (distanceTo <= ru.AttackRange)
             {
                 ru.IsAttacking = true;
                 ru.Combat(closest);
             }
             else
             {
                 if (closest is MeleeUnit)
                 {
                     MeleeUnit closestMu = (MeleeUnit)closest;
                     if (ru.XPos > closestMu.XPos)
                     {
                         ru.Move(0);
                     }
                     else if (ru.YPos < closestMu.YPos)
                     {
                         ru.Move(1);
                     }
                     else if (ru.XPos < closestMu.XPos)
                     {
                         ru.Move(2);
                     }
                     else if (ru.YPos > closestMu.YPos)
                     {
                         ru.Move(3);
                     }
                 }
                 else if (closest is RangedUnit)
                 {
                     RangedUnit closestRu = (RangedUnit)closest;
                     if (ru.XPos > closestRu.XPos)
                     {
                         ru.Move(0);
                     }
                     else if (ru.YPos < closestRu.YPos)
                     {
                         ru.Move(1);
                     }
                     else if (ru.XPos < closestRu.XPos)
                     {
                         ru.Move(2);
                     }
                     else if (ru.YPos > closestRu.YPos)
                     {
                         ru.Move(3);
                     }
                 }
             }
         }
     }
     for (int i = 0; i < map.Builds.Count; i++)
     {
         if (map.Builds[i] is ResourceBuilding)
         {
             ResourceBuilding rb = (ResourceBuilding)map.Builds[i];
             rb.Generator();
         }
         else
         {
             FactoryBuilding fb = (FactoryBuilding)map.Builds[i];
             if (fb.ProductSpeed >= 7)
             {
                 map.Units.Add(fb.Spawn());
                 fb.ProductSpeed = 0;
             }
             fb.ProductSpeed++;
         }
     }
     map.Display(grpMap);
     round++;
 }
Example #4
0
 public void Display(GroupBox groupBox)
 {
     groupBox.Controls.Clear();
     foreach (Unit u in units)
     {
         Button myButt = new Button();
         if (u is MeleeUnit)
         {
             MeleeUnit mu = (MeleeUnit)u;
             myButt.Size     = new Size(20, 20);
             myButt.Location = new Point(mu.XPos * 20, mu.YPos * 20);
             myButt.Text     = mu.Symbol;
             if (mu.Faction == 0)
             {
                 myButt.ForeColor = Color.Red;
             }
             else
             {
                 myButt.ForeColor = Color.Green;
             }
         }
         else
         {
             RangedUnit ru = (RangedUnit)u;
             myButt.Size     = new Size(20, 20);
             myButt.Location = new Point(ru.XPos * 20, ru.YPos * 20);
             myButt.Text     = ru.Symbol;
             if (ru.Faction == 0)
             {
                 myButt.ForeColor = Color.Red;
             }
             else
             {
                 myButt.ForeColor = Color.Green;
             }
         }
         myButt.Click += Unit_Click;
         groupBox.Controls.Add(myButt);
     }
     foreach (Building b in builds)
     {
         Button myButt = new Button();
         if (b is ResourceBuilding)
         {
             ResourceBuilding rb = (ResourceBuilding)b;
             myButt.Size      = new Size(30, 30);
             myButt.Location  = new Point(rb.PosX * 30, rb.PosY * 30);
             myButt.Text      = rb.Symbol;
             myButt.ForeColor = Color.Gray;
         }
         else
         {
             FactoryBuilding fb = (FactoryBuilding)b;
             myButt.Size     = new Size(30, 30);
             myButt.Location = new Point(fb.PosX * 30, fb.PosY * 30);
             myButt.Text     = fb.Symbol;
             if (fb.Faction == 0)
             {
                 myButt.ForeColor = Color.Red;
             }
             else
             {
                 myButt.ForeColor = Color.Green;
             }
         }
         myButt.Click += Building_Click;
         groupBox.Controls.Add(myButt);
     }
 }