using UnityEngine;
using System.Collections;

public class DebrisComponent : MonoBehaviour 
{
    public Vector2 MinMax_Speed;
    public Vector2 MinMax_Rotation;
    float fSpeed;
    float fRotSpeed;

    public float MaxLifetime;
    float fLifeTime;
    
    Vector3 vMoveDirection;

    SpriteRenderer mySpriteRenderer;

    void Awake()
    {
        mySpriteRenderer = GetComponent<SpriteRenderer>();
    }

	// Use this for initialization
	void Start ()
    {
	
	}
	
	// Update is called once per frame
	void Update ()
    {
	    fLifeTime += Time.deltaTime;

        float fRemainTime = MaxLifetime- fLifeTime;
        if( fRemainTime < 0.5f)
        {
            Color fadeColor = new Color(1,1,1, fRemainTime / 0.5f);
            mySpriteRenderer.color = fadeColor;
        }

        transform.position += vMoveDirection * fSpeed * Time.deltaTime;

        //rotate 
        Vector3 vRot = transform.rotation.eulerAngles;
        vRot.z += fRotSpeed * Time.deltaTime;
        transform.rotation = Quaternion.Euler(vRot);

        if( fLifeTime >= MaxLifetime)
            Destroy(gameObject);


	}

    public void Launch()
    {
        //direction!
        vMoveDirection = Random.onUnitSphere;
        vMoveDirection.z = 0;
        vMoveDirection.Normalize();

        fSpeed = Random.Range(MinMax_Speed.x, MinMax_Speed.y);
        fRotSpeed = Random.Range(MinMax_Rotation.x, MinMax_Rotation.y);

    }
}
