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 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(); } } } foreach (Building bu in buildings) { if (bu is FactoryBuilding) { FactoryBuilding fb = (FactoryBuilding)bu; if (fb.Xpos == x && fb.Ypos == y) { txtInfo.Text = ""; txtInfo.Text = fb.ToString(); } } else if (bu is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)bu; if (rb.Xpos == x && rb.Ypos == y) { txtInfo.Text = ""; txtInfo.Text = rb.ToString(); } //when a building is clicked, ToString is called } } }
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); } else if (a is MeleeUnit && b is WizardUnit) { MeleeUnit start = (MeleeUnit)a; WizardUnit end = (WizardUnit)b; distance = Math.Abs(start.Xpos - end.Xpos) + Math.Abs(start.Ypos - end.Ypos); } else if (a is RangedUnit && b is WizardUnit) { RangedUnit start = (RangedUnit)a; WizardUnit end = (WizardUnit)b; distance = Math.Abs(start.Xpos - end.Xpos) + Math.Abs(start.Ypos - end.Ypos); } else if (a is WizardUnit && b is MeleeUnit) { 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 WizardUnit && b is RangedUnit) { WizardUnit start = (WizardUnit)a; RangedUnit end = (RangedUnit)b; distance = Math.Abs(start.Xpos - end.Xpos) + Math.Abs(start.Ypos - end.Ypos); } return(distance); }
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; } } else if (u is WizardUnit && u != this) { 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); }
public override void Combat(Unit attacker) { if (attacker is MeleeUnit) { Health = Health - ((MeleeUnit)attacker).Attack; //if ATTACKER is MELEEUNIT, minus HEALTH by ATTACKER'S Attack } else if (attacker is RangedUnit) { RangedUnit ru = (RangedUnit)attacker; //if ATTACKER is RANGEDUNIT, minus HEALTH by ATTACKER'S ATTACK Health = Health - (ru.Attack - ru.Attackrange); //HEALTH is minused by (ATTACK DAMAGE - The range they are at) } else if (attacker is WizardUnit) { WizardUnit wu = (WizardUnit)attacker; Health = Health - (wu.Attack - wu.Attackrange); } if (Health <= 0) { Death(); } }
public void Update(int x, int y, int temp) { 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.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) { 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.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]; (Unit closest, int distanceto) = wu.Closest(map.Units); if (distanceto <= wu.Attackrange) { wu.isAttacking = true; wu.Combat(closest); } 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); } } else if (closest is WizardUnit) { WizardUnit closestwu = (WizardUnit)closest; if (wu.Xpos > closestwu.Xpos) { wu.Move(0); } else if (wu.Xpos < closestwu.Xpos) { wu.Move(2); } else if (wu.Ypos > closestwu.Ypos) { wu.Move(3); } else if (wu.Ypos < closestwu.Ypos) { wu.Move(1); } } } } /*if(map.Buildings[i] is FactoryBuilding) * { * FactoryBuilding fb = (FactoryBuilding)map.Buildings[i]; * (Unit closest, int distanceTo) = fb.Closest(map.Units); * * if(distanceTo <= 2) * { * fb.Combat(closest); * } * }*/ } map.Display(GBMap, x, y); roundnum++; int genspeed = 5; temp++; if (temp == genspeed) { fb.UnitProduce(genspeed); } }
public void Generate() { // int rng = r.Next(0, 2); 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), 100, 1, 20, (i % 2 == 0 ? 1 : 0), "M", "Grunt"); units.Add(m); } else if (r.Next(0, 2) == 1) { RangedUnit ru = new RangedUnit(r.Next(0, 20), r.Next(0, 20), 100, 1, 20, 5, (i % 2 == 0 ? 1 : 0), "R", "Archer"); units.Add(ru); } else { WizardUnit wu = new WizardUnit(r.Next(0, 20), r.Next(0, 20), 100, 1, 20, 10, (i % 2 == 0 ? 1 : 0), "W", "Wizard"); units.Add(wu); } } for (int i = 0; i < numBuildings; i++) { if (r.Next(0, 2) == 0) { FactoryBuilding fb = new FactoryBuilding(r.Next(0, 20), r.Next(0, 20), 200, r.Next(0, 2), "F"); buildings.Add(fb); } else { ResourceBuilding rb = new ResourceBuilding(r.Next(0, 20), r.Next(0, 20), 200, r.Next(0, 2), "RB"); buildings.Add(rb); } } }
public void Display(GroupBox groupBox, int x, int y) //int x and y are to determine size of grid { groupBox.Controls.Clear(); groupBox.Size = new Size(x, y); 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.Green; } } //MeleeUnit's spawn/display 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; } } //RangedUnit's spawn/display else 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 == 0) { b.ForeColor = Color.Red; } else if (wu.Faction == 1) { b.ForeColor = Color.Green; //wizards can belong on both teams as well as an independent team (as the brief didn't say it had to be only an independent team) } else if (wu.Faction == 2) { b.ForeColor = Color.Aqua; //3rd team. Highly unlikely to spawn however unless max units on load is increased } } b.Click += Unit_Click; groupBox.Controls.Add(b); } foreach (Building bu in buildings) { Button b = new Button(); if (bu is FactoryBuilding) { FactoryBuilding fb = (FactoryBuilding)bu; b.Size = new Size(40, 40); b.Location = new Point(fb.Xpos * 40, fb.Ypos * 40); b.Text = fb.Symbol; if (fb.Faction == 0) { b.ForeColor = Color.Red; } else { b.ForeColor = Color.Green; } } b.Click += Building_Click; groupBox.Controls.Add(b); groupBox.Controls.Add(b); groupBox.Controls.Add(b); groupBox.Controls.Add(b); } //all buildings spawn }