Beispiel #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);
            }
        }
Beispiel #2
0
        public void UpdatePlayerPosition(PositionPacket packet)
        {
            Debug.Log("Moving player " + packet.player);

            StaticManager.Players[packet.player].gameObject.GetComponent <Movement>().SetNextPosition(new Vector3(packet.X, 0, packet.Z));
        }