public override (Building, int) ClosestBuilding(List <Building> buildings) //Checks for closest building { int shortest = 100; Building closest = Spawn; //Closest building will be the building unit came from //Closest Building and Distance foreach (Building b in buildings) { if (b is FactoryBuilding) { FactoryBuilding fb = (FactoryBuilding)b; int distance = Math.Abs(this.XPos - fb.SpawnX) + Math.Abs(this.YPos - fb.SpawnY); if (distance < shortest) { shortest = distance; closest = fb; } } else if (b is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)b; int distance = Math.Abs(this.XPos - rb.XPos) + Math.Abs(this.YPos - rb.YPos); if (distance < shortest) { shortest = distance; closest = rb; } } } return(closest, shortest); }
public void InfoClick() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity); if (hit) { float mouseX = hit.collider.transform.position.x; float mouseY = hit.collider.transform.position.y; foreach (Unit u in map.Units) { if (u is MeleeUnit) { MeleeUnit mu = (MeleeUnit)u; if (mu.XPos == mouseX && mu.YPos == mouseY) { statusInfo.text = mu.ToString(); } } else if (u is RangedUnit) { RangedUnit ru = (RangedUnit)u; if (ru.XPos == mouseX && ru.YPos == mouseY) { statusInfo.text = ru.ToString(); } } else if (u is WizardUnit) { WizardUnit wu = (WizardUnit)u; if (wu.XPos == mouseX && wu.YPos == mouseY) { statusInfo.text = wu.ToString(); } } } foreach (Building bg in map.Buildings) { if (bg is FactoryBuilding) { FactoryBuilding fb = (FactoryBuilding)bg; if (fb.SpawnX == mouseX && fb.SpawnX == mouseY) { statusInfo.text = fb.ToString(); } } else if (bg is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)bg; if (rb.XPos == mouseX && rb.YPos == mouseY) { statusInfo.text = rb.ToString(); } } } } }
public void Generate() //Makes buildings at beginning of game { for (int i = 0; i < numBuild; i++) { if (r.Next(0, 2) == 0) { ResourceBuilding rb = new ResourceBuilding(r.Next(0, mapWidth), r.Next(0, mapHeight), 20, r.Next(0, 2), "{}", 0, 0, 10, 500); Buildings.Add(rb); } else { FactoryBuilding fb = new FactoryBuilding(r.Next(0, mapWidth), r.Next(0, mapHeight), 15, r.Next(0, 2), "[]", r.Next(0, 3), 4); Buildings.Add(fb); } } }
public void DisplayMap() //Displays correct symbol { foreach (Building bg in map.Buildings) { if (bg is FactoryBuilding) { FactoryBuilding fb = (FactoryBuilding)bg; if (fb.Team == 0) { if (fb.IsDead == false) { GameObject obj = Instantiate(factoryTeamOne, new Vector2(fb.SpawnX, fb.SpawnY), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(teamOneDeath, new Vector2(fb.SpawnX, fb.SpawnY), Quaternion.identity); obj.transform.parent = transform; } } else { if (fb.IsDead == false) { GameObject obj = Instantiate(factoryTeamTwo, new Vector2(fb.SpawnX, fb.SpawnY), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(teamTwoDeath, new Vector2(fb.SpawnX, fb.SpawnY), Quaternion.identity); obj.transform.parent = transform; } } } else if (bg is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)bg; if (rb.Team == 0) { if (rb.IsDead == false) { GameObject obj = Instantiate(resourceTeamOne, new Vector2(rb.XPos, rb.YPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(teamOneDeath, new Vector2(rb.XPos, rb.YPos), Quaternion.identity); obj.transform.parent = transform; } } else { if (rb.IsDead == false) { GameObject obj = Instantiate(resourceTeamTwo, new Vector2(rb.XPos, rb.YPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(teamTwoDeath, new Vector2(rb.XPos, rb.YPos), Quaternion.identity); obj.transform.parent = transform; } } } } foreach (Unit u in map.Units) { if (u is MeleeUnit) { MeleeUnit mu = (MeleeUnit)u; if (mu.Team == 0) { if (mu.IsDead == false) { GameObject obj = Instantiate(meleeTeamOne, new Vector2(mu.XPos, mu.YPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(teamOneDeath, new Vector2(mu.XPos, mu.YPos), Quaternion.identity); obj.transform.parent = transform; } } else { if (mu.IsDead == false) { GameObject obj = Instantiate(meleeTeamTwo, new Vector2(mu.XPos, mu.YPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(teamTwoDeath, new Vector2(mu.XPos, mu.YPos), Quaternion.identity); obj.transform.parent = transform; } } } else if (u is RangedUnit) { RangedUnit ru = (RangedUnit)u; if (ru.Team == 0) { if (ru.IsDead == false) { GameObject obj = Instantiate(rangerTeamOne, new Vector2(ru.XPos, ru.YPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(teamOneDeath, new Vector2(ru.XPos, ru.YPos), Quaternion.identity); obj.transform.parent = transform; } } else { if (ru.IsDead == false) { GameObject obj = Instantiate(rangerTeamTwo, new Vector2(ru.XPos, ru.YPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(teamTwoDeath, new Vector2(ru.XPos, ru.YPos), Quaternion.identity); obj.transform.parent = transform; } } } else if (u is WizardUnit) { WizardUnit wu = (WizardUnit)u; if (wu.IsDead == false) { GameObject obj = Instantiate(wizardTeam, new Vector2(wu.XPos, wu.YPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(wizardDeath, new Vector2(wu.XPos, wu.YPos), Quaternion.identity); obj.transform.parent = transform; } } } }
public void UpdateMap() { foreach (Building bg in map.Buildings) { if (bg is FactoryBuilding) { FactoryBuilding fb = (FactoryBuilding)bg; if (fb.ProductionSpeed % rounds == 0 && numResources >= costOfUnits) //Checks if factory building can spawn more units { map.Units.Add(fb.UnitSpawn()); numResources -= costOfUnits; } } else if (bg is ResourceBuilding) //Generates more resources { ResourceBuilding rb = (ResourceBuilding)bg; numResources += rb.ResourceGeneration(); } } 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) // Running Away { mu.Move((Unit.direction)r.Next(0, 4)); } else { (Unit closest, int distanceTo) = mu.Closest(map.Units); (Building closestbuilding, int distance) = mu.ClosestBuilding(map.Buildings); if (distanceTo < distance) //Checks if Unit is closer than building { //Check In Range if (distanceTo <= mu.AttackRange) { mu.IsAttacking = true; mu.Combat(closest); if (mu.IsDead == true) //Returns resources if unit is dead { foreach (Building bg in map.Buildings) { if (bg is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)bg; if (mu.Team == rb.Team) { rb.ResourcesGenerated++; } } } } } else //Move Towards { if (closest is MeleeUnit) { MeleeUnit closestMu = (MeleeUnit)closest; if (mu.XPos > closestMu.XPos) //North { mu.Move(Unit.direction.North); } else if (mu.XPos < closestMu.XPos) //South { mu.Move(Unit.direction.South); } else if (mu.YPos > closestMu.YPos) //West { mu.Move(Unit.direction.West); } else if (mu.YPos < closestMu.YPos) //East { mu.Move(Unit.direction.East); } } else if (closest is RangedUnit) { RangedUnit closestRu = (RangedUnit)closest; if (mu.XPos > closestRu.XPos) //North { mu.Move(Unit.direction.North); } else if (mu.XPos < closestRu.XPos) //South { mu.Move(Unit.direction.South); } else if (mu.YPos > closestRu.YPos) //West { mu.Move(Unit.direction.West); } else if (mu.YPos < closestRu.YPos) //East { mu.Move(Unit.direction.East); } } else if (closest is WizardUnit) { WizardUnit closestWu = (WizardUnit)closest; if (mu.XPos > closestWu.XPos) //North { mu.Move(Unit.direction.North); } else if (mu.XPos < closestWu.XPos) //South { mu.Move(Unit.direction.South); } else if (mu.YPos > closestWu.YPos) //West { mu.Move(Unit.direction.West); } else if (mu.YPos < closestWu.YPos) //East { mu.Move(Unit.direction.East); } } } } else //if building is closer { //Check In Range if (distance <= mu.AttackRange) { mu.IsAttacking = true; closestbuilding.Attacked(mu); } else //Move Towards { if (closestbuilding is FactoryBuilding) { FactoryBuilding closestfb = (FactoryBuilding)closestbuilding; if (mu.XPos > closestfb.SpawnX) //North { mu.Move(Unit.direction.North); } else if (mu.XPos < closestfb.SpawnX) //South { mu.Move(Unit.direction.South); } else if (mu.YPos > closestfb.SpawnY) //West { mu.Move(Unit.direction.West); } else if (mu.YPos < closestfb.SpawnY) //East { mu.Move(Unit.direction.East); } } else if (closestbuilding is ResourceBuilding) { ResourceBuilding closestrb = (ResourceBuilding)closestbuilding; if (mu.XPos > closestrb.XPos) //North { mu.Move(Unit.direction.North); } else if (mu.XPos < closestrb.XPos) //South { mu.Move(Unit.direction.South); } else if (mu.YPos > closestrb.YPos) //West { mu.Move(Unit.direction.West); } else if (mu.YPos < closestrb.YPos) //East { mu.Move(Unit.direction.East); } } } } } } else if (map.Units[i] is RangedUnit) { RangedUnit ru = (RangedUnit)map.Units[i]; if (ru.Health <= ru.MaxHealth * 0.25) //Running away { ru.Move((Unit.direction)r.Next(0, 4)); } (Unit closest, int distanceTo) = ru.Closest(map.Units); (Building closestbuilding, int distance) = ru.ClosestBuilding(map.Buildings); if (distanceTo < distance) //Checks if Unit is closer than building { //Check In Range if (distanceTo <= ru.AttackRange) { ru.IsAttacking = true; ru.Combat(closest); if (ru.IsDead == true) //Returns resources if unit is dead { foreach (Building bg in map.Buildings) { if (bg is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)bg; if (ru.Team == rb.Team) { rb.ResourcesGenerated++; } } } } } else //Move Towards { if (closest is MeleeUnit) { MeleeUnit closestMu = (MeleeUnit)closest; if (ru.XPos > closestMu.XPos) //North { ru.Move(Unit.direction.North); } else if (ru.XPos < closestMu.XPos) //South { ru.Move(Unit.direction.South); } else if (ru.YPos > closestMu.YPos) //West { ru.Move(Unit.direction.West); } else if (ru.YPos < closestMu.YPos) //East { ru.Move(Unit.direction.East); } } else if (closest is RangedUnit) { RangedUnit closestRu = (RangedUnit)closest; if (ru.XPos > closestRu.XPos) //North { ru.Move(Unit.direction.North); } else if (ru.XPos < closestRu.XPos) //South { ru.Move(Unit.direction.South); } else if (ru.YPos > closestRu.YPos) //West { ru.Move(Unit.direction.West); } else if (ru.YPos < closestRu.YPos) //East { ru.Move(Unit.direction.East); } } else if (closest is WizardUnit) { WizardUnit closestWu = (WizardUnit)closest; if (ru.XPos > closestWu.XPos) //North { ru.Move(Unit.direction.North); } else if (ru.XPos < closestWu.XPos) //South { ru.Move(Unit.direction.South); } else if (ru.YPos > closestWu.YPos) //West { ru.Move(Unit.direction.West); } else if (ru.YPos < closestWu.YPos) //East { ru.Move(Unit.direction.East); } } } } else //if building is closer { //Check In Range if (distance <= ru.AttackRange) { ru.IsAttacking = true; closestbuilding.Attacked(ru); } else //Move Towards { if (closestbuilding is FactoryBuilding) { FactoryBuilding closestfb = (FactoryBuilding)closestbuilding; if (ru.XPos > closestfb.SpawnX) //North { ru.Move(Unit.direction.North); } else if (ru.XPos < closestfb.SpawnX) //South { ru.Move(Unit.direction.South); } else if (ru.YPos > closestfb.SpawnY) //West { ru.Move(Unit.direction.West); } else if (ru.YPos < closestfb.SpawnY) //East { ru.Move(Unit.direction.East); } } else if (closestbuilding is ResourceBuilding) { ResourceBuilding closestrb = (ResourceBuilding)closestbuilding; if (ru.XPos > closestrb.XPos) //North { ru.Move(Unit.direction.North); } else if (ru.XPos < closestrb.XPos) //South { ru.Move(Unit.direction.South); } else if (ru.YPos > closestrb.YPos) //West { ru.Move(Unit.direction.West); } else if (ru.YPos < closestrb.YPos) //East { ru.Move(Unit.direction.East); } } } } } else if (map.Units[i] is WizardUnit) { WizardUnit wu = (WizardUnit)map.Units[i]; if (wu.Health <= wu.MaxHealth * 0.5) //Running away { wu.Move((Unit.direction)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(Unit.direction.North); } else if (wu.XPos < closestMu.XPos) //South { wu.Move(Unit.direction.South); } else if (wu.YPos > closestMu.YPos) //West { wu.Move(Unit.direction.West); } else if (wu.YPos < closestMu.YPos) //East { wu.Move(Unit.direction.East); } } else if (closest is RangedUnit) { RangedUnit closestRu = (RangedUnit)closest; if (wu.XPos > closestRu.XPos) //North { wu.Move(Unit.direction.North); } else if (wu.XPos < closestRu.XPos) //South { wu.Move(Unit.direction.South); } else if (wu.YPos > closestRu.YPos) //West { wu.Move(Unit.direction.West); } else if (wu.YPos < closestRu.YPos) //East { wu.Move(Unit.direction.East); } } } } } } }