public void BookPieceTapped(BookPuzzlePiece piece) { if (isSwapping) { return; } Helper.PlayAudioIfSoundOn(tapSound); if (selectedPiece) { if (!selectedPiece.Equals(piece)) { Swap(selectedPiece, piece); DeselectSelectedPiece(); // Check the win in PiecesRotated if you are using the swap animation if (!rotateSwap) { CheckIfWin(); } } else { // deselect piece DeselectSelectedPiece(); } } else { // select this piece SelectPiece(piece); } }
private void DeselectPiece(BookPuzzlePiece piece) { if (piece != null) { if (piece.selectionHalo != null) { piece.selectionHalo.enabled = false; } } selectedHaloClone.GetComponent <SpriteRenderer>().enabled = false; }
private void SetupWithRandomSwaps(int swaps) { // Helper.MuteSound(true); for (int i = 0; i < swaps; i++) { int randomIndex = Random.Range(0, allPieces.Length); int randomIndex2 = Random.Range(0, allPieces.Length); BookPuzzlePiece piece1 = allPieces[randomIndex]; BookPuzzlePiece piece2 = allPieces[randomIndex2]; Swap(piece1, piece2); } // Helper.MuteSound(false); }
private void DeselectSelectedPiece() { // selectedPiece.gameObject.transform.localScale = originalPieceScale; // selectedPiece.GetComponent<SpriteRenderer>().sortingOrder = 0; if (selectedPiece != null) { if (selectedPiece.selectionHalo != null) { selectedPiece.selectionHalo.enabled = false; } } selectedHaloClone.GetComponent <SpriteRenderer>().enabled = false; selectedPiece = null; }
private void SelectPiece(BookPuzzlePiece piece) { selectedPiece = piece; // piece.gameObject.transform.localScale = scaledUpPieceScale; // selectedPiece.GetComponent<SpriteRenderer>().sortingOrder = 2; if (selectedPiece.selectionHalo == null) { selectedHaloClone.GetComponent <SpriteRenderer>().enabled = true; selectedHaloClone.transform.position = piece.gameObject.transform.position; if (isFilingPuzzle) { selectedHaloClone.GetComponent <SpriteRenderer>().sortingOrder = piece.GetComponent <SpriteRenderer>().sortingOrder; } } else { selectedPiece.selectionHalo.sortingOrder = piece.GetComponent <SpriteRenderer>().sortingOrder; selectedPiece.selectionHalo.enabled = true; } }
public void SolvePuzzle() { rotateSwap = false; // Do not animate when we are solving puzzle // We do this with two loops.. Is there a more efficient way? Probably. allPieces = null; // The disabled pieces didn't come as children earlier. Fetch them again after all the pieces are enabled. allPieces = GetComponentsInChildren <BookPuzzlePiece>(); for (int i = 0; i < allPieces.Length; i++) { BookPuzzlePiece piece1 = allPieces[i]; if (!piece1.isCorrectPosition()) { for (int j = 0; j < allPieces.Length; j++) { BookPuzzlePiece piece2 = allPieces[j]; if (piece2.slotPosition == piece1.correctSlotPosition) { Swap(piece1, piece2); } } } } }
private void Swap(BookPuzzlePiece piece1, BookPuzzlePiece piece2) { Debug.Log("Swapping: " + piece1.name + " and: " + piece2.name); // Determine if you will use the swap animation if (!rotateSwap) { // Switch positions immediately since we are not using the swap animation Vector3 cachedPosition = piece1.transform.position; int cachedSlotPosition = piece1.slotPosition; piece1.transform.position = piece2.transform.position; piece1.slotPosition = piece2.slotPosition; piece2.transform.position = cachedPosition; piece2.slotPosition = cachedSlotPosition; } else { /*For the Carnival Gem Box puzzle, check if both the pieces are on a same circle collider.*/ Collider2D[] overlappingPiece1 = Physics2D.OverlapCircleAll(piece1.transform.position, 0.35f); Collider2D[] overlappingPiece2 = Physics2D.OverlapCircleAll(piece2.transform.position, 0.35f); Collider2D commonCollider = null; for (int i = 0; i < overlappingPiece1.Length; i++) { Collider2D piece1Overlap = overlappingPiece1 [i]; for (int j = 0; j < overlappingPiece2.Length; j++) { if (overlappingPiece2[j].Equals(piece1Overlap)) { commonCollider = piece1Overlap; break; } if (commonCollider != null) { break; } } } if (commonCollider != null) { isSwapping = true; swappingPiece1 = piece1; swappingPiece2 = piece2; cachedParent = piece1.transform.parent; // Create a new GameObject between the two pieces and set it as the swapping pieces' parent. GameObject parentGo = new GameObject(); Vector3 midPoint = Vector3.Lerp(piece1.transform.position, piece2.transform.position, 0.5f); parentGo.transform.position = midPoint; piece1.transform.parent = parentGo.transform; piece2.transform.parent = parentGo.transform; // Cache variables for the callback // Rotate around the midpoint parent (swapping the two locations). FontanaPiece fixes the individual pieces rotation so they should still point up. iTween.RotateTo(parentGo, iTween.Hash("z", 180, "time", 0.5, "onComplete", "PiecesRotated", "onCompleteTarget", gameObject)); // Swap the slot positions int cachedSlot = piece1.slotPosition; piece1.slotPosition = piece2.slotPosition; piece2.slotPosition = cachedSlot; } else { DeselectPiece(piece1); DeselectPiece(piece2); } // Helper.PlayAudioIfSoundOn(swapSound); } if (changeSpriteAndZOrder) { piece1.GetComponent <SpriteRenderer>().sortingOrder = piece1.slotPosition; piece1.transform.position = new Vector3(piece1.transform.position.x, piece1.transform.position.y, -piece1.slotPosition / 2.0f); piece2.GetComponent <SpriteRenderer>().sortingOrder = piece2.slotPosition; piece2.transform.position = new Vector3(piece2.transform.position.x, piece2.transform.position.y, -piece2.slotPosition / 2.0f); } }