Ejemplo n.º 1
0
 /// <summary>
 /// Creates a new text field.
 /// </summary>
 /// <param name="font">The SpriteFont to draw with.</param>
 /// <param name="defval">The default value for the field.</param>
 /// <param name="color">What color you want the text to be.</param>
 /// <param name="pos">The position to draw the text field.</param>
 public Textbox(SpriteFont font, String defval, Color color, Vector2 pos)
 {
     this.font = font;
     value = defval;
     this.color = color;
     position = pos;
     help = new InputHelper();
     focused = false;
 }
Ejemplo n.º 2
0
        protected override void Update(GameTime gameTime)
        {
            KeyboardState keys = Keyboard.GetState();
            GamePadState pad = GamePad.GetState(PlayerIndex.One);
            mouse = Mouse.GetState();
            InputHelper help = new InputHelper();
            if (pad.Buttons.Back == ButtonState.Pressed || keys.IsKeyDown(Keys.Escape))
            {
                client.Disconnect("Game closed");
                this.Exit();
            }
            #region Server Logic
            NetIncomingMessage im;
            while ((im = client.ReadMessage()) != null)
            {
                switch (im.MessageType)
                {
                    case NetIncomingMessageType.Data:
                        switch (im.ReadString())
                        {
                            case "Handshake":
                                gameState = GameState.HeroSelect;
                                break;
                            case "GameStart":
                                gameState = GameState.NotTurn;
                                break;
                            case "DrawCard":
                                int cardsDrawn = im.ReadInt32();
                                for (int i = 0; i < cardsDrawn; i++)
                                {
                                    string cardName = im.ReadString();
                                    player.DrawCard(drawPile.Find(x => x.Name == cardName));
                                }
                                break;
                            case "AllPlayed":
                                int players = im.ReadInt32();
                                for (int i = 0; i < players; i++)
                                {
                                    string name = im.ReadString(),
                                           heroName = im.ReadString();
                                    int health = im.ReadInt32(),
                                        numberOfCards = im.ReadInt32();
                                    HeroCard hero = heroCardList.Find(x => x.Name == heroName);
                                    if (name == nameSelect.value) for (int n = 0; n < numberOfCards; n++) im.ReadString();
                                    else
                                    {
                                        if (otherPlayers.ContainsKey(name)) otherPlayers[name] = new OtherPlayer(hero, cardArt, name, health, numberOfCards);
                                        else otherPlayers.Add(name, new OtherPlayer(hero, cardArt, name, health, numberOfCards));
                                        for (int n = 0; n < numberOfCards; n++)
                                        {
                                            string cardName = im.ReadString();
                                            otherPlayers[name].Hand.Add(drawPile.Find(x => x.Name == cardName));
                                        }
                                    }
                                }
                                gameState = GameState.Turn;
                                break;
                        }
                        break;
                }
            }
            #endregion
            #region Game Logic
            switch (gameState)
            {
                case GameState.IPSelect:
                    ipSelect.Update(keys.GetPressedKeys());
                    if (keys.IsKeyDown(Keys.Enter))
                    {
                        client.Start();
                        NetOutgoingMessage hail = client.CreateMessage("Hail!");
                        client.Connect(ip, port, hail);
                    }
                    break;
                case GameState.HeroSelect:
                    Rectangle[] heroes = new Rectangle[8];
                    for (int i = 0; i < 8; i++) heroes[i] = new Rectangle((i % 4) * 250 + 100, (i / 4) * 181 + 100, 250, 181);
                    for (int i = 0; i < heroes.Length; i++) 
                    {
                        if (mouse.LeftButton == ButtonState.Pressed && heroes[i].Contains(new Point(mouse.X, mouse.Y)))
                        {
                            NetOutgoingMessage om = client.CreateMessage();
                            om.Write("HeroSelected");
                            om.Write(i);
                            client.SendMessage(om, NetDeliveryMethod.ReliableOrdered);
                            gameState = GameState.NameSelect;
                            player = new Player(heroCards[i], cardArt);
                        }
                    }
                    break;
                case GameState.NameSelect:
                    nameSelect.Update(keys.GetPressedKeys());
                    if (keys.IsKeyDown(Keys.Enter))
                    {
                        name = nameSelect.value;
                        NetOutgoingMessage om = client.CreateMessage();
                        om.Write("NameSelected");
                        om.Write(nameSelect.value);
                        client.SendMessage(om, NetDeliveryMethod.ReliableOrdered);
                        gameState = GameState.Wait;
                    }
                    break;
                case GameState.NotTurn:
                    bool swapping = false;
                    Card remove = new DeliveryCard(), swap = new DeliveryCard();
                    foreach (Card card in player.Hand)
                    {
                        if (card.MouseOver(new Point(mouse.X, mouse.Y)) && mouse.LeftButton == ButtonState.Pressed && prevMouse.LeftButton == ButtonState.Released)
                        {
                            int index = 0;
                            if (card.GetType() == typeof(SourceCard)) index = 0;
                            else if (card.GetType() == typeof(QualityCard)) index = 1;
                            else if (card.GetType() == typeof(DeliveryCard)) index = 2;
                            else continue;

                            if (selectedCards[index] != null) swapping = true;
                            swap = selectedCards[index];
                            selectedCards[index] = card;
                            remove = card;
                        }
                    }
                    player.Hand.Remove(remove);
                    if (swapping) player.Hand.Add(swap);

                    if (new Rectangle(screenWidth - 225, screenHeight - 144, 200, 119).Contains(new Point(mouse.X, mouse.Y)) && mouse.LeftButton == ButtonState.Pressed)
                    {
                        NetOutgoingMessage playMessage = client.CreateMessage();
                        playMessage.Write("Playing");
                        playMessage.Write(nameSelect.value);
                        foreach (Card card in selectedCards) playMessage.Write(card != null);
                        foreach (Card card in selectedCards) if (card != null) playMessage.Write(card.Name);
                        client.SendMessage(playMessage, NetDeliveryMethod.ReliableOrdered);
                        gameState = GameState.Wait;
                    }
                    break;
            }
            #endregion
            help.Update();
            prevMouse = mouse;
            base.Update(gameTime);
        }