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

public class BumpDetector : MonoBehaviour
{
    public AudioClip soundBounce;

    private AudioSource source;
    
    public UnityEvent onCollision;
	
	[Range(5, 40)]
	public float noise;

    void Start()
    {
        gameObject.AddComponent<AudioSource>();
        source = GetComponent<AudioSource>();
		source.clip = soundBounce;
    }

    void OnCollisionEnter2D (Collision2D col)
    {
        if (col.collider.CompareTag(Tags.player))
        {
			if(transform.parent != null && transform.parent.GetComponent<TurningVisible>() != null)
                transform.parent.GetComponent<TurningVisible>().SetVisiblity(1);

            Wave.createWave(col.transform.position, col.transform.position - transform.position, 270, 15, noise < 1 ? 20 : noise, false);

            source.volume = Random.Range(0.5f, 1f);
            source.PlayOneShot(soundBounce);

            if (onCollision != null) onCollision.Invoke();
        }
	}
}