using UnityEngine;

public class Debris : MonoBehaviour
{
    protected Vector2 speed;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        speed = new Vector2(Random.Range(-0.05f, 0.05f), Random.Range(-0.05f, 0.05f)).normalized * 0.05f;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        transform.Translate(speed);
    }

    public void AnimationFinished()
    {
        Destroy(gameObject);
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Wall"))
        {
            ContactPoint2D contact = collision.GetContact(0);
            speed = Vector2.Reflect(speed, contact.normal);
        } 
        else
        {
            Physics2D.IgnoreCollision(
                gameObject.GetComponent<Collider2D>(), 
                collision.gameObject.GetComponent<Collider2D>(), 
                true
            );
        }
    }
}
