Exemplo n.º 1
0
    /// <summary>
    /// Check if the player is available to move
    /// </summary>
    /// <returns>true if yes, false if not</returns>
    bool CanMove()
    {
        Vector3 direction = Vector3.zero;

        switch (movingDirection)
        {
        case 1:
            direction = Vector3.up;
            break;

        case 2:
            direction = Vector3.right;
            break;

        case 3:
            direction = Vector3.down;
            break;

        case 4:
            direction = Vector3.left;
            break;
        }

        RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, 1.28f, 1);

        if (hit)
        {
            Debug.DrawLine(new Vector3(transform.position.x, transform.position.y, 0), hit.point, Color.red);
            if (hit.transform.tag == wallsTag)//If raycast hit something and it's a wall
            {
                return(false);
            }
            else
            if (hit.transform.tag == cratesTag)   //if hit a crate, we now verify if that crate can move
            {
                Crate theCrate = hit.transform.GetComponent <Crate>();
                if (!theCrate.CanMove(direction))
                {
                    return(false);
                }
                else
                {
                    theCrate.Push(movingDirection, speed);
                }
            }
        }
        return(true);
    }