Пример #1
0
    // Try to move using a target tile in the grid
    public virtual MovementResult TryMoveTo(GridTile targetGridTile, bool animate = true, bool andRotate = true, bool checkMovementCooldown = true)
    {
        // Set the current and target tile
        m_StartGridTile  = _gridObject.m_CurrentGridTile;
        m_TargetGridTile = targetGridTile;
        // Current position and position we'd like to move to
        Vector2Int fromGridPos = _gridObject.m_GridPosition;
        Vector2Int toGridPos   = targetGridTile.m_GridPosition;


        // Checks for the cooldown or if it is moving
        if (checkMovementCooldown && (m_IsMoving || Cooldown.InProgress))
        {
            return(MovementResult.Cooldown);
        }

        // Rotation
        if (andRotate)
        {
            // Try to rotate the object in the direction
            var rotated = TryRotateTo(toGridPos, animate);
            // Return the correct movementresult if we actually turned and triggered the cooldown
            if (rotated && _rotationCausesMovementCooldown && Cooldown.InProgress)
            {
                return(MovementResult.Turned);
            }
        }

        // Declare a new local variable to store wether the movement was succesful or not
        MovementResult movementResult = MovementResult.Moved;

        // Move if possible else return the failed result
        if (_gridObject.m_CurrentGridTile != null && targetGridTile.CanMoveToTile())
        {
            var moved = MoveTo(targetGridTile, animate);
            if (!moved)
            {
                movementResult = MovementResult.Failed;
            }
        }
        else
        {
            movementResult = MovementResult.Failed;
        }

        return(movementResult);
    }