/** * */ 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(); }
/** * Move this card instance and/or other cards in it's card set to the specified target snap transform * @param Transform snap the target snap transform to move this card to * @param Card[] cardSet the set of dragged cards to handle translation all at once * with respect to this card instance. Defaults to null if a card set is not provided. * If the card set has only one card in it then it's assumed that the one card is * this card instance and will be processed as such. * @param MoveTypes moveType the type of move that determines how the move should be tracked in the GameManager. */ public void MoveTo(Transform snap, Card[] cardSet = null, Move.MoveTypes moveType = Move.MoveTypes.NORMAL) { // Prepare the move object m_move = new Move(); m_moveType = moveType; // Set the target card based on value of card set // (if card set is null then create a new card set with this card as the only element) m_move.SetCards(cardSet ?? (new Card[] { this })); // We know that the card has/had a parent m_move.SetPreviousParent(m_startParent); // Need to get what the snap belongs to so that the card is placed in the correct location SnapManager snapManager = snap.GetComponent <SnapManager>(); bool faceDownTarget = snapManager.HasCard() && snapManager.GetTopCard().IsFaceDown(); GameManager.Sections targetSection = snapManager.belongingSection; // Set the next parent in the move m_move.SetNextParent(snapManager.transform); // Need to target the top card in the respective tableau pile and offset the y and z positions Transform tableauHasCardTarget = targetSection.Equals(GameManager.Sections.TABLEAU) && snapManager.HasCard() ? snapManager.GetTopCard().transform : snap; // Setting for reference to new parent snap m_targetTranslateSnap = snap; m_targetTranslatePos = m_targetTranslateSnap.position; // Defaults to target snap position // Keep track of the starting position m_startPos = transform.position; m_draggedCards = cardSet; // Tell the snap manager currently associated with the card(s) to wait until animations are complete SnapManager currentSnap = GetComponentInParent <SnapManager>(); if (currentSnap != null) { // Set the start parent here so that the current snap can be told to stop waiting later SetStartParent(currentSnap.transform); currentSnap.SetWaiting(true); } // Process a bit differently if a card set has been provided if (m_draggedCards != null && m_draggedCards.Length > 1) { // Do a first pass-through to remove parent and bring z-value of each of the dragged cards // to the z-offset dragging value for (int i = 0; i < m_draggedCards.Length; i++) { Card draggedCard = m_draggedCards[i]; draggedCard.transform.parent = null; // Keep track of each card's starting position draggedCard.SetStartPos(draggedCard.transform.position); float yOffset; // Apply y-offset when dragging multiple cards (start without y-offset if there isn't a card on the snap) // Handle case when action was an undo and the top card in the target snap is facedown // (only the first card in the the set of dragged cards should have the face down y-offset applied in this case). if (faceDownTarget && i == 0) { yOffset = GameManager.FACE_DOWN_Y_OFFSET; } else { if (faceDownTarget) { // Need to compensate for the fact that the first card applied face down y-offset yOffset = (GameManager.FOUNDATION_Y_OFFSET * i) + GameManager.FACE_DOWN_Y_OFFSET; } else { // Process normally (e.g., not undoing a flip event) yOffset = GameManager.FOUNDATION_Y_OFFSET * (snapManager.HasCard() ? i + 1 : i); } } Vector3 newTargetPos = new Vector3( tableauHasCardTarget.position.x, tableauHasCardTarget.position.y - yOffset, tableauHasCardTarget.position.z - (i + 1) ); // Set the new translate position for the dragged card draggedCard.SetTargetTranslatePosition(newTargetPos); // Set the z-value of the transform to move to be high enough to hover over all other cards draggedCard.transform.position = new Vector3( draggedCard.transform.position.x, draggedCard.transform.position.y, -(GameManager.Z_OFFSET_DRAGGING + i) ); } } else // Otherwise, process on one card { // transform position is a special case for the Tableau cards due to y-offset in addition to z-offset if (targetSection.Equals(GameManager.Sections.TABLEAU) && snapManager.HasCard()) { Vector3 newTargetPos = new Vector3( tableauHasCardTarget.position.x, tableauHasCardTarget.position.y - (faceDownTarget ? GameManager.FACE_DOWN_Y_OFFSET : GameManager.FOUNDATION_Y_OFFSET), tableauHasCardTarget.position.z - 1 ); m_targetTranslatePos = newTargetPos; } transform.parent = null; // Temporarily detatch from the parent SetStartPos(transform.position); // Keep track of this card instance start position // Set the z-value of the transform to move to be high enough to hover over all other cards transform.position = new Vector3( transform.position.x, transform.position.y, -GameManager.Z_OFFSET_DRAGGING ); } // Add the move to the game manager instance (only if normal move and not undone or redone) if (moveType == Move.MoveTypes.NORMAL) { GameManager.Instance.AddMove(m_move, moveType); } // Track the total number of moves with stats manager StatsManager.Instance.TallyMove(); // Begin the translating animation m_translating = true; }