using UnityEngine;

public class RainControl : MonoBehaviour
{

    public float[] RainSpeedRange;
    public float[] RainEmissionRange;

    public AnimationCurve RainSpeedGraph;
    public AnimationCurve RainEmissionGraph;
    public AnimationCurve RainLifeTimeGraph;
    public float SizeIntervalLength;
    public AnimationCurve RainSizeRangeGraph;

    ParticleSystem RainPrtclSystem;
    GameScript GameScript;

    float[] RainRandomRange = new float[2];

    float RainSpeed;
    float RainEmissionRate;
    float RainStartLifetime;
    float RainStartSize;

    void Start()
    {

        RainPrtclSystem = this.GetComponent<ParticleSystem>();
        GameScript = GameObject.Find("MainGameObject").GetComponent<GameScript>();
    }
	
	void Update()
    {

        var currentSuccess = GameScript.GetSuccessRate() * GameScript.GetTotalProgress();

        RainRandomRange[0] = RainSizeRangeGraph.Evaluate(currentSuccess - SizeIntervalLength / 2f);
        RainRandomRange[1] = RainSizeRangeGraph.Evaluate(currentSuccess + SizeIntervalLength / 2f);

        RainSpeed = Mathf.Lerp(RainSpeedRange[0], RainSpeedRange[1], RainSpeedGraph.Evaluate(currentSuccess));
        RainEmissionRate = Mathf.Lerp(RainEmissionRange[0], RainEmissionRange[1], RainEmissionGraph.Evaluate(currentSuccess));
        RainStartLifetime = RainLifeTimeGraph.Evaluate(currentSuccess);
        RainStartSize = RainRandomRange[0] + Random.value * (RainRandomRange[1] - RainRandomRange[0]);

        RainPrtclSystem.startSpeed = RainSpeed;
        RainPrtclSystem.emissionRate = RainEmissionRate;
        RainPrtclSystem.startLifetime = RainStartLifetime;
        RainPrtclSystem.startSize = RainStartSize;

    }

}