Esempio n. 1
0
    private void OnTilesLinked(GameObject one, GameObject two)
    {
        /* Purpose: called whenever the user links two tiles
         * Parameters: one and two, should be game objects with TileController scripts
         */

        //Debug.Log("OnTilesLinked: Start");

        //We need to make sure these two objects are actually tiles
        //So, try to access their TileControllers
        TileController thisTile  = one.GetComponent <TileController>();
        TileController otherTile = two.GetComponent <TileController>();

        //Debug.Log("Linking two tiles " + thisTile + " and " + otherTile);

        //If both turned out to be tiles . . .
        if (thisTile != null && otherTile != null)
        {
            //We now need to check which cards the two tiles belong to
            //since you can't combine or cancel tiles on the same card
            CardController otherCard = otherTile.GetCard();
            CardController thisCard  = thisTile.GetCard();

            //If both tiles belong to cards
            if (otherCard != null && thisCard != null)
            {
                //Next, we need to find out if the cards that the two tiles belong
                //to themselves belong to even larger cards.
                CardController thisParentCard  = thisCard.GetParentCard();
                CardController otherParentCard = otherCard.GetParentCard();

                //If the two cards are different
                //and if the two tiles represent the same prime factor
                if (!otherCard.Equals(thisCard) &&
                    otherTile.number == thisTile.number)
                {
                    //If the numbers are on the same side of the equation
                    //we'll need to factor out the common factor but not
                    //simply "delete it" from the equation
                    //Debug.Log(thisTile.GetSide());
                    //Debug.Log(otherTile.GetSide());
                    if (thisTile.GetSide().Equals(otherTile.GetSide()))
                    {
                        //If either the two cards do not themselves belong to
                        //an even larger card or they both belong to the same card
                        //Debug.Log("Here we are");
                        //Debug.Log("thisParentCard = " + thisParentCard);
                        //Debug.Log("otherParentCard = " + otherParentCard);
                        if ((thisParentCard == null &&
                             otherParentCard == null)
                            ||
                            (thisParentCard != null &&
                             thisParentCard.Equals(otherParentCard)))
                        {
                            //Debug.Log("Removing tile other " + otherTile.name);

                            //Then we've found a common factor and can extract it!
                            //We need to remove the common tile from both the original cards
                            //Debug.Log(name + " need to remove other card " + otherCard + " and otherTile = " + otherTile);
                            otherCard.RemoveTile(otherTile, false);
                            //if(thisCard.GetChildCards().Count == 0)
                            //{
                            //Debug.Log("Removing tile this " + thisTile);
                            thisCard.RemoveTile(thisTile, true);
                            //}

                            //Debug.Log("Our two parent cards are " + thisParentCard + " and " + otherParentCard);
                            //If the larger card doesn't exist, we need to create it
                            if (thisParentCard == null &&
                                otherParentCard == null)
                            {
                                //if (thisCard.GetChildCards().Count == 0)
                                //{
                                //We need to add the two cards to a new common card
                                //Debug.Log("Creating a new card");
                                List <TileController> commonTiles = new List <TileController>();
                                commonTiles.Add(otherTile);
                                List <CardController> childCards = new List <CardController>();
                                foreach (CardController c in thisCard.GetChildCards())
                                {
                                    childCards.Add(c);
                                }
                                thisCard.MergeWith(otherCard, childCards, commonTiles);
                                //}

                                /*else
                                 * {
                                 *  thisCard.AddChildCard(otherCard);
                                 *  thisCard.ResizeTiles();
                                 * }
                                 */
                            }
                            else if (thisParentCard != null &&
                                     thisParentCard.Equals(otherParentCard))
                            {
                                //We just need to add the common factor to the tiles in
                                //the larger card to which the two cards belong.
                                Debug.Log("Adding extra tile");
                                thisParentCard.AddTile(otherTile);
                                thisParentCard.AdjustCardSizeAndContents();
                                if (thisParentCard.GetParentCard())
                                {
                                    thisParentCard.GetParentCard().AdjustCardSizeAndContents();
                                }
                            }
                            //Debug.Log("Now to distribute Columns");
                            thisTile.GetSide().DistributeColumns();
                        }
                    }
                    else if (thisTile.GetSide().NumColumns() == 1 &&
                             otherTile.GetSide().NumColumns() == 1)
                    {
                        //If either the two cards do not themselves belong to
                        //an even larger card or they both belong to the same card
                        if (thisParentCard == null &&
                            otherParentCard == null)
                        {
                            //Debug.Log("Removing tile other");

                            //Then we've found a common factor and can extract it!
                            //We need to remove the common tile from both the original cards
                            //Debug.Log("Removing tiles");
                            otherCard.RemoveTile(otherTile, true);
                            //Debug.Log("Removing tile this");
                            thisCard.RemoveTile(thisTile, true);
                            if (otherCard.GetTiles().Count > 0)
                            {
                                //Debug.Log("Adjusts other");
                                otherCard.AdjustCardSizeAndContents();
                            }
                            else if (otherCard.GetChildCards().Count > 0)
                            {
                                otherCard.GetColumn().FreeChildCards();
                            }
                            if (thisCard.GetTiles().Count > 0)
                            {
                                //Debug.Log("Adjusts this");
                                thisCard.AdjustCardSizeAndContents();
                            }
                            else if (thisCard.GetChildCards().Count > 0)
                            {
                                thisCard.GetColumn().FreeChildCards();
                            }
                        }
                    }
                }
            }
            currentBoard.GetSide(0).DistributeColumns();
            currentBoard.GetSide(1).DistributeColumns();
        }
        else //Now to check if these are cards
        {
            //We need to make sure these two objects are actually tiles
            //So, try to access their TileControllers
            CardController thisCard  = one.GetComponent <CardController>();
            CardController otherCard = two.GetComponent <CardController>();

            if (thisCard && otherCard)
            {
                int newCardValue = thisCard.GetValue() + otherCard.GetValue();
                Debug.Log(thisCard.GetValue() + " + " + otherCard.GetValue() + " = " + newCardValue);
                if (thisCard.transform.position.x < otherCard.transform.position.x)
                {
                    otherCard.GetSide().RemoveCard(otherCard, true);
                    thisCard.RemoveTiles();
                    AddTilesToThisCard(thisCard, newCardValue);
                }
                else
                {
                    thisCard.GetSide().RemoveCard(thisCard, true);
                    otherCard.RemoveTiles();
                    AddTilesToThisCard(otherCard, newCardValue);
                }
            }
        }
    }