using UnityEngine; using TMPro; using System.Collections; using System.IO.Ports; public class CompressionDetector : MonoBehaviour { public Transform leftHand; public Transform rightHand; public Transform CompressionTarget; // Referentiepunt op de borst public float requiredDepth = 0.06f; // 6 cm compressie public float minTimeBetween = 0.3f; // Minstens 0.3s tussen compressies public int requiredCompressions = 30; public Collider compressionZone; [Header("UI")] public TextMeshProUGUI compressionCounterText; public GameObject compressionUI; // Canvas of panel met teller [Header("UI - Extra Feedback")] public GameObject extraUI; // 👈 Nieuw public TextMeshProUGUI extraUIText; private MainPhase mainPhase; private float lastCompressionTime = 0f; private int compressionCount = 0; private bool sessionActive = false; private bool wereHandsBelowLastFrame = false; // Edge detectie void Awake() { mainPhase = GetComponentInParent(); } void OnEnable() { extraUIText.text = "This person needs CPR! Start by overlapping your hands and pressing down in the center of the chest about 6 cm deep, following the rhythm of the haptic feedback. Watch the counter."; if (compressionUI != null) compressionUI.SetActive(true); if (extraUI != null) extraUI.SetActive(true); StartCompressionSession(); } void OnDisable() { if (compressionUI != null) compressionUI.SetActive(false); if (extraUI != null) extraUI.SetActive(false); sessionActive = false; } void Update() { if (!sessionActive) return; float leftDepth = CompressionTarget.position.y - leftHand.position.y; float rightDepth = CompressionTarget.position.y - rightHand.position.y; bool eitherHandBelow = leftDepth >= requiredDepth || rightDepth >= requiredDepth; if (eitherHandBelow && !wereHandsBelowLastFrame && Time.time - lastCompressionTime > minTimeBetween && HandsInsideZone()) { lastCompressionTime = Time.time; compressionCount++; UpdateUI(); Debug.Log($"Compressie {compressionCount}/{requiredCompressions}"); if (compressionCount >= requiredCompressions) { EndCompressionSession(); } } wereHandsBelowLastFrame = eitherHandBelow; } public void StartCompressionSession() { Debug.Log("Fase gestart: start compressies (30x 6cm)"); sessionActive = true; compressionCount = 0; lastCompressionTime = 0f; wereHandsBelowLastFrame = false; ArduinoManager.Instance.StartConstantPulseToMotor(1, 588); //motor 1 op 104 bpm ArduinoManager.Instance.StartConstantPulseToMotor(2, 588); UpdateUI(); } void EndCompressionSession() { sessionActive = false; Debug.Log("30 compressies voltooid. Fase klaar."); ArduinoManager.Instance.StopMotor(1); //stop motors ArduinoManager.Instance.StopMotor(2); if (compressionUI != null) compressionUI.SetActive(false); if (mainPhase != null) { mainPhase.CompletePhase(); } else { Debug.LogWarning("MainPhase niet gevonden in hiërarchie."); } } void UpdateUI() { if (compressionCounterText != null) { compressionCounterText.text = $"Compressions: {compressionCount} / {requiredCompressions}"; } } bool HandsInsideZone() { return compressionZone.bounds.Contains(leftHand.position) || compressionZone.bounds.Contains(rightHand.position); } }