Beispiel #1
0
        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 && u != this)
                {
                    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 && u != this)
                {
                    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);
        }
Beispiel #2
0
        // shows units once they are clicked
        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 WizardUnit)
                {
                    WizardUnit wu = (WizardUnit)u;
                    if (wu.XPos == x && wu.YPos == y)
                    {
                        txtInfo.Text = "";
                        txtInfo.Text = wu.ToString();
                    }
                    else if (u is MeleeUnit)
                    {
                        MeleeUnit mu = (MeleeUnit)u;
                        if (mu.XPos == x && mu.YPos == y)
                        {
                            txtInfo.Text = "";
                            txtInfo.Text = mu.ToString();
                        }
                    }
                }
                foreach (Building bu in building)
                {
                    if (bu is ResourceBuilding)
                    {
                        ResourceBuilding ru = (ResourceBuilding)bu;
                        if (ru.XPos == x && ru.YPos == y)
                        {
                            txtInfo.Text = "";
                            txtInfo.Text = ru.ToString();
                        }
                    }
                    else if (bu is FactoryBuilding)
                    {
                        FactoryBuilding mu = (FactoryBuilding)bu;
                        if (mu.XPos == x && mu.YPos == y)
                        {
                            txtInfo.Text = "";
                            txtInfo.Text = mu.ToString();
                        }
                    }
                }
            }
        }
Beispiel #3
0
        // save method for units
        public void TheProperSave()
        {
            StreamWriter units     = new StreamWriter("Local_Unit_Save.txt");
            StreamWriter buildings = new StreamWriter("Local_Buildings_Save.txt");

            foreach (Unit u in map.Units)
            {
                if (u is RangedUnit)
                {
                    RangedUnit uu = (RangedUnit)u;
                    if (uu.IsDead)
                    {
                        continue;
                    }
                    uu.Save(units);
                }
                else
                {
                    MeleeUnit uu = (MeleeUnit)u;
                    if (uu.IsDead)
                    {
                        continue;
                    }
                    uu.Save(units);
                }
            }
            foreach (Building u in map.Buildings)
            {
                if (u is FactoryBuilding)
                {
                    FactoryBuilding uu = (FactoryBuilding)u;
                    if (uu.isDead())
                    {
                        continue;
                    }
                    uu.Save(buildings);
                }
                else
                {
                    ResourceBuilding uu = (ResourceBuilding)u;
                    if (uu.isDead())
                    {
                        continue;
                    }
                    uu.Save(buildings);
                }
            }
            units.Close();
            buildings.Close();
        }
Beispiel #4
0
        // combat for attackers
        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(); //DEATH !!!
            }
        }
Beispiel #5
0
        // displays units on map
        public void Display(GroupBox groupBox)
        {
            groupBox.Controls.Clear();
            foreach (Unit u in units)
            {
                Button b = new Button();

                if (u is WizardUnit)
                {
                    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 == 3)
                    {
                        b.ForeColor = Color.Violet;
                    }
                }
                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;
                    }
                }
                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);
            }
            foreach (Building b in building)
            {
                Button button = new Button();
                if (b is FactoryBuilding)
                {
                    FactoryBuilding bb = (FactoryBuilding)b;
                    button.Size     = new Size(20, 20);
                    button.Location = new Point(bb.XPos * 20, bb.YPos * 20);
                    button.Text     = bb.Symbol;
                    if (bb.Faction == 0)
                    {
                        button.ForeColor = Color.Red;
                    }
                    else
                    {
                        button.ForeColor = Color.Green;
                    }
                }
                else
                {
                    ResourceBuilding rb = (ResourceBuilding)b;
                    button.Size     = new Size(20, 20);
                    button.Location = new Point(rb.XPos * 20, rb.YPos * 20);
                    button.Text     = rb.Symbol;
                    if (rb.Faction == 0)
                    {
                        button.ForeColor = Color.Red;
                    }
                    else
                    {
                        button.ForeColor = Color.Green;
                    }
                }
                button.Click += Unit_Click;
                groupBox.Controls.Add(button);
            }
        }
Beispiel #6
0
        // updates game information
        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);
                        (Building bclosest, int bdistance) = mu.Closest(map.Buildings);
                        //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)
                            {
                                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];

                    (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);
                            }
                        }
                    }
                }
            }
            for (int i = 0; i < map.Buildings.Count; i++)
            {
                Building bitch = map.Buildings[i];
                if (bitch is ResourceBuilding)
                {
                    ResourceBuilding bREE = (ResourceBuilding)bitch;

                    switch (bREE.Faction)
                    {
                    case 0:
                    {
                        FactionZeroR += bREE.MineResource();
                    }
                    break;

                    case 1:
                    {
                        FactionOneR += bREE.MineResource();
                    }
                    break;

                    default: break;
                    }
                }
                else if (bitch is FactoryBuilding)
                {
                    FactoryBuilding bFac = (FactoryBuilding)bitch;
                    int             a    = bFac.Faction;
                    switch (a)
                    {
                    case 0:
                    {
                        if (FactionZeroR >= 6)
                        {
                            map.Units.Add(bFac.UnitSpawn());
                        }
                    }
                    break;

                    case 1:
                    {
                        if (FactionOneR >= 6)
                        {
                            map.Units.Add(bFac.UnitSpawn());
                        }
                    }
                    break;

                    default: break;
                    }
                }
                else
                {
                    Console.WriteLine("NANI THE F**K YOU HERE FOR ");
                }
            }


            map.Display(grpMap);
            round++;
        }
Beispiel #7
0
        //Code given by Declan Porter
        public void DeclanFunctionalApproachToTheSaveFunction()
        {
            StreamWriter writer   = new StreamWriter("Local_Save.txt");
            string       Faction0 = "";
            string       Faction1 = "";

            writer.WriteLine("START OF SAVE");
            foreach (Unit u in map.Units)
            {
                if (u is MeleeUnit)
                {
                    MeleeUnit uu = (MeleeUnit)u;
                    if (uu.Faction == 0)
                    {
                        Faction0 += uu.ToString() + "\n";
                    }
                    else
                    {
                        Faction1 += uu.ToString() + "\n";
                    }
                }
                else if (u is RangedUnit)
                {
                    RangedUnit uu = (RangedUnit)u;
                    if (uu.Faction == 0)
                    {
                        Faction0 += uu.ToString() + "\n";
                    }
                    else
                    {
                        Faction1 += uu.ToString() + "\n";
                    }
                }
            }
            writer.WriteLine("UNITS -- FACTION 0");
            writer.WriteLine(Faction0);
            writer.WriteLine("UNITS -- FACTION 1");
            writer.WriteLine(Faction1);
            Faction0 = "";
            Faction1 = "";
            foreach (Building u in map.Buildings)
            {
                if (u is ResourceBuilding)
                {
                    ResourceBuilding uu = (ResourceBuilding)u;
                    if (uu.Faction == 0)
                    {
                        Faction0 += uu.ToString() + "\n";
                    }
                    else
                    {
                        Faction1 += uu.ToString() + "\n";
                    }
                }
                else if (u is FactoryBuilding)
                {
                    FactoryBuilding uu = (FactoryBuilding)u;
                    if (uu.Faction == 0)
                    {
                        Faction0 += uu.ToString() + "\n";
                    }
                    else
                    {
                        Faction1 += uu.ToString() + "\n";
                    }
                }
            }

            writer.WriteLine("BUILDINGS-- FACTION 0");
            writer.WriteLine(Faction0);
            writer.WriteLine("BUILDINGS-- FACTION 1");
            writer.WriteLine(Faction1);
            writer.WriteLine("END OF SAVE");
        }