using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using TMPro; using UnityEngine.Rendering; using UnityEngine.Rendering.Universal; public class goalTimer : MonoBehaviour { public float delay = 30; public bool goal = true; private bool done = false; private int result; [Header("timer")] public TextMeshProUGUI timerText; private float currentTime; public string titleString; public GameObject titleCanvas; public GameObject options; public GameObject gameStateManagerObject; public AudioClip hittingCar; public AudioClip hittingWall; public AudioClip good; public AudioClip ok; public AudioClip timeout; public GameObject backgroundSoundObject; private AudioSource backgroundAudio; public GameObject debug; public bool blurEnd = true; public float maxBlurIntensity = 1.0f; // Maximum blur intensity public float blurDuration = 2.0f; // Time to increase blur intensity (in seconds) private Bloom bloom; // Depth of Field effect (used for blur) private bool isBlurActive = false; public Volume postProcessingVolume; void Start() { currentTime = delay; result = 0; } private void Update() { } //public async Task startLevel() public IEnumerator StartLevel(Action callback) { currentTime = delay; done = false; titleCanvas.transform.Find("title").GetComponentInChildren().text = "" + titleString + ""; backgroundSoundObject.SetActive(true); backgroundAudio = backgroundSoundObject.GetComponentInChildren(); backgroundAudio.time = UnityEngine.Random.Range(0f, 420f); backgroundAudio.Play(); while (!done) { currentTime -= Time.deltaTime; setTimerText(); if (currentTime < 0) { SendAudio(timeout); if(options != null){ options.GetComponent().timeOut(); } Debug.Log("done true!"); done = true; } yield return null; } TriggerEnd(callback); yield return null; //end! } public void SendAudio(AudioClip audioclip){ gameStateManager gamestatemanager = gameStateManagerObject.GetComponent(); gamestatemanager.NextLoadlingAudio(audioclip); } public void TriggerEnd(Action callback) { backgroundAudio.time = 0f; backgroundSoundObject.SetActive(false); // Ensure the post-processing volume has the DepthOfField effect if (postProcessingVolume.profile.TryGet(out var bloomEffect)) { bloom = bloomEffect; bloom.active = false; // Initially disable the blur } else { Debug.LogError("DepthOfField effect is not set up in the Post-Processing Volume!"); } if (bloom != null && !isBlurActive && blurEnd) { Debug.Log("activate:"); StartCoroutine(ActivateBlurEnd(callback)); } else{ Debug.Log("no blur – just end!"); callback?.Invoke(result); } } private IEnumerator ActivateBlurEnd(Action callback) { Debug.Log("ActivateBlur entry!"); isBlurActive = true; // Activate the blur effect bloom.active = true; // Gradually increase the blur intensity float elapsedTime = 0f; while (elapsedTime < blurDuration) { Debug.Log("blurring!"); elapsedTime += Time.deltaTime; float intensity = Mathf.Lerp(0, maxBlurIntensity, elapsedTime / blurDuration); bloom.intensity.Override(intensity); // Adjust the blur's starting distance yield return null; } // After 2 seconds, suddenly deactivate the blur bloom.active = false; isBlurActive = false; callback?.Invoke(result); yield return null; } private void setTimerText() { timerText.text = currentTime.ToString("0"); } public void OnHitObject(){ Debug.Log("hit target!"); SendAudio(hittingCar); result += -10; done = true; } public void OnOutOfBounds(){ Debug.Log("hit target!"); SendAudio(hittingWall); result += -5; done = true; } IEnumerator OnTriggerEnter(Collider collision) { if (goal) { if (collision.tag == "gamecollider") { Debug.Log("player enter!"); yield return new WaitForSeconds(0); Debug.Log("go player!"); // SCORE SendAudio(good); result += (int)currentTime; done = true; } } } public void OptionSelection(bool correctOption) { Debug.Log("OptionSelection reached!!"); //debug.GetComponent().text = "" + "goal timer reached" + ""; if (correctOption) { result += (int)currentTime; } options.SetActive(false); done = true; } }