Пример #1
0
        public override IEnumerator Enter()
        {
            yield return(base.Enter());

            string      activePlayerName = "Player" + owner.Memory.GetData <int>("ActivePlayer");
            Player      activePlayer     = owner.Memory.GetData <Player>(activePlayerName);
            List <Card> plays            = owner.Memory.GetData <List <Card> >("Plays");

            //Check if the active player is playing this hand
            if (activePlayer.playingRound == false)
            {
                owner.Transition <Play>();
            }
            //Check if we are done playing cards
            else if (plays.Count >= 4 || (owner.Memory.GetData <bool>("Alone") && plays.Count >= 3))
            {
                //End the trick
                owner.Transition <EndTrick>();
            }
            else
            {
                bool lockout    = true;
                Card playedCard = null;

                //Wait for the active player to make a play
                System.Action <object, object> func = (object sender, object args) => {
                    if (args == null)
                    {
                        return;
                    }

                    object[]   trueArgs = (object[])args;
                    GameObject zone     = (GameObject)trueArgs[0];
                    playedCard = (Card)trueArgs[1];



                    if (zone.tag == "PlayZone" && activePlayer == playedCard.owner && AIHandler.isValidPlay(playedCard, activePlayer, owner))
                    {
                        owner.Memory.GetData <List <Card> >("KnownCards").Add(playedCard);

                        //Remove the card from the players hand.
                        activePlayer.RemoveCard(playedCard);

                        //Ensure card let go visuals are done now
                        CardInteractionHandlers.CardMouseExit(this, playedCard);

                        //Track the played card
                        plays.Add(playedCard);
                        owner.Memory.GetData <Dictionary <Card, int> >("PlaysMap").Add(playedCard, owner.Memory.GetData <int>("ActivePlayer"));



                        //Move it to a resting position
                        if (activePlayer.isHuman == false)
                        {
                            playedCard.StartCoroutine(GameManager.cardAnimator.FlyTo(Vector3.Lerp(Vector3.zero, activePlayer.gameObject.transform.position, 0.55f), playedCard, GameManager.AnimateGame, true));
                        }
                        else
                        {
                            FMODUnity.RuntimeManager.PlayOneShot("event:/cardPlace" + Random.Range(1, 4), playedCard.transform.position);
                        }

                        //Flip the card up if it is face down
                        if (playedCard.faceDown)
                        {
                            playedCard.StartCoroutine(GameManager.cardAnimator.Flip(playedCard, GameManager.AnimateGame));
                        }

                        //Adjust the hand visuals
                        playedCard.StartCoroutine(GameManager.cardAnimator.AdjustHand(activePlayerName, GameManager.AnimateGame, owner));

                        //Advance the coroutine
                        lockout = false;
                    }
                    else
                    {
                        //Debug.Log("Invalid play, returning card");
                        playedCard.StartCoroutine(GameManager.cardAnimator.FlyTo(playedCard.goalPosition, playedCard, GameManager.AnimateGame));
                    }
                };


                this.AddObserver(func, "CardPlayedInZone" + owner.UID);
                if (activePlayer.isHuman)
                {
                    CardInteractionHandlers.CanPlayCard = true;
                }

                if (activePlayer.isHuman == false)
                {
                    if (GameManager.AnimateGame)
                    {
                        yield return(new WaitForSeconds(0.25f));
                    }
                    yield return(AIHandler.MakePlayDecision(owner.Memory.GetData <int>("ActivePlayer"), owner.Memory.GetData <PointSpread>("Player" + owner.Memory.GetData <int>("ActivePlayer") + "PointSpread"), owner));
                }
                while (lockout)
                {
                    yield return(null);
                }
                CardInteractionHandlers.CanPlayCard = false;
                this.RemoveObserver(func, "CardPlayedInZone" + owner.UID);

                owner.Transition <Play>();
            }
            //Advance the active player
            owner.Memory.SetData("ActivePlayer", (owner.Memory.GetData <int>("ActivePlayer") == 3) ? 0 : owner.Memory.GetData <int>("ActivePlayer") + 1);
        }
