//all the logic that makes the units do their actions and make the game function public void GameLogic(unit[] arrUnits) { gameRounds++; Random rnd = new Random(); bool death; string direction; direction = " "; death = false; OutputString = ""; for (int i = 0; i < this.arrUnits.Length; i++) { death = this.arrUnits[i].Death(); unit closestunit = this.arrUnits[i].ClosestUnit(arrUnits); if (arrUnits[i].unitName == "Wizard") { //wizard check if (death == true) //death check { arrUnits[i].HP = 0; } else { if (this.arrUnits[i] == closestunit) { closestunit = arrUnits[i].ClosestUnit(arrUnits); } //makes sure closest unit worked correctly else { this.arrUnits[i].AttackRange(closestunit); //checks if target is in range if (this.arrUnits[i].HP <= (this.arrUnits[i].maxHP * 0.50)) //checks if units health is low enough to run away { //makes unit run away in random direction 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"; } this.arrUnits[i].Move(randomDirection); this.arrUnits[i].HP += 4; } else { //if hp above runaway amount if (this.arrUnits[i].isAttacking == true) { //makes unit attack Wizard unit = (Wizard)arrUnits[i]; unit.WizardAOE(this.arrUnits); } else {//makes unit move closer to target int oldX, oldY; oldX = this.arrUnits[i].xPos; oldY = this.arrUnits[i].yPos; direction = Direction(this.arrUnits[i], closestunit); this.arrUnits[i].Move(direction); map.MapUpdate(this.arrUnits[i], oldX, oldY); } } } }//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ } else { if (death == true) { arrUnits[i].HP = 0; } else { Building closestBuilding = this.arrUnits[i].ClosestUnit(map.arrBuildings); //sees if the building or unit is closer so that it knows witch one to attack int xDistb = Math.Abs(this.arrUnits[i].xPos - closestBuilding.xPos); int yDistb = Math.Abs(this.arrUnits[i].yPos - closestBuilding.yPos); int totalDistb = xDistb + yDistb; int xDistu = Math.Abs(this.arrUnits[i].xPos - closestunit.xPos); int yDistu = Math.Abs(this.arrUnits[i].yPos - closestunit.yPos); int totalDistu = xDistu + yDistu; if (totalDistb < totalDistu && closestBuilding.hP > 0) {//if building is closer this.arrUnits[i].AttackRange(closestBuilding); if (this.arrUnits[i].HP <= (this.arrUnits[i].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"; } this.arrUnits[i].Move(randomDirection); this.arrUnits[i].HP += 2; } else { if (this.arrUnits[i].isAttacking == true) { this.arrUnits[i].Combat(closestBuilding); } else { int oldX, oldY; oldX = this.arrUnits[i].xPos; oldY = this.arrUnits[i].yPos; direction = Direction(this.arrUnits[i], closestBuilding); this.arrUnits[i].Move(direction); map.MapUpdate(this.arrUnits[i], oldX, oldY); } } } else {//if unit is closer if (this.arrUnits[i] == closestunit) { closestunit = arrUnits[i].ClosestUnit(arrUnits); } else { this.arrUnits[i].AttackRange(closestunit); if (this.arrUnits[i].HP <= (this.arrUnits[i].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"; } this.arrUnits[i].Move(randomDirection); this.arrUnits[i].HP += 2; } else { if (this.arrUnits[i].isAttacking == true) { this.arrUnits[i].Combat(closestunit); } else { int oldX, oldY; oldX = this.arrUnits[i].xPos; oldY = this.arrUnits[i].yPos; direction = Direction(this.arrUnits[i], closestunit); this.arrUnits[i].Move(direction); map.MapUpdate(this.arrUnits[i], oldX, oldY); } } } } } } //compiles a string to output all the tostring stats of the units OutputString += "\n" + this.arrUnits[i].ToString(); OutputString += "\n"; } buildingOutput = " "; for (int i = 0; i < map.arrBuildings.Length; i++) {//buildings do their actions string buildingType = map.arrBuildings[i].GetType().ToString(); string[] buildingArr = buildingType.Split('.'); buildingType = buildingArr[buildingArr.Length - 1]; //typecheck and explicit cast if (buildingType == "ResourceBuilding") { ResourceBuilding building = (ResourceBuilding)map.arrBuildings[i]; if (building.Death() == true) { } //death check else { building.ResourceManagement(); if (building.team == "Team1") {//checks team and adds to their resources that have been gathered t1Resources += building.resourcesGeneratedPerRound; } else { t2Resources += building.resourcesGeneratedPerRound; } } } else if (buildingType == "FactoryBuilding") { FactoryBuilding building = (FactoryBuilding)map.arrBuildings[i]; if (building.Death() == true) { } else { if (building.team == "Team1") { if (t1Resources >= 60) {//checks team and if there are enough resources to be able to spawn a unit and subtracts from the total gathered resources if it creates a unit Array.Resize(ref this.arrUnits, this.arrUnits.Length + 1);; this.arrUnits[this.arrUnits.Length - 1] = building.CreateUnit(); map.arrUnits = this.arrUnits; t1Resources -= 60; } } else { if (t2Resources >= 60) { Array.Resize(ref this.arrUnits, this.arrUnits.Length + 1);; this.arrUnits[this.arrUnits.Length - 1] = building.CreateUnit(); map.arrUnits = this.arrUnits; t2Resources -= 60; } } } }//outputs the buildings tostring buildingOutput += "\n" + map.arrBuildings[i].toString(); buildingOutput += "\n"; }//makes sure the two unit arrays dont differ map.arrUnits = this.arrUnits; }
//CLASS METHODS public void RandomBattlefield() { // method creates a random battlefield and initializes all of the buildings and units on the map int randomX, randomY; int random; randomX = 1; randomY = 1; arrUnits = new unit[sumUnits]; arrBuildings = new Building[sumBuildings]; Random rnd = new Random(); //populate map for (int i = 0; i < arrMap.GetLength(0); i++) { for (int k = 0; k < arrMap.GetLength(1); k++) { arrMap[i, k] = ','; } } //makes units for (int i = 0; i < sumUnits; i++) { randomX = rnd.Next(0, arrMap.GetLength(0)); randomY = rnd.Next(0, arrMap.GetLength(1)); random = rnd.Next(1, 8); while (arrMap[randomX, randomY] == 'x' || arrMap[randomX, randomY] == 'X' || arrMap[randomX, randomY] == 'o' || arrMap[randomX, randomY] == 'O') {//checks that they dont spawn ontop of eachother randomX = rnd.Next(0, arrMap.GetLength(0)); randomY = rnd.Next(0, arrMap.GetLength(1)); } //randomises the unit that will spawn if (random == 1) { MeleeUnit unit = new MeleeUnit("Knight", randomX, randomY, 100, 1, 5, 1, "Team1", 'X', false); arrMap[unit.xPos, unit.yPos] = unit.symbol; arrUnits[i] = unit; } else if (random == 2) { MeleeUnit unit = new MeleeUnit("Knight", randomX, randomY, 100, 1, 5, 1, "Team2", 'x', false); arrMap[unit.xPos, unit.yPos] = unit.symbol; arrUnits[i] = unit; } else if (random == 3) { RangedUnit unit = new RangedUnit("Archer", randomX, randomY, 80, 1, 3, 3, "Team1", 'O', false); arrMap[unit.xPos, unit.yPos] = unit.symbol; arrUnits[i] = unit; } else if (random == 4) { RangedUnit unit = new RangedUnit("Archer", randomX, randomY, 80, 1, 3, 3, "Team2", 'o', false); arrMap[unit.xPos, unit.yPos] = unit.symbol; arrUnits[i] = unit; } else if (random == 5) { Wizard unit = new Wizard("Wizard", randomX, randomY, 100, 1, 5, 2, "Team1", 'W', false); arrMap[unit.xPos, unit.yPos] = unit.symbol; arrUnits[i] = unit; } else if (random == 6) { Wizard unit = new Wizard("Wizard", randomX, randomY, 100, 1, 5, 2, "Team2", 'w', false); arrMap[unit.xPos, unit.yPos] = unit.symbol; arrUnits[i] = unit; } else { Wizard unit = new Wizard("Wizard", randomX, randomY, 150, 1, 7, 2, "neutral", 'M', false); arrMap[unit.xPos, unit.yPos] = unit.symbol; arrUnits[i] = unit; } } //spawns all the buildings for (int k = 0; k < sumBuildings; k++) { randomX = rnd.Next(0, arrMap.GetLength(0)); randomY = rnd.Next(0, arrMap.GetLength(1)); while (arrMap[randomX, randomY] == 'x' || arrMap[randomX, randomY] == 'X' || arrMap[randomX, randomY] == 'o' || arrMap[randomX, randomY] == 'O') {//checks that they dont spawn ontop of eachother randomX = rnd.Next(0, arrMap.GetLength(0)); randomY = rnd.Next(0, arrMap.GetLength(1)); } //randomises building that will spawn random = rnd.Next(1, 7); int spawnYPos; spawnYPos = 0; if (randomY == 19) { spawnYPos = 18; } else if (randomY == 0) { spawnYPos = 1; } else { spawnYPos = randomY - 1; } if (random == 1) { ResourceBuilding building = new ResourceBuilding(randomX, randomY, 200, "Team1", 'R', "Tiberuim", 0, 5, 2000); arrMap[building.xPos, building.yPos] = building.symbol; arrBuildings[k] = building; } else if (random == 2) { ResourceBuilding building = new ResourceBuilding(randomX, randomY, 200, "Team2", 'r', "Tiberuim", 0, 5, 2000); arrMap[building.xPos, building.yPos] = building.symbol; arrBuildings[k] = building; } else if (random == 3) { FactoryBuilding building = new FactoryBuilding(randomX, randomY, 200, "Team1", 'F', "Melee Unit", 20, spawnYPos); arrMap[building.xPos, building.yPos] = building.symbol; arrBuildings[k] = building; } else if (random == 4) { FactoryBuilding building = new FactoryBuilding(randomX, randomY, 200, "Team1", 'F', "Ranged Unit", 20, spawnYPos); arrMap[building.xPos, building.yPos] = building.symbol; arrBuildings[k] = building; } else if (random == 5) { FactoryBuilding building = new FactoryBuilding(randomX, randomY, 200, "Team2", 'f', "Melee Unit", 20, spawnYPos); arrMap[building.xPos, building.yPos] = building.symbol; arrBuildings[k] = building; } else { FactoryBuilding building = new FactoryBuilding(randomX, randomY, 200, "Team2", 'f', "Ranged Unit", 20, spawnYPos); arrMap[building.xPos, building.yPos] = building.symbol; arrBuildings[k] = building; } } }