using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; public class PlayerStressController : MonoBehaviour { public PlayerPickupController pickupController; private Animator animator; [Header("Stress Settings")] public float currentStress = 0f; public float maxStress = 100f; public bool isPanicking = false; public bool isParalyzed = false; // Define the event public static event Action OnPlayerPanic; public static event Action OnPlayerCalm; [Header("Panic Settings")] [SerializeField] private float paralyseDuration = 5f; void Start() { animator = GetComponent(); // Start the repeating loop StartCoroutine(StressCheckLoop()); } IEnumerator StressCheckLoop() { while (true) // Runs for the lifetime of the object { yield return ApplyStressLogic(); // Wait for exactly 1 second before running again yield return new WaitForSeconds(1.0f); } } IEnumerator ApplyStressLogic() { if (isParalyzed) yield break; // Find all objects tagged as "Enemy" List enemies = GameObject.FindGameObjectsWithTag("Interactable").Where(enemy => enemy.GetComponent() != null).ToList(); // Keep stress within 0 and Max currentStress += enemies.Count; // Increase stress based on number of enemies nearby if (pickupController.heldObject != null && pickupController.heldObject.TryGetComponent(out _)) { currentStress += 5; // Increase stress based on number of carried tricks } currentStress = Mathf.Clamp(currentStress, 0, maxStress); if (!isPanicking && currentStress >= maxStress) { Debug.Log($"{currentStress}, {isPanicking}, {isParalyzed}"); yield return HandlePanic(); } else if(isPanicking && currentStress < maxStress) { isPanicking = false; HandleCalm(); Debug.Log("Player has calmed down."); } } IEnumerator HandlePanic() { isPanicking = true; animator.SetBool("isCollapsed", true); Debug.Log("Player is freaking out man!"); // Add effects like screen shake or slowing down the player here OnPlayerPanic?.Invoke(); // Trigger the panic event yield return StartCoroutine(HandleParalyze()); } void HandleCalm() { Debug.Log("Player is feeling better now!"); OnPlayerCalm?.Invoke(); // Trigger the calm event } public void ReduceStress(float amount) { currentStress -= amount; currentStress = Mathf.Clamp(currentStress, 0, maxStress); Debug.Log($"Stress reduced by {amount}. Current Stress: {currentStress}"); } IEnumerator HandleParalyze() { isParalyzed = true; Debug.Log("Player is paralyzed."); yield return new WaitForSecondsRealtime(paralyseDuration); Debug.Log("Player has recovered from paralysis."); isParalyzed = false; } }