using UnityEngine;

namespace RebootKit.Engine.Services.Simulation.Sensors {
    public class RaycastSensor : ISensor {
        public Ray Ray;
        public LayerMask LayerMask;
        public float MaxDistance;

        public bool HasHit { get; private set; }
        public RaycastHit Hit { get; private set; }

        public void Clear() {
            HasHit = false;
            Hit = default;
        }

        public GameObject Sense() {
            HasHit = Physics.Raycast(Ray, out RaycastHit hit, MaxDistance, LayerMask);

            if (HasHit) {
                Hit = hit;
                return Hit.transform.gameObject;
            }

            Hit = default;
            return null;
        }
    }
}