Example #1
0
 public Unit UnitSpawn() //Detirmines what unit spawns
 {
     if (buildingFaction == 0)
     {
         Unit unit;
         if (UnitGenerate == 0)
         {
             MeleeUnit mu = new MeleeUnit(BuildSpawnX,
                                          BuildSpawnY + 1,
                                          50,
                                          1,
                                          10,
                                          0,
                                          "M", "Knight");
             unit = mu;
         }
         else
         {
             RangedUnit ru = new RangedUnit(BuildSpawnX,
                                            BuildSpawnY + 1,
                                            35,
                                            2,
                                            5,
                                            3,
                                            0,
                                            "R", "Wizard");
             unit = ru;
         }
         return(unit);
     }
     else
     {
         Unit unit;
         if (UnitGenerate == 0)
         {
             MeleeUnit mu = new MeleeUnit(BuildSpawnX,
                                          BuildSpawnY + 1,
                                          50,
                                          1,
                                          10,
                                          1,
                                          "m", "knight");
             unit = mu;
         }
         else
         {
             RangedUnit ru = new RangedUnit(BuildSpawnX,
                                            BuildSpawnY + 1,
                                            35,
                                            2,
                                            5,
                                            3,
                                            1,
                                            "r", "Archer");
             unit = ru;
         }
         return(unit);
     }
 }
        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);
            }
            else if (a is WizardUnit && b is MeleeUnit)
            {
                WizardUnit start = (WizardUnit)a;
                MeleeUnit  end   = (MeleeUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            else if (a is WizardUnit && b is RangedUnit)
            {
                WizardUnit start = (WizardUnit)a;
                RangedUnit end   = (RangedUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            else if (a is RougeWizardUnit && b is RangedUnit)
            {
                RougeWizardUnit start = (RougeWizardUnit)a;
                RangedUnit      end   = (RangedUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            else if (a is RougeWizardUnit && b is MeleeUnit)
            {
                RougeWizardUnit start = (RougeWizardUnit)a;
                MeleeUnit       end   = (MeleeUnit)b;
                distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos);
            }
            return(distance);
        }
Example #3
0
        public void Generate()
        {
            for (int i = 0; i < numUnits; i++)//Generate Melee Unit.
            {
                if (rd.Next(0, 2) == 0)
                {
                    MeleeUnit m = new MeleeUnit(rd.Next(0, 20),
                                                rd.Next(0, 20),
                                                100,
                                                1,
                                                20,
                                                (i % 2 == 0 ? 1 : 0),
                                                "M",
                                                "Knight");//Task 2 Q)2.3 added unit types
                    units.Add(m);
                }
                else// Generate Ranged Unit
                {
                    RangedUnit r = new RangedUnit(rd.Next(0, 20),
                                                  rd.Next(0, 20),
                                                  100,
                                                  1,
                                                  20,
                                                  5,
                                                  (i % 2 == 0 ? 1 : 0),
                                                  "R",
                                                  "Wizard");//Task 2 Q)2.3 added unit types
                    units.Add(r);
                }
            }

            for (int j = 0; j < numBuildings; j++)
            {
                if (rd.Next(0, 2) == 0)
                {
                    ResourceBuilding a = new ResourceBuilding(rd.Next(0, 20),
                                                              rd.Next(0, 20),
                                                              500,
                                                              (j % 2 == 0 ? 1 : 0),
                                                              "[RS]");
                    buildings.Add(a);
                }
                else
                {
                    FactoryBuilding f = new FactoryBuilding(rd.Next(0, 20),
                                                            rd.Next(0, 20),
                                                            500,
                                                            (j % 2 == 0 ? 1 : 0),
                                                            "[FS]");
                    buildings.Add(f);
                }
            }
        }
Example #4
0
 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(); //Unit Died.
     }
 }
Example #5
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)
                    {
                        infoTxtBox.Text = "";
                        infoTxtBox.Text = ru.ToString();
                    }
                }
                else if (u is MeleeUnit)
                {
                    MeleeUnit mu = (MeleeUnit)u;
                    if (mu.XPos == x && mu.YPos == y)
                    {
                        infoTxtBox.Text = "";
                        infoTxtBox.Text = mu.ToString();
                    }
                }
                else if (u is WizardUnit)
                {
                    WizardUnit wu = (WizardUnit)u;
                    if (wu.XPos == x && wu.YPos == y)
                    {
                        infoTxtBox.Text = "";
                        infoTxtBox.Text = wu.ToString();
                    }
                }
            }
        }
        public void Update()
        {
            foreach (Building bs in map.Buildings)
            {
                if (bs is FactoryBuilding)
                {
                    FactoryBuilding fb = (FactoryBuilding)bs;
                    if (fb.ProductionSpeed % Round == 0)
                    {
                        map.Units.Add(fb.UnitSpawn());
                    }
                }
            }

            for (int i = 0; i < map.Units.Count; i++)
            {
                if (map.Units[i] is MeleeUnit)
                {
                    MeleeUnit mu = (MeleeUnit)map.Units[i];
                    // mu.Health = 10;//HandiCap
                    if (mu.Health <= mu.MaxHealth * 0.25)//Moves away if unit is damaged
                    {
                        mu.Move(r.Next(0, 4));
                    }
                    else
                    {
                        int  shortest = 100;
                        Unit closestM = 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;
                                    closestM = otherMu;
                                }
                            }
                        }
                        //Check in Range
                        int distanceTo = 0;
                        if (distanceTo <= mu.AttackRange)
                        {
                            mu.IsAttacking = true;
                            mu.Combat(closestM);
                        }
                        else//Move to closestM enemy unit!
                        {
                            if (closestM is MeleeUnit)
                            {
                                MeleeUnit closestMMu = (MeleeUnit)closestM;
                                if (mu.XPos > closestMMu.XPos)
                                {
                                    mu.Move(0);//North
                                }
                                else if (mu.XPos < closestMMu.XPos)
                                {
                                    mu.Move(2);//South
                                }
                                else if (mu.YPos > closestMMu.XPos)
                                {
                                    mu.Move(3);//West
                                }
                                else if (mu.YPos < closestMMu.XPos)
                                {
                                    mu.Move(1);//East
                                }
                            }
                            else if (closestM is RangedUnit)
                            {
                                RangedUnit closestMMu = (RangedUnit)closestM;
                                if (mu.XPos > closestMMu.XPos)
                                {
                                    mu.Move(0);//North
                                }
                                else if (mu.XPos < closestMMu.XPos)
                                {
                                    mu.Move(2);//South
                                }
                                else if (mu.YPos > closestMMu.XPos)
                                {
                                    mu.Move(3);//West
                                }
                                else if (mu.YPos < closestMMu.XPos)
                                {
                                    mu.Move(1);//East
                                }
                            }
                        }
                    }
                }
                if (map.Units[i] is RangedUnit)
                {
                    RangedUnit ru = (RangedUnit)map.Units[i];
                    if (ru.Health <= ru.MaxHealth * 0.25)//Moves away if unit is damaged
                    {
                        ru.Move(r.Next(0, 4));
                    }
                    else
                    {
                        int  shortest = 100;
                        Unit closestR = 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;
                                    closestR = otherRu;
                                }
                            }
                        }
                        int distanceTo = 0;
                        if (distanceTo <= ru.AttackRange)
                        {
                            ru.IsAttacking = true;
                            ru.Combat(closestR);
                        }
                        else
                        {
                            if (closestR is MeleeUnit)
                            {
                                MeleeUnit closestMMu = (MeleeUnit)closestR;
                                if (ru.XPos > closestMMu.XPos)
                                {
                                    ru.Move(0);//North
                                }
                                else if (ru.XPos < closestMMu.XPos)
                                {
                                    ru.Move(2);//South
                                }
                                else if (ru.YPos > closestMMu.XPos)
                                {
                                    ru.Move(3);//West
                                }
                                else if (ru.YPos < closestMMu.XPos)
                                {
                                    ru.Move(1);//East
                                }
                            }
                            else if (closestR is RangedUnit)
                            {
                                RangedUnit closestMMu = (RangedUnit)closestR;
                                if (ru.XPos > closestMMu.XPos)
                                {
                                    ru.Move(0);//North
                                }
                                else if (ru.XPos < closestMMu.XPos)
                                {
                                    ru.Move(2);//South
                                }
                                else if (ru.YPos > closestMMu.XPos)
                                {
                                    ru.Move(3);//West
                                }
                                else if (ru.YPos < closestMMu.XPos)
                                {
                                    ru.Move(1);//East
                                }
                            }
                        }
                    }
                }
                if (map.Units[i] is WizardUnit)
                {
                    WizardUnit wu = (WizardUnit)map.Units[i];
                    if (wu.Health <= wu.MaxHealth * 0.25)//Moves away if unit is damaged
                    {
                        wu.Move(r.Next(0, 4));
                    }
                    else
                    {
                        int  shortest = 100;
                        Unit closestR = wu;
                        foreach (Unit u in map.Units)
                        {
                            if (u is WizardUnit)
                            {
                                WizardUnit otherWu  = (WizardUnit)u;
                                int        distance = Math.Abs(wu.XPos - otherWu.XPos)
                                                      + Math.Abs(wu.YPos - otherWu.YPos);
                                if (distance < shortest)
                                {
                                    shortest = distance;
                                    closestR = otherWu;
                                }
                            }
                        }
                        int distanceTo = 0;
                        if (distanceTo <= wu.AttackRange)
                        {
                            wu.IsAttacking = true;
                            wu.Combat(closestR);
                        }
                        else
                        {
                            if (closestR is MeleeUnit)
                            {
                                MeleeUnit closestMMu = (MeleeUnit)closestR;
                                if (wu.XPos > closestMMu.XPos)
                                {
                                    wu.Move(0);//North
                                }
                                else if (wu.XPos < closestMMu.XPos)
                                {
                                    wu.Move(2);//South
                                }
                                else if (wu.YPos > closestMMu.XPos)
                                {
                                    wu.Move(3);//West
                                }
                                else if (wu.YPos < closestMMu.XPos)
                                {
                                    wu.Move(1);//East
                                }
                            }
                            else if (closestR is RangedUnit)
                            {
                                RangedUnit closestMMu = (RangedUnit)closestR;
                                if (wu.XPos > closestMMu.XPos)
                                {
                                    wu.Move(0);//North
                                }
                                else if (wu.XPos < closestMMu.XPos)
                                {
                                    wu.Move(2);//South
                                }
                                else if (wu.YPos > closestMMu.XPos)
                                {
                                    wu.Move(3);//West
                                }
                                else if (wu.YPos < closestMMu.XPos)
                                {
                                    wu.Move(1);//East
                                }
                            }
                        }
                    }
                }
                //Added Neutral Wizard Faction[Task 3]
                if (map.Units[i] is RougeWizardUnit)
                {
                    RougeWizardUnit rwu = (RougeWizardUnit)map.Units[i];
                    rwu.Health = 30;
                    if (rwu.Health <= rwu.MaxHealth * 0.25)//Moves away if unit is damaged
                    {
                        rwu.Move(r.Next(0, 4));
                    }
                    else
                    {
                        int  shortest = 100;
                        Unit closestR = rwu;
                        foreach (Unit u in map.Units)
                        {
                            if (u is WizardUnit)
                            {
                                WizardUnit otherWu  = (WizardUnit)u;
                                int        distance = Math.Abs(rwu.XPos - otherWu.XPos)
                                                      + Math.Abs(rwu.YPos - otherWu.YPos);
                                if (distance < shortest)
                                {
                                    shortest = distance;
                                    closestR = otherWu;
                                }
                            }
                        }
                        int distanceTo = 0;
                        if (distanceTo <= rwu.AttackRange)
                        {
                            rwu.IsAttacking = true;
                            rwu.Combat(closestR);
                        }
                        else
                        {
                            if (closestR is MeleeUnit)
                            {
                                MeleeUnit closestMMu = (MeleeUnit)closestR;
                                if (rwu.XPos > closestMMu.XPos)
                                {
                                    rwu.Move(0);//North
                                }
                                else if (rwu.XPos < closestMMu.XPos)
                                {
                                    rwu.Move(2);//South
                                }
                                else if (rwu.YPos > closestMMu.XPos)
                                {
                                    rwu.Move(3);//West
                                }
                                else if (rwu.YPos < closestMMu.XPos)
                                {
                                    rwu.Move(1);//East
                                }
                            }
                            else if (closestR is RangedUnit)
                            {
                                RangedUnit closestMMu = (RangedUnit)closestR;
                                if (rwu.XPos > closestMMu.XPos)
                                {
                                    rwu.Move(0);//North
                                }
                                else if (rwu.XPos < closestMMu.XPos)
                                {
                                    rwu.Move(2);//South
                                }
                                else if (rwu.YPos > closestMMu.XPos)
                                {
                                    rwu.Move(3);//West
                                }
                                else if (rwu.YPos < closestMMu.XPos)
                                {
                                    rwu.Move(1);//East
                                }
                            }
                        }
                    }
                }
            }

            //

            map.Display(grpMap);
            round++;
        }
