public void GameLogic(Unit[] units, Building [] buildings) { info = ""; if (roundCheck > 0) { for (i = 0; i < units.Length; i++) { Unit u = (Unit)units[i]; x = u.xPos; y = u.yPos; if (u.Health > 0) { closestUnit = u.ClosestUnit(units, units.Length, u); if (closestUnit == u) { u.combatCheck = false; } else if (u.RangeCheck(closestUnit, u) == true) { u.combatCheck = true; u.CombatHandler(closestUnit, u); } else { u.combatCheck = false; u.MoveUnit(u, closestUnit); map.UpdatePosition(i, x, y); } info += u.ToString(u, units, i); } else { } } for (int k = 0; k < buildings.Length; k++) { Building b = buildings[k]; string buildingType = b.GetType().ToString(); string[] buildArr = buildingType.Split('.'); buildingType = buildArr[buildArr.Length - 1]; if (buildingType == "ResourceBuilding") { ResourceBuilding B = (ResourceBuilding)b; if (B.HP < 0) { B.Death(B); } else { info += B.ToString(buildings, B); if (temp > 4) { temp = 4; B.GenerateResources(); } } } if (buildingType == "FactoryBuilding") { FactoryBuilding B = (FactoryBuilding)b; B.productionSpeed = 5; if (B.HP < 0) { B.Death(B); } else { decimal d = roundCheck; if ((d / B.productionSpeed) % 1 == 0) { Unit u = B.SpawnUnit(B); Array.Resize(ref units, units.Length + 1); units[units.Length - 1] = u; } info += B.ToString(buildings, B); } } } } else { for (i = 0; i < units.Length; i++) { Unit u = (Unit)units[i]; info += u.ToString(u, units, i); } } }
public void GameLogic(Unit[] units, Building [] buildings, int _mapXSize, int _mapYSize) { //max map x and y size mapXSize = _mapXSize; mapYSize = _mapYSize; info = ""; buildingInfo = ""; //check so its not the first round if (roundCheck > 0) { for (i = 0; i < units.Length; i++) { Unit u = (Unit)units[i]; for (int k = 0; k < buildings.Length; k++) { if (u.team != buildings[k].team) { buildingCheck = true; } } x = u.xPos; y = u.yPos; if (u.Health <= 0) { //u.Death(units, i); } else if (buildingCheck == true) { //check for closest building closestBuilding = u.ClosestBuilding(u, buildings); //if in range then attack if (u.BuildingRangeCheck(closestBuilding, u) == true) { u.combatCheck = true; u.BuildingCombat(closestBuilding, u); buildingCheck = false; } else { //move unit to its target u.MoveToBuilding(u, closestBuilding); map.UpdatePosition(u, x, y); buildingCheck = false; u.combatCheck = false; } } else { if (u.Health > 0) { closestUnit = u.ClosestUnit(units, units.Length, u, buildings, map.wizards); if (closestUnit == u) { u.combatCheck = false; } else if (u.RangeCheck(closestUnit, u, map.wizards) == true) { u.combatCheck = true; u.CombatHandler(closestUnit, u, map.unitAmount, units); } else { u.combatCheck = false; u.MoveUnit(u, closestUnit, mapXSize, mapYSize); map.UpdatePosition(u, x, y); } info += u.ToString(u, units, i); } else { } } info += u.ToString(u, units, i); } for (int k = 0; k < buildings.Length; k++) { //explicit cast building Building b = buildings[k]; string buildingType = b.GetType().ToString(); string[] buildArr = buildingType.Split('.'); buildingType = buildArr[buildArr.Length - 1]; if (buildingType == "ResourceBuilding") { ResourceBuilding B = (ResourceBuilding)b; if (B.HP > 0) { //building info buildingInfo += B.ToString(buildings, B); if (temp > 4) { temp = 4; //generates rescources B.GenerateResources(); } } else { //if a building is destroyed B.Death(B, k, buildings); } } if (buildingType == "FactoryBuilding") { FactoryBuilding B = (FactoryBuilding)b; B.productionSpeed = 5; if (B.HP > 0) { decimal d = roundCheck; //check that the round is divisible if ((d / B.productionSpeed) % 1 == 0) { //resize the array and add a unit //Array.Resize(ref units, units.Length + 1); //units[units.Length - 1] = B.SpawnUnit(B); } buildingInfo += B.ToString(buildings, B); } else { B.Death(B, k, buildings); } } } for (int h = 0; h < map.wizards.Length; h++) { Unit u = (Unit)map.wizards[h]; x = u.xPos; y = u.yPos; //if wizard has more than 50% health if ((u.Health * 100 / u.maxHealth) > 50) { closestUnit = u.ClosestUnit(units, units.Length, u, buildings, map.wizards); if (closestUnit == u) { } else if (u.RangeCheck(closestUnit, u, map.wizards) == true) { u.combatCheck = true; u.CombatHandler(closestUnit, u, map.unitAmount, units); } else { u.combatCheck = false; u.MoveUnit(u, closestUnit, mapXSize, mapYSize); map.UpdatePosition(u, x, y); } } else { u.MoveUnit(u, closestUnit, mapXSize, mapYSize); map.UpdatePosition(u, x, y); } info += u.ToString(u, units, h); } } else { for (i = 0; i < units.Length; i++) { //show unit info Unit u = (Unit)units[i]; info += u.ToString(u, units, i); } for (int k = 0; k < buildings.Length; k++) { Building b = buildings[k]; string buildingType = b.GetType().ToString(); string[] buildArr = buildingType.Split('.'); buildingType = buildArr[buildArr.Length - 1]; if (buildingType == "ResourceBuilding") { ResourceBuilding B = (ResourceBuilding)b; buildingInfo += B.ToString(buildings, B); } if (buildingType == "FactoryBuilding") { FactoryBuilding B = (FactoryBuilding)b; buildingInfo += B.ToString(buildings, B); } } for (int h = 0; h < map.wizards.Length; h++) { //show wizard info Unit u = (Unit)map.wizards[h]; info += u.ToString(u, units, i); } } }
//CLASS METHODS public void BattlefieldCreator() { for (int i = 0; i < 20; i++) { for (int k = 0; k < 20; k++) { map[i, k] = Convert.ToChar("."); } } Random rnd = new Random(); units = new Unit[unitAmount]; for (int i = 0; i < unitAmount; i++) { int x = rnd.Next(0, 20); int y = rnd.Next(0, 20); int team = rnd.Next(0, 2); int unit = rnd.Next(0, 2); if (map[x, y] != '.') { for (int k = 0; k < 1000; k++) { x = rnd.Next(0, 20); y = rnd.Next(0, 20); if (map[x, y] == '.') { k = 1000; } } } if (unit == 1 & team == 0) { Unit RangedUnit = new Unit(x, y, 10, 1, 3, 5, team, Convert.ToChar("R"), false, "RangedUnit"); map[x, y] = RangedUnit.symbol; units[i] = RangedUnit; } if (unit == 1 & team == 1) { Unit RangedUnit = new Unit(x, y, 10, 1, 3, 5, team, Convert.ToChar("r"), false, "RangedUnit"); map[x, y] = RangedUnit.symbol; units[i] = RangedUnit; } if (unit == 0 & team == 0) { Unit MeleeUnit = new Unit(x, y, 20, 1, 2, 1, team, Convert.ToChar("M"), false, "MeleeUnit"); map[x, y] = MeleeUnit.symbol; units[i] = MeleeUnit; } if (unit == 0 & team == 1) { Unit MeleeUnit = new Unit(x, y, 20, 1, 2, 1, team, Convert.ToChar("m"), false, "MeleeUnit"); map[x, y] = MeleeUnit.symbol; units[i] = MeleeUnit; } } buildings = new Building[buildingAmount]; for (int j = 0; j < buildings.Length; j++) { int x = rnd.Next(0, 20); int y = rnd.Next(0, 20); int team = rnd.Next(0, 2); int buildingType = rnd.Next(0, 2); if (buildingType == 0) { ResourceBuilding B = new ResourceBuilding(x, y, 100, team, 'O'); map[x, y] = B.Symbol; buildings[j] = B; } else if (buildingType == 1) { FactoryBuilding B = new FactoryBuilding(x, y, 100, team, 'F'); map[x, y] = B.Symbol; buildings[j] = B; } } }
//CLASS METHODS public void BattlefieldCreator(int _mapXSize, int _mapYSize) { //sets the map size mapXSize = _mapXSize; mapYSize = _mapYSize; map = new char[mapXSize, mapYSize]; //fills battlefield with grass for (int i = 0; i < mapXSize; i++) { for (int k = 0; k < mapYSize; k++) { map[i, k] = Convert.ToChar("."); } } //randoms a wizard amount Random rnd = new Random(); wizardAmount = rnd.Next(2, 6); units = new Unit[unitAmount]; wizards = new Unit[wizardAmount]; for (int i = 0; i < unitAmount; i++) { //random values for the map int x = rnd.Next(0, mapXSize); int y = rnd.Next(0, mapYSize); int team = rnd.Next(0, 2); int unit = rnd.Next(0, 2); //checks that unit spawns in a spot with grass if (map[x, y] != '.') { for (int k = 0; k < 1000; k++) { x = rnd.Next(0, mapXSize); y = rnd.Next(0, mapYSize); if (map[x, y] == '.') { k = 1000; } } } //spawns units if (unit == 1 & team == 0) { Unit RangedUnit = new Unit(x, y, 10, 1, 3, 5, team, Convert.ToChar("R"), false, "RangedUnit"); map[x, y] = RangedUnit.symbol; units[i] = RangedUnit; } if (unit == 1 & team == 1) { Unit RangedUnit = new Unit(x, y, 10, 1, 3, 5, team, Convert.ToChar("r"), false, "RangedUnit"); map[x, y] = RangedUnit.symbol; units[i] = RangedUnit; } if (unit == 0 & team == 0) { Unit MeleeUnit = new Unit(x, y, 20, 1, 2, 1, team, Convert.ToChar("M"), false, "MeleeUnit"); map[x, y] = MeleeUnit.symbol; units[i] = MeleeUnit; } if (unit == 0 & team == 1) { Unit MeleeUnit = new Unit(x, y, 20, 1, 2, 1, team, Convert.ToChar("m"), false, "MeleeUnit"); map[x, y] = MeleeUnit.symbol; units[i] = MeleeUnit; } } buildings = new Building[buildingAmount]; //spawn buildings for (int j = 0; j < buildings.Length; j++) { int x = rnd.Next(0, mapXSize); int y = rnd.Next(0, mapYSize); int team = rnd.Next(0, 2); int buildingType = rnd.Next(0, 2); if (buildingType == 0) { ResourceBuilding B = new ResourceBuilding(x, y, 50, team, 'O'); map[x, y] = B.Symbol; buildings[j] = B; } else if (buildingType == 1) { FactoryBuilding B = new FactoryBuilding(x, y, 50, team, 'F'); map[x, y] = B.Symbol; buildings[j] = B; } } //spawn wizards for (int j = 0; j < wizards.Length; j++) { int x = rnd.Next(0, mapXSize); int y = rnd.Next(0, mapYSize); int team = rnd.Next(0, 2); int unit = rnd.Next(0, 2); if (map[x, y] != '.') { for (int k = 0; k < 1000; k++) { x = rnd.Next(0, mapXSize); y = rnd.Next(0, mapYSize); if (map[x, y] == '.') { k = 1000; } } } //creates wizard and puts it in wizard array Unit WizardUnit = new Unit(x, y, 10, 1, 2, 1, 3, 'W', false, "WizardUnit"); map[x, y] = WizardUnit.symbol; wizards[j] = WizardUnit; } }