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

public class BombScript : MonoBehaviour {

    public bool active = false;
    private float timeToExplode = .25f;
    public float timer = 0.0f;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (active)
        {
            if(timer > timeToExplode)
                Explode();
        }
    }

    private void Explode()
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 2f);
        foreach(Collider2D col in colliders)
        {
            if (col.transform == transform) continue;
            if(col.tag == "player")
            {
                col.GetComponent<PlayerController>().Hurt();
            }
            if (col.tag == "destroyable")
            {
                Destroy(col.gameObject);
            }
            if (col.GetComponent<Rigidbody2D>())
                col.GetComponent<Rigidbody2D>().AddForce((col.transform.position - transform.position).normalized * 10f, ForceMode2D.Impulse);
        }
        Destroy(gameObject);
    }

    private void Update()
    {
        if(active)
            if (timer <= timeToExplode) timer += Time.deltaTime;
    }
}
