using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchingDirection : MonoBehaviour
{
    public ContactFilter2D castFilter;
    public float groundDistance = 0.05f;
    public float wallDistance = 0.2f;
    public float ceilingDistance = 0.05f;

    CapsuleCollider2D touchingColl;

    RaycastHit2D[] groundHits = new RaycastHit2D[5];
    RaycastHit2D[] wallHits = new RaycastHit2D[5];
    RaycastHit2D[] ceilingHits = new RaycastHit2D[5];

    private Vector2 wallCheckDirection => gameObject.transform.localScale.x > 0 ? Vector2.right : Vector2.left;

    [SerializeField]
    private bool _isGrounded;

    public bool IsGrounded {
        get { 
            return _isGrounded;
        } private set {
            _isGrounded = value;
        }
    }

    [SerializeField]
    private bool _isOnwall;

    public bool IsOnWall
    {
        get
        {
            return _isOnwall;
        }
        private set
        {
            _isOnwall = value;
        }
    }

    [SerializeField]
    private bool _isOnCeiling;    

    public bool IsOnCeiling
    {
        get
        {
            return _isOnCeiling;
        }
        private set
        {
            _isOnCeiling = value;
        }
    }

    private void Awake()
    {
        touchingColl = GetComponent<CapsuleCollider2D>();
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        IsGrounded = touchingColl.Cast(Vector2.down, castFilter, groundHits, groundDistance) > 0;
        IsOnWall = touchingColl.Cast(wallCheckDirection, castFilter, wallHits, wallDistance) > 0;
        IsOnCeiling = touchingColl.Cast(Vector2.up, castFilter, ceilingHits, ceilingDistance) > 0;
    }
}
