private static MementoNode NodeFromElement(GameMemento previous, XElement element) { var type = element.Attribute("type").Value; IUpdateAction action; switch (type) { case MoveBallToken: { var(src, dst) = MoveActionParamsFromElement(element.Element(MoveParamsElement)); action = MoveBallAction.New(src, dst); break; } case MovePieceToken: { var(src, dst) = MoveActionParamsFromElement(element.Element(MoveParamsElement)); action = MovePieceAction.New(src, dst); break; } case PassToken: action = new PassAction(); break; default: throw new ArgumentOutOfRangeException(); } return(previous.Update(action)); }
public void SaveGameStatistics(QuickLife script) { GameMemento newMemento = Instantiate(MementoType).GetComponent <GameMemento>(); newMemento.InitializeGame(this); script.SaveMemento(newMemento); }
// коли користувач створює нову гру то генеруємо карту і ворогів // інакше це робимо в RestoreState private void GenerateWorld(bool isFirstGame) { timer = new Timer(); if (!isFirstGame) { countOfEnemies = 0; GenerateMap(); GenerateEnemies(); } GenerateCharacter(); enemyTimer.Start(); GameState = new GameMemento(level, countOfEnemies, xml.GetUserById(currentUserId), score); createStats(level, character.killedEnemies); saveBtn.Location = new Point(width - 300, 700); saveBtn.Size = new Size(120, 40); saveBtn.BackgroundImage = Image.FromFile(pathForBackground2); saveBtn.FlatStyle = FlatStyle.Popup; saveBtn.Font = new Font(saveBtn.Font.Name, 13, FontStyle.Bold); saveBtn.ForeColor = Color.White; saveBtn.Enabled = false; saveBtn.Text = "Зберегти"; saveBtn.Click += new EventHandler((o, ev) => { history.CreateGameSave(characterState, enemyStateList, MapState, GameState); saveBtn.Enabled = false; exitBtn.Enabled = false; }); this.Controls.Add(saveBtn); exitBtn.Location = new Point(width - 170, 700); exitBtn.Size = new Size(120, 40); exitBtn.BackgroundImage = Image.FromFile(pathForBackground2); exitBtn.FlatStyle = FlatStyle.Popup; exitBtn.Font = new Font(exitBtn.Font.Name, 13, FontStyle.Bold); exitBtn.ForeColor = Color.White; exitBtn.Text = "Вийти"; exitBtn.Enabled = false; exitBtn.Click += new EventHandler((o, ev) => { Application.Exit(); }); this.Controls.Add(exitBtn); background.Image = Image.FromFile(pathForBackground); background.Location = new Point(0, 0); background.Size = new Size(750, 750); background.SizeMode = PictureBoxSizeMode.StretchImage; background.BringToFront(); this.Controls.Add(background); character.sprite.BackgroundImage = background.Image; }
public static XElement MementoToElement(GameMemento memento) { var(mementoRoot, nodes) = memento.Deconstruct(); return(new XElement(MementoElementName, RootToElement(mementoRoot), new XElement(MoveHistoryElement, nodes.ZipWithIndex(NodeToElement)))); }
public void Cancel() { GameMemento state = gameCaretaker.Restore(); this.field = state.field; this.x0 = state.x0; this.y0 = state.y0; this.counter = state.counter; }
public static void Save() { //TODO add function to allow change names SaveLoad.savedGame = GameMemento.current; BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(Application.persistentDataPath + "/savedGames.gd"); bf.Serialize(file, SaveLoad.savedGame); file.Close(); }
public object GetCurrentState() { // Gather state Console.WriteLine("\nSaving state ...\n"); _gameState = new GameMemento(); _gameState.Level = Level; _gameState.LevelName = LevelName; _gameState._levelNum = _levelNum; return(_gameState); }
public void RestoreSettingsFromMemento_TestSettings_SettingsInGameRestoredFromMemento() { // arrange Mock <IPlayfield> playfieldStub = new Mock <IPlayfield>(); GameMemento testMemento = new GameMemento(_testGameSettings, 15, playfieldStub.Object); IGame testedGame = new Impl.Game(_defaultGameSettings); // act testedGame.RestoreFromMemento(testMemento); // assert Assert.That(testedGame.Settings, Is.EqualTo(testMemento.Settings)); }
public void RestoreState(object savedState) { if (savedState.GetType() == typeof(GameMemento)) { GameMemento oldState = (GameMemento)savedState; // Restore state Console.WriteLine("\nRestoring state ...\n"); Level = _gameState.Level; LevelName = _gameState.LevelName; _levelNum = _gameState._levelNum; } }
private void SaveMemento(object sender, EventArgs e) { GameMemento memento = _gameLogic.SaveMemento(); if (_stepBack.Count == 0) { _stepBack.Push(memento); } else if (_stepBack.Peek().Turn != memento.Turn) { _stepBack.Push(memento); } _allowedStepsBack = (_allowedStepsBack + 1 > 3) ? _allowedStepsBack : ++_allowedStepsBack; }
/// Registers a memento to save on disk. /// Overwrites previous saves that used the same id. public void RegisterForSave(GameId id, GameMemento memento) { var exists = Saves.TryGetValue(id, out var entry); if (exists) { entry.Memento = memento; } else { var metadata = new GameMetadataBundle(id); Saves.Add(id, new SaveEntry(metadata, memento)); } }
public void RestorePlayfieldFromMemento_TestPlayfield_PlayfieldInGameRestoredFromMemento(Frame[,] randomBoard) { // arrange Mock <IPlayfield> playfieldStub = new Mock <IPlayfield>(); playfieldStub.SetupGet(x => x.Board).Returns(randomBoard); GameMemento mementoStub = new GameMemento(_defaultGameSettings, 15, playfieldStub.Object); IGame testedGame = new Impl.Game(_defaultGameSettings); // act testedGame.RestoreFromMemento(mementoStub); // assert Assert.That(testedGame.Playfield, Is.EqualTo(mementoStub.Playfield)); }
public GameMemento ReadGameDataFromFile() { GameMemento game = null; XmlElement xRoot = xDoc.DocumentElement; foreach (XmlNode node in xRoot) { if (node.Name == "game") { game = new GameMemento(Convert.ToInt32(node["level"].InnerText), Convert.ToInt32(node["countOfEnemies"].InnerText), node["username"].InnerText, Convert.ToInt32(node["score"].InnerText)); } } return(game); }
public void Shift(int position) { int x, y; PositionToCoordinates(position, out x, out y); if (Math.Abs(x0 - x) + Math.Abs(y0 - y) != 1) { return; } field[x0, y0] = field[x, y]; field[x, y] = 0; x0 = x; y0 = y; counter++; moves++; GameMemento state = new GameMemento(field, x0, y0, counter); gameCaretaker.Save(state); }
public static void Load() { //displays files and their names string filepath = Application.persistentDataPath; DirectoryInfo d = new DirectoryInfo(filepath); foreach (var file in d.GetFiles("*.gd")) { Debug.Log(file.Name); } //TODO after user selects the file, load the game if (File.Exists(Application.persistentDataPath + "/savedGames.gd")) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open); SaveLoad.savedGame = (GameMemento)bf.Deserialize(file); file.Close(); } }
public void LoadGame(GameMemento memento) { CurrentLevel = memento.GetState(); CurrentLevel.Load(NewGame.gameCanvas, NewGame.background, NewGame.MainStackPanel); }
public void SetMemento(GameMemento memento) { Level = memento.Level; Score.Number = memento.Score; Score.Date = memento.Date; }
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"); }
public void LoadGame(GameMemento memento) { _state = memento.GetState(); }
private void RestoreGame(string gamePath) { if (gamePath != null) { WorkWithXml savedData = new WorkWithXml("gamesaves/" + gamePath); characterState = savedData.ReadCharacterDataFromFile(); character.RestoreState(characterState); character.SaveState(); GameState = savedData.ReadGameDataFromFile(); level = GameState.level; score = GameState.score; Map = savedData.ReadMapDataFromFile(); Map.SaveState(); int t = 0; for (int i = 0; i < 25; i++) { for (int j = 0; j < 25; j++) { if (Map.map[i, j] == 1) { GenerateBorder(i, j); } else if (Map.map[i, j] == 2) { GenerateDoors(i, j, t); t++; } } } enemyStateList = savedData.ReadEnemyDataFromFile(); enemyList = new Enemy1[enemyStateList.Length]; for (int i = 0; i < enemyStateList.Length; i++) { if (enemyStateList[i] == null) { break; } enemyList[i] = new Enemy1(enemyStateList[i].id, enemyStateList[i].HitPoints, enemyStateList[i].damage, enemyStateList[i].Name, enemyStateList[i].sprite); this.Controls.Add(enemyList[i].sprite); countOfEnemies++; } if (countOfEnemies == 0) { OpenDoors(); } GenerateWorld(true); } else { PictureBox sprite = new PictureBox(); sprite.Image = Image.FromFile(pathForCharacterGoBack); sprite.SizeMode = PictureBoxSizeMode.StretchImage; sprite.Location = new Point(300, 300); sprite.Size = new Size(sizeOfSides, sizeOfSides); characterState = new HeroMemento(300, 100, 0, sprite); character.RestoreState(characterState); characterState = character.SaveState(); GenerateWorld(false); } }
public SaveEntry(GameMetadataBundle bundle, GameMemento memento) { _metadata = bundle; Memento = memento; IsOnDisk = false; }
/// Update the metadata to describe the given memento. public void Update(GameMemento memento) { SaveDate = DateTime.Now; LatestState = memento.State; }
public void TestReadWriteRoundTrip(GameMemento memento) { Assert.AreEqual(memento, Deserializer.MementoFromElement(Serializer.MementoToElement(memento))); }