Exemplo n.º 1
0
        void ClientUpdate()
        {
            Packet p = client.GetNextReceived();

            switch (clientState)
            {
            case ClientState.CONNECTING:
            {
                if (!client.Connected)
                {
                    Debug.WriteLine("Sending connecting");
                    client.Send(new Connecting());
                }
                else
                {
                    if (p != null)
                    {
                        if (p.Code == Code.LevelGenerationData)
                        {
                            LevelGenData data = (LevelGenData)p;
                            seed = data.Seed;

                            level = new LevelGenerator(seed, data.Size, false);

                            //Create Player
                            player = new Player(0, 0, key);

                            //Create UI
                            ui = new PlayerUI(player);

                            //Add Player
                            level.AddEntity(player);

                            //Init level
                            level.Init();

                            client.Send(new AddOtherPlayer(player.GetX(), player.GetY()));

                            clientState = ClientState.ENTITY_EXCHANGE;
                        }
                        else
                        {
                            Debug.WriteLine("Failed connection!");
                            //Go back to menu
                            if (Frame.CanGoBack)
                            {
                                Frame.GoBack();
                            }
                        }
                    }
                }
                break;
            }

            case ClientState.ENTITY_EXCHANGE:
            {
                while (p != null)
                {
                    if (p.Code == Code.OtherPlayerCreationData)
                    {
                        AddOtherPlayer otherPlayer = (AddOtherPlayer)p;

                        OtherPlayer other = new OtherPlayer(otherPlayer.X, otherPlayer.Y, artificial);

                        level.AddEntity(other);

                        p = client.GetNextReceived();
                        if (p.Code != Code.OtherPlayerID)
                        {
                            Debug.WriteLine("Something is extremely wrong");
                        }
                        else
                        {
                            other.AddID(((OtherPlayerID)p).ID);
                        }
                    }
                    else if (p.Code == Code.OtherPlayerID)
                    {
                        OtherPlayerID playerID = (OtherPlayerID)p;
                        player.AddID(playerID.ID);

                        clientState = ClientState.READY;
                    }
                    p = client.GetNextReceived();
                }
                break;
            }

            case ClientState.READY:
            {
                while (p != null)
                {
                    if (p.Code == Code.Pong)
                    {
                        gotpong = true;
                        Pong po = (Pong)p;
                        Debug.WriteLine("Last ping: " + (watch.ElapsedMilliseconds - po.Time) + " ms");
                    }
                    else if (p.Code == Code.EntityXYCorrection)
                    {
                        level.AddCorrection((EntityCorrection)p);
                    }
                    else if (p.Code == Code.Input)
                    {
                        artificial.Update((Input)p);
                    }
                    p = client.GetNextReceived();
                }
                client.Send(new Input(key.up, key.down, key.left, key.right));
                break;
            }
            }
        }