Example #7
0
        //public void GenerateBuilding() //Makes Buildings
        //{
        //    for (int i = 0; i < numBuildings; i++)
        //    {
        //        if (rd.Next(0, 2) == 0)
        //        {
        //            ResourceBuilding a = new ResourceBuilding(rd.Next(0, 20),
        //                                                      rd.Next(0, 20),
        //                                                      500,
        //                                                      (i % 2 == 0 ? 1 : 0),
        //                                                      "[RS]");
        //            buildings.Add(a);
        //        }
        //        else
        //        {
        //            FactoryBuilding f = new FactoryBuilding(rd.Next(0, 20),
        //                                                      rd.Next(0, 20),
        //                                                      500,
        //                                                      (i % 2 == 0 ? 1 : 0),
        //                                                      "[FS]");
        //            buildings.Add(f);
        //        }
        //    }

        //}

        public void Display(GroupBox groupBox)
        {
            //GenerateBuilding();
            groupBox.Controls.Clear();
            foreach (Unit u in units)
            {
                Button b = new Button();
                if (u is MeleeUnit)
                {
                    MeleeUnit mu = (MeleeUnit)u;
                    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.Green;
                    }
                    b.Click += Unit_Click;
                    groupBox.Controls.Add(b);
                }
                else
                {
                    RangedUnit ru = (RangedUnit)u;
                    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.Green;
                    }
                    b.Click += Unit_Click;
                    groupBox.Controls.Add(b);
                }
            }
            foreach (Building d in buildings)
            {
                Button bb = new Button();
                if (d is ResourceBuilding)
                {
                    ResourceBuilding Ab = (ResourceBuilding)d;
                    bb.Size     = new Size(40, 40);
                    bb.Location = new Point(Ab.buildingX * 20, Ab.buildingY * 20);
                    bb.Text     = Ab.buildingSymbol;
                    if (Ab.buildingFaction == 0)
                    {
                        bb.ForeColor = Color.Red;
                    }
                    else
                    {
                        bb.ForeColor = Color.Green;
                    }
                    bb.Click += Building_Click;
                    groupBox.Controls.Add(bb);
                }
                else
                {
                    FactoryBuilding FB = (FactoryBuilding)d;
                    bb.Size     = new Size(40, 40);
                    bb.Location = new Point(FB.buildingX * 20, FB.buildingY * 20);
                    bb.Text     = FB.buildingSymbol;
                    if (FB.buildingFaction == 0)
                    {
                        bb.ForeColor = Color.Red;
                    }
                    else
                    {
                        bb.ForeColor = Color.Green;
                    }
                    bb.Click += Building_Click;
                    groupBox.Controls.Add(bb);
                }
            }
        }
