//Checking when starting to drag public void OnBeginDrag(PointerEventData eventData) { if (!Card.isFlippingOn && !GameController.isTranslationOn && !isDragging) { Debug.Log("BeginDrag"); parentToReturnTo = this.transform.parent; isDragging = true; if (Canvas != null) { draggingItem.transform.position = eventData.position; this.transform.SetParent(draggingItem.transform); } if (parentToReturnTo.gameObject.tag == "FrontPile") { //Refereces to the table pile it comes from TablePilesDrop thisCardTablePile = parentToReturnTo.gameObject.GetComponent <TablePilesDrop>(); if (thisCardTablePile.thisPileList[thisCardTablePile.thisPileList.Count - 1] != this.gameObject) { int i = 0; while (thisCardTablePile.thisPileList[i] != this.gameObject) { i++; } for (i += 1; i < thisCardTablePile.thisPileList.Count; ++i) { thisCardTablePile.thisPileList[i].GetComponent <Card>().parentToReturnTo = thisCardTablePile.thisPileList[i].gameObject.transform.parent; thisCardTablePile.thisPileList[i].transform.SetParent(draggingItem.transform); } } } GetComponent <CanvasGroup>().blocksRaycasts = false; } }
//Translation coroutine for the translation animation. Can be called by 3 different translation types: from deck to table, from deck to discard pile and from tables to the drop zones public IEnumerator Translation(Transform thisTransform, Vector3 startPos, Vector3 endPos, float value, MoveType moveType, int tableNumber, int translationType) { isTranslationOn = true; float rate = (moveType == MoveType.Time) ? 1.0f / value : 1.0f / Vector3.Distance(startPos, endPos) * value; float t = 0.0f; while (t < 1.0) { t += Time.deltaTime * rate; thisTransform.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0.0f, 1.0f, t)); yield return(null); } //The initial translation of the cards to the tables if (translationType == 0) { //Getting a reference to the flipped card object and script FlippedTablePiles flippedTablePile = TablePiles[tableNumber].gameObject.GetComponent <FlippedTablePiles>(); //Setting the parent to the flipped table object thisTransform.SetParent(flippedTablePile.gameObject.transform); //Adding it to the flipped table list structure flippedTablePile.thisFlippedPileList.Add(thisTransform.gameObject); //Correcting the order in the children for correct rendering flippedTablePile.gameObject.transform.GetChild(flippedTablePile.gameObject.transform.childCount - 1).SetSiblingIndex(flippedTablePile.gameObject.transform.childCount - 2); //Get the last card of that pile and flip it if (flippedTablePile.gameObject.transform.childCount == tableNumber + 2) { //Get reference to last card Card lastCard = flippedTablePile.thisFlippedPileList[flippedTablePile.thisFlippedPileList.Count - 1].GetComponent <Card>(); //flip it StartCoroutine(lastCard.FlippingBackCardAnimation(lastCard.gameObject.transform, new Vector3(0, -180, 0), 0.5f)); //Re parent the last card to the front pile Transform frontPileTransform = TablePiles[tableNumber].transform.GetChild(TablePiles[tableNumber].transform.childCount - 1); lastCard.gameObject.transform.SetParent(frontPileTransform); lastCard.parentToReturnTo = frontPileTransform; //Update the front and back pile lists and the front pile value GameObject frontCards = TablePiles[tableNumber].transform.GetChild(TablePiles[tableNumber].transform.childCount - 1).gameObject; TablePilesDrop thisTablePileDrop = frontCards.GetComponent <TablePilesDrop>(); thisTablePileDrop.thisColor = lastCard.thisColor; thisTablePileDrop.currentValue = lastCard.value; thisTablePileDrop.thisSeme = lastCard.thisSeme; flippedTablePile.thisFlippedPileList.Remove(lastCard.gameObject); thisTablePileDrop.thisPileList.Add(lastCard.gameObject); } } //The translation from the deck to the discard pile else if (translationType == 1) { Card thisCard = thisTransform.gameObject.GetComponent <Card>(); StartCoroutine(thisCard.FlippingBackCardAnimation(thisTransform, new Vector3(0, -180, 0), 0.5f)); thisTransform.SetParent(discardPile.transform); thisCard.parentToReturnTo = discardPile.transform; } //The translation from discard pile or table to drop areas else if (translationType == 2) { for (int i = 0; i < DropAreas.Count; ++i) { DropZone thisDropZone = DropAreas[i].GetComponent <DropZone>(); if (thisDropZone.thisSeme == thisTransform.gameObject.GetComponent <Card>().thisSeme) { thisTransform.SetParent(thisDropZone.gameObject.transform); break; } } } isTranslationOn = false; }
//Same concept of the drop area but slightly different behaviour. TO DO Repetition (Refactor by combining his and the other into a single function like for translate in gameController) public void OnDrop(PointerEventData eventData) { Card card = eventData.pointerDrag.GetComponent <Card>(); if (card != null) { if ((thisColor != card.thisColor && card.value == currentValue - 1)) //Change it to only allow when there are not covered cards { GameController.moves += 1; gameController.MovesText.text = GameController.moves.ToString(); //Here get the reference to the previous parent and if it's a tablepile update the value. if it's the discard pile update that if (card.parentToReturnTo.gameObject.tag == "FrontPile") { //Getting references to the flipped and front list of its previous tablePile TablePilesDrop oldPile = card.parentToReturnTo.gameObject.GetComponent <TablePilesDrop>(); FlippedTablePiles oldFlippedTablePile = oldPile.gameObject.GetComponentInParent <FlippedTablePiles>(); oldPile.thisPileList.Remove(card.gameObject); Transform draggingItemTransform = card.gameObject.transform.parent; card.parentToReturnTo = this.transform; //On drop fires before end drag so I can override Parent to return to Card lastCard = card; thisPileList.Add(card.gameObject); //You can also use the old table pile list insted of the children of the dragging item (cleaner, change later) if (draggingItemTransform.childCount > 1) { for (int i = 1; i < draggingItemTransform.childCount; ++i) { draggingItemTransform.GetChild(i).gameObject.GetComponent <Card>().parentToReturnTo = this.transform; lastCard = draggingItemTransform.GetChild(i).gameObject.GetComponent <Card>(); oldPile.thisPileList.Remove(lastCard.gameObject); thisPileList.Add(lastCard.gameObject); } } currentValue = lastCard.value; thisColor = lastCard.thisColor; thisSeme = lastCard.thisSeme; if (oldPile.thisPileList.Count == 0) //If the front list has 0 cards { //Check if the back cards have any cards left if (oldFlippedTablePile.gameObject.transform.childCount > 1) { GameController.score += 5; gameController.ScoreText.text = GameController.score.ToString(); Card lastFlippedCard = oldFlippedTablePile.thisFlippedPileList[oldFlippedTablePile.thisFlippedPileList.Count - 1].GetComponent <Card>(); Debug.Log("Last Flipped Card: " + lastFlippedCard.gameObject); oldPile.currentValue = lastFlippedCard.value; oldPile.thisColor = lastFlippedCard.thisColor; oldPile.thisSeme = lastFlippedCard.thisSeme; oldFlippedTablePile.thisFlippedPileList.Remove(lastFlippedCard.gameObject); oldPile.thisPileList.Add(lastFlippedCard.gameObject); lastFlippedCard.gameObject.transform.SetParent(oldPile.gameObject.transform); lastFlippedCard.parentToReturnTo = oldPile.gameObject.transform; StartCoroutine(lastFlippedCard.FlippingBackCardAnimation(lastFlippedCard.gameObject.transform, new Vector3(0, -180, 0), 0.5f)); } else { oldPile.currentValue = 14; oldPile.thisColor = Card.Color.NEUTRAL_COLOR; oldPile.thisSeme = Card.Seme.NEUTRAL_SEME; } } //Give the same value of the next card in the list else if (oldPile.thisPileList.Count != 0) { Card nextCardInList = oldPile.thisPileList[oldPile.thisPileList.Count - 1].GetComponent <Card>(); oldPile.currentValue = nextCardInList.value; oldPile.thisColor = nextCardInList.thisColor;; oldPile.thisSeme = nextCardInList.thisSeme; } } //If the card comes from a discard Pile else if (card.parentToReturnTo.gameObject.tag == "DiscardPile") { GameController.score += 5; gameController.ScoreText.text = GameController.score.ToString(); card.parentToReturnTo = this.transform; currentValue = card.value; thisColor = card.thisColor; thisSeme = card.thisSeme; thisPileList.Add(card.gameObject); //Here Update the Discard Pile discardPile.GetComponent <DiscardPile>().discardPileList.Remove(card.gameObject); } else if (card.parentToReturnTo.gameObject.tag == "DropArea") { GameController.score -= 15; if (GameController.score < 0) { GameController.score = 0; } gameController.ScoreText.text = GameController.score.ToString(); //Here Update the DropArea card.parentToReturnTo.gameObject.GetComponent <DropZone>().thisDropZoneList.RemoveAt(0); card.parentToReturnTo.gameObject.GetComponent <DropZone>().currentValue -= 1; card.parentToReturnTo = this.transform; currentValue = card.value; thisColor = card.thisColor; thisSeme = card.thisSeme; thisPileList.Add(card.gameObject); } } } }
//event triggers when a card is dropped on this drop zone public void OnDrop(PointerEventData eventData) { Card card = eventData.pointerDrag.GetComponent <Card>(); if (card.gameObject.transform.parent.childCount == 1) //If it's just one card you can drop it on the dropping zones { if (card != null) { if ((thisSeme == card.thisSeme && card.value == currentValue + 1)) { GameController.score += 10; GameController.moves += 1; gameController.ScoreText.text = GameController.score.ToString(); gameController.MovesText.text = GameController.moves.ToString(); thisDropZoneList.Add(card.gameObject); //Here get the reference to the previous parent and if it's a tablepile update the value. if it's the discard pile update that if (card.parentToReturnTo.gameObject.tag == "FrontPile") { //Getting references to the flipped and front list of its previous tablePile TablePilesDrop oldPile = card.parentToReturnTo.gameObject.GetComponent <TablePilesDrop>(); FlippedTablePiles oldFlippedTablePile = oldPile.gameObject.GetComponentInParent <FlippedTablePiles>(); oldPile.thisPileList.Remove(card.gameObject); Transform draggingItemTransform = card.gameObject.transform.parent; card.parentToReturnTo = this.transform; //On drop fires before end drag so I can override Parent to return to Card lastCard = card; currentValue = lastCard.value; if (oldPile.thisPileList.Count == 0) //If the front list has 0 cards { //Check if the back cards have any cards left if (oldFlippedTablePile.gameObject.transform.childCount > 1) { Card lastFlippedCard = oldFlippedTablePile.thisFlippedPileList[oldFlippedTablePile.thisFlippedPileList.Count - 1].GetComponent <Card>(); Debug.Log("Last Flipped Card: " + lastFlippedCard.gameObject); oldPile.currentValue = lastFlippedCard.value; oldPile.thisColor = lastFlippedCard.thisColor; oldPile.thisSeme = lastFlippedCard.thisSeme; oldFlippedTablePile.thisFlippedPileList.Remove(lastFlippedCard.gameObject); oldPile.thisPileList.Add(lastFlippedCard.gameObject); lastFlippedCard.gameObject.transform.SetParent(oldPile.gameObject.transform); StartCoroutine(lastFlippedCard.FlippingBackCardAnimation(lastFlippedCard.gameObject.transform, new Vector3(0, -180, 0), 0.5f)); } else { oldPile.currentValue = 14; oldPile.thisColor = Card.Color.NEUTRAL_COLOR; oldPile.thisSeme = Card.Seme.NEUTRAL_SEME; } } //Give the same value of the next card in the list else if (oldPile.thisPileList.Count != 0) { Card nextCardInList = oldPile.thisPileList[oldPile.thisPileList.Count - 1].GetComponent <Card>(); oldPile.currentValue = nextCardInList.value; oldPile.thisColor = nextCardInList.thisColor;; oldPile.thisSeme = nextCardInList.thisSeme; } } else { card.parentToReturnTo = this.transform; currentValue = card.value; discardPile.GetComponent <DiscardPile>().discardPileList.Remove(card.gameObject); } //Check if player has won if (card.value == 13) { int completePiles = 0; for (int i = 0; i < 4; ++i) { DropZone thisDropZone = allDropAreas[i].GetComponent <DropZone>(); if (thisDropZone.thisDropZoneList.Count != 13) { return; } else { completePiles++; if (completePiles == 4) { //Game is Won winningScreen.SetActive(true); winningScore.text = GameController.score.ToString(); } } } } } } } }