private void OnGUI()
    {
        if (GameManager.AccessInstance() == null)
        {
            GUILayout.Label("Game not running, UI disabled.", EditorStyles.boldLabel);
        }
        else
        {
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Update"))
            {
                //Gather data
                display = new List <string>();
                GeneticHandler gH = GameManager.GeneticHandler;
                display.Add("Count: " + gH.population.Count + "\tTime:" + (Time.time - gH.timeStamp) + "\t Full Time: " + (Time.time - gH.originTimeStamp) + "\tAllTrials: " + gH.trialsMasterCount + "\tTPS:" + (gH.trialsMasterCount / (Time.time - gH.originTimeStamp)));
                //pull strings for all the info
                for (int i = 0; i < gH.population.Count; i++)
                {
                    GeneticHandler.GeneSequence item = gH.population[i];
                    display.Add(i + ": " + item.returned + "/" + item.sent + " @ " + item.ToString());
                }
            }
            if (GUILayout.Button("DumpPopulation"))
            {
                //Count all the current stats files
                string fileName       = "PopDump";
                string fileExtenstion = ".txt";
                int    num            = 0;
                while (File.Exists(Application.persistentDataPath + fileName + num + fileExtenstion))
                {
                    num++;
                }
                Debug.LogError("Logged to : \n" + Application.persistentDataPath + fileName + num + fileExtenstion);

                //Prepare the file string
                string output = "= new List<GeneSequence>(\nnew GeneSequence[] {\n";
                foreach (GeneticHandler.GeneSequence gS in GameManager.GeneticHandler.population)
                {
                    output += "(GeneticSequence)new int[] {";
                    for (int i = 0; i < gS.genes.Length; i++)
                    {
                        output += "" + gS.genes[i];
                        if (i != gS.genes.Length - 1)
                        {
                            output += ",";
                        }
                        output += " ";
                    }
                    output += "},\n";
                }
                output += "\n}\n);";
                //Only save the file if we have something to report.
                if (output != "")
                {
                    File.WriteAllText(Application.persistentDataPath + fileName + num + fileExtenstion, output);
                }
            }
            EditorGUILayout.EndHorizontal();

            scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
            //Draw most recent data
            for (int i = 0; i < display.Count; i++)
            {
                GUILayout.Label(display[i], EditorStyles.label);
            }
            EditorGUILayout.EndScrollView();
        }
    }
Exemple #2
0
 public void Transition <T>() where T : State, new()
 {
     //Debug.Log("Request Transition: " + typeof(T).Name);
     GameManager.AccessInstance().StartCoroutine(TransitionTo <T>());
 }
Exemple #3
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);
            }
        }