Esempio n. 1
0
        /// <summary>
        /// Register network players so we have all of them
        /// </summary>
        public void RegisterMPNetworkPlayer(MPNetworkPlayer newPlayer)
        {
            Debug.Log("Player joined");

            connectedPlayers.Add(newPlayer);
            newPlayer.BecameReady += OnPlayerSetReady;

            // Send initial scene message
            string sceneName = SceneManager.GetActiveScene().name;

            if (sceneName == GameSceneName)
            {
                newPlayer.OnEnterGameScene();
            }
            else if (sceneName == MainMenuSceneName)
            {
                newPlayer.OnEnterLobbyScene();
            }

            if (playerJoined != null)
            {
                playerJoined(newPlayer);
            }

            newPlayer.GameDetailsReady += FireGameModeUpdated;
        }
Esempio n. 2
0
 /// <summary>
 /// Called on the server when a player is set to ready
 /// </summary>
 public virtual void OnPlayerSetReady(MPNetworkPlayer player)
 {
     if (AllPlayersReady() && serverPlayersReadied != null)
     {
         serverPlayersReadied();
     }
 }
Esempio n. 3
0
        public override void OnClientSceneChanged(NetworkConnection conn)
        {
            Debug.Log("OnClientSceneChanged");

            base.OnClientSceneChanged(conn);

            PlayerController pc = conn.playerControllers[0];

            if (!pc.unetView.isLocalPlayer)
            {
                return;
            }

            string sceneName = SceneManager.GetActiveScene().name;

            if (sceneName == GameSceneName)
            {
                state = NetworkState.InGame;

                // Tell all network players that they're in the game scene
                for (int i = 0; i < connectedPlayers.Count; ++i)
                {
                    MPNetworkPlayer np = connectedPlayers[i];
                    if (np != null)
                    {
                        np.OnEnterGameScene();
                    }
                }
            }
            else if (sceneName == MainMenuSceneName)
            {
                if (state != NetworkState.Inactive)
                {
                    if (gameType == NetworkGameType.SinglePlayer)
                    {
                        state = NetworkState.Pregame;
                    }
                    else
                    {
                        state = NetworkState.InLobby;
                    }
                }

                // Tell all network players that they're in the lobby scene
                for (int i = 0; i < connectedPlayers.Count; ++i)
                {
                    MPNetworkPlayer np = connectedPlayers[i];
                    if (np != null)
                    {
                        np.OnEnterLobbyScene();
                    }
                }
            }

            if (sceneChanged != null)
            {
                sceneChanged(false, sceneName);
            }
        }
Esempio n. 4
0
        public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
        {
            // Intentionally not calling base here - we want to control the spawning of prefabs
            Debug.Log("OnServerAddPlayer");

            MPNetworkPlayer newPlayer = Instantiate <MPNetworkPlayer>(_mpPlayerPrefab);

            DontDestroyOnLoad(newPlayer);
            NetworkServer.AddPlayerForConnection(conn, newPlayer.gameObject, playerControllerId);
        }
Esempio n. 5
0
 /// <summary>
 /// Reset the ready states for all players
 /// </summary>
 public void ClearAllReadyStates()
 {
     for (int i = 0; i < connectedPlayers.Count; ++i)
     {
         MPNetworkPlayer player = connectedPlayers[i];
         if (player != null)
         {
             player.ClearReady();
         }
     }
 }
Esempio n. 6
0
        public override void OnServerRemovePlayer(NetworkConnection conn, PlayerController player)
        {
            Debug.Log("OnServerRemovePlayer");
            base.OnServerRemovePlayer(conn, player);

            MPNetworkPlayer connectedPlayer = GetPlayerForConnection(conn);

            if (connectedPlayer != null)
            {
                Destroy(connectedPlayer);
                connectedPlayers.Remove(connectedPlayer);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Makes the server change to the correct game scene for our map, and tells all clients to do the same
        /// </summary>
        public void ProgressToGameScene()
        {
            // Clear all client's ready states
            ClearAllReadyStates();

            // Remove us from matchmaking lists
            UnlistMatch();

            // Update will change scenes once loading screen is visible
            _sceneChangeMode = SceneChangeMode.Game;

            // Tell NetworkPlayers to show their loading screens
            for (int i = 0; i < connectedPlayers.Count; ++i)
            {
                MPNetworkPlayer player = connectedPlayers[i];
                if (player != null)
                {
                    player.RpcPrepareForLoad();
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Clients also destroy their copies of MPNetworkPlayer
        /// </summary>
        public override void OnStopClient()
        {
            Debug.Log("OnStopClient");
            base.OnStopClient();

            for (int i = 0; i < connectedPlayers.Count; ++i)
            {
                MPNetworkPlayer player = connectedPlayers[i];
                if (player != null)
                {
                    Destroy(player.gameObject);
                }
            }

            connectedPlayers.Clear();

            if (clientStopped != null)
            {
                clientStopped();
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Deregister network players
        /// </summary>
        public void DeregisterMPNetworkPlayer(MPNetworkPlayer removedPlayer)
        {
            Debug.Log("Player left");
            int index = connectedPlayers.IndexOf(removedPlayer);

            if (index >= 0)
            {
                connectedPlayers.RemoveAt(index);
            }

            if (playerLeft != null)
            {
                playerLeft(removedPlayer);
            }

            removedPlayer.GameDetailsReady -= FireGameModeUpdated;

            if (removedPlayer != null)
            {
                removedPlayer.BecameReady -= OnPlayerSetReady;
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Server destroys MPNetworkPlayer objects
        /// </summary>
        public override void OnStopServer()
        {
            base.OnStopServer();
            Debug.Log("OnStopServer");

            for (int i = 0; i < connectedPlayers.Count; ++i)
            {
                MPNetworkPlayer player = connectedPlayers[i];
                if (player != null)
                {
                    NetworkServer.Destroy(player.gameObject);
                }
            }

            connectedPlayers.Clear();

            // Reset this
            networkSceneName = string.Empty;

            if (serverStopped != null)
            {
                serverStopped();
            }
        }