using UnityEngine;

public class BushBehavior : MonoBehaviour
{
    [SerializeField] Transform core;
    [SerializeField] float radius = 0.1f;
    [SerializeField] Animator animator;

    static readonly int TouchHash = Animator.StringToHash("Touch");

    SlimeWalk[] slimes;
    bool[] wasInside;

    void Awake()
    {
        if (core == null) core = transform;
        if (animator == null) animator = GetComponent<Animator>();
    }

    void Start()
    {
        slimes = Object.FindObjectsByType<SlimeWalk>(FindObjectsSortMode.None);
        wasInside = new bool[slimes.Length];
    }

    void Update()
    {
        if (animator == null || core == null || slimes == null) return;

        Vector2 c = core.position;
        float r2 = radius * radius;

        for (int i = 0; i < slimes.Length; i++)
        {
            if (slimes[i] == null) continue;
            Vector2 p = slimes[i].transform.position;
            bool inside = (p - c).sqrMagnitude <= r2;
            if (inside && !wasInside[i])
            {
                animator.SetTrigger(TouchHash);
            }
            wasInside[i] = inside;
        }
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.yellow;
        Vector3 c = core != null ? core.position : transform.position;
        Gizmos.DrawWireSphere(c, radius);
    }
}
