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

public class ImpactDestruction : MonoBehaviour
{
    public float impactRadius = 1.0f;
//    public Camera globalCoroutineObject; // WTF UGYL HACK

    void Start()
    {
        StartCoroutine(DeleteMyself(gameObject));
//        Destroy(gameObject, 3.0f);
    }

    void OnTriggerEnter2D(Collider2D other)
    {
//        Debug.Log("Weapon colliding with " + other.name);

        if (other.sharedMaterial && other.sharedMaterial.name == "Player")
        {
            Debug.Log("Giving damage to player " + other.name);
//            PlayerController killer = other.GetComponent<PlayerController>();
            PlayerController killer = null; // TODO
            other.GetComponent<PlayerHealth>().TakeDamage(100.0f, killer);
            AudioManager.Play(AudioManager.Sounds.HUMAN_HIT_1);
        }

        var circleShape = new Pix.Shapes.Circle(impactRadius, transform.position, 0);
        circleShape.Change(other.transform.gameObject);

        /*        if (!RemoveTerrainBack)
                    circleShape.ChangeChildren(go => !go.name.EndsWith(".Back"), gameObject);
                else
        */


        circleShape.ChangeChildren(gameObject);

        Debug.Log("Killing impact");
//        GetComponent<CircleCollider2D>().enabled = false;

//        Destroy(gameObject, 3.0f);
//		if(globalCoroutineObject)
//			globalCoroutineObject.StartCoroutine(DeleteMyself(gameObject));
//		else
//        	StartCoroutine(DeleteMyself(gameObject));
    }

    IEnumerator DeleteMyself(GameObject owner)
    {
        yield return new WaitForEndOfFrame();
        yield return new WaitForEndOfFrame();
        yield return new WaitForEndOfFrame();
        GetComponent<CircleCollider2D>().enabled = false;
    }
}
