Пример #1
0
    // 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);
        }
    }
Пример #2
0
        // Checking

        void CheckState(DNMState expectedState)
        {
            if (state != expectedState)
            {
                WrongState(state.ToString());
            }
        }
Пример #3
0
 public void OnStateChanged(DNMState newState)
 {
     foreach (var l in listeners)
     {
         l.OnStateChanged(state);
     }
 }
Пример #4
0
        // 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));
        }
Пример #5
0
        public override void OnStopClient()
        {
            state = DNMState.Off;
            this.cachedLocalPlayers.Clear();

            OnClientDisconnected();
        }
Пример #6
0
        /************* 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);
        }
Пример #7
0
        void SetState(DNMState newState)
        {
            if (state == newState)
            {
                Log.Warn("Repeated state");
                return;
            }

            state = newState;
            Info.Set("DNMState", state.ToString());
            OnStateChanged(state);
        }
Пример #8
0
        /************* 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);
            }
        }
Пример #9
0
 // 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");
     }
 }
Пример #10
0
        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();
            }
        }