Exemplo n.º 1
0
    void OnMouseDown()
    {
        //if there is no sprite in the gem or when the board is shifting do nothing
        if (render.sprite == null || BoardManager.board.IsShifting)
        {
            return;
        }

        if (isSelected)                         //if you select an already selected gem, deselect
        {
            Deselect();
        }
        else
        {
            //else if you don't have a previously selected gem and selected a new gem, the gem gets selected
            if (previousSelected == null)
            {
                Select();
            }
            else
            {
                //else if you have a previously selected gem and it is a neighbour of the currently selected gem, they swap.
                if (GetAllAdjacentTiles().Contains(previousSelected.gameObject))
                {
                    //they swap their sprite not the gameObject but looks like they are actually swapping!
                    SwapSprite(previousSelected.render);
                    previousSelected.ClearAllMatches();
                    previousSelected.Deselect();
                    ClearAllMatches();
                }
                else
                {
                    //else if you have a previously selected gem but select non neighbour gem,
                    //previous gem gets deselected and the current one gets selected
                    previousSelected.GetComponent <Gem>().Deselect();
                    Select();
                }
            }
        }
    }