Exemple #1
0
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            MouseState mouse = Mouse.GetState();

            if (gameState != GameStage.Shuffle && deckPile.ShuffleDeck(discardPile))
            {
                nextState = gameState;
                gameState = GameStage.ReShuffle;
            }

            switch (gameState)
            {
            case GameStage.ReShuffle:
            case GameStage.Shuffle:
                if (deckPile.UpdateShuffleDeck(gameTime))
                {
                    gameState = nextState;
                }
                break;

            case GameStage.Start:
                if (numDraws > 0)
                {
                    if (done)
                    {
                        done = false;
                        SSEngine.Player1First = !SSEngine.Player1First;
                        numDraws--;
                    }
                    else
                    {
                        gameState = GameStage.Drawing;
                    }
                }
                else
                {
                    AIOpponent.Init(SSEngine.VersionStage);
                    gameState = GameStage.DeckToDiscard;
                }
                break;

            case GameStage.DeckToDiscard:
                bool finished;
                finished = deckPile.UpdateDeckPile(gameTime, discardPile);
                if (finished)
                {
                    changedSuit = discardPile[0].CardSuit;
                    gameState   = GameStage.Run;
                    if (discardPile[0].Rank == RankValue.Three || discardPile[0].Rank == RankValue.Four ||
                        discardPile[0].Rank == RankValue.Deuce || discardPile[0] == SSEngine.DeckSprites["QueenSpades"] ||
                        discardPile[0].Rank == RankValue.Eight)
                    {
                        SSEngine.Player1First = !SSEngine.Player1First;
                        cardAblities(discardPile[0]);
                        numExtraDraws = numTotalSets;
                    }
                    else
                    {
                        numTotalSets = 1;
                        cardAblities(discardPile[0]);
                    }
                }
                break;

            case GameStage.Drawing:
                if (SSEngine.Player1First == SSEngine.Player1)
                {
                    done = deckPile.AddingCardsToHand(gameTime, discardPile, playerCards1);
                }
                else
                {
                    done = deckPile.AddingCardsToHand(gameTime, discardPile, playerCards2);
                }
                if (done)
                {
                    gameState = GameStage.Start;
                }
                break;

            case GameStage.DrawingCard:
                if (deckPile.AddingCardsToHand(gameTime, discardPile, playerCards2))
                {
                    gameState = GameStage.Run;
                }
                break;

            case GameStage.Draw:
                if (SSEngine.Player1First == SSEngine.Player1)
                {
                    done = deckPile.AddingCardsToHand(gameTime, discardPile, playerCards2);
                }
                else
                {
                    done = deckPile.AddingCardsToHand(gameTime, discardPile, playerCards1);
                }
                if (done && --numDraws == 0)
                {
                    gameState = GameStage.Run;
                }
                break;

            case GameStage.Run:
                if ((playerCards1.Count == 0 || playerCards2.Count == 0) && numTotalSets == 0)
                {
                    gameState = GameStage.End;
                    break;
                }

                if (numDraws == 0 && numTotalSets == 0)
                {
                    numTotalSets          = 1;
                    numExtraDraws         = 1;
                    numHaveSet            = 0;
                    cardAbility           = String.Empty;
                    SSEngine.Player1First = !SSEngine.Player1First;
                    if (SSEngine.Player1SetThree == SSEngine.Player1First)
                    {
                        SSEngine.Player1SetThree = null;
                    }
                }
                bool activate = false;
                if (SSEngine.Player1First == SSEngine.Player1)
                {
                    activate = playerCards1.UpdateHand(gameTime, mouse, discardPile, changedSuit, ref cardAbility);
                }
                else if (SSEngine.VersionStage != MenuStage.LAN && SSEngine.VersionStage != MenuStage.Online)
                {
                    if (selectedIndex == -1)
                    {
                        selectedIndex = AIOpponent.Opponent(playerCards2, discardPile, changedSuit);
                        if (numExtraDraws > 0 && selectedIndex == -1)
                        {
                            gameState = GameStage.DrawingCard;
                        }
                        if (selectedIndex == -1 && numExtraDraws == 0)
                        {
                            numTotalSets = 0;
                        }
                    }
                    if (selectedIndex != -1)
                    {
                        activate = playerCards2.UpdateHand(gameTime, selectedIndex, discardPile);
                    }
                    else if (numExtraDraws > 0)
                    {
                        numExtraDraws--;
                    }
                }
                else if (selectedIndex != -1)
                {
                    activate = playerCards2.UpdateHand(gameTime, selectedIndex, discardPile);
                }

                if (activate)
                {
                    if (numTotalSets > 0)
                    {
                        numTotalSets--;
                    }
                    numHaveSet++;
                    if (discardPile.Count > 1 && discardPile[discardPile.Count - 2].Rank == RankValue.Three &&
                        numExtraDraws == 1)
                    {
                        numExtraDraws++;
                    }
                    if (numExtraDraws > 0)
                    {
                        numExtraDraws--;
                    }
                    selectedIndex = -1;
                    ability(discardPile[discardPile.Count - 1]);
                }
                if (numExtraDraws > 0)
                {
                    activate = false;
                    if (SSEngine.Player1First == SSEngine.Player1)
                    {
                        deckPile.ShuffleDeck(discardPile);
                        activate = deckPile.UpdateDeckPile(gameTime, mouse, playerCards1);
                    }
                    if (activate)
                    {
                        numExtraDraws--;
                    }
                }
                else
                {
                    finished = endTurnButton.UpdateButton(mouse, gameTime);
                    if (finished)
                    {
                        numTotalSets = 0;
                        if (SSEngine.VersionStage == MenuStage.LAN)
                        {
                            SSEngine.LANClient.Send((byte)NetGame.EndTurn);
                        }
                        else if (SSEngine.VersionStage == MenuStage.Online)
                        {
                            NetOutgoingMessage msg = SSEngine.Peer.CreateMessage();
                            msg.Write((byte)NetGame.EndTurn);
                            SSEngine.Peer.SendMessage(msg, SSEngine.Peer.Connections[0], NetDeliveryMethod.ReliableOrdered);
                        }
                    }
                }
                finished = backButton.UpdateButton(mouse, gameTime);
                if (finished)
                {
                    if (SSEngine.VersionStage == MenuStage.LAN)
                    {
                        SSEngine.LANClient.Send((byte)NetGame.Back);
                        SSEngine.LANClient = null;
                    }
                    else if (SSEngine.VersionStage == MenuStage.Online)
                    {
                        NetOutgoingMessage msg = SSEngine.Peer.CreateMessage();
                        msg.Write((byte)NetGame.Back);
                        SSEngine.Peer.SendMessage(msg, SSEngine.Peer.Connections[0], NetDeliveryMethod.ReliableOrdered);
                        System.Threading.Thread.Sleep(1000);
                        SSEngine.Peer.Shutdown("Lost Connection.");
                        SSEngine.Peer = null;
                    }
                    SSEngine.VersionStage = MenuStage.Play;
                }
                break;

            case GameStage.EightAbility:
                if (!SSEngine.Player1First && SSEngine.VersionStage != MenuStage.LAN && SSEngine.VersionStage != MenuStage.Online)
                {
                    switch (SSEngine.VersionStage)
                    {
                    case MenuStage.VeryEasy:
                        AIOpponent.EightAbilityVE(ref changedSuit); break;

                    case MenuStage.Easy:
                    case MenuStage.Medium:
                        AIOpponent.EightAbilityEM(ref changedSuit, playerCards2); break;

                    case MenuStage.Hard:
                    case MenuStage.VeryHard:
                        AIOpponent.EightAbilityHVH(ref changedSuit, playerCards2); break;
                    }
                    if (numSameRank < 3)
                    {
                        gameState = GameStage.Run;
                    }
                    else
                    {
                        gameState = GameStage.Draw;
                    }
                }
                else
                {
                    eightAbilityP(gameTime, mouse);
                }
                break;

            case GameStage.End:
                // Implement end game mechanics.
                if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                {
                    SSEngine.VersionStage = MenuStage.Play;
                    SSEngine.Disconnected = false;
                    SSEngine.LANClient    = null;
                    SSEngine.Peer         = null;
                }
                cardAbility = String.Empty;
                break;
            }

            if (SSEngine.VersionStage == MenuStage.LAN && SSEngine.LANClient.HasMessage() &&
                !(deckPile.Deck.Count != 3 && (NetGame)SSEngine.LANClient.PeekMessage().Bytes[0] == NetGame.Shuffle) &&
                gameState != GameStage.ReShuffle)
            {
                Message msg = SSEngine.LANClient.ReceiveMessage();
                switch ((NetGame)msg.Bytes[0])
                {
                case NetGame.SuitChanged:
                    if (msg.Bytes.Length == 3)
                    {
                        gameState = (GameStage)msg.Bytes[2];
                    }
                    else
                    {
                        gameState = GameStage.Run;
                    }
                    changedSuit = (Suit)msg.Bytes[1];
                    break;

                case NetGame.Draw:
                    gameState = GameStage.DrawingCard;
                    break;

                case NetGame.EndTurn:
                    numTotalSets = 0;
                    break;

                case NetGame.Back:
                    gameState             = GameStage.End;
                    SSEngine.Disconnected = true;
                    break;

                case NetGame.Shuffle:
                    if (discardPile.Count > 1)
                    {
                        discardPile.DiscardList.RemoveRange(0, discardPile.Count - 1);
                    }
                    for (int j = 1; j < msg.Bytes.Length - 1; j += 2)
                    {
                        deckPile.Deck.Add(SSEngine.DeckSprites[(RankValue)msg.Bytes[j] + "" + (Suit)msg.Bytes[j + 1]]);
                    }
                    if (gameState != GameStage.Shuffle)
                    {
                        nextState = gameState;
                        gameState = GameStage.ReShuffle;
                    }

                    break;

                case NetGame.Set:
                    selectedIndex = msg.Bytes[1];
                    break;

                default:
                    break;
                }
            }
            else if (SSEngine.VersionStage == MenuStage.Online)
            {
                NetIncomingMessage inc = SSEngine.Peer.PeekMessage();

                if (inc != null && !(deckPile.Deck.Count != 3 && (NetGame)inc.PeekByte() == NetGame.Shuffle) &&
                    gameState != GameStage.ReShuffle)
                {
                    inc = SSEngine.Peer.ReadMessage();
                    switch (inc.MessageType)
                    {
                    case NetIncomingMessageType.Data:
                        switch ((NetGame)inc.ReadByte())
                        {
                        case NetGame.SuitChanged:
                            changedSuit = (Suit)inc.ReadByte();
                            if (inc.LengthBytes == 1)
                            {
                                gameState = (GameStage)inc.ReadByte();
                            }
                            else
                            {
                                gameState = GameStage.Run;
                            }
                            break;

                        case NetGame.Draw:
                            gameState = GameStage.DrawingCard;
                            break;

                        case NetGame.EndTurn:
                            numTotalSets = 0;
                            break;

                        case NetGame.Back:
                            gameState             = GameStage.End;
                            SSEngine.Disconnected = true;
                            break;

                        case NetGame.Shuffle:
                            if (discardPile.Count > 1)
                            {
                                discardPile.DiscardList.RemoveRange(0, discardPile.Count - 1);
                            }
                            byte   numBytes = inc.ReadByte();
                            byte[] data     = inc.ReadBytes(numBytes);
                            for (int j = 0; j < data.Length; j += 2)
                            {
                                deckPile.Deck.Add(SSEngine.DeckSprites[(RankValue)data[j] + "" + (Suit)data[j + 1]]);
                            }
                            if (gameState != GameStage.Shuffle)
                            {
                                nextState = gameState;
                                gameState = GameStage.ReShuffle;
                            }
                            break;

                        case NetGame.Set:
                            selectedIndex = inc.ReadByte();
                            break;

                        default:
                            break;
                        }
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        switch (inc.SenderConnection.Status)
                        {
                        case NetConnectionStatus.Disconnected:
                            break;
                        }
                        break;

                    default:
                        break;
                    }
                }
            }

            base.Update(gameTime);
        }
