/// <summary> /// Called on the server when a game pause needs to be scheduled. The server informs all other clients that they /// must pause the game at the specified time. /// </summary> private void OnServerPauseGame() { NetworkGameState networkGameState = new NetworkGameState { Previous = GameManager.GameState, Current = EGameState.Paused, Time = GameTime.Time + 5 }; var networkConnections = new List <NetworkConnection>(); foreach (var clientControllers in _networkClientControllers) { NetworkServer.SendToClient(clientControllers.Connection.connectionId, MsgGameStateChange, networkGameState); networkConnections.Add(clientControllers.Connection); } _awaitingConfirmation = new Tuple <uint, EGameState, List <NetworkConnection> > ( GameTime.BackgroundTime, EGameState.Paused, networkConnections ); }
private void OnServerResumeGame() { // TODO: The network game state should be custom made for each client based on latency // I.E Client 1 with latency of 5 ticks, Client 2 with latency of 10 ticks // Send time based on largest latency + x (maybe 5?) (largest latency - latency + x) // Client 1 receives a timer of 10 // Client 2 receives a timer of 5 NetworkGameState networkGameState = new NetworkGameState { Previous = GameManager.GameState, Current = EGameState.Active, Time = 5 }; NetworkServer.SendToAll(MsgGameStateChange, networkGameState); Debug.Log("[FileTransfer] - OnServerResumeGame()"); }
public void ManualDisconnect() { if (client == null || _isQuitting) { return; } if (client.connection.connectionId == 0) { // Tell all clients to disconnect before stopping host NetworkGameState newState = new NetworkGameState { Current = EGameState.Disconnecting, Previous = GameManager.GameState }; NetworkServer.SendToAll(MsgGameStateChange, newState); StartCoroutine(StopHostAfterClientsDisconnect()); } else { StartCoroutine(StopClientAfterUpdatingData()); } }
/// <summary> /// Called on each client when the game state changes. The client schedules a change of game state at the designated time. /// /// E.G. The server will call this method informing the clients to pause the game at a specific time. /// </summary> /// <param name="networkMessage"></param> private void OnClientUpdateGameState(NetworkMessage networkMessage) { // Find the game state change to process and at which time NetworkGameState networkGameState = networkMessage.ReadMessage <NetworkGameState>(); switch (networkGameState.Previous) { case EGameState.NotLoaded: { // Ewwww, FindObjectsOfType is nasty var playerControllers = FindObjectsOfType <PlayerController>(); var worldControllers = FindObjectsOfType <WorldController>(); foreach (var playerController in playerControllers) { playerController.Startup(GameManager); } foreach (var worldController in worldControllers) { worldController.Startup(GameManager); } _onBeginLoading?.Invoke(); client.Send(MsgGameStateConfirmation, new NetworkStateConfirmation { NewState = EGameState.Waking }); break; } case EGameState.Initialised: { break; } case EGameState.Paused: { if (networkGameState.Current == EGameState.Active) { // New client that just connected goes from waking to active, starts the tick timer if (GameManager.GameState == EGameState.Waking) { GameTime.ListenForBackgroundTick(networkGameState.Time, _onBeginGame); } else { GameTime.ListenForBackgroundTick(networkGameState.Time, _onResume); } } break; } case EGameState.Active: { if (networkGameState.Current == EGameState.Paused) { GameTime.ListenForSpecificPostTick(networkGameState.Time, OnClientPause); } break; } default: break; } if (networkGameState.Current == EGameState.Disconnecting && networkMessage.conn.connectionId != 0) { StartCoroutine(StopClientAfterUpdatingData()); } Debug.Log("<color=#4688f2><b>[CustomNetworkManager]</b></color> - OnClientUpdateGameState(NetworkMessage)\n" + $"Change in game state: {networkGameState.Previous} -> {networkGameState.Current}"); }