/// <summary> /// Handles messages from server /// </summary> private void HandleClientMessages() { string[] message; while ((message = client.GetServerMessage()) != null) { int position = 1; MessageType mt = Network.GetMessageType(message); switch (mt) { case MessageType.GameState: GameState oldState = currentClientGameState; GameState newState = currentClientGameState = (GameState)int.Parse(message[position++]); Network.AddMessage("Changing game state from " + oldState + " to " + newState); switch (newState) { case GameState.INVALID: break; case GameState.NotConnected: case GameState.Lobby: sm.SwitchScreen("main"); return; case GameState.Starting: if (mapUI != null) mapUI.Dispose(); if (game != null) game.Dispose(); mapUI = null; game = null; player = null; return; case GameState.Started: return; case GameState.Finished: sm.CallSubscreen("escapemenu", client, server, player); return; default: break; } break; case MessageType.GameTransfer: game = new WildmenGame(); game.Deserialize(mt, message, ref position, game); game.SendData = client.StartSend; game.ReceiveData = client.GetServerMessage; MakeMapUI(game); break; case MessageType.PlayerAssign: Debug.Assert(game != null); int playerId = int.Parse(message[position++]); player = game.Players.First(p => p.Id == playerId); mapUI.AssignControllingPlayer(player); break; default: messageQueue.Enqueue(message); break; } } }
/// <summary> /// Creates a MapUI and assigns network source and target for net communication /// </summary> /// <param id="game">Active WildmenGame game</param> private void MakeMapUI(WildmenGame game) { mapUI = new MapUI(game); mapUI.Initialize(); if (!serverView) { game.SendData = client.StartSend; game.ReceiveData = this.GetServerMessage; } }
/// <summary> /// Method called when the screen is switched from /// </summary> public override void OnDeactivate() { if (mapUI != null) mapUI.Dispose(); mapUI = null; if (game != null) game.Dispose(); game = null; player = null; if (client != null) client.Dispose(); client = null; if (server != null) server.Dispose(); }