public override /*Overrides Reset in unit class*/ (Units, float) Nearest(List <Units> units) //Calculating where the players will return to Accessing actual damage in unit class { float nearest = 100f; //Battle Area Units closest = this; //Closest Unit and Distance foreach (Units u in units) { if (u is MeleeUnits && u != this) { MeleeUnits otherMu = (MeleeUnits)u; float distance = Math.Abs(this.GameObject.transform.position.x - otherMu.GameObject.transform.position.x) + Math.Abs(this.GameObject.transform.position.z - otherMu.GameObject.transform.position.z); if (distance < nearest) { nearest = distance; closest = otherMu; } } else if (u is Ranged_Units && u != this) { Ranged_Units otherRu = (Ranged_Units)u; float distance = Math.Abs(this.GameObject.transform.position.x - otherRu.GameObject.transform.position.x) + Math.Abs(this.GameObject.transform.position.z - otherRu.GameObject.transform.position.z); if (distance < nearest) { nearest = distance; closest = otherRu; } } else if (u is Wizard_Units && u != this) { Wizard_Units otherWu = (Wizard_Units)u; float distance = Math.Abs(this.GameObject.transform.position.x - otherWu.GameObject.transform.position.x) + Math.Abs(this.GameObject.transform.position.z - otherWu.GameObject.transform.position.z); if (distance < nearest) { nearest = distance; closest = otherWu; } } } return(closest, nearest); }
private void Display() { foreach (Units u in unit) { if (u is MeleeUnits) { MeleeUnits mu = (MeleeUnits)u; Instantiate(mu.GameObject, mu.GameObject.Tansform.position, Quaternion.identity); } else if (u is Ranged_Units) { Ranged_Units ru = (Ranged_Units)u; Instantiate(ru.GameObject, ru.GameObject.transform.position, Quaternion.identity); } else if (u is Wizard_Units) { Wizard_Units wu = (Wizard_Units)u; Instantiate(wu.GameObject, wu.GameObject.transform.position, Quaternion.identity); } } }
// Update is called once per frame void Update() { foreach (Buildings b in building) { if (b is Resource_Buildings) { Resource_Buildings rb = (Resource_Buildings)b; resourcesMade = rb.CreateBuildings(); } } CreateUnits();//Calling method Display(); for (int i = 0; i < unit.Count; i++) { if (unit[i] is MeleeUnits) { MeleeUnits mu = (MeleeUnits)unit[i]; if (mu.health <= mu.maxhealth * 0.25) { mu.Move(rnd.Next(0, 4)); } else { (Units nearest, float distanceTo) = mu.Nearest(unit); if (distanceTo <= mu.ackrange) { mu.Attacking = true; mu.Conflict(nearest); } else { if (nearest is MeleeUnits) { MeleeUnits nearestMu = (MeleeUnits)nearest; if (mu.GameObject.transform.position.x > nearestMu.GameObject.transform.position.x)//North movement { mu.Move(0); } else if (mu.GameObject.transform.position.x > nearestMu.GameObject.transform.position.x)//South Movement { mu.Move(2); } else if (mu.GameObject.transform.position.z > nearestMu.GameObject.transform.position.z)//West movement { mu.Move(3); } else if (mu.GameObject.transform.position.z > nearestMu.GameObject.transform.position.z)//East movement { mu.Move(1); } } else if (nearest is Ranged_Units) { Ranged_Units nearestRu = (Ranged_Units)nearest; if (mu.GameObject.transform.position.x > nearestRu.GameObject.transform.position.x) { mu.Move(0); } else if (mu.GameObject.transform.position.x > nearestRu.GameObject.transform.position.x)//South Movement { mu.Move(2); } else if (mu.GameObject.transform.position.z > nearestRu.GameObject.transform.position.z)//West movement { mu.Move(3); } else if (mu.GameObject.transform.position.z > nearestRu.GameObject.transform.position.z)//East movement { mu.Move(1); } } else if (nearest is Wizard_Units) { Wizard_Units nearestWu = (Wizard_Units)nearest; if (mu.GameObject.transform.position.x > nearestWu.GameObject.transform.position.x) { mu.Move(0); } else if (mu.GameObject.transform.position.x > nearestWu.GameObject.transform.position.x)//South Movement { mu.Move(2); } else if (mu.GameObject.transform.position.z > nearestWu.GameObject.transform.position.z)//West movement { mu.Move(3); } else if (mu.GameObject.transform.position.z > nearestWu.GameObject.transform.position.z)//East movement { mu.Move(1); } } } } } else if (unit[i] is Ranged_Units) { Ranged_Units ru = (Ranged_Units)unit[i]; (Units nearest, float distanceTo) = ru.Nearest(unit); if (distanceTo <= ru.ackrange) { ru.Attacking = true; ru.Conflict(nearest); } else { if (nearest is MeleeUnits) { MeleeUnits nearestMu = (MeleeUnits)nearest; if (ru.GameObject.transform.position.x > nearestMu.GameObject.transform.position.x) { ru.Move(0); } else if (ru.GameObject.transform.position.x < nearestMu.GameObject.transform.position.x) { ru.Move(2); } else if (ru.GameObject.transform.position.z > nearestMu.GameObject.transform.position.z) { ru.Move(3); } else if (ru.GameObject.transform.position.z < nearestMu.GameObject.transform.position.z) { ru.Move(1); } } else if (nearest is Ranged_Units) { Ranged_Units nearestRu = (Ranged_Units)nearest; if (ru.GameObject.transform.position.x > nearestRu.GameObject.transform.position.x) { ru.Move(0); } else if (ru.GameObject.transform.position.x < nearestRu.GameObject.transform.position.x) { ru.Move(2); } else if (ru.GameObject.transform.position.z < nearestRu.GameObject.transform.position.z) { ru.Move(3); } else if (ru.GameObject.transform.position.z < nearestRu.GameObject.transform.position.z) { ru.Move(1); } } else if (nearest is Wizard_Units) { Wizard_Units nearestWu = (Wizard_Units)nearest; if (ru.GameObject.transform.position.x > nearestWu.GameObject.transform.position.x) { ru.Move(0); } else if (ru.GameObject.transform.position.x < nearestWu.GameObject.transform.position.x) { ru.Move(2); } else if (ru.GameObject.transform.position.z > nearestWu.GameObject.transform.position.x) { ru.Move(3); } else if (ru.GameObject.transform.position.z < nearestWu.GameObject.transform.position.z) { ru.Move(1); } } } } else if (unit[i] is Wizard_Units) { Wizard_Units wu = (Wizard_Units)unit[i]; if (wu.health <= wu.maxhealth * 0.5) { wu.Move(rnd.Next(0, 4)); } else { (Units nearest, float distanceTo) = wu.Nearest(unit); if (distanceTo <= wu.ackrange) { wu.Attacking = true; wu.Conflict(nearest); } else { if (nearest is Ranged_Units) { Ranged_Units nearestRu = (Ranged_Units)nearest; if (wu.GameObject.transform.position.x > nearestRu.GameObject.transform.position.x) { wu.Move(0); } else if (wu.GameObject.transform.position.x < nearestRu.GameObject.transform.position.x) { wu.Move(2); } else if (wu.GameObject.transform.position.z > nearestRu.GameObject.transform.position.z) { wu.Move(3); } else if (wu.GameObject.transform.position.z < nearestRu.GameObject.transform.position.z) { wu.Move(1); } } else if (nearest is Wizard_Units) { Wizard_Units nearestWu = (Wizard_Units)nearest; if (wu.GameObject.transform.position.x > nearestWu.GameObject.transform.position.x) { wu.Move(0); } else if (wu.GameObject.transform.position.x < nearestWu.GameObject.transform.position.x) { wu.Move(2); } else if (wu.GameObject.transform.position.z > nearestWu.GameObject.transform.position.z) { wu.Move(3); } else if (wu.GameObject.transform.position.z < nearestWu.GameObject.transform.position.z) { wu.Move(1); } } } } } } Display(); //Calling display method foreach (Units u in unit) { if (u is MeleeUnits) { MeleeUnits mu = (MeleeUnits)u; if (mu.team == 0) { txtMelee_Unit.text = "Melee 1" + Convert.ToString(mu.health) + "Health"; } if (mu.team == 1) { txtMelee_Unit.text = "Melee 2" + Convert.ToString(mu.health) + "Health"; } mu.Move(rnd.Next(0, 4)); if (mu.health <= 0) { unit.Remove(u); } } else if (u is Ranged_Units) { Ranged_Units ru = (Ranged_Units)u; if (ru.team == 0) { txtRanged_Unit1.text = "Ranged1" + Convert.ToString(ru.health) + "Health"; } if (ru.team == 1) { txtRanged_Units2.text = "Ranged2" + Convert.ToString(ru.health) + "Health"; } ru.Move(rnd.Next(0, 4)); if (ru.health <= 0) { unit.Remove(u); } } else if (u is Wizard_Units) { Wizard_Units wu = (Wizard_Units)u; if (wu.team == 0) { txtWizard_Unit.text = "Wizard 1" + Convert.ToString(wu.health) + "Health"; } if (wu.team == 1) { txtWizard_Units2.text = "Wizard 2" + Convert.ToString(wu.health) + "Health"; } wu.Move(rnd.Next(0, 4)); if (wu.health <= 0) { unit.Remove(u); } } } }
public void CreateUnits() { Vector3 pos = new Vector3(); foreach (Buildings b in building) { if (((((resourcesMade - mCost) >= 0) || (resourcesMade - rCost) >= 0) || (resourcesMade - wCost) >= 0) && (rnd.Next(0, 3) == 0)) { Resource_Buildings r = (Resource_Buildings)b; if (r.Team == 0) { foreach (Buildings b1 in building) { if (b1 is Factory_Buildings) { Factory_Buildings fb = (Factory_Buildings)b1; if (fb.Team == 0) { Position = fb.GameObject.transform.position; Position.x += rnd.Next(1, 30); Position.z += rnd.Next(1, 30); Melee_Units1.transform.position = pos; MeleeUnits mu = new MeleeUnits(Melee_Units1, "Ranged1", 20, 15, 5, 1, 0); mu.Name += mu.count; unit.Add(mu); } } } } } else if (r.Team == 1) { foreach (Buildings b1 in building) { if (b1 is Factory_Buildings) { Factory_Buildings fb = (Factory_Buildings)b1; if (fb.Team == 0) { Position = fb.GameObject.transform.position; Position.x += rnd.Next(1, 40); Position.z += rnd.Next(1, 40); Melee_Units2.transform.position = pos; MeleeUnits mu = new MeleeUnits("Melee 2", 30, 10, 1, 1, 1, Melee_Units2); mu.Name += mu.count; unit.Add(mu); } } } } else if (((((resourcesMade - mCost) >= 0) || (resourcesMade - rCost) >= 0) || (resourcesMade - wCost) >= 0) && (rnd.Next(0, 3) == 1)) { Resource_Buildings r = (Resource_Buildings)b; if (r.Team == 0) { foreach (Buildings b1 in building) { if (b1 is Factory_Buildings) { Factory_Buildings fb = (Factory_Buildings)b1; if (fb.Team == 0) { pos = fb.GameObject.transform.position; Position.x += rnd.Next(1, 40); Position.z += rnd.Next(1, 40); Ranged_Units1.transform.position = pos; Ranged_Units ru = new Ranged_Units(Ranged_Units1, "Ranged 1 ", 40, 15, 5, 1, 1); ru.Name += ru.count; unit.Add(ru); } } } } else if (r.Team == 1) { foreach (Buildings b1 in building) { if (b is Factory_Buildings) { Factory_Buildings f = (Factory_Buildings)b; if (f.Team == 0) { pos = f.GameObject.transform.position; Position.x += rnd.Next(1, 40); Position.z += rnd.Next(1, 40); Ranged_Units2.transform.position = pos; Ranged_Units ru = new Ranged_Units(Ranged_Units2, "Ranged 2 ", 40, 15, 5, 1, 1); ru.Name += ru.count; unit.Add(ru); } } } } } else if (((((resourcesMade - mCost) >= 0) || (resourcesMade - rCost) >= 0) || (resourcesMade - wCost) >= 0) && (rnd.Next(0, 3) == 2)) { Resource_Buildings r = (Resource_Buildings)b; if (r.Team == 0) { foreach (Buildings b1 in building) { if (b is Factory_Buildings) { Factory_Buildings f = (Factory_Buildings)b; if (f.Team == 0) { pos = f.GameObject.transform.position; Position.x += rnd.Next(1, 30); Position.z += rnd.Next(1, 40); Wizzard1.transform.position = pos; Wizard_Units wu = new Wizard_Units(Wizzard1, "Wizzard 1 ", 35, 20, 4, 1, 1); wu.Name += wu.count; unit.Add(wu); } } } } else if (r.Team == 1) { foreach (Buildings b1 in building) { if (b1 is Factory_Buildings) { Factory_Buildings fb = (Factory_Buildings)b; if (fb.Team == 0) { pos = fb.GameObject.transform.position; Position.x += rnd.Next(1, 30); Position.z += rnd.Next(1, 30); Wizzard2.transform.position = pos; Wizard_Units wu = new Wizard_Units(Wizzard2, "Wizzard 2 ", 35, 20, 4, 1, 1); wu.Name += wu.count; unit.Add(wu); } } } } } } }