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

public class ExplosionController : MonoBehaviour
{
    //public Sprite Explosion;

    private float Lifetime = 1f;

    //private float MinScale = .25f;
    private float MaxScale = .7f;
    
    private float MaxRotation = 45f;
    private float MinRotation = 15f;

    private float StartLifetime;

    // Start is called before the first frame update
    void Start()
    {
        StartLifetime = Time.time;
        this.transform.Rotate(Vector3.forward, Random.Range(0, 359));
        this.transform.localScale = new Vector3(0f, 0f, 1f);
    }

    private void FixedUpdate()
    {
        this.transform.Rotate(Vector3.forward, 1);

        if (Time.time - StartLifetime > Lifetime)
        {
            Destroy(this.gameObject);
        }

        float duration = Time.time - StartLifetime;
        if (duration == 0) { duration = .001f; }
        float scaleFactor = duration / Lifetime * MaxScale;
        float alphaFactor = 1 / scaleFactor;
        this.transform.localScale = new Vector3(scaleFactor, scaleFactor, 1);
        SpriteRenderer sr = this.GetComponent<SpriteRenderer>();
        Color c = sr.color;
        c.a = alphaFactor;
        sr.color = c;
    }
}
