using UnityEngine;
using System.Collections;

public class Pulsar : MonoBehaviour
{
    Renderer rend;
    LineRenderer line;

    Color baseColor;
    Color mixColor;
    Color currentColor;

    public float x, y;
    public float xRadius;
    public float yRadius;
    public int segments;

    float timer;
    bool outwardInward = false;

    void Start()
    {
        rend = gameObject.GetComponentInChildren<Renderer>();
        rend.material.color = new Color(255,255, 255, 255);

        line = gameObject.GetComponent<LineRenderer>();
        line.useWorldSpace = false;
        line.SetVertexCount(segments + 1);
        line.SetWidth(.1f, .1f);
        CreatePoints(segments, x, y, xRadius, yRadius);
        //line.enabled = false;

        timer = 0f;
    }

    public float Frac(float value)
    {
        return value - (int)value;
    }

    void Update()
    {
        timer = outwardInward ? (timer + 1) + Time.deltaTime * 0.5f : (timer + 1) - Time.deltaTime * 0.5f;
        
        float newSize = ((Frac(timer)) * 3f);

        gameObject.transform.localScale = new Vector3 (newSize, newSize, newSize);
        line.SetWidth(newSize, newSize);
        
        //colorTimer = Frac(colorTimer += Time.deltaTime);
        gameObject.renderer.material.color = new Color(255, 255, 255, Mathf.Sin(newSize/2.5f));
    }

    void CreatePoints(int segments, float x, float y, float xRad, float yRad)
    {
        float xx;
        float yy;
        float z = 0f;

        float angle = 20f;

        for (int i = 0; i < (segments + 1); i++)
        {
            xx = Mathf.Sin(Mathf.Deg2Rad * angle) * xRad + x;
            yy = Mathf.Cos(Mathf.Deg2Rad * angle) * yRad + y;

            line.SetPosition(i, new Vector3(xx, yy, z));

            angle += (360f / segments);
        }
    }

    public void setOutward(bool set)
    {
        outwardInward = set;
    }
}
