Ejemplo n.º 1
0
        /**
         * Get the nearest snap manager to a given card. This function assumes that
         * the given card is not translating and not flipping (e.g., not performing any
         * animations). This function uses a collision vector that works like a
         * laser pointer from the origin of the card snap about the y-axis and z-axis to determine
         * if there are any snap managers nearby vertically. This
         * function is intended to be used only if the card has been double clicked.
         *
         * @param card the card to use as reference in determining the nearest snap manager.
         * @return the nearest snap manager. Returns null if no nearest snap manager exists.
         */
        public static SnapManager GetNearestSnapManager(Card card)
        {
            SnapManager nearestSnap = null;

            if (card != null)
            {
                Vector3    collisionVector = new Vector3(10.0f, 10.0f, 1000.0f);
                Collider[] hitColliders    = Physics.OverlapBox(card.transform.position, collisionVector);

                // First pass will be to find the snap manager
                // Iterate backwards to start with the nearest collisions relative to the card
                for (int i = hitColliders.Length - 1; i >= 0; i--)
                {
                    Collider hitCollider = hitColliders[i];
                    if (hitCollider.transform.CompareTag("Snap"))
                    {
                        // No need to continue once the snap has been found
                        nearestSnap = hitCollider.transform.GetComponent <SnapManager>();
                        break;
                    }
                }

                // If a snap couldn't be found then attempt to get the parent snap off of a collided card
                if (nearestSnap == null)
                {
                    // Iterate backwards to start with the nearest collisions relative to the card
                    for (int i = hitColliders.Length - 1; i >= 0; i--)
                    {
                        Collider hitCollider = hitColliders[i];
                        if (hitCollider.transform.CompareTag("Card"))
                        {
                            // Attempt to get snap from collided card
                            Card collidedCard = hitCollider.transform.GetComponent <Card>();
                            nearestSnap = collidedCard.GetComponentInParent <SnapManager>();

                            // No need to continue once the snap has been found
                            if (nearestSnap != null)
                            {
                                break;
                            }
                        }
                    }
                }
            }

            return(nearestSnap);
        }
