Beispiel #1
0
        private void OnClientEnterScene(ushort id, ServerPlayerEnterScenePacket packet)
        {
            if (!_playerData.TryGetValue(id, out var playerData))
            {
                Logger.Warn(this, $"Received EnterScene packet from {id}, but player is not in mapping");
                return;
            }

            // Read the values in from the packet
            var newSceneName    = packet.NewSceneName;
            var position        = packet.Position;
            var scale           = packet.Scale;
            var animationClipId = packet.AnimationClipId;

            Logger.Info(this, $"Received EnterScene packet from ID {id}, new scene: {newSceneName}");

            // Store it in their PlayerData object
            playerData.CurrentScene      = newSceneName;
            playerData.LastPosition      = position;
            playerData.LastScale         = scale;
            playerData.LastAnimationClip = animationClipId;

            // Create a PlayerEnterScene packet containing the ID
            // of the player entering the scene and the respective values
            var enterScenePacket = new ClientPlayerEnterScenePacket {
                ScenePlayerData = new ScenePlayerData {
                    Id              = id,
                    Username        = playerData.Username,
                    Position        = position,
                    Scale           = scale,
                    Team            = playerData.Team,
                    AnimationClipId = animationClipId,
                }
            };

            enterScenePacket.CreatePacket();

            // Create the AlreadyInScene packet
            var alreadyInScenePacket = new ClientAlreadyInScenePacket();

            foreach (var idPlayerDataPair in _playerData.GetCopy())
            {
                // Skip source player
                if (idPlayerDataPair.Key == id)
                {
                    continue;
                }

                var otherPlayerData = idPlayerDataPair.Value;

                // Send the packet to all clients on the new scene
                // to indicate that this client has entered their scene
                if (otherPlayerData.CurrentScene.Equals(newSceneName))
                {
                    Logger.Info(this, $"Sending enter scene packet to {idPlayerDataPair.Key}");
                    _netServer.SendTcp(idPlayerDataPair.Key, enterScenePacket);

                    Logger.Info(this, $"Sending that {idPlayerDataPair.Key} is already in scene to {id}");

                    // Also send a packet to the client that switched scenes,
                    // notifying that these players are already in this new scene.
                    // We do this by adding a new player data instance to the AlreadyInScene packet
                    alreadyInScenePacket.ScenePlayerData.Add(new ScenePlayerData {
                        Id              = idPlayerDataPair.Key,
                        Username        = otherPlayerData.Username,
                        Position        = otherPlayerData.LastPosition,
                        Scale           = otherPlayerData.LastScale,
                        Team            = otherPlayerData.Team,
                        AnimationClipId = otherPlayerData.LastAnimationClip,
                    });
                }
            }

            // Now we send the AlreadyInScene packet after it is completely populated,
            // or not in case of no players in the scene already
            _netServer.SendTcp(id, alreadyInScenePacket.CreatePacket());
        }
Beispiel #2
0
        private void OnPlayerEnterScene(ClientPlayerEnterScenePacket packet)
        {
            Logger.Info(this, $"Received PlayerEnterScene packet for ID: {packet.ScenePlayerData.Id}");

            EnterPlayerInScene(packet.ScenePlayerData);
        }
Beispiel #3
0
        private void OnHelloServer(ushort id, HelloServerPacket packet)
        {
            Logger.Info(this, $"Received Hello packet from ID {id}");

            // Start by sending the new client the current Server Settings
            var settingsUpdatePacket = new GameSettingsUpdatePacket {
                GameSettings = _gameSettings
            };

            settingsUpdatePacket.CreatePacket();

            _netServer.SendTcp(id, settingsUpdatePacket);

            // Read username from packet
            var username = packet.Username;

            // Read scene name from packet
            var sceneName = packet.SceneName;

            // Read the rest of the data, since we know that we have it
            var position    = packet.Position;
            var scale       = packet.Scale;
            var currentClip = packet.AnimationClipId;

            // Create new player data object
            var playerData = new ServerPlayerData(
                username,
                sceneName,
                position,
                scale,
                currentClip
                );

            // Store data in mapping
            _playerData[id] = playerData;

            // Create PlayerConnect packet
            var playerConnectPacket = new ClientPlayerConnectPacket {
                Id       = id,
                Username = username
            };

            playerConnectPacket.CreatePacket();

            // Create PlayerEnterScene packet
            var enterScenePacket = new ClientPlayerEnterScenePacket {
                ScenePlayerData = new ScenePlayerData {
                    Id              = id,
                    Username        = playerData.Username,
                    Position        = position,
                    Scale           = scale,
                    Team            = playerData.Team,
                    AnimationClipId = currentClip,
                }
            };

            enterScenePacket.CreatePacket();

            // Create the AlreadyInScene packet
            var alreadyInScenePacket = new ClientAlreadyInScenePacket();

            // Loop over all other clients and skip over the client that just connected
            foreach (var idPlayerDataPair in _playerData.GetCopy())
            {
                if (idPlayerDataPair.Key == id)
                {
                    continue;
                }

                var otherPlayerData = idPlayerDataPair.Value;

                // Send the PlayerConnect packet to all other clients
                _netServer.SendTcp(idPlayerDataPair.Key, playerConnectPacket);

                // Send the EnterScene packet only to clients in the same scene
                if (otherPlayerData.CurrentScene.Equals(sceneName))
                {
                    Logger.Info(this, $"Sending a EnterScene packet to ID: {idPlayerDataPair.Key}");
                    _netServer.SendTcp(idPlayerDataPair.Key, enterScenePacket);

                    // Also send the source client that this player is in their scene,
                    // by adding a new player data instance to the AlreadyInScene packet
                    alreadyInScenePacket.ScenePlayerData.Add(new ScenePlayerData {
                        Id              = idPlayerDataPair.Key,
                        Username        = otherPlayerData.Username,
                        Position        = otherPlayerData.LastPosition,
                        Scale           = otherPlayerData.LastScale,
                        Team            = otherPlayerData.Team,
                        AnimationClipId = otherPlayerData.LastAnimationClip,
                    });
                }
            }

            // Now we send the AlreadyInScene packet after it is completely populated,
            // or not in case of no players in the scene already
            _netServer.SendTcp(id, alreadyInScenePacket.CreatePacket());
        }