Esempio n. 1
0
        public override (Unit, int) Closest(List <Unit> units)// detrmines the closest other unit to current unit.
        {
            int  shortest = 100;
            Unit closest  = this;



            foreach (Unit u in units)
            {
                if (u is MeleeUnit)
                {
                    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)
                {
                    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);
        }
Esempio n. 2
0
        public void Generate()
        {
            for (int i = 0; i < numUnits; i++)
            {
                if (rnd.Next(0, 3) == 0)
                {
                    MeleeUnit mu = new MeleeUnit(rnd.Next(0, width), rnd.Next(0, height), 100, 1, 10, (i % 2 == 0 ? 1 : 0), "M"); // creates melee unit object
                    units.Add(mu);
                }
                else
                {
                    RangedUnit ru = new RangedUnit(rnd.Next(0, width), rnd.Next(0, height), 100, 1, 30, 5, (i % 2 == 0 ? 1 : 0), "R"); // creates ranged unit object
                    units.Add(ru);
                }

                WizardUnit wu = new WizardUnit(rnd.Next(0, width), rnd.Next(0, height), 100, 1, 5, 5, "W"); // Creates wizard object
                units.Add(wu);
            }

            for (int i = 0; i < numBuildings; i++) //creates factory buildings
            {
                FactoryBuilding f = new FactoryBuilding(rnd.Next(0, width), rnd.Next(0, height), 400, (i % 2 == 0 ? 1 : 0), 5, "FB", (rnd.Next(0, 2) == 0 ? true : false));
                buildings.Add(f);
            }

            for (int i = 0; i < numBuildings; i++) // Creates resource buildings
            {
                ResourceBuilding r = new ResourceBuilding(rnd.Next(0, width), rnd.Next(0, height), 400, (i % 2 == 0 ? 1 : 0), 100, "RB", "Food", 20);
                buildings.Add(r);
            }
        }
        public override void Combat(Unit attacker) // allows for the building to be attacked
        {
            if (attacker is MeleeUnit)
            {
                Health = Health - ((MeleeUnit)attacker).Attack;
            }
            else if (attacker is RangedUnit)
            {
                RangedUnit ru = (RangedUnit)attacker;
                Health = Health - (ru.Attack);
            }

            if (Health <= 0)
            {
                Death();
            }
        }
Esempio n. 4
0
        public override void Combat(Unit attacker)  // the unit takes damage from other unit types
        {
            if (attacker is MeleeUnit)
            {
                Health = Health - ((MeleeUnit)attacker).Attack;
            }
            else if (attacker is RangedUnit)
            {
                RangedUnit ru = (RangedUnit)attacker;
                Health = Health - (ru.Attack);
            }



            if (Health <= 0)
            {
                Death();
            }
        }
Esempio n. 5
0
        public Unit ProduceUnit(int fac)// Produces units from the factory buildings
        {
            if (unitToProduce == true && IsDead == false && maxRound < 100)
            {
                MeleeUnit mu = new MeleeUnit(XPos, spawnPoint, 100, 1, 20, fac, "M");
                unitProduction = mu;
                maxRound++;
            }


            else if (unitToProduce == false && maxRound < 100 && IsDead == false)
            {
                RangedUnit ru = new RangedUnit(XPos, spawnPoint, 100, 1, 15, 5, fac, "R");

                unitProduction = ru;
                maxRound++;
            }


            return(unitProduction);
        }
        public void Update()// Update method is called each tick of the timer in the form. This contains all the necessary code for the game to funcion with each tick.
        {
            for (int i = 0; i < map.Units.Count; i++)
            {
                if (map.Units[i] is MeleeUnit)
                {
                    for (int j = 0; j < map.Buildings.Count; j++)
                    {
                        MeleeUnit mu = (MeleeUnit)map.Units[i];
                        if (map.Buildings[j] is ResourceBuilding)
                        {
                            ResourceBuilding rb = (ResourceBuilding)map.Buildings[j];



                            if (mu.Health <= mu.MaxHealth * 0.25)// if the unit is below 25% hp, the unit flees in a random direction.
                            {
                                mu.Move(r.Next(0, 4));
                            }
                            else
                            {
                                (Unit closest, int distanceTo) = mu.Closest(map.Units);



                                if (distanceTo <= mu.AttackRange)
                                {
                                    mu.IsAttacking = true; //The unit atacks other units
                                    mu.Combat(closest);    // The unit can be attacked
                                    rb.Combat(closest);    //Allows for the builds to be attacked
                                }
                                else
                                {
                                    if (closest is MeleeUnit) // The unit moves towards the closest unit
                                    {
                                        MeleeUnit closestMu = (MeleeUnit)closest;
                                        if (mu.XPos > closestMu.XPos)
                                        {
                                            mu.Move(0);
                                        }
                                        else if (mu.XPos < closestMu.XPos)
                                        {
                                            mu.Move(2);
                                        }
                                        else if (mu.YPos > closestMu.YPos)
                                        {
                                            mu.Move(3);
                                        }
                                        else if (mu.YPos < closestMu.YPos)
                                        {
                                            mu.Move(1);
                                        }
                                    }
                                    else if (closest is RangedUnit)
                                    {
                                        RangedUnit closestRu = (RangedUnit)closest;
                                        if (mu.XPos > closestRu.XPos)
                                        {
                                            mu.Move(0);
                                        }
                                        else if (mu.XPos < closestRu.XPos)
                                        {
                                            mu.Move(2);
                                        }
                                        else if (mu.YPos > closestRu.YPos)
                                        {
                                            mu.Move(3);
                                        }
                                        else if (mu.YPos < closestRu.YPos)
                                        {
                                            mu.Move(1);
                                        }

                                        else if (closest is WizardUnit)
                                        {
                                            WizardUnit closestWu = (WizardUnit)closest;
                                            if (mu.XPos > closestWu.XPos)
                                            {
                                                mu.Move(0);
                                            }
                                            else if (mu.XPos < closestWu.XPos)
                                            {
                                                mu.Move(2);
                                            }
                                            else if (mu.YPos > closestWu.YPos)
                                            {
                                                mu.Move(3);
                                            }
                                            else if (mu.YPos < closestWu.YPos)
                                            {
                                                mu.Move(1);
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        else if (map.Buildings[j] is FactoryBuilding)
                        {
                            FactoryBuilding fb = (FactoryBuilding)map.Buildings[j];



                            if (mu.Health <= mu.MaxHealth * 0.25) // if the unit is below 25% hp, the unit flees in a random direction.
                            {
                                mu.Move(r.Next(0, 4));
                            }
                            else
                            {
                                (Unit closest, int distanceTo) = mu.Closest(map.Units);



                                if (distanceTo <= mu.AttackRange)
                                {
                                    mu.IsAttacking = true; //The unit atacks other units
                                    mu.Combat(closest);    // The unit can be attacked
                                    fb.Combat(closest);    //Allows for the builds to be attacked
                                }
                                else
                                {
                                    if (closest is MeleeUnit)
                                    {
                                        MeleeUnit closestMu = (MeleeUnit)closest;
                                        if (mu.XPos > closestMu.XPos)
                                        {
                                            mu.Move(0);
                                        }
                                        else if (mu.XPos < closestMu.XPos)
                                        {
                                            mu.Move(2);
                                        }
                                        else if (mu.YPos > closestMu.YPos)
                                        {
                                            mu.Move(3);
                                        }
                                        else if (mu.YPos < closestMu.YPos)
                                        {
                                            mu.Move(1);
                                        }
                                    }
                                    else if (closest is RangedUnit)
                                    {
                                        RangedUnit closestRu = (RangedUnit)closest;
                                        if (mu.XPos > closestRu.XPos)
                                        {
                                            mu.Move(0);
                                        }
                                        else if (mu.XPos < closestRu.XPos)
                                        {
                                            mu.Move(2);
                                        }
                                        else if (mu.YPos > closestRu.YPos)
                                        {
                                            mu.Move(3);
                                        }
                                        else if (mu.YPos < closestRu.YPos)
                                        {
                                            mu.Move(1);
                                        }

                                        else if (closest is WizardUnit)
                                        {
                                            WizardUnit closestWu = (WizardUnit)closest;
                                            if (mu.XPos > closestWu.XPos)
                                            {
                                                mu.Move(0);
                                            }
                                            else if (mu.XPos < closestWu.XPos)
                                            {
                                                mu.Move(2);
                                            }
                                            else if (mu.YPos > closestWu.YPos)
                                            {
                                                mu.Move(3);
                                            }
                                            else if (mu.YPos < closestWu.YPos)
                                            {
                                                mu.Move(1);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                else if (map.Units[i] is RangedUnit)
                {
                    for (int j = 0; j < map.Buildings.Count; j++)
                    {
                        RangedUnit ru = (RangedUnit)map.Units[i];
                        if (map.Buildings[j] is ResourceBuilding)
                        {
                            ResourceBuilding rb = (ResourceBuilding)map.Buildings[j];



                            (Unit closest, int distanceTo) = ru.Closest(map.Units);



                            if (distanceTo <= ru.AttackRange) // Checks to see which unit is closest to attack
                            {
                                ru.IsAttacking = true;        //the unit attacks other units in range
                                ru.Combat(closest);           // the unit can be attacked
                                rb.Combat(closest);           //Allows for the builds to be attacked
                            }
                            else
                            {
                                if (closest is MeleeUnit)
                                {
                                    MeleeUnit closestMu = (MeleeUnit)closest;
                                    if (ru.XPos > closestMu.XPos)
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestMu.XPos)
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestMu.YPos)
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestMu.YPos)
                                    {
                                        ru.Move(1);
                                    }
                                }
                                else if (closest is RangedUnit)
                                {
                                    RangedUnit closestRu = (RangedUnit)closest;
                                    if (ru.XPos > closestRu.XPos)
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestRu.XPos)
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestRu.YPos)
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestRu.YPos)
                                    {
                                        ru.Move(1);
                                    }
                                }

                                else if (closest is WizardUnit)
                                {
                                    WizardUnit closestWu = (WizardUnit)closest;
                                    if (ru.XPos > closestWu.XPos)
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestWu.XPos)
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestWu.YPos)
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestWu.YPos)
                                    {
                                        ru.Move(1);
                                    }
                                }
                            }
                        }


                        else if (map.Buildings[j] is FactoryBuilding)
                        {
                            FactoryBuilding fb = (FactoryBuilding)map.Buildings[j];



                            (Unit closest, int distanceTo) = ru.Closest(map.Units);



                            if (distanceTo <= ru.AttackRange) // Checks to see which unit is closest to attack
                            {
                                ru.IsAttacking = true;        // allows the unit to attack others
                                ru.Combat(closest);           // Allows for the unnnit to be attacked
                                fb.Combat(closest);           //Allows for the builds to be attacked
                            }
                            else
                            {
                                if (closest is MeleeUnit)
                                {
                                    MeleeUnit closestMu = (MeleeUnit)closest;
                                    if (ru.XPos > closestMu.XPos)
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestMu.XPos)
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestMu.YPos)
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestMu.YPos)
                                    {
                                        ru.Move(1);
                                    }
                                }
                                else if (closest is RangedUnit)
                                {
                                    RangedUnit closestRu = (RangedUnit)closest;
                                    if (ru.XPos > closestRu.XPos)
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestRu.XPos)
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestRu.YPos)
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestRu.YPos)
                                    {
                                        ru.Move(1);
                                    }
                                }

                                else if (closest is WizardUnit)
                                {
                                    WizardUnit closestWu = (WizardUnit)closest;
                                    if (ru.XPos > closestWu.XPos)
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestWu.XPos)
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestWu.YPos)
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestWu.YPos)
                                    {
                                        ru.Move(1);
                                    }
                                }
                            }
                        }



                        else if (map.Units[i] is WizardUnit)
                        {
                            WizardUnit wu = (WizardUnit)map.Units[i];



                            if (wu.Health <= wu.MaxHealth * 0.50)// Wizard flees if they are below 50% hp
                            {
                                wu.Move(r.Next(0, 4));
                            }
                            else
                            {
                                (Unit closest, int distanceTo) = wu.Closest(map.Units);



                                if (distanceTo <= wu.AttackRange)
                                {
                                    wu.IsAttacking = true; //allows for the uit to attack other units
                                    wu.Combat(closest);    //Allows for the unit to be attacked
                                }
                                else
                                {
                                    if (closest is MeleeUnit)
                                    {
                                        MeleeUnit closestMu = (MeleeUnit)closest;
                                        if (wu.XPos > closestMu.XPos)
                                        {
                                            wu.Move(0);
                                        }
                                        else if (wu.XPos < closestMu.XPos)
                                        {
                                            wu.Move(2);
                                        }
                                        else if (wu.YPos > closestMu.YPos)
                                        {
                                            wu.Move(3);
                                        }
                                        else if (wu.YPos < closestMu.YPos)
                                        {
                                            wu.Move(1);
                                        }
                                    }
                                    else if (closest is RangedUnit)
                                    {
                                        RangedUnit closestRu = (RangedUnit)closest;
                                        if (wu.XPos > closestRu.XPos)
                                        {
                                            wu.Move(0);
                                        }
                                        else if (wu.XPos < closestRu.XPos)
                                        {
                                            wu.Move(2);
                                        }
                                        else if (wu.YPos > closestRu.YPos)
                                        {
                                            wu.Move(3);
                                        }
                                        else if (wu.YPos < closestRu.YPos)
                                        {
                                            wu.Move(1);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }



            currUnits.Text = "Number of units on the map: " + map.count + "";
            map.Display(grpMap);
            Round++;
        }
Esempio n. 7
0
        public void Display(GroupBox grpBox)
        {
            grpBox.Controls.Clear();

            foreach (Unit u in units)
            {
                Button b = new Button();
                if (u is MeleeUnit) // Creates the buttons on the form to display the melee units
                {
                    MeleeUnit mu = (MeleeUnit)u;
                    b.Size     = new Size(20, 20);
                    b.Location = new Point(mu.XPos * width, mu.YPos * height);
                    b.Text     = mu.Symbol;
                    if (mu.Faction == 0)
                    {
                        b.ForeColor = Color.Red;
                    }
                    else
                    {
                        b.ForeColor = Color.Blue;
                    }
                }
                else if (u is RangedUnit)
                {
                    RangedUnit ru = (RangedUnit)u;  // Creates the buttons on the form to display the ranged units
                    b.Size     = new Size(20, 20);
                    b.Location = new Point(ru.XPos * width, ru.YPos * height);
                    b.Text     = ru.Symbol;
                    if (ru.Faction == 0)
                    {
                        b.ForeColor = Color.Red;
                    }
                    else
                    {
                        b.ForeColor = Color.Blue;
                    }
                }
                else if (u is WizardUnit)
                {
                    WizardUnit wu = (WizardUnit)u; // Creates the information link on the form to display the wizard units
                    b.Size      = new Size(20, 20);
                    b.Location  = new Point(wu.XPos * width, wu.YPos * height);
                    b.Text      = wu.Symbol;
                    b.ForeColor = Color.Green;
                }

                b.Click += Unit_Click;
                grpBox.Controls.Add(b);
            }



            foreach (Building b in buildings)
            {
                Button B = new Button();
                if (b is FactoryBuilding)
                {
                    FactoryBuilding fb = (FactoryBuilding)b;   // Creates the buttons on the form to display the factory buildings

                    if (unitSpawnTimer == fb.ProductionSpeed && resources != 0)
                    {
                        for (int i = 0; i < numBuildings; i++)
                        {
                            units.Add(fb.ProduceUnit(fb.Faction));
                            resources = resources - 20;
                        }

                        unitSpawnTimer = 0;
                    }


                    B.Size     = new Size(30, 30);
                    B.Location = new Point(fb.XPos * width, fb.YPos * height);
                    B.Text     = fb.Symbol;

                    if (fb.Faction == 0)
                    {
                        B.ForeColor = Color.Red;
                    }
                    else
                    {
                        B.ForeColor = Color.Blue;
                    }
                }
                else
                {
                    ResourceBuilding rb = (ResourceBuilding)b;  // Creates the buttons on the form to display the resource buildings

                    B.Size     = new Size(30, 30);
                    B.Location = new Point(rb.XPos * width, rb.YPos * height);
                    B.Text     = rb.Symbol;
                    rb.ResourceManagement();
                    resources = rb.resourceGenerated;

                    if (rb.Faction == 0)
                    {
                        B.ForeColor = Color.Red;
                    }
                    else
                    {
                        B.ForeColor = Color.Blue;
                    }
                }

                B.Click += Unit_Click;
                grpBox.Controls.Add(B);
            }
            unitSpawnTimer++;
            count = units.Count();
        }
Esempio n. 8
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) // allows for the button to display information to the text box for the ranged unit
                {
                    RangedUnit ru = (RangedUnit)u;
                    if (ru.XPos == x && ru.YPos == y)
                    {
                        txtInfo.Text = "";
                        txtInfo.Text = ru.ToString();
                    }
                }
                else if (u is MeleeUnit)  // allows for the button to display information to the text box for the melee unit
                {
                    MeleeUnit mu = (MeleeUnit)u;
                    if (mu.XPos == x && mu.YPos == y)
                    {
                        txtInfo.Text = "";
                        txtInfo.Text = mu.ToString();
                    }
                }

                else if (u is WizardUnit) // allows for the button to display information to the text box for the wizard unit
                {
                    WizardUnit wu = (WizardUnit)u;
                    if (wu.XPos == x && wu.YPos == y)
                    {
                        txtInfo.Text = "";
                        txtInfo.Text = wu.ToString();
                    }
                }
            }

            foreach (Building B in buildings)
            {
                if (B is FactoryBuilding)
                {
                    FactoryBuilding fb = (FactoryBuilding)B; // allows for the button to display information to the text box for the factory building
                    if (fb.XPos == x && fb.YPos == y)
                    {
                        txtInfo.Text = "";
                        txtInfo.Text = fb.ToString();
                    }
                }
                else if (B is ResourceBuilding)
                {
                    ResourceBuilding rb = (ResourceBuilding)B; // allows for the button to display information to the text box for the resource building
                    if (rb.XPos == x && rb.YPos == y)
                    {
                        txtInfo.Text = "";
                        txtInfo.Text = rb.ToString() + "           Resources Produced: " + resources;
                    }
                }
            }
        }