Example #8
0
        public void Generate()
        {
            for (int i = 0; i < numUnits; i++)//Generate Melee Unit.
            {
                if (rd.Next(0, 4) == 0)
                {
                    MeleeUnit m = new MeleeUnit(rd.Next(0, 20),
                                                rd.Next(0, 20),
                                                100,
                                                1,
                                                20,
                                                (i % 2 == 0 ? 1 : 0),
                                                "M",
                                                "Knight");
                    units.Add(m);
                }
                else if (rd.Next(0, 4) == 1) // Generate Ranged Unit
                {
                    RangedUnit r = new RangedUnit(rd.Next(0, 20),
                                                  rd.Next(0, 20),
                                                  100,
                                                  1,
                                                  20,
                                                  5,
                                                  (i % 2 == 0 ? 1 : 0),
                                                  "R",
                                                  "Musketeer");// [Update]: For task 3 I changed the units from wizards to musketeers as wizards are now their own unit type.
                    units.Add(r);
                }
                else if (rd.Next(0, 4) == 2)
                {
                    WizardUnit w = new WizardUnit(rd.Next(0, 20),
                                                  rd.Next(0, 20),
                                                  100,
                                                  1,
                                                  20,
                                                  5,
                                                  (i % 2 == 0 ? 1 : 0),
                                                  "W",
                                                  "Wizard");//Task 3: Added the wizard.
                    units.Add(w);
                }
                else //[Task 3] Generates Neutral Wizzards in their own faction.
                {
                    RougeWizardUnit wr = new RougeWizardUnit(rd.Next(0, 20),
                                                             rd.Next(0, 20),
                                                             100,
                                                             1,
                                                             20,
                                                             5,
                                                             (i % 2 == 0 ? 1 : 0),
                                                             "W",
                                                             "Neutral_Wizard");//Task 3: Added the Neutral wizard. aka Rouge wizard as they are in their own faction.
                    units.Add(wr);
                }
            }

            for (int j = 0; j < numBuildings; j++)
            {
                if (rd.Next(0, 2) == 0)
                {
                    ResourceBuilding a = new ResourceBuilding(rd.Next(0, 20),
                                                              rd.Next(0, 20),
                                                              500,
                                                              (j % 2 == 0 ? 1 : 0),
                                                              "[RS]");
                    buildings.Add(a);
                }
                else
                {
                    FactoryBuilding f = new FactoryBuilding(rd.Next(0, 20),
                                                            rd.Next(0, 20),
                                                            500,
                                                            (j % 2 == 0 ? 1 : 0),
                                                            "[FS]");
                    buildings.Add(f);
                }
            }
        }