/// <summary> /// Load all the game content. /// </summary> public override void LoadContent() { base.LoadContent(); Dice.LoadAssets(ScreenManager.Game.Content); ContentManager content = ScreenManager.Game.Content; background = content.Load <Texture2D>(@"Images\bg"); // When reaching a gameplay screen, we know that there is a yacht state in the current state object YachtState yachtState = (YachtState)PhoneApplicationService.Current.State[Constants.YachtStateKey]; InitializeDiceHandler(yachtState.PlayerDiceState, gameType); yachtState.PlayerDiceState = diceHandler.DiceState; diceHandler.PositionDice(); if (gameType == GameTypes.Offline) { InitializeGameStateHandler(yachtState.YachGameState); yachtState.YachGameState = gameStateHandler.State; } else { // Register for network notifications NetworkManager.Instance.GameStateArrived += ServerGameStateArrived; NetworkManager.Instance.GameOver += GameOverEnded; NetworkManager.Instance.Banned += Banned; NetworkManager.Instance.GameUnavailable += GameUnavailable; NetworkManager.Instance.ServiceError += ServerErrorOccurred; // Get the updated game state instead of initializing from the state object NetworkManager.Instance.GetGameState(); } }
/// <summary> /// Saves the game-state data from the game's state object in isolated storage. Assumes the game state object /// contains game-state data. /// </summary> public static void SaveGameState() { if (PhoneApplicationService.Current.State.ContainsKey(Constants.YachtStateKey)) { try { using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { YachtState yachtState = (YachtState)PhoneApplicationService.Current.State[Constants.YachtStateKey]; string fileName = yachtState.YachGameState.GameType == GameTypes.Offline ? Constants.YachtStateFileNameOffline : Constants.YachtStateFileNameOnline; using (IsolatedStorageFileStream fileStream = isolatedStorageFile.CreateFile(fileName)) { using (XmlWriter writer = XmlWriter.Create(fileStream)) { yachtState.WriteXml(writer); } } } } catch { // There was an error saving data to isolated storage. Not much that we can do about it. } } }
/// <summary> /// Loads a game-state object from isolated storage and places it in the game's state object. /// </summary> /// <param name="gameType">The type of game for which to load the data.</param> /// <returns>True if game data was successfully loaded from isolated storage and false otherwise.</returns> public static bool LoadGameState(GameTypes gameType) { string fileName; switch (gameType) { case GameTypes.Offline: fileName = Constants.YachtStateFileNameOffline; break; case GameTypes.Online: fileName = Constants.YachtStateFileNameOnline; break; default: fileName = null; break; } try { using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { // Check whether or not the data file exists if (isolatedStorageFile.FileExists(fileName)) { // If the file exits, open it and read its contents using (IsolatedStorageFileStream fileStream = isolatedStorageFile.OpenFile(fileName, FileMode.Open)) { using (XmlReader reader = XmlReader.Create(fileStream)) { YachtState yachtState = new YachtState(); // Read the xml declaration to get it out of the way reader.Read(); yachtState.ReadXml(reader); PhoneApplicationService.Current.State[Constants.YachtStateKey] = yachtState; } } return(true); } } return(false); } catch { return(false); } }
/// <summary> /// Handler called when a game state is received from the server. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void ServerGameStateArrived(object sender, YachtGameStateEventArgs e) { if (gameStateHandler == null) { InitializeGameStateHandler(e.GameState); YachtState yachtState = (YachtState)PhoneApplicationService.Current.State[Constants.YachtStateKey]; yachtState.NetworkManagerState = NetworkManager.Instance; // Update state object with the new game state yachtState.YachGameState = gameStateHandler.State; // Reset the dice state if it is not valid for this turn if (diceHandler.DiceState.ValidForTurn != e.GameState.StepsMade) { diceHandler.Reset(false); } NetworkManager.Instance.ScoreCardArrived += ServerScoreCardArrived; } else { if (e.GameState.StepsMade != gameStateHandler.State.StepsMade) { diceHandler.Reset(false); } gameStateHandler.SetState(e.GameState); } diceHandler.DiceState.ValidForTurn = gameStateHandler.State.StepsMade; AudioManager.PlaySoundRandom("TurnChange", 2); if (e.GameState.Players[e.GameState.CurrentPlayer].PlayerID == NetworkManager.Instance.playerID) { NetworkManager.Instance.GetScoreCard(); } }