public override Unit ClosestUnit(Unit[] Units) // finds the closest unit { int xDist, yDist, totalDist, closestDist, checkValue; Unit closestUnit, thisUnit; closestDist = 19; closestUnit = Units[1]; checkValue = 69; thisUnit = Units[1]; for (int i = 0; i < Units.Length; i++) { string unitType = Units[i].GetType().ToString(); string[] Type = unitType.Split('.'); unitType = Type[Type.Length - 1]; if (unitType == "MeleeUnits") { MeleeUnits un = (MeleeUnits)Units[i]; if (this.xPos == un.xPos && this.yPos == un.yPos || un.Death() || this.team == un.team) { if (this.xPos == un.xPos && this.yPos == un.yPos) { thisUnit = un; } } else { xDist = Math.Abs(this.xPos - un.xPos); yDist = Math.Abs(this.yPos - un.yPos); totalDist = xDist + yDist; if (totalDist < closestDist) { closestDist = totalDist; closestUnit = Units[i]; checkValue = 42; } } } else { RangedUnits un = (RangedUnits)Units[i]; if (this.xPos == un.xPos && this.yPos == un.yPos || un.Death() || this.team == un.team) { if (this.xPos == un.xPos && this.yPos == un.yPos) { thisUnit = un; } } else { xDist = Math.Abs(this.xPos - un.xPos); yDist = Math.Abs(this.yPos - un.yPos); totalDist = xDist + yDist; if (totalDist < closestDist) { closestDist = totalDist; closestUnit = Units[i]; checkValue = 42; } } } if (checkValue == 69) { return(thisUnit); } else { return(closestUnit); } } if (checkValue == 69) { return(thisUnit); } else { return(closestUnit); } }
public void GameLogic(Unit[] ArrUnits) { gameRounds++; Random rnd = new Random(); bool death; string direction; direction = " "; death = false; OutputString = ""; for (int i = 0; i < ArrUnits.Length; i++) { string unitType = ArrUnits[i].GetType().ToString(); string[] Type = unitType.Split('.'); unitType = Type[Type.Length - 1]; if (unitType == "MeleeUnit") { MeleeUnits un = (MeleeUnits)ArrUnits[i]; death = un.Death(); if (death == true) { } else { Unit closestunit; closestunit = un.ClosestUnit(ArrUnits); if (un == closestunit) { } else { un.AttackRange(closestunit); if (un.HP <= (un.maxHP * 0.25)) { string randomDirection; int random; random = rnd.Next(1, 5); if (random == 1) { randomDirection = "left"; } else if (random == 2) { randomDirection = "right"; } else if (random == 3) { randomDirection = "up"; } else { randomDirection = "down"; } un.Move(randomDirection); } else { if (un.isAttacking == true) { un.Combat(closestunit); } else { int oldX, oldY; oldX = un.xPos; oldY = un.yPos; direction = Direction(un, closestunit); un.Move(direction); map.MapUpdate(un, oldX, oldY); } } } } OutputString += "\n" + un.ToString(); } else { RangedUnits un = (RangedUnits)ArrUnits[i]; death = un.Death(); if (death == true) { } else { Unit closestunit; closestunit = un.ClosestUnit(ArrUnits); if (un == closestunit) { } else { un.AttackRange(closestunit); if (un.HP <= (un.maxHP * 0.25)) { string randomDirection; int random; random = rnd.Next(1, 5); if (random == 1) { randomDirection = "left"; } else if (random == 2) { randomDirection = "right"; } else if (random == 3) { randomDirection = "up"; } else { randomDirection = "down"; } un.Move(randomDirection); } else { if (un.isAttacking == true) { un.Combat(closestunit); } else { int oldX, oldY; oldX = un.xPos; oldY = un.yPos; direction = Direction(un, closestunit); un.Move(direction); map.MapUpdate(un, oldX, oldY); } } } } OutputString += "\n" + un.ToString(); } OutputString += "\n"; } }