using UnityEngine; using TMPro; using System.Collections; public class DefibPadChecker : MonoBehaviour { [Header("Target Positions")] public Transform padTargetLeft; public Transform padTargetRight; public Transform leftControllerAnchor; public Transform rightControllerAnchor; [Header("Tolerantie")] public float tolerance = 0.05f; [Header("UI")] public GameObject defibUI; // Het canvas of UI-panel public TextMeshProUGUI defibText; // De daadwerkelijke tekst in de UI private bool readyToShock = false; private bool shockDelivered = false; private bool sessionActive = false; private Coroutine pulseRoutine; //haptic feedback private MainPhase mainPhase; void Awake() { mainPhase = GetComponentInParent(); } void OnEnable() { StartDefibSession(); if (defibUI != null) defibUI.SetActive(true); } void OnDisable() { if (defibUI != null) defibUI.SetActive(false); sessionActive = false; } public void StartDefibSession() { Debug.Log("Fase gestart: Defibrillatie voorbereiding"); sessionActive = true; shockDelivered = false; readyToShock = false; if (defibText != null) { defibText.text = "Oh no, he’s not going to make it, defibrillation is required! Grab both paddles and follow the haptic feedback. \nThe closer you get to the correct placement, the shorter the intervals between the pulses. Pulses stop once in position."; } else { Debug.LogWarning("defibText niet gekoppeld in Inspector!"); } if (pulseRoutine != null) { StopCoroutine(pulseRoutine); } pulseRoutine = StartCoroutine(PulseByDistance()); } void Update() { if (!sessionActive || shockDelivered) return; float distLeft = Vector3.Distance(leftControllerAnchor.position, padTargetLeft.position); float distRight = Vector3.Distance(rightControllerAnchor.position, padTargetRight.position); bool leftCorrect = distLeft < tolerance; bool rightCorrect = distRight < tolerance; readyToShock = leftCorrect && rightCorrect; if (readyToShock) { if (defibText != null) defibText.text = "Pads are correctly placed. Press both index finger triggers simultaneously to deliver the shock."; if (OVRInput.Get(OVRInput.RawButton.RIndexTrigger) && OVRInput.Get(OVRInput.RawButton.LIndexTrigger)) { DeliverShock(); } } } void DeliverShock() { shockDelivered = true; sessionActive = false; Debug.Log("Shock delivered!"); if (defibText != null) defibText.text = "Shock delivered!"; if (defibUI != null) defibUI.SetActive(false); if (pulseRoutine != null) { StopCoroutine(pulseRoutine); pulseRoutine = null; ArduinoManager.Instance.StopMotor(1); ArduinoManager.Instance.StopMotor(2); } ArduinoManager.Instance.SendPulseToMotor(1, 1.0f, 1000f); // motor 1: 1 sec, vol vermogen ArduinoManager.Instance.SendPulseToMotor(2, 1.0f, 1000f); // motor 2: idem if (mainPhase != null) { mainPhase.CompletePhase(); } else { Debug.LogWarning("MainPhase niet gevonden in hiërarchie."); } } IEnumerator PulseByDistance() { float maxDistance = 1.5f; float minInterval = 0.1f; float maxInterval = 1.2f; float intensity = 1.0f; float duration = 6.0f; float leftTimer = 0f; float rightTimer = 0f; while (sessionActive && !shockDelivered) { float distLeft = Vector3.Distance(leftControllerAnchor.position, padTargetLeft.position); float distRight = Vector3.Distance(rightControllerAnchor.position, padTargetRight.position); if (distLeft < tolerance && distRight < tolerance) { Debug.Log("Beide pads correct geplaatst — pulsen stoppen."); yield break; } // LINKERHAND → MOTOR 1 float leftT = Mathf.Clamp01(distLeft / maxDistance); float leftInterval = Mathf.Lerp(minInterval, maxInterval, leftT); leftTimer += Time.deltaTime; if (distLeft >= tolerance && leftTimer >= leftInterval) { ArduinoManager.Instance.SendPulseToMotor(1, intensity, duration); leftTimer = 0f; } // RECHTERHAND → MOTOR 2 float rightT = Mathf.Clamp01(distRight / maxDistance); float rightInterval = Mathf.Lerp(minInterval, maxInterval, rightT); rightTimer += Time.deltaTime; if (distRight >= tolerance && rightTimer >= rightInterval) { ArduinoManager.Instance.SendPulseToMotor(2, intensity, duration); rightTimer = 0f; } yield return null; // volgende frame } } }