Ejemplo n.º 2
0
        /**
         *
         */
        private void HandleTouchUp(Touch touch)
        {
            // Don't process if dragging isn't currently allowed
            if (!DraggingIsAllowed())
            {
                Init();
                return;
            }

            // Don't process if target card is performing an animation
            if (m_isDoingAnimation)
            {
                Init();
                return;
            }

            if (m_isCard || (m_currentObject != null && m_currentObject.CompareTag("Snap")))
            {
                m_screenPoint = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
                //Vector3 curPosition = Camera.main.ScreenToWorldPoint(m_screenPoint) + m_offset;
                Vector3 curPosition = m_screenPoint;
                if (GameManager.DEBUG_MODE)
                {
                    Debug.Log("Stopped dragging at: " + curPosition);
                    Debug.Log("Starting point was: " + m_startPos);
                }

                // Mouse up will be determined as a click if the current position is the same as the start position
                if (IsClick(curPosition))
                {
                    if (m_currentObject.CompareTag("Snap"))
                    {
                        if (m_currentObject.transform.parent.CompareTag("Stock"))
                        {
                            // Only way to get to this point is if the stock was clicked and there are no cards on it
                            // Transfer all cards attached to talon back to stock
                            GameManager.Instance.SetBlocked(true);
                            GameManager.Instance.ReplinishStock();
                        }
                    }
                    else
                    {
                        Card cardOfInterest = m_currentObject.GetComponent <Card>();
                        if (cardOfInterest.GetStartParent() != null)
                        {
                            string cardParentSetTag = cardOfInterest.GetStartParent().parent.tag;

                            // Determine if double click
                            float timeDiff = Time.timeSinceLevelLoad - m_timeSinceLastClick;
                            if (GameManager.DEBUG_MODE)
                            {
                                Debug.Log("Time difference since last click: " + timeDiff);
                            }
                            bool doubleClick = (m_timeSinceLastClick >= 0) && timeDiff <= CLICK_DIFF_TIME_THRESHOLD;

                            Transform nextMove = null;

                            // Don't apply double-click logic to stock
                            // Also don't permit click spamming for drawing cards (use game manager action/event block)
                            if (cardParentSetTag.Equals("Stock") && !GameManager.Instance.IsBlocked())
                            {
                                // Move the card to the talon pile once it has been clicked on the stock (draw card)
                                GameManager.Instance.SetBlocked(true); // Place temporary lock to prevent concurrent actions/events
                                cardOfInterest.MoveTo(GameManager.Instance.GetTalonPile());
                            }
                            else if (doubleClick || SettingsManager.Instance.IsSingleTapAutoCompleteTrigger())
                            {
                                // Determine the next valid move. Supply the dragged cards count so prevent the scenario in which
                                // more than one card is dragged to a foundation in one event.
                                nextMove = GameManager.Instance.GetNextAvailableMove(cardOfInterest, m_draggedCards.Length);
                                if (GameManager.DEBUG_MODE)
                                {
                                    Debug.Log("Next Move: " + nextMove);
                                }

                                // If double click and there is a valid next move
                                // Then, automatically move the double clicked card to the most appropriate location.
                                if (nextMove)
                                {
                                    // Move all cards in set of dragged cards (can be 1)
                                    GameManager.Instance.SetBlocked(true); // Place temporary lock to prevent concurrent actions/events
                                    cardOfInterest.MoveTo(nextMove, m_draggedCards);
                                }
                            }

                            // Handle cleanup case
                            if (!cardParentSetTag.Equals("Stock") && !nextMove)
                            {
                                // Need to set the parent back to the original parent for card(s) in the set of dragged cards.
                                foreach (Card card in m_draggedCards)
                                {
                                    card.transform.parent = card.GetStartParent();

                                    // Ensure all mesh colliders are re-enabled (corner case when double clicking a face down card)
                                    card.GetComponent <MeshCollider>().enabled = true;
                                }

                                // Unblock actions/events since invalid click
                                GameManager.Instance.SetBlocked(false);
                            }
                        }
                    }

                    // Keep track of the time of last click
                    m_timeSinceLastClick = Time.timeSinceLevelLoad;
                    return;
                }

                // Don't allow dropping stock cards or face-down cards
                Card cardCheck = m_currentObject.GetComponent <Card>();
                bool valid     = !m_isStockCard && cardCheck != null && !cardCheck.IsFaceDown();

                // Validate the dragged location to determine if the card should be snapped back to original location
                // or snapped to the respective target (e.g., attempted drag location)
                Vector3 collisionVector = new Vector3(10.0f, 10.0f, 1000.0f);
                bool    collides        = Physics.CheckBox(curPosition, collisionVector);

                if (collides && valid)
                {
                    Collider[] hitColliders = Physics.OverlapBox(curPosition, collisionVector);
                    int        i            = 0;
                    while (i < hitColliders.Length)
                    {
                        Transform collidedTransform = hitColliders[i].GetComponent <Transform>();
                        if (GameManager.DEBUG_MODE)
                        {
                            Debug.Log("Would collide with object: " + collidedTransform);
                        }

                        // Snap to the snap location if there is one
                        if (collidedTransform.CompareTag("Snap"))
                        {
                            // Don't allow dropping dragged cards in prohibited locations
                            SnapManager snapManager = collidedTransform.GetComponent <SnapManager>();
                            if (GameManager.PROHIBITED_DROP_LOCATIONS.Contains(collidedTransform.parent.tag))
                            {
                                if (GameManager.DEBUG_MODE)
                                {
                                    Debug.Log("Can't manually drop card in " + collidedTransform.parent.tag);
                                }
                                valid = false;
                                break;
                            }

                            // Make sure there isn't already a card attached to the snap (otherwise need to search for card)
                            if (snapManager.HasCard())
                            {
                                if (GameManager.DEBUG_MODE)
                                {
                                    Debug.Log("Snap already has a card, skipping...");
                                }
                            }
                            else
                            {
                                if (GameManager.DEBUG_MODE)
                                {
                                    Debug.Log("Placing card(s) in: " + collidedTransform.parent.tag);
                                }

                                // Set the new position relative to the snap, adjusting the z value appropriately
                                Vector3 newPos = new Vector3(
                                    collidedTransform.position.x,
                                    collidedTransform.position.y,
                                    -1.0f                        // Set to a z value of -1 for initial card in stack
                                    );

                                // Need to iterate the set of dragged cards and adjust the position accordingly
                                bool isFoundation = collidedTransform.parent.CompareTag("Foundations");

                                // Assert that there is only one card being placed if target is foundation
                                if (isFoundation && m_draggedCards.Length > 1)
                                {
                                    if (GameManager.DEBUG_MODE)
                                    {
                                        Debug.Log("Cannot move more than one card at once to a foundation.");
                                    }
                                    valid = false;
                                    break;
                                }

                                // General validation step to take card value and suit into consideration
                                valid = snapManager.IsValidMove(m_draggedCards[0]);
                                if (valid)
                                {
                                    float yOffset = isFoundation ? 0.0f : GameManager.FOUNDATION_Y_OFFSET;
                                    int   j       = 0;
                                    foreach (Card card in m_draggedCards)
                                    {
                                        Vector3 cardPosition = card.transform.position;
                                        Vector3 newCardPos   = new Vector3(newPos.x, newPos.y - (yOffset * j), newPos.z - j);
                                        card.transform.position = newCardPos;

                                        // Add the card to the stack
                                        card.transform.parent = collidedTransform;

                                        // Re-enable the mesh colliders on the cards
                                        card.GetComponent <MeshCollider>().enabled = true;

                                        j++;
                                    }
                                }

                                break;
                            }
                        }
                        else if (collidedTransform.CompareTag("Card"))
                        {
                            // Determine if the card was the same one that is being dragged/dropped
                            if (collidedTransform.Equals(m_currentObject.transform))
                            {
                                if (GameManager.DEBUG_MODE)
                                {
                                    Debug.Log("Collided object is self, skipping...");
                                }
                            }
                            else
                            {
                                // Get the card object to determine if the respective card is stackable
                                Card targetCard = collidedTransform.GetComponent <Card>();
                                if (!targetCard.IsStackable())
                                {
                                    if (GameManager.DEBUG_MODE)
                                    {
                                        Debug.Log("Card is not stackable, skipping...");
                                    }
                                }
                                else
                                {
                                    // Reference the snap manager the card is attached to
                                    SnapManager snapManager = targetCard.GetComponentInParent <SnapManager>();

                                    if (GameManager.DEBUG_MODE)
                                    {
                                        Debug.Log("Placing card(s) in: " + collidedTransform.parent.parent.tag);
                                    }
                                    bool isFoundation = collidedTransform.parent.parent.CompareTag("Foundations");

                                    // Assert that there is only one card being placed if target is foundation
                                    if (isFoundation && m_draggedCards.Length > 1)
                                    {
                                        if (GameManager.DEBUG_MODE)
                                        {
                                            Debug.Log("Cannot move more than one card at once to a foundation.");
                                        }
                                        valid = false;
                                        break;
                                    }

                                    // General validation step to take card value and suit into consideration
                                    valid = snapManager.IsValidMove(m_draggedCards[0]);
                                    if (valid)
                                    {
                                        float yOffset = isFoundation ? 0.0f : GameManager.FOUNDATION_Y_OFFSET;

                                        // Offset y position by specified foundation y-offset
                                        // so that the card that is below is still shown
                                        Vector3 newPos = new Vector3(
                                            collidedTransform.position.x,
                                            collidedTransform.position.y - yOffset,
                                            collidedTransform.position.z - 1.0f
                                            );

                                        // Need to iterate the set of dragged cards and adjust the position accordingly
                                        int j = 0;
                                        foreach (Card card in m_draggedCards)
                                        {
                                            Vector3 cardPosition = card.transform.position;
                                            Vector3 newCardPos   = new Vector3(newPos.x, newPos.y - (yOffset * j), newPos.z - j);
                                            card.transform.position = newCardPos;

                                            // Add the card to the stack (note that the parent is not the collided transform)
                                            card.transform.parent = collidedTransform.parent;

                                            // Re-enable the mesh colliders on the cards
                                            card.GetComponent <MeshCollider>().enabled = true;

                                            j++;
                                        }
                                    }

                                    break;
                                }
                            }
                        }
                        else
                        {
                            // If collided with anything else other than a card or a snap then deemed as invalid
                            valid = false;
                        }

                        i++;
                    }
                }

                // Need to do one last check to determine if the cards were moved to the same location
                if (valid)
                {
                    Card    topCard         = m_draggedCards[0];
                    Vector3 topCardStartPos = topCard.GetStartPos();
                    valid = !(topCard.transform.position.x == topCardStartPos.x &&
                              topCard.transform.position.y == topCardStartPos.y);
                }

                if (!valid)
                {
                    if (GameManager.DEBUG_MODE)
                    {
                        Debug.Log("Invalid Move.");
                    }

                    // If the drag location is deemed invalid then we should snap back to starting position
                    // Need to iterate the list of dragged cards and set each card back to their respective
                    // starting position and starting parent
                    if (m_draggedCards != null)
                    {
                        foreach (Card card in m_draggedCards)
                        {
                            // Hanle corner case for when origin was the stock pile
                            if (m_originSnapManager.BelongsTo(GameManager.Sections.STOCK))
                            {
                                Vector3 startPos = card.GetStartPos();
                                bool    samePos  = card.transform.position.x == startPos.x &&
                                                   card.transform.position.y == startPos.y;
                                // Only handle this corner case if the card's curren position isn't the same
                                // as it's starting position.
                                if (!samePos)
                                {
                                    Transform talonTransform = GameManager.Instance.GetTalonPile();
                                    card.transform.position = talonTransform.position;
                                    card.transform.parent   = talonTransform;
                                }
                            }
                            else
                            {
                                card.transform.position = card.GetStartPos();
                                card.transform.parent   = card.GetStartParent();
                            }

                            // Re-enable the mesh colliders on the cards
                            card.GetComponent <MeshCollider>().enabled = true;
                        }
                    }
                }
                else
                {
                    // Register the manual move if it was valid
                    Move move = new Move();
                    move.SetCards(m_draggedCards);
                    move.SetPreviousParent(m_originSnapManager.transform);
                    move.SetNextParent(m_draggedCards[0].transform.parent);
                    GameManager.Instance.AddMove(move, Move.MoveTypes.NORMAL);

                    // Play the card set sound one shot so that other clips can play at the same time
                    AudioSource cardSetSound = SettingsManager.Instance.cardSetSound;
                    cardSetSound.PlayOneShot(SettingsManager.Instance.cardSetSoundClip);

                    // Track the total number of moves with stats manager
                    StatsManager.Instance.TallyMove();
                }

                // Remove temporary locks set during dragging
                if (m_dragged)
                {
                    // Can stop waiting now that the move is complete
                    // Evaluate only if origin snap manager isn't null
                    if (m_originSnapManager != null)
                    {
                        m_originSnapManager.SetWaiting(false);
                    }

                    GameManager.Instance.SetBlocked(false);
                    m_dragged = false;
                }
            }

            // Always initialize as a last step
            Init();
        }
