Example #1
0
    public void OnBeginDrag(PointerEventData eventData)
    {
        if (Game.State != GameController.GameState.Idle)
        {
            return;
        }
        if (Piece.Removed)
        {
            return;
        }

        m_horizontal = Mathf.Abs(m_initPos.x - eventData.position.x) > Mathf.Abs(m_initPos.y - eventData.position.y);

        if (m_horizontal)
        {
            m_min = Math.Max(Piece.Col - 1, 0);
            m_max = Math.Min(Piece.Col + 1, Game.Cols - 1);
        }
        else
        {
            m_min = Math.Max(Piece.Row - 1, 0);
            m_max = Math.Min(Piece.Row + 1, Game.Rows - 1);
        }

        m_dragging = true;

        PieceSelectionManager.Clear();
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        if (Game.State != GameController.GameState.Idle)
        {
            return;
        }
        if (Piece.Removed)
        {
            return;
        }

        if (PieceSelectionManager.Piece == Piece)
        {
            PieceSelectionManager.Clear();
        }
        else if (PieceSelectionManager.Piece)
        {
            PieceSelectionManager.Other = Piece;
        }
        else
        {
            PieceSelectionManager.Piece = Piece;
        }

        if (PieceSelectionManager.Piece && PieceSelectionManager.Other)
        {
            if (IsAdjacent(PieceSelectionManager.Piece, PieceSelectionManager.Other))
            {
                Game.SwapPieces(PieceSelectionManager.Piece, PieceSelectionManager.Other);

                if (!Game.WillMerge(PieceSelectionManager.Piece) && !Game.WillMerge(PieceSelectionManager.Other))
                {
                    Game.SwapPieces(PieceSelectionManager.Piece, PieceSelectionManager.Other);

                    AnimationController.Instance.AnimateSwapRevert(PieceSelectionManager.Piece, PieceSelectionManager.Other);
                }
                else
                {
                    AnimationController.Instance.AnimateSwap(PieceSelectionManager.Piece, PieceSelectionManager.Other);
                }
            }

            PieceSelectionManager.Clear();
        }
    }