using UnityEngine;
using System.Collections;
using Framework.Entities;
using Framework.Utility;
using Framework.Extensions;

public class ExplosionBehaviour : CoreBehaviour
{
    public UnityTimer timer = new UnityTimer(0.5f, false);

    public GameObject creator;
    private MeshRenderer renderer;
    private Color startColour;

    protected override void InnerAwake()
    {
        this.renderer = GetComponent<MeshRenderer>();
        this.startColour = this.renderer.material.color;
    }

    protected override void InnerStart()
    {
        Collider[] hitColliders = Physics.OverlapSphere(this.transform.position, this.transform.localScale.x/2f);
        for (int i = 0; i < hitColliders.Length; i++)
        {
            if (hitColliders[i].gameObject != this.creator)
            {
                hitColliders[i].gameObject.Interact(new Health.HurtAction(25));
            }
        }
    }

    protected override void InnerUpdate()
    {
        this.timer.Advance();
        this.renderer.material.color = new Color(this.startColour.r, this.startColour.g, this.startColour.b, (1 - (this.timer.GetCurrent() / this.timer.GetEnd())));
        if (this.timer.IsComplete())
        {
            Destroy(this.gameObject);
        }
    }
}