Ejemplo n.º 3
0
        /**
         *
         */
        private void HandleTouchDown(Touch touch)
        {
            // Don't process if dragging isn't currently allowed
            if (!DraggingIsAllowed())
            {
                return;
            }

            Ray raycast = Camera.main.ScreenPointToRay(touch.position);

            if (Physics.Raycast(raycast, out RaycastHit raycastHit))
            {
                m_screenPoint   = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
                m_startPos      = m_screenPoint;
                m_currentObject = raycastHit.collider.gameObject;
                m_isCard        = raycastHit.collider.CompareTag("Card");
                if (m_isCard)
                {
                    // Assert that the card isn't face down (permit if card is in stock pile)
                    Card        targetCard = m_currentObject.GetComponent <Card>();
                    SnapManager parentSnap = targetCard.GetComponentInParent <SnapManager>();

                    // Don't deem as card if the target card is face down and doesn't belong to the stock
                    m_isCard = !(targetCard.IsFaceDown() && !parentSnap.BelongsTo(GameManager.Sections.STOCK));
                }
            }

            // If card, we need to get a list of the cards that are to be dragged (through the use of the Snap Manager)
            if (m_isCard && m_currentObject.transform.parent != null)
            {
                Card targetCard = m_currentObject.GetComponent <Card>();
                m_isDoingAnimation = targetCard.IsTranslating() || targetCard.IsFlipping();
                if (m_isDoingAnimation)
                {
                    if (GameManager.DEBUG_MODE)
                    {
                        Debug.Log("Card is doing an animation and cannot be dragged!");
                    }
                    return;
                }

                // Initialize the dragged cards list by referencing the set of cards that are attached to the
                // respective snap that one or many cards are to be dragged from.
                m_draggedCards = m_currentObject.GetComponentInParent <SnapManager>().GetCardSet(targetCard);
                if (m_draggedCards != null && m_draggedCards.Length > 0)
                {
                    // Check the first card to see if it's a stock card
                    if (GameManager.DEBUG_MODE)
                    {
                        Debug.Log("Tag Clicked: " + m_draggedCards[0].transform.parent.parent.tag);
                    }
                    m_isStockCard = m_draggedCards[0].transform.parent.parent.CompareTag("Stock");
                    if (GameManager.DEBUG_MODE)
                    {
                        Debug.Log("Is Stock Card: " + m_isStockCard);
                    }

                    // Set each dragged card's start position and parent
                    int i = 0;
                    m_originSnapManager = m_draggedCards[0].GetComponentInParent <SnapManager>();
                    m_originSnapManager.SetWaiting(true); // Wait until cards are dropped and validated before flipping any cards in tableau

                    foreach (Card card in m_draggedCards)
                    {
                        card.SetStartPos(card.transform.position);
                        card.SetStartParent(card.transform.parent);

                        // Temporarily disable the mesh collider for all cards except the first one in the set of dragged cards.
                        if (i != 0)
                        {
                            card.GetComponent <MeshCollider>().enabled = false;
                        }
                        i++;
                    }
                }
            }
            else
            {
                // Only initialize if not a snap
                if (!m_currentObject.CompareTag("Snap"))
                {
                    Init();
                }
            }
        }
