Exemplo n.º 1
0
        /// <summary>
        /// Reset replication, instruct all clients to load the new scene, actually
        /// load the new scene on the server and finally create a new GameMode instance.
        /// </summary>
        /// <param name="sceneName"></param>
        public void LoadScene(string sceneName)
        {
            // Cleanup
            if (gameMode != null)
            {
                gameMode.StartToLeaveMap();
            }

            IsLoadingScene = true;
            ++loadSceneGeneration;
            numLoadScenePlayerAcks = 0;
            loadSceneName          = sceneName;

            server.ReplicaManager.Reset();
            if (sceneHandle.IsValid())
            {
                Addressables.UnloadSceneAsync(sceneHandle);
            }



            // Instruct clients
            var bs = new BitStream();

            bs.Write((byte)MessageId.LoadScene);
            bs.Write(sceneName);
            bs.Write(loadSceneGeneration);

            server.NetworkInterface.BroadcastBitStream(bs, PacketReliability.ReliableSequenced);

            // Disable ReplicaViews during level load
            foreach (var connection in server.connections)
            {
                var replicaView = server.ReplicaManager.GetReplicaView(connection);
                if (replicaView == null)
                {
                    continue;
                }

                replicaView.IsLoadingLevel = true;
            }

            // Load new map
            Debug.Log($"[Server] Loading level {sceneName}");
#if UNITY_EDITOR
            if (SceneManager.GetSceneByName(sceneName).isLoaded)
            {
                SceneManager.UnloadScene(sceneName);
            }
#endif
            sceneHandle            = Addressables.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
            sceneHandle.Completed += ctx => {
                IsLoadingScene = false;

                gameMode = CreateGameModeForScene(sceneName);
                Assert.IsNotNull(gameMode);

                Debug.Log($"[Server] New GameMode <i>{gameMode}</i>");
            };
        }
Exemplo n.º 2
0
        void OnNewIncomingConnection(Connection connection)
        {
            Debug.Log($"[Server] <b>New connection</b> <i>{connection}</i>");

            // Send load scene packet if we loaded one previously
            if (loadSceneName != null)
            {
                var bs2 = new BitStream();
                bs2.Write((byte)MessageId.LoadScene);
                bs2.Write(loadSceneName);
                bs2.Write(loadSceneGeneration);

                server.NetworkInterface.SendBitStream(bs2, PacketReliability.ReliableSequenced, connection);
            }

            var newPC = CreatePlayerController(connection);

            world.playerControllers.Add(newPC);

            var replicaView = CreateReplicaView(connection);

            server.ReplicaManager.AddReplicaView(replicaView);

            if (gameMode != null)   // null if there's no ongoing match
            {
                gameMode.HandleNewPlayer(newPC);
            }
        }
Exemplo n.º 3
0
        IEnumerator Foo()
        {
            yield return(new WaitForSeconds(1));

            var bs = new BitStream();

            bs.Write((byte)MessageId.LoadSceneDone);
            bs.Write(currentLoadedSceneGeneration);

            Client.networkInterface.Send(bs, PacketReliability.ReliableUnordered);
        }
Exemplo n.º 4
0
 public void Serialize(BitStream bs)
 {
     bs.Write(Amount);
 }