public void LoadGame() { GameSerializableData data = null; BinaryFormatter binaryFormatter = new BinaryFormatter(); try { using (FileStream fs = new FileStream(_gameSavedDataPath, FileMode.Open)) data = (GameSerializableData)binaryFormatter.Deserialize(fs); } catch (FileNotFoundException) { MessageBox.Show("File with saved game does not exist!"); return; } catch (SerializationException) { MessageBox.Show("File with saved game is corrupted!"); return; } Game.GameField = new GameField(data.GameFieldSerializableData.HorizCellsCount, data.GameFieldSerializableData.VertCellsCount, null); Game.Player1 = new Player(data.Player1.SwordsmanImageUri, data.Player1.ArcherImageUri, data.Player1.PeasantImageUri); Game.Player2 = new Player(data.Player2.SwordsmanImageUri, data.Player2.ArcherImageUri, data.Player2.PeasantImageUri); Game.IsPlayer1Selected = data.IsPlayer1Selected; LoadPlayers(data); }
public void SaveGame() { var data = new GameSerializableData() { IsPlayer1Selected = Game.IsPlayer1Selected }; data.Player1 = GetPlayerSerializableData(Game.Player1); data.Player2 = GetPlayerSerializableData(Game.Player2); data.GameFieldSerializableData = GetGameFieldSerializableData(Game.GameField); var binaryFormatter = new BinaryFormatter(); using (var fs = new FileStream(_gameSavedDataPath, FileMode.Create)) binaryFormatter.Serialize(fs, data); }
public void LoadPlayers(GameSerializableData data) { foreach (var item in data.Player1.UnitStacksSerializableData) { UnitStack unitStack = new UnitStack(item.UnitType, item.UnitsCapacity) { Units = new Stack <Unit>(), CellIndex = item.CellIndex }; foreach (var unit in item.Units) { Unit u = SimpleUnitFactory.CreateUnit(item.UnitType); u.CurrentHitPoints = unit.CurrentHitPoints; unitStack.Units.Push(u); } unitStack.Cell = Game.GameField.GameFieldCells[unitStack.CellIndex]; Game.Player1.UnitStacks.Add(unitStack); Game.GameField.GameFieldCells[unitStack.CellIndex].UnitStack = Game.Player1.UnitStacks.Last(); Game.GameField.GameFieldCells[unitStack.CellIndex].MaxUnitNumber = data.GameFieldSerializableData.GameFieldCellsSerializableData[unitStack.CellIndex].MaxUnitNumber; Game.GameField.GameFieldCells[unitStack.CellIndex].CurrentUnitNumber = data.GameFieldSerializableData.GameFieldCellsSerializableData[unitStack.CellIndex].CurrentUnitNumber; switch (Game.GameField.GameFieldCells[unitStack.CellIndex].UnitStack.UnitType) { case UnitType.Swordsman: Game.GameField.GameFieldCells[unitStack.CellIndex].UnitImage = Game.Player1.SwordsmanImage; break; case UnitType.Archer: Game.GameField.GameFieldCells[unitStack.CellIndex].UnitImage = Game.Player1.ArcherImage; break; case UnitType.Peasant: Game.GameField.GameFieldCells[unitStack.CellIndex].UnitImage = Game.Player1.PeasantImage; break; default: break; } } foreach (var item in data.Player2.UnitStacksSerializableData) { UnitStack unitStack = new UnitStack(item.UnitType, item.UnitsCapacity) { Units = new Stack <Unit>(), CellIndex = item.CellIndex }; foreach (var unit in item.Units) { Unit u = SimpleUnitFactory.CreateUnit(item.UnitType); u.CurrentHitPoints = unit.CurrentHitPoints; unitStack.Units.Push(u); } unitStack.Cell = Game.GameField.GameFieldCells[unitStack.CellIndex]; Game.Player2.UnitStacks.Add(unitStack); Game.GameField.GameFieldCells[unitStack.CellIndex].UnitStack = Game.Player2.UnitStacks.Last(); Game.GameField.GameFieldCells[unitStack.CellIndex].MaxUnitNumber = data.GameFieldSerializableData.GameFieldCellsSerializableData[unitStack.CellIndex].MaxUnitNumber; Game.GameField.GameFieldCells[unitStack.CellIndex].CurrentUnitNumber = data.GameFieldSerializableData.GameFieldCellsSerializableData[unitStack.CellIndex].CurrentUnitNumber; switch (Game.GameField.GameFieldCells[unitStack.CellIndex].UnitStack.UnitType) { case UnitType.Swordsman: Game.GameField.GameFieldCells[unitStack.CellIndex].UnitImage = Game.Player2.SwordsmanImage; break; case UnitType.Archer: Game.GameField.GameFieldCells[unitStack.CellIndex].UnitImage = Game.Player2.ArcherImage; break; case UnitType.Peasant: Game.GameField.GameFieldCells[unitStack.CellIndex].UnitImage = Game.Player2.PeasantImage; break; default: break; } } }