Пример #1
0
        public void ReceiveMessages(object peer)
        {
            NetIncomingMessage message;

            while ((message = client.ReadMessage()) != null)
            {
                Debug.Log("Message recieved from server");

                switch (message.MessageType)
                {
                case NetIncomingMessageType.Data:
                    //get packet type
                    byte packetType = message.ReadByte();

                    //create packet
                    Packet packet;

                    switch (packetType)
                    {
                    case (byte)PacketTypes.LocalPlayerPacket:
                        packet = new LocalPlayerPacket();
                        packet.NetIncomingMessageToPacket(message);
                        ExtractLocalPlayerInformation((LocalPlayerPacket)packet);
                        break;

                    case (byte)PacketTypes.PlayerDisconnectsPacket:
                        packet = new PlayerDisconnectsPacket();
                        packet.NetIncomingMessageToPacket(message);
                        DisconnectPlayer((PlayerDisconnectsPacket)packet);
                        break;

                    case (byte)PacketTypes.PositionPacket:
                        packet = new PositionPacket();
                        packet.NetIncomingMessageToPacket(message);
                        UpdatePlayerPosition((PositionPacket)packet);
                        break;

                    case (byte)PacketTypes.DicePacket:
                        packet = new DicePacket();
                        packet.NetIncomingMessageToPacket(message);
                        UpdateDiceFace((DicePacket)packet);
                        break;

                    case (byte)PacketTypes.SpawnPacket:
                        packet = new SpawnPacket();
                        packet.NetIncomingMessageToPacket(message);
                        SpawnPlayer((SpawnPacket)packet);
                        break;

                    default:
                        Debug.Log("Unhandled packet type");
                        break;
                    }
                    break;

                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.ErrorMessage:
                case NetIncomingMessageType.WarningMessage:
                case NetIncomingMessageType.VerboseDebugMessage:
                    string text = message.ReadString();
                    Debug.Log(text);
                    break;

                default:
                    Debug.Log("Unhandled message type");
                    break;
                }

                client.Recycle(message);
            }
        }
Пример #2
0
        public void ExtractLocalPlayerInformation(LocalPlayerPacket packet)
        {
            Debug.Log("Local ID is " + packet.ID);

            StaticManager.LocalPlayerID = packet.ID;
        }