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) { Info.text = mu.Info(); } } else if (u is RangedUnit) { RangedUnit ru = (RangedUnit)u; if (ru.xPos == mouseX && ru.yPos == mouseY) { Info.text = ru.Info(); } } else if (u is WizzardUnit) { WizzardUnit wu = (WizzardUnit)u; if (wu.xPos == mouseX && wu.yPos == mouseY) { Info.text = wu.Info(); } } } foreach (Building bg in map.Buildings) { if (bg is FactoryBuilding) { FactoryBuilding fb = (FactoryBuilding)bg; if (fb.xPos == mouseX && fb.yPos == mouseY) { Info.text = fb.Info(); } } else if (bg is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)bg; if (rb.xPos == mouseX && rb.yPos == mouseY) { Info.text = rb.Info(); } } } } }
public override void Combat(Unit attacker) { if (attacker is MeleeUnit) { base.health = base.health - ((MeleeUnit)attacker).attack; } else if (attacker is RangedUnit) { RangedUnit ru = (RangedUnit)attacker; base.health = base.health - (ru.Attack - ru.attackRange); } else if (attacker is WizzardUnit) { WizzardUnit WizardUnit = (WizzardUnit)attacker; if (WizardUnit.xPos - 1 == xPos && WizardUnit.yPos + 1 == yPos) { health = health - WizardUnit.attack; } if (WizardUnit.yPos + 1 == yPos) { health = health - WizardUnit.attack; } if (WizardUnit.yPos + 1 == yPos && WizardUnit.xPos + 1 == xPos) { health = health - WizardUnit.attack; } if (WizardUnit.xPos + 1 == xPos) { health = health - WizardUnit.attack; } if (WizardUnit.xPos + 1 == xPos && WizardUnit.yPos - 1 == yPos) { health = health - WizardUnit.attack; } if (WizardUnit.yPos - 1 == yPos) { health = health - WizardUnit.attack; } if (WizardUnit.xPos - 1 == xPos && WizardUnit.yPos - 1 == yPos) { health = health - WizardUnit.attack; } if (WizardUnit.xPos - 1 == xPos) { health = health - WizardUnit.attack; } } if (base.health <= 0) { Death(); //it does the big deaded } }
public override (Unit, int) EnemyDistance(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 WizzardUnit && u != this) { WizzardUnit otherWu = (WizzardUnit)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 SpawnUnit() { if (Team == 0) { Unit unit; if (Unit_type == 0) { MeleeUnit m = new MeleeUnit(xpos, ypos + 1, 100, 1, 30, 0, "M", this); unit = m; } else if (Unit_type == 1) { RangedUnit ru = new RangedUnit(xpos, ypos + 1, 100, 1, 25, 5, 0, "R", this); unit = ru; } else { WizzardUnit wu = new WizzardUnit(xpos, ypos + 1, 100, 1, 20, 1, 1, "W"); unit = wu; } return(unit); } else { Unit unit; if (Unit_type == 0) { MeleeUnit m = new MeleeUnit(xpos, ypos + 1, 100, 1, 30, 1, "M", this); unit = m; } else if (Unit_type == 1) { RangedUnit ru = new RangedUnit(xpos, ypos + 1, 100, 1, 25, 5, 1, "R", this); unit = ru; } else { WizzardUnit wu = new WizzardUnit(xpos, ypos + 1, 100, 1, 20, 1, 1, "W"); unit = wu; } return(unit); } }
public void display() { 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(FactoryT1, new Vector2(fb.xPos, fb.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT1, new Vector2(fb.xPos, fb.yPos), Quaternion.identity); obj.transform.parent = transform; } } else { if (fb.IsDead == false) { GameObject obj = Instantiate(FactoryT2, new Vector2(fb.xPos, fb.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT2, new Vector2(fb.xPos, fb.yPos), 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(ResourceT1, new Vector2(rb.xPos, rb.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT1, new Vector2(rb.xPos, rb.yPos), Quaternion.identity); obj.transform.parent = transform; } } else { if (rb.IsDead == false) { GameObject obj = Instantiate(ResourceT2, new Vector2(rb.xPos, rb.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT2, 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(MeleeT1, new Vector2(mu.xPos, mu.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT1, new Vector2(mu.xPos, mu.yPos), Quaternion.identity); obj.transform.parent = transform; } } else { if (mu.IsDead == false) { GameObject obj = Instantiate(MeleeT2, new Vector2(mu.xPos, mu.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT2, 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(RangedT1, new Vector2(ru.xPos, ru.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT1, new Vector2(ru.xPos, ru.yPos), Quaternion.identity); obj.transform.parent = transform; } } else { if (ru.IsDead == false) { GameObject obj = Instantiate(RangedT2, new Vector2(ru.xPos, ru.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT2, new Vector2(ru.xPos, ru.yPos), Quaternion.identity); obj.transform.parent = transform; } } } else if (u is WizzardUnit) { WizzardUnit wu = (WizzardUnit)u; if (wu.IsDead == false) { GameObject obj = Instantiate(WizzardTeam, new Vector2(wu.xPos, wu.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(WizzardDeath, new Vector2(wu.xPos, wu.yPos), Quaternion.identity); obj.transform.parent = transform; } } } }
public void action() { foreach (Building b in map.Buildings) { if (b is FactoryBuilding) { FactoryBuilding f = (FactoryBuilding)b; if (f.Production_Speed % round == 0 && resources >= UnitCost) { map.Units.Add(f.SpawnUnit()); } } if (b is ResourceBuilding) { ResourceBuilding rs = (ResourceBuilding)b; resources = rs.GenerateResources(); } } 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(r.Next(0, 4)); } else { (Unit closest, int distanceTo) = mu.EnemyDistance(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.isAttack = 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(0); } else if (mu.xPos < closestMu.xPos) //South { mu.Move(2); } else if (mu.yPos > closestMu.yPos) //West { mu.Move(3); } else if (mu.yPos < closestMu.yPos) //East { mu.Move(1); } } else if (closest is RangedUnit) { RangedUnit closestRu = (RangedUnit)closest; if (mu.xPos > closestRu.xPos) //North { mu.Move(0); } else if (mu.xPos < closestRu.xPos) //South { mu.Move(2); } else if (mu.yPos > closestRu.yPos) //West { mu.Move(3); } else if (mu.yPos < closestRu.yPos) //East { mu.Move(1); } } if (closest is WizzardUnit) { WizzardUnit closestwu = (WizzardUnit)closest; if (mu.xPos > closestwu.xPos) //North { mu.Move(0); } else if (mu.xPos < closestwu.xPos) //South { mu.Move(2); } else if (mu.yPos > closestwu.yPos) //West { mu.Move(3); } else if (mu.yPos < closestwu.yPos) //East { mu.Move(1); } } } } else { //Check In Range if (distanceTo <= mu.attackRange) { mu.isAttack = true; mu.Combat(closest); } else //Move Towards { if (closestbuilding is FactoryBuilding) { FactoryBuilding closestfb = (FactoryBuilding)closestbuilding; if (mu.xPos > closestfb.xPos) //North { mu.Move(1); } else if (mu.xPos < closestfb.xPos) //South { mu.Move(2); } else if (mu.yPos > closestfb.yPos) //West { mu.Move(3); } else if (mu.yPos < closestfb.yPos) //East { mu.Move(4); } } else if (closestbuilding is ResourceBuilding) { ResourceBuilding closestrb = (ResourceBuilding)closestbuilding; if (mu.xPos > closestrb.xPos) //North { mu.Move(1); } else if (mu.xPos < closestrb.xPos) //South { mu.Move(2); } else if (mu.yPos > closestrb.yPos) //West { mu.Move(3); } else if (mu.yPos < closestrb.yPos) //East { mu.Move(4); } } } } } } else if (map.Units[i] is RangedUnit) { RangedUnit ru = (RangedUnit)map.Units[i]; if (ru.health <= ru.maxHealth * 0.25) // Running Away is commented out to make for a more interesting battle - and some insta-deaths { ru.Move(r.Next(0, 4)); } else { (Unit closest, int distanceTo) = ru.EnemyDistance(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.isAttack = 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(0); } else if (ru.xPos < closestMu.xPos) //South { ru.Move(2); } else if (ru.yPos > closestMu.yPos) //West { ru.Move(3); } else if (ru.yPos < closestMu.yPos) //East { ru.Move(1); } } else if (closest is RangedUnit) { RangedUnit closestRu = (RangedUnit)closest; if (ru.xPos > closestRu.xPos) //North { ru.Move(0); } else if (ru.xPos < closestRu.xPos) //South { ru.Move(2); } else if (ru.yPos > closestRu.yPos) //West { ru.Move(3); } else if (ru.yPos < closestRu.yPos) //East { ru.Move(1); } } if (closest is WizzardUnit) { WizzardUnit closestMu = (WizzardUnit)closest; if (ru.xPos > closestMu.xPos) //North { ru.Move(0); } else if (ru.xPos < closestMu.xPos) //South { ru.Move(2); } else if (ru.yPos > closestMu.yPos) //West { ru.Move(3); } else if (ru.yPos < closestMu.yPos) //East { ru.Move(1); } } } } else { //Check In Range if (distanceTo <= ru.attackRange) { ru.isAttack = true; ru.Combat(closest); } else //Move Towards { if (closestbuilding is FactoryBuilding) { FactoryBuilding closestfb = (FactoryBuilding)closestbuilding; if (ru.xPos > closestfb.xPos) //North { ru.Move(1); } else if (ru.xPos < closestfb.xPos) //South { ru.Move(2); } else if (ru.yPos > closestfb.yPos) //West { ru.Move(3); } else if (ru.yPos < closestfb.yPos) //East { ru.Move(4); } } else if (closestbuilding is ResourceBuilding) { ResourceBuilding closestrb = (ResourceBuilding)closestbuilding; if (ru.xPos > closestrb.xPos) //North { ru.Move(1); } else if (ru.xPos < closestrb.xPos) //South { ru.Move(2); } else if (ru.yPos > closestrb.yPos) //West { ru.Move(3); } else if (ru.yPos < closestrb.yPos) //East { ru.Move(4); } } } } } } else if (map.Units[i] is WizzardUnit) { WizzardUnit wu = (WizzardUnit)map.Units[i]; if (wu.health <= wu.maxHealth * 0.50) // Wizzards will run away when on half health or less { wu.Move(r.Next(0, 4)); } else { (Unit closest, int distanceTo) = wu.EnemyDistance(map.Units); //Check In Range if (distanceTo <= wu.attackRange) { wu.isAttack = true; wu.Combat(closest); } else //Move Towards { if (closest is MeleeUnit) { MeleeUnit closestMu = (MeleeUnit)closest; if (wu.xPos > closestMu.xPos) //North { wu.Move(0); } else if (wu.xPos < closestMu.xPos) //South { wu.Move(2); } else if (wu.yPos > closestMu.yPos) //West { wu.Move(3); } else if (wu.yPos < closestMu.yPos) //East { wu.Move(1); } } else if (closest is RangedUnit) { RangedUnit closestRu = (RangedUnit)closest; if (wu.xPos > closestRu.xPos) //North { wu.Move(0); } else if (wu.xPos < closestRu.xPos) //South { wu.Move(2); } else if (wu.yPos > closestRu.yPos) //West { wu.Move(3); } else if (wu.yPos < closestRu.yPos) //East { wu.Move(1); } } } } } } }