コード例 #1
0
 //Added the Wizard and Neutral faction Wizards to the combat method for Task 3
 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 WizardUnit)
     {
         WizardUnit wu = (WizardUnit)attacker;
         Health = Health - (wu.Attack - wu.AttackRange);
     }
     else if (attacker is RougeWizardUnit)
     {
         RougeWizardUnit wu = (RougeWizardUnit)attacker;
         Health = Health - (wu.Attack - wu.AttackRange);
     }
     if (Health <= 0)
     {
         Death(); //Unit Died.
     }
 }
コード例 #2
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);
            }
            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);
        }
コード例 #3
0
ファイル: Map.cs プロジェクト: MackayNel/Mackay_Nel_Task_3
        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();
                    }
                }
            }
        }
コード例 #4
0
        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++;
        }
コード例 #5
0
ファイル: Map.cs プロジェクト: MackayNel/Mackay_Nel_Task_3
        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);
                }
            }
        }
コード例 #6
0
ファイル: Map.cs プロジェクト: MackayNel/Mackay_Nel_Task_3
        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 if (u is RangedUnit)
                {
                    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);
                }
                else if (u is WizardUnit)
                {//Displays Wizards
                    WizardUnit wu = (WizardUnit)u;
                    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.Green;
                    }

                    b.Click += Unit_Click;
                    groupBox.Controls.Add(b);
                }
                else
                {
                    //Displays Neutral Wizards.
                    Button          wb = new Button();
                    RougeWizardUnit Rw = (RougeWizardUnit)u;
                    wb.Size      = new Size(20, 20);
                    wb.Location  = new Point(Rw.XPos * 20, Rw.YPos * 20);
                    wb.Text      = Rw.Symbol;
                    wb.ForeColor = Color.Purple;

                    wb.Click += NeutralFaction_Click;
                    groupBox.Controls.Add(wb);
                }
            }
            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);
                }
            }
        }