public void SaveGame(string fileName) { TextWriter tw = new StreamWriter(fileName); XmlSerializer xs = new XmlSerializer(typeof(SaveGameData)); SaveGameData saveGameData = new SaveGameData(); saveGameData.BackgroundMap = backgroundMap; saveGameData.ForegroundMap = foregroundMap; saveGameData.OngroundMap = new List<string>(); List<StringBuilder> tempOngroundMap = new List<StringBuilder>(); for (int i = 0; i < 30; ++i) tempOngroundMap.Add(new StringBuilder("0000000000000000000000000000000000000000")); saveGameData.TankStates = new List<TankState>(); saveGameData.AmmoStates = new List<AmmoState>(); saveGameData.wallStates = new List<BlockSpriteState>(); saveGameData.EffectStates = new List<EffectState>(); saveGameData.ItemStates = new List<ItemState>(); // Tank data. foreach (Tank t in tanks.Values) saveGameData.TankStates.Add(t.State); // Ammo data. foreach (Ammo a in ammo) saveGameData.AmmoStates.Add(a.State); // BlockSprite data. foreach (BlockSprite bs in blockMap.Values) if (bs.Type != BlockSpriteType.Invisible && bs.Type != BlockSpriteType.Invulnerable) saveGameData.wallStates.Add(bs.State); else { switch (bs.Type) { case BlockSpriteType.Invulnerable: tempOngroundMap[bs.MapPoint.Y][bs.MapPoint.X] = 'x'; break; case BlockSpriteType.Invisible: tempOngroundMap[bs.MapPoint.Y][bs.MapPoint.X] = 'y'; break; } } foreach (var sb in tempOngroundMap) saveGameData.OngroundMap.Add(sb.ToString()); // Effect data. foreach (Effect e in effects) saveGameData.EffectStates.Add(e.State); // Item data. foreach (Item it in items) saveGameData.ItemStates.Add(it.State); // Next item time. saveGameData.nextItemTime = gameLogic.NextItemTime; xs.Serialize(tw, saveGameData); tw.Close(); }
public void LoadGame(String fileName) { TextReader tr = new StreamReader(fileName); XmlSerializer xs = new XmlSerializer(typeof(SaveGameData)); SaveGameData saveGameData = new SaveGameData(); saveGameData = (SaveGameData)xs.Deserialize(tr); tr.Close(); backgroundMap = saveGameData.BackgroundMap; foregroundMap = saveGameData.ForegroundMap; blockMap.Clear(); ConstructOngroundMap(saveGameData.OngroundMap, false); // Recreate tanks. tanks.Clear(); userTanks.Clear(); foreach (TankState ts in saveGameData.TankStates) Factory.CreateTank(ts, tanks); // Ammo. ammo.Clear(); foreach (AmmoState ammoState in saveGameData.AmmoStates) Factory.CreateAmmo(ammoState); // BlockSprite. foreach (BlockSpriteState bss in saveGameData.wallStates) Factory.CreateBlockSprite(bss); // Effect. effects.Clear(); foreach (EffectState e in saveGameData.EffectStates) Factory.CreateEffect(e); // Item. items.Clear(); foreach (ItemState itemState in saveGameData.ItemStates) Factory.CreateItem(itemState, items); // Next item time. gameLogic.NextItemTime = saveGameData.nextItemTime; }