Ejemplo n.º 4
0
        /**
         * Perform the core function for running the auto complete process.
         * This coroutine will rapidly move cards from the tableau to the foundation
         * and automatically complete the game once a valid winning state has been
         * determined by the system.
         */
        private IEnumerator AutoWinCoroutine()
        {
            SetDoingAutoWin(true);

            int attempts    = 0;
            int maxAttempts = 5000;

            // Build min priority queue based on all cards in tableau (prioritize lower value cards)
            PriorityQueue <Card> cardQueue = new PriorityQueue <Card>(true);

            // Add max attempts in case there is some corner case that occurs and causes a fail
            while (attempts < maxAttempts)
            {
                yield return(new WaitForEndOfFrame());

                // Break out of the loop if the win state has been met
                if (HasWon())
                {
                    break;
                }

                SnapManager[] tableauSnaps = tableau.GetComponentsInChildren <SnapManager>();
                foreach (SnapManager tableauSnap in tableauSnaps)
                {
                    // Get only the top card for now
                    Card topCard = tableauSnap.GetTopCard();
                    if (topCard != null)
                    {
                        cardQueue.Enqueue(topCard.value, topCard);
                    }
                }

                // Dequeue cards from priority queue and move each one to the foundation
                while (cardQueue.Count > 0)
                {
                    Card      priorityCard = cardQueue.Dequeue();
                    Transform nextMove     = GetNextAvailableMove(priorityCard);

                    // Only process move if one existed and if it is to a foundation
                    if (nextMove)
                    {
                        SnapManager currentSnapManager = priorityCard.GetComponentInParent <SnapManager>();
                        SnapManager nextSnapManager    = nextMove.GetComponent <SnapManager>();
                        bool        validMove          = nextSnapManager.belongingSection.Equals(Sections.FOUNDATIONS);
                        if (validMove)
                        {
                            // Make note of the current amount of attached cards
                            int currentCardCount = nextSnapManager.GetCardCount();

                            priorityCard.MoveTo(nextMove);

                            // Wait until the card is finished translating, is attached to the snap, and the current
                            // card count has incremented by one
                            yield return(new WaitUntil(() => {
                                return priorityCard.transform.parent != null &&
                                !priorityCard.IsTranslating() &&
                                nextSnapManager.GetCardCount() >= (currentCardCount + 1);
                            }));

                            // Add the current snap manager's top card to the priority queue
                            Card nextTopCard = currentSnapManager.GetTopCard();
                            if (nextTopCard != null)
                            {
                                cardQueue.Enqueue(nextTopCard.value, nextTopCard);
                            }
                        }
                    }
                }

                attempts++;
            }

            SetDoingAutoWin(false);
            m_autoWinComplete = true;
        }