using System; using System.Collections; using UnityEngine; using UnityEngine.UI; public class BearIsAwake : MonoBehaviour { public Transform Penguin; public Image bearTimer; private float timer = 0f; private bool wakingUp = false; public BearAwareness bearAwareness; public PositionReset[] thingstoReset; private bool isWaiting = false; public AudioClip BG; public AudioClip Awake; private void Start() { BearAwareness.HasWokenUp += IsAwake; thingstoReset = FindObjectsOfType(); } private void Update() { if (wakingUp) { timer += Time.deltaTime; bearTimer.fillAmount = timer / bearAwareness.timeToWakeUp; } } public void IsAwake(float time) { wakingUp = true; StartCoroutine(SleepIsOver(time)); } IEnumerator SleepIsOver(float time) { yield return new WaitForSeconds(time); //Vector3 direction = Penguin.position - transform.position; //float distance = direction.magnitude; //direction.Normalize(); float elapsed = 0f; while (elapsed < 2f) { foreach (PositionReset things in thingstoReset) { if (things.GetComponent() != null) { Vector3 dir = things.transform.position - transform.position; float distance = dir.magnitude; if (Physics.Raycast(transform.position, dir.normalized, out RaycastHit hit, distance)) { Debug.Log($"Ray zu {things.name} trifft {hit.collider.name}"); if (hit.collider.name == "StandUpCollider") { Debug.Log("Your Dead!"); foreach (PositionReset objekts in thingstoReset) { objekts.Reset(); SoundEffectManager.Play("Fail"); } bearAwareness.awareness = 0; bearAwareness.awarenessSlider.value = 0; wakingUp = false; timer = 0f; bearTimer.fillAmount = 0; } else if(!isWaiting) { Debug.Log("What?"); StartCoroutine(DelayedAction()); bearAwareness.awareness = 25; bearAwareness.awarenessSlider.value = bearAwareness.awarenessSlider.maxValue /2; } } Debug.DrawLine(transform.position, things.transform.position, Color.red); } } elapsed += Time.deltaTime; } BearAwareness.isPaused = false; //if (Physics.Raycast(transform.position, direction, out RaycastHit hit, distance)) //{ // Debug.Log("Getroffen: " + hit.collider.name); // if (hit.collider.name == "StandUpCollider") // { // Debug.Log("Your Dead!"); // foreach (PositionReset things in thingstoReset) // { // things.Reset(); // } // // bearAwareness.awareness = 0; // bearAwareness.awarenessSlider.value = 0; // // wakingUp = false; // timer = 0f; // bearTimer.fillAmount = 0; // } // else if(!isWaiting) // { // Debug.Log("What?"); // StartCoroutine(DelayedAction()); // } // // //} Debug.DrawLine(transform.position, Penguin.position, Color.red, 10f); } private void OnTriggerEnter(Collider other) { BearAwareness.isPaused = true; SoundEffectManager.Play("Alert"); BackgroundManager.StopBG(); BackgroundManager.PlayBG(true,Awake); bearAwareness.awareness = 50; bearAwareness.awarenessSlider.value = bearAwareness.awarenessSlider.maxValue; IsAwake(bearAwareness.timeToWakeUp); } IEnumerator DelayedAction() { isWaiting = true; wakingUp = false; timer = 0f; yield return new WaitForSeconds(2f); bearTimer.fillAmount = 0; isWaiting = false; Debug.Log("Back to Sleep"); BackgroundManager.StopBG(); BackgroundManager.PlayBG(false,BG); } }