public override void Combat(int type) { if (type == 0) { if (closestUnit is MeleeUnit) { MeleeUnit M = (MeleeUnit)closestUnit; M.Health -= Attack; } else if (closestUnit is RangedUnits) { RangedUnits R = (RangedUnits)closestUnit; R.Health -= Attack; } else if (closestUnit is WizzardUnits) { WizzardUnits R = (WizzardUnits)closestUnit; R.Health -= Attack; } } else if (type == 1) { if (closestBuilding is FactoryBuildings) { FactoryBuildings FB = (FactoryBuildings)closestBuilding; FB.Health -= Attack; } else if (closestBuilding is ResourceBuildings) { ResourceBuildings RB = (ResourceBuildings)closestBuilding; RB.Health -= Attack; } } }
public override void Move(int type) { //Moves towards closest enemey if (Health > MaxHealth * 0.25) { if (type == 0) { if (closestUnit is MeleeUnit) { MeleeUnit closestUnitM = (MeleeUnit)closestUnit; if (closestUnitM.PosX > posX && PosX < 20) { posX++; } else if (closestUnitM.PosX < posX && posX > 0) { posX--; } if (closestUnitM.PosY > posY && PosY < 20) { posY++; } else if (closestUnitM.PosY < posY && posY > 0) { posY--; } } else if (closestUnit is RangedUnits) { RangedUnits closestUnitR = (RangedUnits)closestUnit; if (closestUnitR.PosX > posX && PosX < 20) { posX++; } else if (closestUnitR.PosX < posX && posX > 0) { posX--; } if (closestUnitR.PosY > posY && PosY < 20) { posY++; } else if (closestUnitR.PosY < posY && posY > 0) { posY--; } } else if (closestUnit is WizzardUnits) { WizzardUnits closestUnitR = (WizzardUnits)closestUnit; if (closestUnitR.PosX > posX && PosX < 20) { posX++; } else if (closestUnitR.PosX < posX && posX > 0) { posX--; } if (closestUnitR.PosY > posY && PosY < 20) { posY++; } else if (closestUnitR.PosY < posY && posY > 0) { posY--; } } } else { if (closestBuilding is FactoryBuildings) { FactoryBuildings closestBuildingFB = (FactoryBuildings)closestBuilding; if (closestBuildingFB.PosX > posX && PosX < 20) { posX++; } else if (closestBuildingFB.PosX < posX && posX > 0) { posX--; } if (closestBuildingFB.PosY > posY && PosY < 20) { posY++; } else if (closestBuildingFB.PosY < posY && posY > 0) { posY--; } } else if (closestBuilding is ResourceBuildings) { ResourceBuildings closestBuildingRB = (ResourceBuildings)closestBuilding; if (closestBuildingRB.PosX > posX && PosX < 20) { posX++; } else if (closestBuildingRB.PosX < posX && posX > 0) { posX--; } if (closestBuildingRB.PosY > posY && PosY < 20) { posY++; } else if (closestBuildingRB.PosY < posY && posY > 0) { posY--; } } } } else //Moves the unit in a direction that is determined by random { int direction = Random.Range(0, 4); if (direction == 0 && PosX < 19) { posX++; } else if (direction == 1 && posX > 0) { posX--; } else if (direction == 2 && posY < 19) { posY++; } else if (direction == 3 && posY > 0) { posY--; } } }
public override void CheckAttackRange(List <Unit> uni, List <buildings> build) { units = uni; building = build; closestUnit = ClosestEnemy(); closestBuilding = ClosestBuilding(); int enemyType; int xDis = 0, yDis = 0; int uDistance = 10000, bDistance = 10000; int distance; if (closestUnit is MeleeUnit) { MeleeUnit M = (MeleeUnit)closestUnit; xDis = Mathf.Abs((PosX - M.PosX) * (PosX - M.PosX)); yDis = Mathf.Abs((PosY - M.PosY) * (PosY - M.PosY)); uDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } else if (closestUnit is RangedUnits) { RangedUnits R = (RangedUnits)closestUnit; xDis = Mathf.Abs((PosX - R.PosX) * (PosX - R.PosX)); yDis = Mathf.Abs((PosY - R.PosY) * (PosY - R.PosY)); uDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } else if (closestUnit is WizzardUnits) { WizzardUnits R = (WizzardUnits)closestUnit; xDis = Mathf.Abs((PosX - R.PosX) * (PosX - R.PosX)); yDis = Mathf.Abs((PosY - R.PosY) * (PosY - R.PosY)); uDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } if (closestBuilding is FactoryBuildings) { FactoryBuildings FB = (FactoryBuildings)closestBuilding; xDis = Mathf.Abs((PosX - FB.PosX) * (PosX - FB.PosX)); yDis = Mathf.Abs((PosY - FB.PosY) * (PosY - FB.PosY)); bDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } else if (closestBuilding is ResourceBuildings) { ResourceBuildings RB = (ResourceBuildings)closestBuilding; xDis = Mathf.Abs((PosX - RB.PosX) * (PosX - RB.PosX)); yDis = Mathf.Abs((PosY - RB.PosY) * (PosY - RB.PosY)); bDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } if (units[0] != null) { if (uDistance < bDistance) { distance = uDistance; enemyType = 0; } else { distance = bDistance; enemyType = 1; } } else { distance = bDistance; enemyType = 1; } //Checks to see if they are below 25% health so they move rather than attacking if (Health > MaxHealth * 0.25) { if (distance <= AttackRange) { IsAttacking = true; Combat(enemyType); } else { IsAttacking = false; Move(enemyType); } } else { Move(enemyType); } }
public void GenerateBattleField() // method to allow the random number of units, including the ranged and the melee units { for (int i = 0; i < BuildingNum; i++) { int UnitNum = Random.Range(0, 2); string UnitName; if (UnitNum == 0) { UnitName = "Melee"; } else { UnitName = "Ranged"; } ResourceBuildings DiamondMine = new ResourceBuildings(0, 0, 100, Faction.Overwatch, "**"); BitCoinMine.Add(DiamondMine); FactoryBuildings Barrack = new FactoryBuildings(0, 0, 100, Faction.Overwatch, "$$", Random.Range(3, 10), UnitName); Barracks.Add(Barrack); } for (int i = 0; i < BuildingNum; i++) { int UnitNum = Random.Range(0, 2); string UnitName; if (UnitNum == 0) { UnitName = "Melee"; } else { UnitName = "Ranged"; } ResourceBuildings DiamondMine = new ResourceBuildings(0, 0, 100, Faction.Talon, "**"); BitCoinMine.Add(DiamondMine); FactoryBuildings barrack = new FactoryBuildings(0, 0, 100, Faction.Talon, "$$", Random.Range(3, 10), UnitName); Barracks.Add(barrack); } for (int i = 0; i < BuildingNum; i++) { WizzardUnits wizzard = new WizzardUnits("Wizard", 0, 0, Faction.Neutral, 20, 2, 3, 1, "^", false); wizzardUnits.Add(wizzard); } foreach (ResourceBuildings u in BitCoinMine) { for (int i = 0; i < BitCoinMine.Count; i++) { int xPos = Random.Range(0, mapHeight); int yPos = Random.Range(0, mapWidth); while (xPos == BitCoinMine[i].PosX && yPos == BitCoinMine[i].PosY && xPos == Barracks[i].PosX && yPos == Barracks[i].PosY) { xPos = Random.Range(0, mapHeight); yPos = Random.Range(0, mapWidth); } u.PosX = xPos; u.PosY = yPos; } buildingMap[u.PosY, u.PosX] = (buildings)u; buildings.Add(u); } foreach (FactoryBuildings u in Barracks) { for (int i = 0; i < Barracks.Count; i++) { int xPos = Random.Range(0, mapHeight); int yPos = Random.Range(0, mapWidth); while (xPos == Barracks[i].PosX && yPos == Barracks[i].PosY && xPos == BitCoinMine[i].PosX && yPos == BitCoinMine[i].PosY) { xPos = Random.Range(0, mapHeight); yPos = Random.Range(0, mapWidth); } u.PosX = xPos; u.PosY = yPos; } buildingMap[u.PosY, u.PosX] = (buildings)u; buildings.Add(u); u.SpawnPointY = u.PosY; if (u.PosX < 19) { u.SpawnPointX = u.PosX + 1; } else { u.SpawnPointX = u.PosX - 1; } } foreach (WizzardUnits u in wizzardUnits) { for (int i = 0; i < wizzardUnits.Count; i++) { int xPos = Random.Range(0, mapHeight); int yPos = Random.Range(0, mapWidth); while (xPos == BitCoinMine[i].PosX && yPos == BitCoinMine[i].PosY && xPos == Barracks[i].PosX && yPos == Barracks[i].PosY && xPos == wizzardUnits[i].PosX && yPos == wizzardUnits[i].PosY) { xPos = Random.Range(0, mapHeight); yPos = Random.Range(0, mapWidth); } u.PosX = xPos; u.PosY = yPos; } uniMap[u.PosY, u.PosX] = (Unit)u; units.Add(u); } Populate(); PlaceBuildings(); }