Ejemplo n.º 1
0
        public override void LoadContent(SpriteBatch spriteBatch)
        {
            base.LoadContent(spriteBatch);

            // Load content
            tableTexture    = MainGame.Context.Content.Load <Texture2D>("Table");
            freeCellTexture = MainGame.Context.Content.Load <Texture2D>("Cards/freeCell");
            headerFont      = MainGame.Context.Content.Load <SpriteFont>("Fonts/Arial_24");
            buttonFont      = MainGame.Context.Content.Load <SpriteFont>("Fonts/Arial_18");

            // Load sound effects
            cardMoveSoundEffect            = MainGame.Context.Content.Load <SoundEffect>("Audio/card_move");
            cardSelectPrimarySoundEffect   = MainGame.Context.Content.Load <SoundEffect>("Audio/card_primary_select");
            cardSelectSecondarySoundEffect = MainGame.Context.Content.Load <SoundEffect>("Audio/card_secondary_select");
            foundationPileAddSoundEffect   = MainGame.Context.Content.Load <SoundEffect>("Audio/foundation_add");

            CardSuit[] cardSuits = Enum.GetValues(typeof(CardSuit)).Cast <CardSuit>().ToArray();
            foundationPiles = new FoundationPile[cardSuits.Length];

            // Initialize the foundation piles.
            for (int i = 0; i < cardSuits.Length; i++)
            {
                Texture2D pileTexture = FoundationPile.GetTexture(cardSuits[i]);

                // We use the reverse index since we are positioning
                // the foundation piles from the right of the screen.
                int reverseIndex = foundationPiles.Length - i - 1;

                // Foundation piles are placed from the right of the screen.
                // We find the total space from the current card to the final card and then apply the horizontal padding to the right side.
                float totalSapceFromCurrent = pileTexture.Width * (reverseIndex + 1) + PileHorizontalSpacing * reverseIndex + PileGroupHorizontalPadding;
                float positionX             = MainGame.GameScreenWidth - totalSapceFromCurrent;

                foundationPiles[i] = new FoundationPile(cardSuits[i], new RectangleF(positionX, PileGroupPositionY, pileTexture.Width, pileTexture.Height));
            }

            // Initialize the free cells
            freeCells = new FreeCell[FreeCellPileCount];
            for (int i = 0; i < freeCells.Length; i++)
            {
                // Free cells are placed from the left of the screen
                float positionX = PileGroupHorizontalPadding + i * (freeCellTexture.Width + PileHorizontalSpacing);
                freeCells[i] = new FreeCell(new RectangleF(positionX, PileGroupPositionY, freeCellTexture.Width, freeCellTexture.Height));
            }

            // Initialize the deck
            deck = new Deck();
            deck.Shuffle(GameSeed);

            // Initialize the tableau piles
            InitializeTableauPiles();

            // Populate the tableau piles
            for (int i = 0; i < deck.Count; i++)
            {
                tableauPiles[i % tableauPiles.Length].Push(deck[i], true);
            }

            // Update each tableau pile rectangle.
            foreach (TableauPile tableauPile in tableauPiles)
            {
                tableauPile.UpdateRectangles();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets up the game and creates a new deal based on the given seed.
        /// </summary>
        public IEnumerator NewGame(int seed)
        {
            yield return(new WaitForEndOfFrame()); // to give itween a chance to avoid it's initalization bug.

            GetComponent <AudioSource>().Play();

            HasWon = false;

            foreach (var card in FindObjectsOfType <Card>())
            {
                Destroy(card.gameObject);
            }

            foreach (var deck in FindObjectsOfType <Deck>())
            {
                if (deck != Dealer)
                {
                    Destroy(deck.gameObject);
                }
            }

            GameBehavior.Cards.Clear();

            Moves     = new Stack <Move>();
            redoMoves = new Stack <Move>();

            FreeCells.Clear();
            for (int i = 0; i < Cells; i++)
            {
                CreateFreeCell();
            }

            GoalCells.Clear();
            var startPositionX = 0.625f + 0.05f;
            var xSpacing       = Suits <= 6 ? 1.24f : 6.2f / (Suits - 1);

            for (int i = 0; i < Suits; i++)
            {
                //  var deck = (Instantiate(GameBehavior.Instance.DeckPrefab) as GameObject).GetComponent<Deck>();
                // var deck = (Instantiate(GameBehavior.Instance.DeckAPrefab) as GameObject).GetComponent<Deck>();
                Deck deck = null;
                if (Screen.orientation == ScreenOrientation.Portrait)
                //      if (Input.deviceOrientation == DeviceOrientation.Portrait)
                {
                    deck = (Instantiate(GameBehavior.Instance.DeckOtherAPrefab) as GameObject).GetComponent <Deck>();
                    deck.gameObject.tag = "HelpCard";
                }
                else if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight)
                {
                    deck = (Instantiate(GameBehavior.Instance.DeckAPrefab) as GameObject).GetComponent <Deck>();
                    deck.gameObject.tag = "HelpCard";
                }
                TempADeck.Add(deck);
                deck.gameObject.tag = "HelpCard";
                deck.CardSpacerY    = 0.001f;
                deck.MaxCardsSpace  = 15;
                //   deck.transform.position = new Vector3(startPositionX + (i * xSpacing), 3.3f, 0);
                if (Screen.orientation == ScreenOrientation.Portrait)
                {
                    deck.transform.position = new Vector3((0.225f) + (i * 0.5f), 3.3f, 0);
                }
                else if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight)
                {
                    //old   deck.transform.position = new Vector3(-6.3f, 1.2f + startPositionX - (i * 1.6f), 0);
                    deck.transform.position = new Vector3(+6.3f, 1.2f + startPositionX - (i * 1.6f), 0);
                }
                deck.transform.parent = transform;

                deck.Type  = DeckType.Goal;
                deck.Index = i;
                decksArray2.Add(deck);
                GoalCells.Add(deck);
            }

            PlayCells.Clear();
            startPositionX = ((Stacks - 1) / 2f) * -1.24f;
            for (int i = 0; i < Stacks; i++)
            {
                Deck deck = null;
                if (Screen.orientation == ScreenOrientation.Portrait)
                {
                    deck = (Instantiate(GameBehavior.Instance.DeckSmallOtherPrefab) as GameObject).GetComponent <Deck>();
                    deck.gameObject.tag = "StackCard";
                }
                else if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight)
                {
                    deck = (Instantiate(GameBehavior.Instance.DeckPrefab) as GameObject).GetComponent <Deck>();
                    deck.gameObject.tag = "StackCard";
                }
                deck.gameObject.tag = "StackCard";
                deck.enabled        = true;
                deck.CardSpacerY    = PlayStackVerticalSpace;
                deck.MaxCardsSpace  = 13;
                if (Screen.orientation == ScreenOrientation.Portrait)
                {
                    // deck.transform.position = new Vector3(startPositionX + (i * 1.24f), 2.8f, 0);
                    deck.transform.position = new Vector3(-1.775f + (i * 0.5f), 1.6f, 0);
                }
                else if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight)
                {
                    //   deck.transform.position = new Vector3(startPositionX + (i * 1.24f), 1.6f, 0);
                    deck.MaxCardsSpace = 13;
                    //   startPositionX = ((Stacks - 1) / 2f);
                    deck.transform.position = new Vector3(startPositionX + (i * 1.24f), 2.8f, 0);
                }
                deck.transform.parent = transform;

                deck.Type = DeckType.Play;

                PlayCells.Add(deck);
                decksArray4.Add(deck);
            }
            //    stackReference = GameObject.FindGameObjectsWithTag("StackCard");
            // stackReference[0].transform.GetChild(0).transform.gameObject.transform.localScale = new Vector3(1, 1, 1);
            Dealer.CreateCards(Suits);

            var random = new System.Random(seed);

            Dealer.Shuffle(3, random);
            Dealer.FlipAllCards();

            Dealer.CardSpacerX   = 0;
            Dealer.MaxCardsSpace = 20;
            // for (int i = 0; i < 4; i++) {

            // }



            foreach (var card in Dealer.Cards)
            {
                card.Visible = true;
            }

            var count = 0;

            while (Dealer.Cards.Count > 0)
            {
                Dealer.Draw(PlayCells[count % PlayCells.Count], 1);
                count++;
            }

            if (Difficulty < 0)
            {
                for (int i = 0; i < -Difficulty * Suits; i++)
                {
                    // find all aces thru threes that aren't on top of deck already
                    var potentialCards = (from c in GameBehavior.Cards where c.Number <= 3 && c.Deck.TopCard != c select c).ToArray();

                    if (potentialCards.Length > 0)
                    {
                        var card = potentialCards[random.Next(0, potentialCards.Length)];
                        //print("Pushing up " + card.ToString());
                        card.SetDeck(card.Deck.Cards.IndexOf(card) + 1);
                    }
                }
            }
            else
            {
                for (int i = 0; i < Difficulty * Suits; i++)
                {
                    // find all aces thru threes that aren't on bottom of deck already
                    var potentialCards = (from c in GameBehavior.Cards where c.Number <= 3 && c.Deck.BottomCard != c select c).ToArray();

                    if (potentialCards.Length > 0)
                    {
                        var card = potentialCards[random.Next(0, potentialCards.Length)];
                        //print("Pushing down " + card.ToString());
                        card.SetDeck(card.Deck.Cards.IndexOf(card) - 1);
                    }
                }
            }

            CalculateDragableCards();

            stackReference = GameObject.FindGameObjectsWithTag("StackCard");
            for (int i = 0; i < Stacks; i++)
            {
                stackReference[i].transform.GetChild(0).transform.gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
            }
            StartTime = Time.time;
        }