public void DoSave(SaveGameData data, PlanetsSystem[] systems, Player[] players) { fileName = "SAVE" + data.Slot + ".sav"; fullpath = "C:\\Users\\" + username + "\\Documents\\Saves\\"; Directory.CreateDirectory(Path.GetDirectoryName(fullpath + fileName)); if (File.Exists(fullpath + fileName)) { File.Delete(fullpath + fileName); //If there's already a file in that slot, delete it first! or.. //...if the new file is shorter than the original, junk characters get added to the end of the xml file! } FileStream stream = File.Open(fullpath + fileName, FileMode.OpenOrCreate); XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData), extratypes); //Player[] playerArray = new Player[1]; //playerArray[0] = player; //Have to put the player object into an array because Info is an array of object arrays :/ object[][] info = new object[2][]; info[0] = systems; info[1] = players; data.Info = info; try { serializer.Serialize(stream, data); } finally { stream.Close(); } }
public Account() { SaveData = new SaveGameData(); SaveData.DateT = DateTime.Now; player = new Player(); saveGame = new SaveGame(SaveData); }
public void SaveAccount(SaveGameData data, PlanetsSystem[] systems, Player[] AIPlayers) { Player[] players = new Player[AIPlayers.Length + 1]; for (int i = 0; i < players.Length; i++) { if (i < players.Length - 1) { players[i] = AIPlayers[i]; } else players[i] = player; } SaveData.Score = data.Score; if (SaveData.Score != 0) { saveGame.DoSave(SaveData, systems, players); } else return; }
public SaveGameData LoadAccount(SaveGameData data, int Slot, ref PlanetsSystem[] systems, ref Player player, ref AIPlayer[] aiplayers) { slot = Slot; data = saveGame.LoadData(slot, ref systems, ref player, ref aiplayers); return data; }
public SaveGameData LoadData(int slot, ref PlanetsSystem[] systems, ref Player player, ref AIPlayer[] AIPlayers) { object[][] info = new object[2][]; SaveGameData data; data = new SaveGameData(); string Fullpath = "C:\\Users\\" + username + "\\Documents\\Saves\\SAVE" + slot + ".sav"; if (File.Exists(Fullpath)) { FileStream stream = File.Open(Fullpath, FileMode.Open, FileAccess.Read); XmlReader reader = new XmlTextReader(stream); try { if (File.Exists(Fullpath)) { // Read the data from the file XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData), extratypes); if (serializer.CanDeserialize(reader)) { data = (SaveGameData)serializer.Deserialize(reader); // info[0] = (object[])data.Info[0]; //array of additional objects systems = new PlanetsSystem[data.Info[0].Length]; // for (int i = 0; i < data.Info[0].Length; i++) { systems[i] = (PlanetsSystem)info[0][i]; } Player[] playerArray = (Player[])data.Info[1][0]; List<Player> playerList = playerArray.ToList(); player = playerList[playerList.Count - 1]; playerList.RemoveAt(playerList.Count - 1); for (int i = 0; i < playerList.Count; i++) { AIPlayers[i] = (AIPlayer)playerList[i]; } } } } finally { // Close the file stream.Close(); } } return data; }
public override void LoadContent(Account acc, NewGameDetails details) { user = acc; SysTex = content.Load<Texture2D>("sun"); RingTex = content.Load<Texture2D>("ring"); background = content.Load<Texture2D>("galaxyBackground"); Font = content.Load<SpriteFont>("Font"); ShipTex = content.Load<Texture2D>("Ship1"); base.sf = content.Load<SpriteFont>("Font"); Texture2D sectorOutline = content.Load<Texture2D>("outline"); for (int i = 1; i <= 20; i++) { planetTextures[i - 1] = content.Load<Texture2D>("Planets/planet" + i); } HumanPlayer = new Player(details.playerFaction, 1); AllPlayers.Add(HumanPlayer); HumanPlayer.playerListPosition = AllPlayers.Count - 1; screentitle = "OVERVIEW SCREEN"; data = user.SaveData; prev = new SaveGameData(); aiPlayers = new AIPlayer[details.NumAI]; //plus 1 so the player can be added to the array when serializing for (int i = 0; i < details.NumAI; i++) { aiPlayers[i] = new AIPlayer(); aiPlayers[i].playerNumber = i; AllPlayers.Add(aiPlayers[i]); aiPlayers[i].playerListPosition = AllPlayers.Count - 1; } map.Initialize(details.NumSystems, SysTex, RingTex, details.MapSizeVector, planetTextures, ref aiPlayers, ref humanPlayer, sectorOutline, Font, device, graphics, spriteBatch); zoom = 1.0f; Systems = map.AllSystems; selectedSystem = map.AllSystems[map.selectedSystem]; BackgroundRec = new Rectangle( 0 - (graphics.PreferredBackBufferWidth / 3), 0 - (graphics.PreferredBackBufferHeight / 3), background.Width, background.Height); UpdateResourcesDetails(); UpdateEndDayResources(); gameSpeed = 1f; CurrentView = ViewType.MapView; elapsedDays = 0; elapsedHours = 0; paused = false; planetsTaxChange.Clear(); planetsTaxChangeNames.Clear(); }