void Update () {
        
        if (Input.GetMouseButtonDown(0) && _currentPieceToMove == null)
        {

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit[] hits = Physics.RaycastAll(ray, 30f);

            int pieceHitIndex = -1;

            for ( int hitsTrav = 0; hitsTrav < hits.Length; hitsTrav++ ){
                if ( hits[hitsTrav].collider.transform.name.Contains("Piece") ){
                    pieceHitIndex = hitsTrav;
                    break;
                }
            }


            if (pieceHitIndex > -1)
            {
                _currentClickedPiece = hits[pieceHitIndex].collider.gameObject;
                _mouseDownPosition = Input.mousePosition;
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            if (_currentClickedPiece != null)
            {

#region "Piece Movement Work"

                //Check which direction this piece needs to be moved
                ESPMovementDirection CalcMovementDirection = ESPMovementDirection.None;
                Vector3 currentMousePosition = Input.mousePosition;

                int DetectionThreshold = 20;

                //Check for left right movement
                if (currentMousePosition.y > _mouseDownPosition.y - DetectionThreshold &&
                        currentMousePosition.y < _mouseDownPosition.y + DetectionThreshold)
                {
                    if (currentMousePosition.x < _mouseDownPosition.x)
                        CalcMovementDirection = ESPMovementDirection.Left;
                    else if (currentMousePosition.x > _mouseDownPosition.x)
                        CalcMovementDirection = ESPMovementDirection.Right;

                }
                //Check for top down movement
                else if(currentMousePosition.x > _mouseDownPosition.x - DetectionThreshold &&
                            currentMousePosition.x < _mouseDownPosition.x + DetectionThreshold)
                {
                    if (currentMousePosition.y < _mouseDownPosition.y)
                        CalcMovementDirection = ESPMovementDirection.Down;
                    else if (currentMousePosition.y > _mouseDownPosition.y)
                        CalcMovementDirection = ESPMovementDirection.Top;
                }


                //If that slot doesn`t have a piece already in it and this piece can move there?
                if (CalcMovementDirection != ESPMovementDirection.None)
                {

                    _currentPieceToMove = _currentClickedPiece;
                    _curPieceMovDirection = CalcMovementDirection;

                    int curPieceRow = 0;
                    int curPieceCol = 0;

                    PieceInstToArrIndex(_currentPieceToMove, out curPieceRow, out curPieceCol);

                    _pieceFromCol = curPieceCol;
                    _pieceFromRow = curPieceRow;


                    switch (_curPieceMovDirection)
                    {
                        case ESPMovementDirection.Left:
                            //Check for any piece present in left area
                            if (curPieceCol > 0)
                            {
                                if (_pieceInstances[curPieceRow][curPieceCol - 1] == null)
                                {
                                    goto MovementAllowed;
                                }
                            }
                            goto default;

                        case ESPMovementDirection.Right:
                            if (curPieceCol < TotalCols - 1)
                            {
                                if (_pieceInstances[curPieceRow][curPieceCol + 1] == null)
                                {
                                    goto MovementAllowed;
                                }
                            }
                            goto default;

                        case ESPMovementDirection.Top:
                            if (curPieceRow < TotalRows - 1)
                            {
                                if (_pieceInstances[curPieceRow + 1][curPieceCol] == null)
                                {
                                    goto MovementAllowed;
                                }
                            }
                            goto default;

                        case ESPMovementDirection.Down:
                            if (curPieceRow > 0)
                            {
                                if (_pieceInstances[curPieceRow - 1][curPieceCol] == null)
                                {
                                    goto MovementAllowed;
                                }
                            }
                            goto default;


                        MovementAllowed:
                            Debug.Log("movement allowed for direction " + _curPieceMovDirection);
                            break;

                        default:
                            //Cancel movement
                            _currentPieceToMove = null;
                            _curPieceMovDirection = ESPMovementDirection.None;

                            _pieceFromRow = -1;
                            _pieceFromCol = -1;
                            break;
                    }

                }

#endregion

                _mouseDownPosition = Vector3.zero;
                _currentClickedPiece = null;

            }
        }


#region "Piece Movement Animation Work"

        if (_currentPieceToMove != null)
        {
            switch (_curPieceMovDirection)
            {
                case ESPMovementDirection.Left:
                    if (SlidePieceLeft(_currentPieceToMove, _pieceFromRow, _pieceFromCol))
                        goto default;
                    break;

                case ESPMovementDirection.Right:
                    if (SlidePieceRight(_currentPieceToMove, _pieceFromRow, _pieceFromCol))
                        goto default;
                    break;

                case ESPMovementDirection.Top:
                    if (SlidePieceUp(_currentPieceToMove, _pieceFromRow, _pieceFromCol))
                        goto default;
                    break;

                case ESPMovementDirection.Down:
                    if (SlidePieceDown(_currentPieceToMove, _pieceFromRow, _pieceFromCol))
                        goto default;
                    break;


                default:
                    //Reset for next animation
                    _currentPieceToMove = null;
                    _curPieceMovDirection = ESPMovementDirection.None;

                    _pieceFromRow = -1;
                    _pieceFromCol = -1;

                    OnPieceMovementComplete();
                    PlaySFXSound(PieceSlide);

                    //Check if puzzle is solved
                    if (IsPuzzleSolved())
                    {
                        OnPuzzleSolved();
                        PlaySFXSound(PuzzleCompletionSound);
                    }
                    
                    
                    break;
            }
        }

#endregion


    }
Beispiel #2
0
    void Update()
    {
        if (Input.GetMouseButtonDown(0) && _currentPieceToMove == null)
        {
            Ray          ray  = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit[] hits = Physics.RaycastAll(ray, 30f);

            int pieceHitIndex = -1;

            for (int hitsTrav = 0; hitsTrav < hits.Length; hitsTrav++)
            {
                if (hits[hitsTrav].collider.transform.name.Contains("Piece"))
                {
                    pieceHitIndex = hitsTrav;
                    break;
                }
            }


            if (pieceHitIndex > -1)
            {
                _currentClickedPiece = hits[pieceHitIndex].collider.gameObject;
                _mouseDownPosition   = Input.mousePosition;
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            if (_currentClickedPiece != null)
            {
                #region "Piece Movement Work"

                //Check which direction this piece needs to be moved
                ESPMovementDirection CalcMovementDirection = ESPMovementDirection.None;
                Vector3 currentMousePosition = Input.mousePosition;

                int DetectionThreshold = 20;

                //Check for left right movement
                if (currentMousePosition.y > _mouseDownPosition.y - DetectionThreshold &&
                    currentMousePosition.y < _mouseDownPosition.y + DetectionThreshold)
                {
                    if (currentMousePosition.x < _mouseDownPosition.x)
                    {
                        CalcMovementDirection = ESPMovementDirection.Left;
                    }
                    else if (currentMousePosition.x > _mouseDownPosition.x)
                    {
                        CalcMovementDirection = ESPMovementDirection.Right;
                    }
                }
                //Check for top down movement
                else if (currentMousePosition.x > _mouseDownPosition.x - DetectionThreshold &&
                         currentMousePosition.x < _mouseDownPosition.x + DetectionThreshold)
                {
                    if (currentMousePosition.y < _mouseDownPosition.y)
                    {
                        CalcMovementDirection = ESPMovementDirection.Down;
                    }
                    else if (currentMousePosition.y > _mouseDownPosition.y)
                    {
                        CalcMovementDirection = ESPMovementDirection.Top;
                    }
                }


                //If that slot doesn`t have a piece already in it and this piece can move there?
                if (CalcMovementDirection != ESPMovementDirection.None)
                {
                    _currentPieceToMove   = _currentClickedPiece;
                    _curPieceMovDirection = CalcMovementDirection;

                    int curPieceRow = 0;
                    int curPieceCol = 0;

                    PieceInstToArrIndex(_currentPieceToMove, out curPieceRow, out curPieceCol);

                    _pieceFromCol = curPieceCol;
                    _pieceFromRow = curPieceRow;


                    switch (_curPieceMovDirection)
                    {
                    case ESPMovementDirection.Left:
                        //Check for any piece present in left area
                        if (curPieceCol > 0)
                        {
                            if (_pieceInstances[curPieceRow][curPieceCol - 1] == null)
                            {
                                goto MovementAllowed;
                            }
                        }
                        goto default;

                    case ESPMovementDirection.Right:
                        if (curPieceCol < TotalCols - 1)
                        {
                            if (_pieceInstances[curPieceRow][curPieceCol + 1] == null)
                            {
                                goto MovementAllowed;
                            }
                        }
                        goto default;

                    case ESPMovementDirection.Top:
                        if (curPieceRow < TotalRows - 1)
                        {
                            if (_pieceInstances[curPieceRow + 1][curPieceCol] == null)
                            {
                                goto MovementAllowed;
                            }
                        }
                        goto default;

                    case ESPMovementDirection.Down:
                        if (curPieceRow > 0)
                        {
                            if (_pieceInstances[curPieceRow - 1][curPieceCol] == null)
                            {
                                goto MovementAllowed;
                            }
                        }
                        goto default;


MovementAllowed:
                        Debug.Log("movement allowed for direction " + _curPieceMovDirection);
                        break;

                    default:
                        //Cancel movement
                        _currentPieceToMove   = null;
                        _curPieceMovDirection = ESPMovementDirection.None;

                        _pieceFromRow = -1;
                        _pieceFromCol = -1;
                        break;
                    }
                }

                #endregion

                _mouseDownPosition   = Vector3.zero;
                _currentClickedPiece = null;
            }
        }


        #region "Piece Movement Animation Work"

        if (_currentPieceToMove != null)
        {
            switch (_curPieceMovDirection)
            {
            case ESPMovementDirection.Left:
                if (SlidePieceLeft(_currentPieceToMove, _pieceFromRow, _pieceFromCol))
                {
                    goto default;
                }
                break;

            case ESPMovementDirection.Right:
                if (SlidePieceRight(_currentPieceToMove, _pieceFromRow, _pieceFromCol))
                {
                    goto default;
                }
                break;

            case ESPMovementDirection.Top:
                if (SlidePieceUp(_currentPieceToMove, _pieceFromRow, _pieceFromCol))
                {
                    goto default;
                }
                break;

            case ESPMovementDirection.Down:
                if (SlidePieceDown(_currentPieceToMove, _pieceFromRow, _pieceFromCol))
                {
                    goto default;
                }
                break;


            default:
                //Reset for next animation
                _currentPieceToMove   = null;
                _curPieceMovDirection = ESPMovementDirection.None;

                _pieceFromRow = -1;
                _pieceFromCol = -1;

                OnPieceMovementComplete.Invoke();
                E_OnPieceMovementComplete();
                _audioManager.PlaySFXSound(PieceSlide);

                //Check if puzzle is solved
                if (IsPuzzleSolved())
                {
                    OnPuzzleSolved.Invoke();
                    E_OnPuzzleSolved();
                    _audioManager.PlaySFXSound(PuzzleCompletionSound);
                }


                break;
            }
        }

        #endregion
    }