//Determines the distance between two units 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); } //Add wizards else if (a is WizardUnit && b is WizardUnit) { WizardUnit start = (WizardUnit)a; WizardUnit end = (WizardUnit)b; distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos); } else if (a is MeleeUnit && b is WizardUnit) { 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 RangedUnit && b is WizardUnit) { WizardUnit start = (WizardUnit)a; RangedUnit end = (RangedUnit)b; distance = Math.Abs(start.XPos - end.XPos) + Math.Abs(start.YPos - end.YPos); } return(distance); }
//Handling additional distance calculations 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) { 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; } } else if (u is WizardUnit) { WizardUnit otherWu = (WizardUnit)u; int distance = Math.Abs(this.XPos - otherWu.XPos) + Math.Abs(this.YPos - otherWu.YPos); if (distance < shortest) { shortest = distance; closest = otherWu; } } } return(closest, shortest); }
//Handles combat 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); } if (Health <= 0) { Death(); } }
//Adds a unit's info to the ToString public void Unit_Click(object sender, EventArgs e) { int x, y; Button b = (Button)sender; x = b.Location.X / 30; y = b.Location.Y / 30; 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 MeleeUnit) { MeleeUnit mu = (MeleeUnit)u; if (mu.XPos == x && mu.YPos == y) { txtInfo.Text = ""; txtInfo.Text = mu.ToString(); } } else if (u is WizardUnit) { WizardUnit wu = (WizardUnit)u; if (wu.XPos == x && wu.YPos == y) { txtInfo.Text = ""; txtInfo.Text = wu.ToString(); } } } }
//Handles generation of the units public void Generate() { //PvP Teams for (int i = 0; i < numUnits; i++) { if (r.Next(0, 3) == 0) //Generate Melee Unit { MeleeUnit m = new MeleeUnit(r.Next(0, width), r.Next(0, height), 100, 1, 25, (i % 2 == 0 ? 1 : 0), "M/"); units.Add(m); } else if (r.Next(0, 3) == 1) // Generate Ranged Unit { RangedUnit ru = new RangedUnit(r.Next(0, width), r.Next(0, height), 75, 2, 20, 5, (i % 2 == 0 ? 1 : 0), "R}"); units.Add(ru); } else { WizardUnit wu = new WizardUnit(r.Next(0, width), r.Next(0, height), 50, 5, 50, 1, 3, "W-"); units.Add(wu); } } for (int k = 0; k < numBuildings; k++) { if (r.Next(0, 2) == 0) //Generate Resource Building { ResourceBuilding rb = new ResourceBuilding(r.Next(0, width), r.Next(0, height), 150, (k % 2 == 0 ? 1 : 0), "[G]", false); buildings.Add(rb); } else //Generate Unit Building { FactoryBuilding fb = new FactoryBuilding(r.Next(0, width), r.Next(0, height), 200, (k % 2 == 0 ? 1 : 0), "[F]", false, (r.Next(0, 2) == 1 ? "Melee" : "Ranged")); buildings.Add(fb); } } }
//Displays the units onto the form public void Display(GroupBox groupBox) { groupBox.Controls.Clear(); //Adding Units foreach (Unit u in units) { Button b = new Button(); if (u is MeleeUnit) { MeleeUnit mu = (MeleeUnit)u; b.Size = new Size(30, 30); b.Location = new Point(mu.XPos * 30, mu.YPos * 30); b.Text = mu.Symbol; if (mu.Faction == 0) { b.ForeColor = Color.HotPink; } else { b.ForeColor = Color.Blue; } } else if (u is RangedUnit) { RangedUnit ru = (RangedUnit)u; b.Size = new Size(30, 30); b.Location = new Point(ru.XPos * 30, ru.YPos * 30); b.Text = ru.Symbol; if (ru.Faction == 0) { b.ForeColor = Color.HotPink; } else { b.ForeColor = Color.Blue; } } else { WizardUnit wu = (WizardUnit)u; b.Size = new Size(30, 30); b.Location = new Point(wu.XPos * 30, wu.YPos * 30); b.Text = wu.Symbol; b.ForeColor = Color.Black; } b.Click += Unit_Click; groupBox.Controls.Add(b); } //Adding Buildings foreach (Building bud in buildings) { Button b = new Button(); if (bud is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)bud; b.Size = new Size(30, 30); b.Location = new Point(rb.XPos * 30, rb.YPos * 30); b.Text = rb.Symbol; if (rb.Faction == 0) { b.ForeColor = Color.HotPink; } else { b.ForeColor = Color.Blue; } } else { FactoryBuilding fb = (FactoryBuilding)bud; b.Size = new Size(30, 30); b.Location = new Point(fb.XPos * 30, fb.YPos * 30); b.Text = fb.Symbol; if (fb.Faction == 0) { b.ForeColor = Color.HotPink; } else { b.ForeColor = Color.Blue; } } b.Click += Building_Click; groupBox.Controls.Add(b); } }
//Updates the map to display the units' movement, combat and deaths 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 == 0) { mu.Move(-1); } else 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 buildingclosest, int distanceToBuilding) = mu.BuildingClosest(map.Buildings); if (distanceTo > distanceToBuilding) { //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 { //Check In Range if (distanceTo <= mu.AttackRange) { mu.IsAttacking = true; mu.Combat(closest); } else //Move Towards { if (buildingclosest is FactoryBuilding) { FactoryBuilding closestMu = (FactoryBuilding)buildingclosest; 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 (buildingclosest is ResourceBuilding) { ResourceBuilding closestRu = (ResourceBuilding)buildingclosest; 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]; if (ru.Health == 0) { ru.Move(-1); } else if (ru.Health <= ru.MaxHealth * 0.25) { ru.Move(r.Next(0, 4)); } else { (Unit closest, int distanceTo) = ru.Closest(map.Units); (Building buildingclosest, int distanceToBuilding) = ru.BuildingClosest(map.Buildings); if (distanceTo > distanceToBuilding) { //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); } } } } else { //Check In Range if (distanceTo <= ru.AttackRange) { ru.IsAttacking = true; ru.Combat(closest); } else //Move Towards { if (buildingclosest is FactoryBuilding) { FactoryBuilding closestMu = (FactoryBuilding)buildingclosest; 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 (buildingclosest is ResourceBuilding) { ResourceBuilding closestRu = (ResourceBuilding)buildingclosest; 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); } } } } } } else if (map.Units[i] is WizardUnit) { WizardUnit wu = (WizardUnit)map.Units[i]; if (wu.Health == 0) { wu.Move(-1); } else if (wu.Health <= wu.MaxHealth * 0.5) // Running Away { wu.Move(r.Next(0, 4)); } else { (Unit closest, int distanceTo) = wu.Closest(map.Units); //Check In Range if (distanceTo <= wu.AttackRange) { wu.IsAttacking = true; wu.Combat(closest); } else //Move Towards { if (closest is MeleeUnit) { MeleeUnit closestMu = (MeleeUnit)closest; if (wu.XPos > closestMu.XPos) //North { wu.Move(0); } else if (wu.XPos < closestMu.XPos) //South { wu.Move(2); } else if (wu.YPos > closestMu.YPos) //West { wu.Move(3); } else if (wu.YPos < closestMu.YPos) //East { wu.Move(1); } } else if (closest is RangedUnit) { RangedUnit closestRu = (RangedUnit)closest; if (wu.XPos > closestRu.XPos) //North { wu.Move(0); } else if (wu.XPos < closestRu.XPos) //South { wu.Move(2); } else if (wu.YPos > closestRu.YPos) //West { wu.Move(3); } else if (wu.YPos < closestRu.YPos) //East { wu.Move(1); } } } } } } map.Display(grpMap); round++; }