Пример #1
0
    // Smooth interpolation, this code must be called by blocks
    public bool MoveObject(Vector2Int origin, Vector2Int direction, float time, out Coroutine coroutine)
    {
        coroutine = null;
        // Check valid direction
        if (!(direction.magnitude == 1.0f && (Mathf.Abs(direction.x) == 1 || Mathf.Abs(direction.y) == 1)))
        {
            return(false);
        }

        // Check if origin object exists
        if (!IsInBoardBounds(origin))
        {
            return(false);
        }

        BoardObject bObject = board[origin.x, origin.y].GetPlacedObject();

        if (bObject == null)
        {
            return(false);                 // No object to move
        }
        // Check if direction in valid
        if (IsInBoardBounds(origin + direction))
        {
            // A valid direction
            BoardCell fromCell = board[origin.x, origin.y];
            BoardCell toCell   = board[origin.x + direction.x, origin.y + direction.y];

            if (toCell.GetPlacedObject() != null)
            {
                if (bObject.GetAnimator() != null)
                {
                    if (bObject.GetDirection() == BoardObject.Direction.LEFT ||
                        bObject.GetDirection() == BoardObject.Direction.RIGHT ||
                        bObject.GetDirection() == BoardObject.Direction.DOWN_RIGHT ||
                        bObject.GetDirection() == BoardObject.Direction.UP_LEFT)
                    {
                        if (direction.y != 0)
                        {
                            bObject.GetAnimator().Play("Collision");
                        }
                        else
                        {
                            bObject.GetAnimator().Play("Collision2");
                        }
                    }
                    else
                    {
                        if (direction.y != 0)
                        {
                            bObject.GetAnimator().Play("Collision2");
                        }
                        else
                        {
                            bObject.GetAnimator().Play("Collision");
                        }
                    }
                }
                return(false);
            }

            coroutine = StartCoroutine(InternalMoveObject(fromCell, toCell, time));
            return(true);
        }
        else
        {
            // Not a valid direction
            return(false);
        }
    }