Пример #2
0
        public override IEnumerator Exit()
        {
            yield return(base.Exit());

            //Hide the trump selector
            owner.Memory.GetData <TrumpSelector>("TrumpSelector").gameObject.SetActive(false);

            //If we are leaving the Trump round and have selected a trump, prepare regular play
            if (owner.Memory.HasKey <Card.Suit>("Trump"))
            {
                if (owner.Memory.GetData <Player>("Player" + owner.Memory.GetData <int>("ActivePlayer")).isHuman == false && GameManager.AnimateGame)
                {
                    GameManager.AccessInstance().StartCoroutine(HandleAISpeach(GameManager.SpawnAIText(owner.Memory.GetData <int>("ActivePlayer"), 2, owner)));
                    if (owner.Memory.GetData <bool>("Alone"))
                    {
                        GameManager.AccessInstance().StartCoroutine(HandleAISpeach(GameManager.SpawnAIText(owner.Memory.GetData <int>("ActivePlayer"), 3, owner)));
                    }
                }

                //Organize AI and player's hands
                Player[]      players       = new Player[] { owner.Memory.GetData <Player>("Player0"), owner.Memory.GetData <Player>("Player1"), owner.Memory.GetData <Player>("Player2"), owner.Memory.GetData <Player>("Player3") };
                System.Action OrganizeHands = () => {
                    foreach (Player p in players)
                    {
                        if (p.isHuman)
                        {
                            Card.Suit trump = owner.Memory.GetData <Card.Suit>("Trump");
                            System.Func <Card, int> calcVal = (Card c) => {
                                //Intial value is as printed on the card
                                int val = (int)c.value;
                                //Clump up same suits
                                val += ((int)c.suit) * 30;
                                //If on trump, add value
                                if (c.suit == trump)
                                {
                                    val += 500;
                                }
                                //Override on-color Jack
                                if (c.suit == trump.SameColorSuit() && c.value == Card.Value.Jack)
                                {
                                    val = 99995;
                                }
                                //Override on-trump Jack
                                if (c.suit == trump && c.value == Card.Value.Jack)
                                {
                                    val = 99999;
                                }
                                return(val);
                            };
                            p.GetHand().Sort((Card x, Card y) => {
                                if (calcVal(x) < calcVal(y))
                                {
                                    return(-1);
                                }
                                else
                                {
                                    return(1);
                                }
                            });
                            p.GetHand().Reverse();
                        }
                        else
                        {
                            System.Action <List <Card> > ShuffleTheList = (List <Card> shuffleList) => {
                                int n = shuffleList.Count;
                                while (n > 1)
                                {
                                    n--;
                                    int k;
                                    //Shuffle, but don't let things stay in the same spot
                                    do
                                    {
                                        k = Random.Range(0, n + 1);
                                    } while (k == n);
                                    Card value = shuffleList[k];
                                    shuffleList[k] = shuffleList[n];
                                    shuffleList[n] = value;
                                }
                            };
                            ShuffleTheList(p.GetHand());
                        }
                        //Animate the cards where they belong
                        p.StartCoroutine(GameManager.cardAnimator.AdjustHand(p.gameObject.name, GameManager.AnimateGame, owner));
                    }
                };


                //Create a stamp to track what trump is
                if (GameManager.AnimateGame)
                {
                    owner.Memory.SetData <GameObject>("TrumpIndicator", GameManager.SpawnTrumpIndicator(owner.Memory.GetData <Card.Suit>("Trump"), owner.Memory.GetData <int>("ActivePlayer") % 2));
                }
                Deck d = owner.Memory.GetData <Deck>("GameDeck");

                //If the revealed card is still face up, have the dealer switch with it.
                if (owner.Memory.GetData <Card>("RevealedCardFromKittie").faceDown == false &&
                    //The following is some going alone logic. First check if someone went alone.
                    //The specific case is when our partner calls a loner and we are the dealer so we need to skip doing the pickup
                    //So we check for the specific case, and then compare it to false.
                    //First we check for someone having gone alone. Then we check to make sure that was person was on our team, and then we check to see if it was not the dealer.
                    (owner.Memory.GetData <bool>("Alone") &&
                     owner.Memory.GetData <int>("TrumpCaller") % 2 == owner.Memory.GetData <int>("Dealer") % 2 &&
                     owner.Memory.GetData <int>("TrumpCaller") != owner.Memory.GetData <int>("Dealer")) == false)
                {
                    //Add the revealed card to the dealer's hand
                    Card   rCard      = owner.Memory.GetData <Card>("RevealedCardFromKittie");
                    string dealerName = "Player" + owner.Memory.GetData <int>("Dealer");
                    Player dealer     = owner.Memory.GetData <Player>(dealerName);
                    dealer.AddCard(rCard);
                    if (GameManager.AnimateGame)
                    {
                        yield return(GameManager.cardAnimator.Orient(rCard, dealerName, GameManager.AnimateGame, owner));
                    }

                    //Hide the card
                    if (GameManager.AnimateGame)
                    {
                        if (dealer.isHuman == false && GameManager.ShowAllCards == false)
                        {
                            yield return(GameManager.cardAnimator.Flip(rCard, GameManager.AnimateGame));
                        }
                    }

                    if (GameManager.AnimateGame)
                    {
                        yield return(GameManager.cardAnimator.AdjustHand(dealerName, GameManager.AnimateGame, owner));
                    }

                    OrganizeHands();
                    //Wait for the cards to orient
                    if (GameManager.AnimateGame)
                    {
                        foreach (Player player in players)
                        {
                            foreach (Card card in player.GetHand())
                            {
                                while (card.animating)
                                {
                                    yield return(null);
                                }
                            }
                        }
                    }


                    //Add Listeners for Card interaction
                    CardInteractionHandlers.EnableCardInteraction(this);
                    if (dealer.isHuman)
                    {
                        CardInteractionHandlers.CanPlayCard = true;
                    }

                    bool lockout = true;
                    System.Action <object, object> func = (object sender, object args) => {
                        if (args == null)
                        {
                            return;
                        }

                        object[]   trueArgs = (object[])args;
                        GameObject zone     = (GameObject)trueArgs[0];
                        Card       card     = (Card)trueArgs[1];

                        if (zone.tag == "PlayZone")
                        {
                            //Set the played card as the revealed card
                            owner.Memory.SetData("RevealedCardFromKittie", card);

                            //Remove the card from the dealers hand.
                            dealer.RemoveCard(card);
                            //Do exit visuals to signify the card has left hand control
                            CardInteractionHandlers.CardMouseExit(this, card);

                            //Adjust the hand visuals
                            card.StartCoroutine(GameManager.cardAnimator.AdjustHand(dealerName, GameManager.AnimateGame, owner));

                            //Advance the coroutine
                            lockout = false;
                        }
                        else
                        {
                            card.StartCoroutine(GameManager.cardAnimator.FlyTo(card.goalPosition, card, GameManager.AnimateGame));
                        }
                    };
                    this.AddObserver(func, "CardPlayedInZone" + owner.UID);
                    //Run the AI
                    if (owner.Memory.GetData <Player>(dealerName).isHuman == false)
                    {
                        yield return(AIHandler.MakeTrumpDiscardDecision(owner.Memory.GetData <int>("Dealer"), owner.Memory.GetData <PointSpread>("Player" + owner.Memory.GetData <int>("ActivePlayer") + "PointSpread"), owner));
                    }
                    if (GameManager.AnimateGame)
                    {
                        //Yield until the player plays one
                        while (lockout)
                        {
                            yield return(null);
                        }
                    }
                    //Remove listeners for card interaction
                    CardInteractionHandlers.CanPlayCard = false;
                    CardInteractionHandlers.DisableCardInteraction(this);
                    this.RemoveObserver(func, "CardPlayedInZone" + owner.UID);
                }
                else
                {
                    OrganizeHands();
                    //Wait for the cards to orient
                    if (GameManager.AnimateGame)
                    {
                        foreach (Player player in players)
                        {
                            foreach (Card card in player.GetHand())
                            {
                                while (card.animating)
                                {
                                    yield return(null);
                                }
                            }
                        }
                    }
                }
                Card revealedCard = owner.Memory.GetData <Card>("RevealedCardFromKittie");

                //Flip the card over if needed
                if (revealedCard.faceDown == false)
                {
                    revealedCard.StartCoroutine(GameManager.cardAnimator.Flip(revealedCard, GameManager.AnimateGame));
                }

                //Move card to the deck
                if (GameManager.AnimateGame)
                {
                    yield return(GameManager.cardAnimator.FlyTo(d.basePosition, revealedCard, GameManager.AnimateGame));
                }
                //Add revealed card back to deck
                d.Place(new Card[] { revealedCard });

                //Slide the deck these cards offscreen
                foreach (Card card in d)
                {
                    card.SetOrdering(-3);
                    card.StartCoroutine(GameManager.cardAnimator.FlyTo(Vector3.LerpUnclamped(Vector3.zero, owner.Memory.GetData <Player>("Player" + owner.Memory.GetData <int>("Dealer")).gameObject.transform.position, 3.4f), card, GameManager.AnimateGame));
                }

                //Reset active player to right of dealer
                owner.Memory.SetData("ActivePlayer", (owner.Memory.GetData <int>("Dealer") == 3) ? 0 : owner.Memory.GetData <int>("Dealer") + 1);

                //Game should be good to start, wait a moment before starting play
                if (GameManager.AnimateGame)
                {
                    yield return(new WaitForSeconds(0.25f));
                }

                //Enable regular card interaction
                CardInteractionHandlers.EnableCardInteraction(this);
            }
            else
            {
                if (owner.Memory.GetData <Player>("Player" + owner.Memory.GetData <int>("ActivePlayer")).isHuman == false && GameManager.AnimateGame)
                {
                    GameManager.AccessInstance().StartCoroutine(HandleAISpeach(GameManager.SpawnAIText(owner.Memory.GetData <int>("ActivePlayer"), 1, owner)));
                    if (GameManager.AnimateGame)
                    {
                        yield return(new WaitForSeconds(0.5f));
                    }
                }
                //Move on the active player
                owner.Memory.SetData("ActivePlayer", (owner.Memory.GetData <int>("ActivePlayer") == 3) ? 0 : owner.Memory.GetData <int>("ActivePlayer") + 1);
            }
        }