private void DestroyMap() { Array.Clear(Map.map, 0, Map.map.Length); this.Controls.Clear(); MapState = Map.SaveState(); }
public void FillCargosTest_AddCargoToMap_MementoShouldContainTheSameCargo() { // Arrange var cargoMock = new Mock <Cargo>(); Map map = new Map(); map.Cargos.Add(cargoMock.Object); // Act MapMemento mm = new MapMemento(map); //Assert CollectionAssert.AreEqual(map.Cargos, mm.Cargos); }
public void RestoreState(MapMemento memento) { this.map = memento.map; }
private void GenerateMap() { DestroyMap(); Map = new Map(); Random rnd = new Random(); int countOfDoors = 0; for (int i = 0; i < 25; i++) { for (int j = 0; j < 25; j++) { if (i == 0) { if (rnd.Next(0, 50) == 1 && countOfDoors <= 2 && j != 0 && j != 24) { GenerateDoors(i, j, countOfDoors); Map.map[i, j] = 2; countOfDoors++; } else { GenerateBorder(i, j); Map.map[i, j] = 1; } } else if (i == 24) { if (rnd.Next(0, 50) == 1 && countOfDoors <= 2 && j != 0 && j != 24) { GenerateDoors(i, j, countOfDoors); Map.map[i, j] = 2; countOfDoors++; } else { GenerateBorder(i, j); Map.map[i, j] = 1; } } else if (j == 0) { if (rnd.Next(0, 50) == 1 && countOfDoors <= 2 && i != 0 && i != 24) { GenerateDoors(i, j, countOfDoors); Map.map[i, j] = 2; countOfDoors++; } else { GenerateBorder(i, j); Map.map[i, j] = 1; } } else if (j == 24) { if (rnd.Next(0, 50) == 1 && countOfDoors <= 2 && i != 0 && i != 24) { GenerateDoors(i, j, countOfDoors); Map.map[i, j] = 2; countOfDoors++; } else { if (countOfDoors == 0) { GenerateDoors(i, j, countOfDoors); Map.map[i, j] = 2; countOfDoors++; } else { GenerateBorder(i, j); Map.map[i, j] = 1; } } } else { if (Map.map[i - 1, j] != 2 && Map.map[i + 1, j] != 2 && Map.map[i, j + 1] != 2 && Map.map[i, j - 1] != 2) { if (rnd.Next(1, 100) < 10) { GenerateStoneMoss(i, j); Map.map[i, j] = 1; } else { Map.map[i, j] = 0; } } } } } MapState = Map.SaveState(); }
public void CreateGameSave(HeroMemento hero, EnemyMemento[] Enemies, MapMemento Map, GameMemento gameState) { WorkWithXml xml = new WorkWithXml("users.xml"); // Game saving data XDocument doc = new XDocument(); XElement save = new XElement("gamesave"); XElement game = new XElement("game"); XElement username = new XElement("username", gameState.userName); XElement level = new XElement("level", gameState.level); XElement score = new XElement("score", gameState.score); XElement countOfEnemies = new XElement("countOfEnemies", gameState.countOfEnemies); game.Add(username); game.Add(level); game.Add(score); game.Add(countOfEnemies); // Character saving data XElement character = new XElement("character"); XElement hp = new XElement("hitPoints", hero.HitPoints); XElement damage = new XElement("Damage", hero.Damage); XElement killedEnemies = new XElement("KilledEnemies", hero.KilledEnemies); XElement locX = new XElement("locationX", hero.sprite.Location.X); XElement locY = new XElement("locationY", hero.sprite.Location.Y); character.Add(hp); character.Add(damage); character.Add(killedEnemies); character.Add(locX); character.Add(locY); // Enemies saving data XElement enemies = new XElement("enemies"); foreach (EnemyMemento Enemy in Enemies) { XElement enemy = new XElement("enemy"); XElement id = new XElement("id", Enemy.id); XElement name = new XElement("name", Enemy.Name); XElement enemyHp = new XElement("hitPoints", Enemy.HitPoints); XElement enemyDamage = new XElement("damage", Enemy.damage); XElement locationX = new XElement("locationX", Enemy.sprite.Location.X); XElement locationY = new XElement("locationY", Enemy.sprite.Location.Y); enemy.Add(id); enemy.Add(name); enemy.Add(enemyHp); enemy.Add(enemyDamage); enemy.Add(locationX); enemy.Add(locationY); enemies.Add(enemy); } // Map saving data XElement map = new XElement("map"); for (int i = 0; i < Map.map.GetLength(0); i++) { XElement row = new XElement("row"); for (int j = 0; j < Map.map.GetLength(1); j++) { XElement cell = new XElement("cell", Map.map[i, j]); row.Add(cell); } map.Add(row); } save.Add(game); save.Add(character); save.Add(enemies); save.Add(map); doc.Add(save); doc.Save(@"database/gamesaves/" + gameState.userName + "_" + gameState.level.ToString() + "_" + gameState.score.ToString() + ".xml"); }