//CLASS METHODS public void RandomBattlefield() { int randomX, randomY; arrUnits = new unit[sumUnits]; Random rnd = new Random(); for (int i = 0; i < 20; i++) { for (int k = 0; k < 20; k++) { arrMap[i, k] = ','; } } for (int i = 0; i < sumUnits; i++) { randomX = rnd.Next(0, 20); randomY = rnd.Next(0, 20); random = rnd.Next(1, 5); while (arrMap[randomX, randomY] == 'x' || arrMap[randomX, randomY] == 'X' || arrMap[randomX, randomY] == 'o' || arrMap[randomX, randomY] == 'O') { randomX = rnd.Next(0, 20); randomY = rnd.Next(0, 20); } if (random == 1) { MeleeUnit unit = new MeleeUnit(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(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(randomX, randomY, 80, 1, 3, 3, "Team1", 'O', false); arrMap[unit.xPos, unit.yPos] = unit.symbol; arrUnits[i] = unit; } else { RangedUnit unit = new RangedUnit(randomX, randomY, 80, 1, 3, 3, "Team2", 'o', false); arrMap[unit.xPos, unit.yPos] = unit.symbol; arrUnits[i] = unit; } } }
public unit CreateUnit() { unitsProduced += 1; if (unitToProduce == "Ranged Unit") { RangedUnit unit = new RangedUnit("Archer", this.xPos, spawnYPos, 80, 1, 3, 1, this.team, 'O', false); return(unit); } else { MeleeUnit unit = new MeleeUnit("Knight", this.xPos, spawnYPos, 100, 1, 5, 1, this.team, 'X', false); return(unit); } }
public void MapUpdate(unit unitMove, int xOld, int yOld) { string unitType = unitMove.GetType().ToString(); string[] Type = unitType.Split('.'); unitType = Type[Type.Length - 1]; if (unitType == "MeleeUnit") { MeleeUnit un = (MeleeUnit)unitMove; arrMap[xOld, yOld] = ','; arrMap[un.xPos, un.yPos] = un.symbol; } if (unitType == "RangedUnit") { RangedUnit un = (RangedUnit)unitMove; arrMap[xOld, yOld] = ','; arrMap[un.xPos, un.yPos] = un.symbol; } }
private void btnLoad_Click(object sender, EventArgs e) { //Loads saved simulation from textfile into program and initializes the simulation load = true; int count; FileStream ufs = new FileStream("Saves/Units/units", FileMode.Open, FileAccess.Read); FileStream bfs = new FileStream("Saves/Buildings/buildings", FileMode.Open, FileAccess.Read); StreamReader usr = new StreamReader(ufs); StreamReader bsr = new StreamReader(bfs); for (int i = 0; i < 20; i++) { for (int k = 0; k < 20; k++) { gameEngine.map.arrMap[i, k] = ','; } } count = File.ReadAllLines("Saves/Units/units").Length; gameEngine.map.arrUnits = new unit[count]; for (int i = 0; i < count; i++) { string line = usr.ReadLine(); string[] arrline = line.Split(','); char uSymbol = Convert.ToChar(arrline[0]); int x = Convert.ToInt32(arrline[1]); int y = Convert.ToInt32(arrline[2]); int Maxhp = Convert.ToInt32(arrline[3]); int HP = Convert.ToInt32(arrline[4]); string Name = (arrline[5]); string Team = (arrline[6]); int Speed = Convert.ToInt32(arrline[7]); bool IsAttacking = Convert.ToBoolean(arrline[8]); int Attack = Convert.ToInt32(arrline[9]); int AttackRange = Convert.ToInt32(arrline[10]); if (uSymbol == 'x' || uSymbol == 'X') { MeleeUnit un = new MeleeUnit(Name, x, y, HP, Speed, Attack, AttackRange, Team, uSymbol, IsAttacking); gameEngine.map.arrUnits[i] = un; gameEngine.map.arrMap[un.xPos, un.yPos] = un.symbol; } else if (uSymbol == 'o' || uSymbol == 'O') { RangedUnit un = new RangedUnit(Name, x, y, HP, Speed, Attack, AttackRange, Team, uSymbol, IsAttacking); gameEngine.map.arrUnits[i] = un; gameEngine.map.arrMap[un.xPos, un.yPos] = un.symbol; } else { Wizard un = new Wizard(Name, x, y, HP, Speed, Attack, AttackRange, Team, uSymbol, IsAttacking); gameEngine.map.arrUnits[i] = un; gameEngine.map.arrMap[un.xPos, un.yPos] = un.symbol; } } ufs.Close(); usr.Close(); count = File.ReadAllLines("Saves/Buildings/buildings").Length; gameEngine.map.arrBuildings = new Building[count]; for (int i = 0; i < count; i++) { string line = bsr.ReadLine(); string[] arrline = line.Split(','); char bSymbol = Convert.ToChar(arrline[0]); int x = Convert.ToInt32(arrline[1]); int y = Convert.ToInt32(arrline[2]); string team = (arrline[3]); int maxHP = Convert.ToInt32(arrline[4]); int HP = Convert.ToInt32(arrline[5]); if (bSymbol == 'F') { int Production = Convert.ToInt32(arrline[6]); int Yspawn = Convert.ToInt32(arrline[7]); string UnitProduction = (arrline[8]); int unitsProduced = Convert.ToInt32(arrline[9]); FactoryBuilding bld = new FactoryBuilding(x, y, HP, team, bSymbol, UnitProduction, Production, Yspawn); gameEngine.map.arrBuildings[i] = bld; gameEngine.map.arrMap[bld.XPos, bld.YPos] = bld.Symbol; } else { string Resource = (arrline[6]); int GatheringRate = Convert.ToInt32(arrline[7]); int Gathered = Convert.ToInt32(arrline[8]); int ResourcePool = Convert.ToInt32(arrline[9]); ResourceBuilding bld = new ResourceBuilding(x, y, HP, team, bSymbol, Resource, Gathered, GatheringRate, ResourcePool); gameEngine.map.arrBuildings[i] = bld; gameEngine.map.arrMap[bld.XPos, bld.YPos] = bld.Symbol; } } bfs.Close(); bsr.Close(); lblMap.Text = gameEngine.map.MapDisplay(); timer1.Start(); }
//CLASS METHODS public void RandomBattlefield() { int randomX, randomY; int random; randomX = 1; randomY = 1; arrUnits = new unit[sumUnits]; arrBuildings = new Building[sumBuildings]; Random rnd = new Random(); for (int i = 0; i < 20; i++) { for (int k = 0; k < 20; k++) { arrMap[i, k] = ','; } } for (int i = 0; i < sumUnits; i++) { randomX = rnd.Next(0, 20); randomY = rnd.Next(0, 20); random = rnd.Next(1, 5); while (arrMap[randomX, randomY] == 'x' || arrMap[randomX, randomY] == 'X' || arrMap[randomX, randomY] == 'o' || arrMap[randomX, randomY] == 'O') { randomX = rnd.Next(0, 20); randomY = rnd.Next(0, 20); } 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 { RangedUnit unit = new RangedUnit("Archer", randomX, randomY, 80, 1, 3, 3, "Team2", 'o', false); arrMap[unit.xPos, unit.yPos] = unit.symbol; arrUnits[i] = unit; } } for (int k = 0; k < sumBuildings; k++) { randomX = rnd.Next(0, 20); randomY = rnd.Next(0, 20); while (arrMap[randomX, randomY] == 'x' || arrMap[randomX, randomY] == 'X' || arrMap[randomX, randomY] == 'o' || arrMap[randomX, randomY] == 'O') { randomX = rnd.Next(0, 20); randomY = rnd.Next(0, 20); } 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", 10, 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", 10, 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", 10, spawnYPos); arrMap[building.xPos, building.yPos] = building.symbol; arrBuildings[k] = building; } else { FactoryBuilding building = new FactoryBuilding(randomX, randomY, 200, "Team2", 'F', "Ranged Unit", 10, spawnYPos); arrMap[building.xPos, building.yPos] = building.symbol; arrBuildings[k] = building; } } }
//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; } } }