Exemple #2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            MouseState mouse = Mouse.GetState();

            switch (menuStage)
            {
            case MenuStage.SplashScreen:
                if (gameTime.TotalGameTime.Seconds > 2)
                {
                    menuStage = MenuStage.MainMenu;
                }
                break;

            case MenuStage.MainMenu:
                UpdateButtons(mainButtons, gameTime, mouse); break;

            case MenuStage.Play:
                UpdateButtons(playButtons, gameTime, mouse);
                for (int i = 0; i < playButtons.Length - 1; i++)
                {
                    if (playButtons[i].IsClicked && playButtons[i].Stage != MenuStage.TwoPlayer)
                    {
                        SSEngine.VersionStage = playButtons[i].Stage;
                        selectedStage         = MenuStage.RockPaperScissors;
                        SSEngine.Player1      = true;
                    }
                }
                break;

            case MenuStage.TwoPlayer:
                UpdateButtons(twoPlayerBtns, gameTime, mouse);
                if (twoPlayerBtns[0].IsClicked)     // Online Button.
                {
                    SSEngine.Player1      = true;
                    SSEngine.VersionStage = twoPlayerBtns[0].Stage;
                    selectedStage         = MenuStage.Connect;
                    if (SSEngine.Peer == null)
                    {
                        Client.MainClient();
                    }
                }
                else if (twoPlayerBtns[1].IsClicked)     // LAN Button.
                {
                    SSEngine.Player1      = true;
                    SSEngine.VersionStage = twoPlayerBtns[1].Stage;
                    selectedStage         = MenuStage.Connect;
                    if (SSEngine.LANClient == null)
                    {
                        SSEngine.LANClient = new BroadcastClient();
                        if (SSEngine.LANClient.IsListening)
                        {
                            SSEngine.LANClient.Send((byte)MenuStage.Connect);
                        }
                    }
                }
                break;

            case MenuStage.Connect:
                if (SSEngine.VersionStage == MenuStage.LAN && SSEngine.LANClient.HasMessage())
                {
                    Message msg = SSEngine.LANClient.ReceiveMessage();
                    if (msg.Bytes[0] == (byte)MenuStage.Connect)
                    {
                        SSEngine.Player1 = true;
                        SSEngine.LANClient.Send((byte)MenuStage.Connected);
                        menuStage = MenuStage.Connected;
                    }
                    else if (msg.Bytes[0] == (byte)MenuStage.Connected)
                    {
                        SSEngine.LANClient.Send((byte)MenuStage.Connected);
                        SSEngine.Player1 = false;
                        menuStage        = MenuStage.RockPaperScissors;
                    }
                }
                else if (SSEngine.VersionStage == MenuStage.Online && SSEngine.IsHost.HasValue)
                {
                    if (Server.ConForm.cbClient.Checked && Server.ConForm.cbHost.Checked)     // Is a host or client.
                    {
                        SSEngine.Player1 = SSEngine.IsHost.Value;
                        menuStage        = MenuStage.RockPaperScissors;
                        System.Threading.Thread.Sleep(1000);
                        Server.ConForm.Close();
                    }
                }
                if (SSEngine.VersionStage == MenuStage.LAN && backButton.UpdateButton(mouse, gameTime))
                {
                    menuStage = MenuStage.Play;
                }
                break;

            case MenuStage.Connected:
                if (SSEngine.LANClient.HasMessage())
                {
                    Message msg = SSEngine.LANClient.ReceiveMessage();
                    if (msg.Bytes[0] == (byte)MenuStage.Connected)
                    {
                        menuStage = MenuStage.RockPaperScissors;
                    }
                }
                break;

            case MenuStage.RockPaperScissors:
                UpdateButtons(rocPapSciBtns, gameTime, mouse);
                UpdateRockPaperScissors();
                break;

            case MenuStage.RockPaperScissorsWait:
                if (SSEngine.VersionStage == MenuStage.LAN && SSEngine.LANClient.HasMessage())
                {
                    Message msg = SSEngine.LANClient.ReceiveMessage();
                    if (SSEngine.Player1.Value)
                    {
                        playerTwo = msg.Bytes[0];
                    }
                    else
                    {
                        playerOne = msg.Bytes[0];
                    }
                    RockPaperScissorsGame(playerOne, playerTwo);
                    menuStage = MenuStage.RockPaperScissorsPlaced;
                }
                else if (SSEngine.VersionStage == MenuStage.Online && SSEngine.Peer.PeekMessage() != null)
                {
                    NetIncomingMessage msg = SSEngine.Peer.ReadMessage();
                    if (SSEngine.Player1.Value)
                    {
                        playerTwo = msg.ReadByte();
                    }
                    else
                    {
                        playerOne = msg.ReadByte();
                    }
                    RockPaperScissorsGame(playerOne, playerTwo);
                    menuStage = MenuStage.RockPaperScissorsPlaced;
                }
                if (backButton.UpdateButton(mouse, gameTime))
                {
                    menuStage = MenuStage.Play;
                }
                break;

            case MenuStage.RockPaperScissorsPlaced:
                if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                {
                    if (playerOne != playerTwo)
                    {
                        if (SSEngine.VersionStage == MenuStage.LAN && !SSEngine.LANClient.HasMessage())
                        {
                            menuStage = MenuStage.WaitToPlay;
                            SSEngine.LANClient.Send((byte)menuStage);
                        }
                        else if (SSEngine.VersionStage == MenuStage.LAN && SSEngine.LANClient.HasMessage())
                        {
                            Message msg = SSEngine.LANClient.ReceiveMessage();
                            if ((MenuStage)msg.Bytes[0] == MenuStage.WaitToPlay)
                            {
                                SSEngine.LANClient.Send((byte)MenuStage.WaitToPlay);
                                menuStage = MenuStage.InGame;
                                game      = new SSGame(this);
                                Components.Add(game);
                                game.Enabled = true;
                                game.Visible = true;
                            }
                        }
                        else if (SSEngine.VersionStage == MenuStage.Online && SSEngine.Peer.PeekMessage() == null)
                        {
                            menuStage = MenuStage.WaitToPlay;
                            NetOutgoingMessage msg = SSEngine.Peer.CreateMessage();
                            msg.Write((byte)MenuStage.WaitToPlay);
                            SSEngine.Peer.SendMessage(msg, SSEngine.Peer.Connections[0], NetDeliveryMethod.ReliableOrdered);
                        }
                        else if (SSEngine.VersionStage == MenuStage.Online && SSEngine.Peer.PeekMessage() != null)
                        {
                            NetIncomingMessage inc = SSEngine.Peer.ReadMessage();
                            if ((MenuStage)inc.ReadByte() == MenuStage.WaitToPlay)
                            {
                                NetOutgoingMessage msg = SSEngine.Peer.CreateMessage();
                                msg.Write((byte)MenuStage.WaitToPlay);
                                SSEngine.Peer.SendMessage(msg, SSEngine.Peer.Connections[0], NetDeliveryMethod.ReliableOrdered);
                                menuStage = MenuStage.InGame;
                                game      = new SSGame(this);
                                Components.Add(game);
                                game.Enabled = true;
                                game.Visible = true;
                            }
                        }

                        if (SSEngine.VersionStage != MenuStage.LAN && SSEngine.VersionStage != MenuStage.Online)
                        {
                            menuStage = MenuStage.InGame;
                            game      = new SSGame(this);
                            Components.Add(game);
                            game.Enabled = true;
                            game.Visible = true;
                        }
                    }
                    else
                    {
                        menuStage = MenuStage.RockPaperScissors;
                    }
                    playerOne = -1;
                    playerTwo = -1;
                }
                break;

            case MenuStage.WaitToPlay:
                if (SSEngine.VersionStage == MenuStage.LAN && SSEngine.LANClient.HasMessage())
                {
                    Message msg = SSEngine.LANClient.ReceiveMessage();
                    if ((MenuStage)msg.Bytes[0] == MenuStage.WaitToPlay)
                    {
                        menuStage = MenuStage.InGame;
                        game      = new SSGame(this);
                        Components.Add(game);
                        game.Enabled = true;
                        game.Visible = true;
                    }
                }
                else if (SSEngine.VersionStage == MenuStage.Online && SSEngine.Peer.PeekMessage() != null)
                {
                    NetIncomingMessage inc = SSEngine.Peer.ReadMessage();
                    if ((MenuStage)inc.ReadByte() == MenuStage.WaitToPlay)
                    {
                        NetOutgoingMessage msg = SSEngine.Peer.CreateMessage();
                        msg.Write((byte)MenuStage.WaitToPlay);
                        SSEngine.Peer.SendMessage(msg, SSEngine.Peer.Connections[0], NetDeliveryMethod.ReliableOrdered);
                        menuStage = MenuStage.InGame;
                        game      = new SSGame(this);
                        Components.Add(game);
                        game.Enabled = true;
                        game.Visible = true;
                    }
                }
                break;

            case MenuStage.InGame:
                if (SSEngine.VersionStage == MenuStage.Play)
                {
                    Components.Remove(game);
                    menuStage = MenuStage.Play;
                }
                break;

            case MenuStage.GameRules:
                UpdateButtons(gameRulesBtns, gameTime, mouse); break;

            case MenuStage.Settings:
                UpdateButtons(settingsBtns, gameTime, mouse); break;
            }

            base.Update(gameTime);
        }