using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Rendering.Universal;

public class Lichtsteuerung : MonoBehaviour
{
    private float startTime;
    private float endTime;
    private float startIntensity;
    private float endIntensity;
    private Light2D lightComponent;
    private bool fadingInProgress;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        lightComponent = GetComponent<Light2D>();
        startTime = 0f;
        endTime = 0f;
        startIntensity = lightComponent.intensity;
        endIntensity = lightComponent.intensity;
        fadingInProgress = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Time.time < endTime && Time.time >= startTime) {
            float factor = (Time.time - startTime) / (endTime - startTime);
            lightComponent.intensity = factor * endIntensity + (1f - factor) * startIntensity;
        } else if (fadingInProgress) {
            fadingInProgress = false;
            lightComponent.intensity = endIntensity;
        }
    }

    public void FadeToIntensity(float intensity, float duration) {
        startTime = Time.time;
        endTime = startTime + duration;
        startIntensity = lightComponent.intensity;
        endIntensity = intensity;
        fadingInProgress = true;
    }
}
