Пример #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>");
            };
        }
Пример #2
0
 protected virtual ApprovalResult OnApproveConnection(BitStream bs)
 {
     return(new ApprovalResult()
     {
         Approved = true
     });
 }
Пример #3
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);
            }
        }
Пример #4
0
        void OnPossessPawn(BitStream bs)
        {
            var replicaId = bs.ReadReplicaId();
            var pawnIdx   = bs.ReadByte();

            currentReplicaPossess = replicaId;
            pawnIdxToPossess      = pawnIdx;
        }
Пример #5
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);
        }
Пример #6
0
        void OnLoadScene(BitStream bs)
        {
            var sceneName  = bs.ReadString();
            var generation = bs.ReadByte();

            if (currentLoadedSceneGeneration != generation)
            {
                currentLoadedSceneGeneration = generation;

                World.StartCoroutine(LoadScene(sceneName));
            }
        }
Пример #7
0
        void OnLoadSceneDone(Connection connection, BitStream bs)
        {
            var generation = bs.ReadByte();

            if (generation != loadSceneGeneration)
            {
                return;
            }

            Debug.Log("[Server] On load scene done: <i>" + connection + "</i> (generation=" + generation + ")");

            ++numLoadScenePlayerAcks;

            //
            var replicaView = server.ReplicaManager.GetReplicaView(connection);

            if (replicaView != null)
            {
                replicaView.IsLoadingLevel = false;
                server.ReplicaManager.ForceReplicaViewRefresh(replicaView);
            }
        }
Пример #8
0
 public void Deserialize(BitStream bs)
 {
     Amount = bs.ReadByte();
 }
Пример #9
0
 public void Serialize(BitStream bs)
 {
     bs.Write(Amount);
 }