public void Unit_Click(object sender, EventArgs e) { int x, y; Button myButt = (Button)sender; x = myButt.Location.X / 20; y = myButt.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 MeleeUnit) { MeleeUnit mu = (MeleeUnit)u; if (mu.XPos == x && mu.YPos == y) { txtInfo.Text = ""; txtInfo.Text = mu.ToString(); } } } }
public override (Unit, int) Closest(List <Unit> units) { int shortest = 100; Unit closest = this; 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); }
public Unit Spawn() { if (unitType == 0) { RangedUnit r = new RangedUnit(PosX, spawnPoint, "archer", 100, 1, 20, 5, Faction, "R"); return(r); } else { MeleeUnit m = new MeleeUnit(PosX, spawnPoint, "soldier", 100, 1, 20, Faction, "M"); return(m); } }
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(); } }
public void Generate() { for (int i = 0; i < numUnits; i++) { if (r.Next(0, 2) == 0) { MeleeUnit m = new MeleeUnit(r.Next(0, 20), r.Next(0, 20), "Soldier", 100, 1, 20, i % 2 == 0 ? 1 : 0, "M"); units.Add(m); } else { RangedUnit ru = new RangedUnit(r.Next(0, 20), r.Next(0, 20), "Archer", 100, 1, 20, 5, i % 2 == 0 ? 1 : 0, "R"); units.Add(ru); } } for (int j = 0; j < numBuilds; j++) { if (r.Next(0, 2) == 0) { if (r.Next(0, 2) == 0) { FactoryBuilding fb = new FactoryBuilding(r.Next(0, 20), r.Next(0, 20), 0, 200, 5, j % 2 == 0 ? 1 : 0, "FB"); builds.Add(fb); } else { FactoryBuilding fb = new FactoryBuilding(r.Next(0, 20), r.Next(0, 20), 1, 200, 5, j % 2 == 0 ? 1 : 0, "FB"); builds.Add(fb); } } else { ResourceBuilding rb = new ResourceBuilding(r.Next(0, 20), r.Next(0, 20), "Swords", 200, 2, 60, j % 2 == 0 ? 1 : 0); builds.Add(rb); } } }
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) { mu.Move(r.Next(0, 4)); } else { (Unit closest, int distanceTo) = mu.Closest(map.Units); if (distanceTo <= mu.AttackRange) { mu.IsAttacking = true; mu.Combat(closest); } else { if (closest is MeleeUnit) { MeleeUnit closestMu = (MeleeUnit)closest; if (mu.XPos > closestMu.XPos) { mu.Move(0); } else if (mu.YPos < closestMu.YPos) { mu.Move(1); } else if (mu.XPos < closestMu.XPos) { mu.Move(2); } else if (mu.YPos > closestMu.YPos) { mu.Move(3); } } else if (closest is RangedUnit) { RangedUnit closestRu = (RangedUnit)closest; if (mu.XPos > closestRu.XPos) { mu.Move(0); } else if (mu.YPos < closestRu.YPos) { mu.Move(1); } else if (mu.XPos < closestRu.XPos) { mu.Move(2); } else if (mu.YPos > closestRu.YPos) { mu.Move(3); } } } } } else if (map.Units[i] is RangedUnit) { RangedUnit ru = (RangedUnit)map.Units[i]; (Unit closest, int distanceTo) = ru.Closest(map.Units); if (distanceTo <= ru.AttackRange) { ru.IsAttacking = true; ru.Combat(closest); } else { if (closest is MeleeUnit) { MeleeUnit closestMu = (MeleeUnit)closest; if (ru.XPos > closestMu.XPos) { ru.Move(0); } else if (ru.YPos < closestMu.YPos) { ru.Move(1); } else if (ru.XPos < closestMu.XPos) { ru.Move(2); } else if (ru.YPos > closestMu.YPos) { ru.Move(3); } } else if (closest is RangedUnit) { RangedUnit closestRu = (RangedUnit)closest; if (ru.XPos > closestRu.XPos) { ru.Move(0); } else if (ru.YPos < closestRu.YPos) { ru.Move(1); } else if (ru.XPos < closestRu.XPos) { ru.Move(2); } else if (ru.YPos > closestRu.YPos) { ru.Move(3); } } } } } for (int i = 0; i < map.Builds.Count; i++) { if (map.Builds[i] is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)map.Builds[i]; rb.Generator(); } else { FactoryBuilding fb = (FactoryBuilding)map.Builds[i]; if (fb.ProductSpeed >= 7) { map.Units.Add(fb.Spawn()); fb.ProductSpeed = 0; } fb.ProductSpeed++; } } map.Display(grpMap); round++; }
public void Display(GroupBox groupBox) { groupBox.Controls.Clear(); foreach (Unit u in units) { Button myButt = new Button(); if (u is MeleeUnit) { MeleeUnit mu = (MeleeUnit)u; myButt.Size = new Size(20, 20); myButt.Location = new Point(mu.XPos * 20, mu.YPos * 20); myButt.Text = mu.Symbol; if (mu.Faction == 0) { myButt.ForeColor = Color.Red; } else { myButt.ForeColor = Color.Green; } } else { RangedUnit ru = (RangedUnit)u; myButt.Size = new Size(20, 20); myButt.Location = new Point(ru.XPos * 20, ru.YPos * 20); myButt.Text = ru.Symbol; if (ru.Faction == 0) { myButt.ForeColor = Color.Red; } else { myButt.ForeColor = Color.Green; } } myButt.Click += Unit_Click; groupBox.Controls.Add(myButt); } foreach (Building b in builds) { Button myButt = new Button(); if (b is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)b; myButt.Size = new Size(30, 30); myButt.Location = new Point(rb.PosX * 30, rb.PosY * 30); myButt.Text = rb.Symbol; myButt.ForeColor = Color.Gray; } else { FactoryBuilding fb = (FactoryBuilding)b; myButt.Size = new Size(30, 30); myButt.Location = new Point(fb.PosX * 30, fb.PosY * 30); myButt.Text = fb.Symbol; if (fb.Faction == 0) { myButt.ForeColor = Color.Red; } else { myButt.ForeColor = Color.Green; } } myButt.Click += Building_Click; groupBox.Controls.Add(myButt); } }