public void InstantiatePlayCells() { // Instantiate play cells (7) for (int iPlayCell = 0; iPlayCell < 7; iPlayCell++) { Vector3 newPositionForPlayCell = new Vector3(playCellPrefab.transform.position.x + iPlayCell * playCellXOffset, playCellPrefab.transform.position.y, playCellPrefab.transform.position.z); GameObject playCellGO = Instantiate(playCellPrefab, newPositionForPlayCell, Quaternion.identity); playCells[iPlayCell] = playCellGO; // Filling play cells with cards for (int iPlayCellCards = 0; iPlayCellCards < iPlayCell + 1; iPlayCellCards++) { // Move the card from the deck to the play cell Card m_card = m_deckClosedCards.allCardsInDeck[allCardsIndex].GetComponent <Card>(); PlayCells m_playCells = playCellGO.GetComponent <PlayCells>(); // Place the card on top of another one m_card.frontSR.sortingOrder = m_playCells.cardCount; m_card.backSR.sortingOrder = m_playCells.cardCount; m_card.suitRenderer.sortingOrder = m_playCells.cardCount; m_card.valueRenderer.sortingOrder = m_playCells.cardCount; m_playCells.cardCount++; m_card.inPlayCell = true; // Our card is no longer in deck m_card.inDeck = false; // Expand map Vector3 newPositionForCard; if (iPlayCellCards == iPlayCell) { m_card.isOpen = true; m_card.isFirst = true; m_card.ApplySettings(); newPositionForCard = new Vector3(m_playCells.transform.position.x, m_playCells.transform.position.y - iPlayCellCards * playCellYOffsetForClosedCards, -m_playCells.cardCount); } else { newPositionForCard = new Vector3(m_playCells.transform.position.x, m_playCells.transform.position.y - iPlayCellCards * playCellYOffsetForClosedCards, -m_playCells.cardCount); } m_card.transform.position = newPositionForCard; allCardsIndex++; } } }
/// <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>(); ClearStaircases(); 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>(); deck.CardSpacerY = 0.001f; deck.MaxCardsSpace = 15; deck.transform.position = new Vector3(startPositionX + (i * xSpacing), 3.3f, 0); deck.transform.parent = transform; deck.Type = DeckType.Goal; deck.Index = i; GoalCells.Add(deck); } PlayCells.Clear(); startPositionX = ((Stacks - 1) / 2f) * -1.24f; for (int i = 0; i < Stacks; i++) { var deck = (Instantiate(GameBehavior.Instance.DeckPrefab) as GameObject).GetComponent <Deck>(); deck.enabled = true; deck.CardSpacerY = PlayStackVerticalSpace; deck.MaxCardsSpace = 13; deck.transform.position = new Vector3(startPositionX + (i * 1.24f), 1.6f, 0); deck.transform.parent = transform; deck.Type = DeckType.Play; PlayCells.Add(deck); } Dealer.CreateCards(Suits); var random = new System.Random(seed); Dealer.Shuffle(3, random); Dealer.FlipAllCards(); Dealer.CardSpacerX = 0; Dealer.MaxCardsSpace = 20; 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(); StartTime = Time.time; }
void OnMouseUp() { // Not dragging anymore mouseDragging = false; if (isOpen && !inDeck) { // Disable box collider BoxCollider2D boxCollider2D = GetComponent <BoxCollider2D>(); boxCollider2D.enabled = false; RaycastHit2D hit; hit = Physics2D.Raycast(transform.position + (-oldCardPosition), Vector3.forward); // Check if can be moved Card otherCard = null; PlayCells m_playCells = null; CollectionCells m_collCells = null; if (hit.collider != null) { otherCard = hit.collider.GetComponent <Card>(); m_playCells = hit.collider.GetComponent <PlayCells>(); m_collCells = hit.collider.GetComponent <CollectionCells>(); } if (m_playCells != null && // Is there a playCell m_playCells.isFirst && // There are no other cards above the cell value == 13 // Only the king can land in an empty playCell ) { // Attach the card to this cell transform.position = new Vector3(hit.collider.transform.position.x, hit.collider.transform.position.y, hit.collider.transform.position.z + 10); transform.SetParent(null); SpriteRenderer playCellSR = m_playCells.GetComponent <SpriteRenderer>(); frontSR.sortingOrder = playCellSR.sortingOrder + 1; backSR.sortingOrder = playCellSR.sortingOrder + 1; suitRenderer.sortingOrder = frontSR.sortingOrder + 1; valueRenderer.sortingOrder = frontSR.sortingOrder + 1; m_playCells.isFirst = false; isFirst = true; inCollCell = false; inDeckHelper = false; inPlayCell = true; } else // collCell + Ace check if (m_collCells != null && // Is there a collCell m_collCells.isFirst && // There are no other cards above the cell value == 1 // Only an ace can land in an empty collCell ) { // Attach the card to this cell transform.position = new Vector3(hit.collider.transform.position.x, hit.collider.transform.position.y, hit.collider.transform.position.z - 1); transform.SetParent(null); SpriteRenderer collCellSR = m_collCells.GetComponent <SpriteRenderer>(); frontSR.sortingOrder = collCellSR.sortingOrder + 1; backSR.sortingOrder = collCellSR.sortingOrder + 1; suitRenderer.sortingOrder = frontSR.sortingOrder + 1; valueRenderer.sortingOrder = frontSR.sortingOrder + 1; m_collCells.isFirst = false; isFirst = true; inPlayCell = false; inDeckHelper = false; inCollCell = true; } else // On an open card in cell inCell7 if (otherCard != null && // Is there another card otherCard.inPlayCell && // The other card must be in one of 7 cells otherCard.isFirst && otherCard.isOpen && // Is another card open? otherCard.isCardColorRed != isCardColorRed && // Is the color of the other card otherCard.value == value + 1 // The value of the other card should be greater by one ) { // Attach a map to this map transform.position = new Vector3(hit.collider.transform.position.x, hit.collider.transform.position.y - gameManager.playCellYOffsetForOpenCards, hit.collider.transform.position.z - 1); transform.SetParent(hit.collider.transform); frontSR.sortingOrder = otherCard.frontSR.sortingOrder + 1; backSR.sortingOrder = otherCard.backSR.sortingOrder + 1; suitRenderer.sortingOrder = otherCard.suitRenderer.sortingOrder + 1; valueRenderer.sortingOrder = otherCard.valueRenderer.sortingOrder + 1; otherCard.isFirst = false; if (transform.childCount < 3) { isFirst = true; } inCollCell = false; inDeckHelper = false; inPlayCell = true; } else // To an open card in cell inCell4 if (otherCard != null && // Is there another card isFirst && otherCard.inCollCell && // The other card must be in one of the 4 cells otherCard.isFirst && otherCard.isOpen && // Is another card open? otherCard.suit == suit && // The colors must match otherCard.value == value - 1 // The value of the other card should be less by one ) { // Attach a map to this map transform.position = new Vector3(hit.collider.transform.position.x, hit.collider.transform.position.y, hit.collider.transform.position.z - 1); transform.SetParent(null); frontSR.sortingOrder = otherCard.frontSR.sortingOrder + 1; backSR.sortingOrder = otherCard.backSR.sortingOrder + 1; suitRenderer.sortingOrder = frontSR.sortingOrder + 1; valueRenderer.sortingOrder = frontSR.sortingOrder + 1; otherCard.isFirst = false; isFirst = true; inPlayCell = false; inDeckHelper = false; inCollCell = true; } // Return the card to its original location else if (!inDeck) { transform.position = oldPositionInCell; transform.SetParent(oldParent); frontSR.sortingOrder = backgroundSRSortingOrder; backSR.sortingOrder = backgroundSRSortingOrder; suitRenderer.sortingOrder = suitSRSortingOrder; valueRenderer.sortingOrder = valueSRSortingOrder; // Reset the hit beam hit = new RaycastHit2D(); } // Automatically open the map under the old place of this map if (hit.collider != null && (m_playCells != null || m_collCells != null || otherCard != null)) { RaycastHit2D hitDownCard; hitDownCard = Physics2D.Raycast(oldPositionInCell, Vector3.forward); Card oldDownCard = null; PlayCells cell7Down = null; CollectionCells cell4Down = null; if (hitDownCard.collider != null) { oldDownCard = hitDownCard.collider.GetComponent <Card>(); cell7Down = hitDownCard.collider.GetComponent <PlayCells>(); cell4Down = hitDownCard.collider.GetComponent <CollectionCells>(); } if (oldDownCard != null) { oldDownCard.isFirst = true; oldDownCard.isOpen = true; oldDownCard.ApplySettings(); } else if (cell7Down != null) { cell7Down.isFirst = true; } else if (cell4Down != null) { cell4Down.isFirst = true; } } // Enable box collider boxCollider2D.enabled = true; } if (inDeck) { // No longer in the deck inDeck = false; inDeckHelper = true; } }