Ejemplo n.º 1
0
        void ReadPlayerStates()
        {
            while (m_player.Net.IsDataAvailable) {
                // Get the new state for a remote player
                NetworkGamer sender;
                m_player.Net.ReceiveData(Global.packetReader, out sender);
                if (sender.IsLocal)
                    continue;

                // Make sure that remote player's are activated for play after getting a packet from them
                Player remotePlayer = sender.Tag as Player;
                remotePlayer.Active = true;

                // Store the players current state, updating each value below if a change came over the network
                byte direction = remotePlayer.State.DirectionFlags;
                Vector2 position = remotePlayer.Position;
                FacingDirection facing = remotePlayer.State.Facing;

                // Update the remote player's state on this machine
                PlayerStateChange changes = new PlayerStateChange(Global.packetReader.ReadByte());
                if (changes.DirectionChanged)
                    direction = Global.packetReader.ReadByte();
                if (changes.UpdatePosition) {
                    HalfVector2 packedVector = new HalfVector2();
                    packedVector.PackedValue = Global.packetReader.ReadUInt32();
                    position = packedVector.ToVector2();
                }
                if (changes.UpdateFacing) {
                    facing = (FacingDirection)Global.packetReader.ReadByte();
                }

                remotePlayer.State.DirectionFlags = direction;
                remotePlayer.Position = position;
                remotePlayer.Facing = facing;
                remotePlayer.Active = true;

                NetInfo netInfo = new NetInfo();
                netInfo.direction = direction;
                netInfo.position = position;
                netInfo.facing = facing;

                m_networkGUI.AddMessage("Receiving: " + netInfo);
            }
        }
Ejemplo n.º 2
0
        public void SendPlayerState()
        {
            PlayerStateChange stateChange = new PlayerStateChange(true, true, true);

            // Convert/Compress data values for optimal usage of bandwidth
            HalfVector2 position = new HalfVector2(m_player.Position);

            // Write the data to each player in the sesion
            Global.packetWriter.Write(stateChange.DataToBeSent);
            Global.packetWriter.Write(m_player.State.DirectionFlags);
            Global.packetWriter.Write(position.PackedValue);
            Global.packetWriter.Write((byte)m_player.Facing);
            m_player.Net.SendData(Global.packetWriter, SendDataOptions.InOrder);

            // Display what was sent in the GUI
            NetInfo info = new NetInfo();
            info.direction = m_player.State.DirectionFlags;
            info.facing = m_player.Facing;
            info.position = m_player.Position;
            m_networkGUI.AddMessage("Sent: " + info);
        }