// DNMListener methods public void OnStateChanged(DNMState state) { if (state == DNMState.Off) { panels.OpenPanel(mainMenuPanel); } else if (state == DNMState.Offline) { lobbyPanel.SetMode(Mode.OfflineMode); panels.OpenPanel(lobbyPanel); } else if (state == DNMState.Host) { lobbyPanel.SetMode(Mode.OnlineMode, true); panels.OpenPanel(lobbyPanel); } else if (state == DNMState.CreatingHost) { // do nothing } else if (state == DNMState.StartingAsClient) { panels.OpenPanel(connectingPanel); } else if (state == DNMState.Client) { lobbyPanel.SetMode(Mode.OnlineMode, false); panels.OpenPanel(lobbyPanel); } else { Log.Warn("Unknown new state: {0}", state); } }
// Checking void CheckState(DNMState expectedState) { if (state != expectedState) { WrongState(state.ToString()); } }
public void OnStateChanged(DNMState newState) { foreach (var l in listeners) { l.OnStateChanged(state); } }
// only server to remotes void SendStatusToClient(bool accepted, NetworkConnection conn) { string sceneName = GetSceneName(); var state = GameState.NoGame; MessageBase extraMessage = null; if (accepted) { state = gameState; if (gameState == GameState.NoGame || gameState == GameState.WillStart) { // noop } else if (gameState == GameState.Playing || gameState == GameState.GameOver) { if (gameServer == null) { Log.Error("No tengo server :("); } else { var initialMessages = new List <MessageBase>(); gameServer.WriteInitialData(initialMessages); extraMessage = new StartGameMessage(sceneName, initialMessages); } } else { Log.Error("Unexpected state {0}...", gameState); } } conn.Send(MsgType.InitialStatus, new StatusMessage(accepted, sceneName, state, extraMessage)); }
public override void OnStopClient() { state = DNMState.Off; this.cachedLocalPlayers.Clear(); OnClientDisconnected(); }
/************* DUAL METHODS *************/ public bool StartAsHost(SceneData initialScene, MatchInfo hostInfo = null) { if (state != DNMState.Off) { Debug.Log("Invalid call of StartAsHost"); return(false); } if (cachedLocalPlayers.Count == 0) { Debug.LogWarning("Hosting with no local players"); } playerMap = new Dictionary <int, PlayerWrapper>(); NetworkClient localClient; if (hostInfo == null) { localClient = StartHost(); } else { localClient = StartHost(hostInfo); } bool hostStarted = localClient != null; if (hostStarted) { state = DNMState.Host; if (gameState != GameState.NoGame) { Debug.LogWarning("Already in game mode"); gameState = GameState.NoGame; } SetServerInfo("Hosting", networkAddress); currentScene = initialScene; // synchronized variables: currentSceneName = currentScene.englishName; currentMinPlayers = currentScene.minPlayers; currentMaxPlayers = currentScene.maxPlayers; currentSize = currentScene.defaultSize; } else { cachedLocalPlayers.Clear(); } return(hostStarted); }
void SetState(DNMState newState) { if (state == newState) { Log.Warn("Repeated state"); return; } state = newState; Info.Set("DNMState", state.ToString()); OnStateChanged(state); }
/************* CLIENT CALLBACKS *************/ // called on client when connected to a server public override void OnClientConnect(NetworkConnection connectionToServer) { bool isLocalClient = NetworkServer.active; if (isLocalClient) { if (state != DNMState.Host) { Debug.LogError("Invalid state"); } if (gameState != GameState.NoGame) { Debug.LogError("Invalid state"); } // add hosted players for (int i = 0; i < cachedLocalPlayers.Count; i++) { NewPlayerMessage newPlayerMessage = new NewPlayerMessage(); newPlayerMessage.playerName = cachedLocalPlayers[i].playerName; ClientScene.AddPlayer(connectionToServer, (short)i, newPlayerMessage); } OnClientConnected(true); } else { if (state != DNMState.Connecting) { Debug.LogError("Invalid state"); } state = DNMState.Client; SetServerInfo("Client", ""); //ClientScene.Ready(connectionToServer); // TODO is ready ?? NewPlayerMessage newPlayerMessage = new NewPlayerMessage(); newPlayerMessage.playerName = GetPlayerName(); // ask the server to add player for this remote client ClientScene.AddPlayer(connectionToServer, 0, newPlayerMessage); OnClientConnected(false); } }
// stop host / client / connection attempt public void Stop() { if (state == DNMState.Host) { this.StopHost(); state = DNMState.Off; this.cachedLocalPlayers.Clear(); } else if (state == DNMState.Connecting || state == DNMState.Client) { this.StopClient(); state = DNMState.Off; } else { Debug.LogError("Invalid state"); } }
public void StartAsClient(MatchInfo hostInfo = null) { if (state != DNMState.Off) { Debug.LogError("Invalid state"); return; } state = DNMState.Connecting; SetServerInfo("Connecting", ""); if (hostInfo != null) { StartClient(hostInfo); } else { StartClient(); } }