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 override void Combat(Unit attacker) //if unit is being attacked { if (attacker is MeleeUnit) { Health = Health - ((MeleeUnit)attacker).Attack; } else if (attacker is RangedUnit) { RangedUnit ru = (RangedUnit)attacker; Health = Health - (ru.Attack + ru.AttackRange); } else if (attacker is WizardUnit) //wizard has AOE { WizardUnit wu = (WizardUnit)attacker; if (wu.XPos - 1 == XPos && wu.YPos - 1 == YPos) { Health -= wu.Attack; } else if (wu.XPos + 1 == XPos && wu.YPos + 1 == YPos) { Health -= wu.Attack; } else if (wu.XPos - 1 == XPos && wu.YPos + 1 == YPos) { Health -= wu.Attack; } else if (wu.XPos + 1 == XPos && wu.YPos - 1 == YPos) { Health -= wu.Attack; } else if (wu.XPos - 1 == XPos && wu.YPos == YPos) { Health -= wu.Attack; } else if (wu.XPos + 1 == XPos && wu.YPos == YPos) { Health -= wu.Attack; } else if (wu.XPos == XPos && wu.YPos + 1 == YPos) { Health -= wu.Attack; } else if (wu.XPos == XPos && wu.YPos - 1 == YPos) { Health -= wu.Attack; } } if (Health <= 0) { Death(); //Unit is dead } }
public override (Unit, int) Closest(List <Unit> units) //looks for closest unit { int shortest = 100; Unit closest = this; //Closest Unit and Distance foreach (Unit u in units) { if (u is MeleeUnit) { 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) { 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) { 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 Unit UnitSpawn() //Each building makes either ranged or melee or wizard { Unit unit; if (UnitGen == 0) { MeleeUnit mu = new MeleeUnit("Ninja", SpawnX, SpawnY + 1, 50, 1, 10, Team, "m", this); unit = mu; } else if (UnitGen == 1) { RangedUnit ru = new RangedUnit("Archer", SpawnX, SpawnY + 1, 35, 2, 5, 3, Team, "r", this); unit = ru; } else { WizardUnit wu = new WizardUnit("Wizard", SpawnX, SpawnY + 1, 25, 2, 8, 1, 2, "w"); unit = wu; } return(unit); }
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); } } } } } } }