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

public class IsPushingTester: MonoBehaviour
{
    private Animator animator;
    private Rigidbody2D rb;
    private bool isColliding;
    public AudioSource audioSource; // Reference to the AudioSource component
	public AudioClip pushSound; // The sound clip to be played
    public bool isPlaying = false; // Boolean to control the sound loop
    

    void Start()
    {
        animator = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
        isColliding = false;
    }

    void Update()
    {
        if (isColliding && rb.velocity.magnitude > 0.1f)
        {
            animator.SetBool("IsPushing", true);
             
                if(!isPlaying){
                audioSource.clip = pushSound;
                audioSource.loop = true;
                audioSource.Play();
                isPlaying = true;}
           
        }
        else
        {
	  
            animator.SetBool("IsPushing", false);
            audioSource.Stop();
            isPlaying = false;
	   
        }
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("PressurePlatable") && collision.collider is EdgeCollider2D)
        {
            isColliding = true;
        }
    }

    void OnCollisionExit2D(Collision2D collision)
    {
	if (collision.gameObject.CompareTag("PressurePlatable") && collision.collider is EdgeCollider2D)
        {
      
            isColliding = false;
        }
    }
}