public override void Update(float deltaTime)
        {
            NetIncomingMessage msg;
            while ((msg = client.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                    case NetIncomingMessageType.DiscoveryResponse:
                        Debug.WriteLine("Server message: " + msg.ReadString());

                        NetOutgoingMessage approval = client.CreateMessage();
                        approval.Write(GameClient.APPROVAL_BYTE);
                        approval.Write(GameClient.JOIN_BYTE);
                        approval.Write(nick);
                        client.Connect(msg.SenderEndPoint, approval);
                        break;

                    case NetIncomingMessageType.Data:
                        byte msgCode = msg.ReadByte();
                        if (msgCode == (byte)GameClient.DataMessageTypes.HostsListResponse)
                        {
                            hosts.Clear();
                            int count = msg.ReadInt32();
                            for (int i = 0; i < count; ++i)
                            {
                                long uid = msg.ReadInt64();
                                string name = msg.ReadString();
                                HostLabel field = new HostLabel(name, uid, font);
                                field.ScaleX = field.ScaleY = 0.5f;
                                field.X = GameMain.ScreenWidth / 10;
                                field.Y = pointerLabel.Y + pointerLabel.Height + (i + 1) * field.Height;
                                guiContainer.AddChild(field);
                                field.AddEventListener(Event.TOUCH_END, onTouchUp);
                                hosts.Add(field);

                                Debug.WriteLine(uid + ": " + name);
                            }
                        }
                        if (msgCode == (byte)GameClient.DataMessageTypes.HostConnectionResponse)
                        {
                            byte response = msg.ReadByte();
                            if (response != 0x01)
                            {
                                Debug.WriteLine("Error: wrong uid");
                                break;
                            }
                            string remoteNick = msg.ReadString();
                            JoinGameApproved(remoteNick);
                        }
                        break;
                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();

                        if (status == NetConnectionStatus.Connected)
                        {
                            Debug.WriteLine("Connected");

                            NetOutgoingMessage listRequest = client.CreateMessage();
                            listRequest.Write((byte)GameClient.DataMessageTypes.HostsListRequest);
                            client.SendMessage(listRequest, NetDeliveryMethod.ReliableOrdered);
                        }

                        if (status == NetConnectionStatus.Disconnected)
                            Debug.WriteLine("Disconnect");
                        break;
                }
            }

            for (int i = 0; i < bgLayers.Length; ++ i)
            {
                var layer = bgLayers[i];
                layer.X -= 100 * deltaTime * LAYER_DEPTHS[i];
                if (layer.X <= -layer.Width / 2)
                {
                    layer.X += layer.Width / 2;
                }
            }
        }
示例#2
0
        public override void Update(float deltaTime)
        {
            NetIncomingMessage msg;

            while ((msg = client.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                case NetIncomingMessageType.DiscoveryResponse:
                    Debug.WriteLine("Server message: " + msg.ReadString());

                    NetOutgoingMessage approval = client.CreateMessage();
                    approval.Write(GameClient.APPROVAL_BYTE);
                    approval.Write(GameClient.JOIN_BYTE);
                    approval.Write(nick);
                    client.Connect(msg.SenderEndPoint, approval);
                    break;

                case NetIncomingMessageType.Data:
                    byte msgCode = msg.ReadByte();
                    if (msgCode == (byte)GameClient.DataMessageTypes.HostsListResponse)
                    {
                        hosts.Clear();
                        int count = msg.ReadInt32();
                        for (int i = 0; i < count; ++i)
                        {
                            long      uid   = msg.ReadInt64();
                            string    name  = msg.ReadString();
                            HostLabel field = new HostLabel(name, uid, font);
                            field.ScaleX = field.ScaleY = 0.5f;
                            field.X      = GameMain.ScreenWidth / 10;
                            field.Y      = pointerLabel.Y + pointerLabel.Height + (i + 1) * field.Height;
                            guiContainer.AddChild(field);
                            field.AddEventListener(Event.TOUCH_END, onTouchUp);
                            hosts.Add(field);

                            Debug.WriteLine(uid + ": " + name);
                        }
                    }
                    if (msgCode == (byte)GameClient.DataMessageTypes.HostConnectionResponse)
                    {
                        byte response = msg.ReadByte();
                        if (response != 0x01)
                        {
                            Debug.WriteLine("Error: wrong uid");
                            break;
                        }
                        string remoteNick = msg.ReadString();
                        JoinGameApproved(remoteNick);
                    }
                    break;

                case NetIncomingMessageType.StatusChanged:
                    NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();

                    if (status == NetConnectionStatus.Connected)
                    {
                        Debug.WriteLine("Connected");

                        NetOutgoingMessage listRequest = client.CreateMessage();
                        listRequest.Write((byte)GameClient.DataMessageTypes.HostsListRequest);
                        client.SendMessage(listRequest, NetDeliveryMethod.ReliableOrdered);
                    }

                    if (status == NetConnectionStatus.Disconnected)
                    {
                        Debug.WriteLine("Disconnect");
                    }
                    break;
                }
            }

            for (int i = 0; i < bgLayers.Length; ++i)
            {
                var layer = bgLayers[i];
                layer.X -= 100 * deltaTime * LAYER_DEPTHS[i];
                if (layer.X <= -layer.Width / 2)
                {
                    layer.X += layer.Width / 2;
                }
            }
        }