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; // Creates the buttons on the form to display the ranged units if (ru.XPos == x && ru.YPos == y) { txtInfo.Text = ""; txtInfo.Text = ru.ToString(); } } else if (u is MeleeUnit) { MeleeUnit mu = (MeleeUnit)u; // Creates the buttons on the form to display the melee units { if (mu.XPos == x && mu.YPos == y) { txtInfo.Text = ""; txtInfo.Text = mu.ToString(); } } } foreach (Building B in buildings) { if (B is FactoryBuilding) // Creates the information link on the form to display the factory buildings { FactoryBuilding fb = (FactoryBuilding)B; if (fb.XPos == x && fb.YPos == y) { txtInfo.Text = ""; txtInfo.Text = fb.ToString(); } } else if (B is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)B; // Creates the information link on the form to display the resource buildings if (rb.XPos == x && rb.YPos == y) { txtInfo.Text = ""; txtInfo.Text = rb.ToString(); } } } } }
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 - ru.AttackRange); } if (Health <= 0) { Death(); } }
public Unit ProduceUnit(int fac)// Produces units from the factory buildings { if (unitToProduce == true) { MeleeUnit mu = new MeleeUnit(XPos, spawnPoint, 100, 1, 20, fac, "M"); unitProduction = mu; } else if (unitToProduce == false) { RangedUnit ru = new RangedUnit(XPos, spawnPoint, 100, 1, 15, 5, fac, "R"); unitProduction = ru; } return(unitProduction); }
public void Display(GroupBox grpBox) { grpBox.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.Blue; } } else { 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.Blue; } } 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) { for (int i = 0; i < numBuildings; i++) { units.Add(fb.ProduceUnit(fb.Faction)); } unitSpawnTimer = 0; } B.Size = new Size(30, 30); B.Location = new Point(fb.XPos * 20, fb.YPos * 20); 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 * 20, rb.YPos * 20); B.Text = rb.Symbol; rb.ResourceManagement(); if (rb.Faction == 0) { B.ForeColor = Color.Red; } else { B.ForeColor = Color.Blue; } } B.Click += Unit_Click; grpBox.Controls.Add(B); } unitSpawnTimer++; }
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) // Checks to see which unit is closest to attack { mu.IsAttacking = true; //the unit attacks other units in range mu.Combat(closest); // the unit can 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 (map.Units[i] is RangedUnit) { RangedUnit ru = (RangedUnit)map.Units[i]; (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 } 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); } } } } } map.Display(grpMap); Round++; }