diff --git a/Assets/Scripts/HurdleManager.cs b/Assets/Scripts/HurdleManager.cs index 99f7e99..ef6a162 100644 --- a/Assets/Scripts/HurdleManager.cs +++ b/Assets/Scripts/HurdleManager.cs @@ -97,32 +97,38 @@ public class HurdleManager : MonoBehaviour } } + void CountTaps() + { + if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)) + { + currentTapCount++; + Debug.Log($"Tap {currentTapCount}/{requiredTaps}"); + } + } + void CheckForJump() { - if (currentHurdle == null) return; + if (currentHurdle == null || playerRunner.IsJumping()) return; float remainingDistance = GetRemainingDistance(playerRunner.transform.position, currentHurdle.position); - // When very close to hurdle - if (remainingDistance < 1f && challengeCompleted) + // When very close to hurdle and challenge is completed + if (remainingDistance < 5f && challengeCompleted) { - // Trigger jump - TriggerJump(); - - // Remove hurdle and end challenge after jump - hurdles.Remove(currentHurdle); - if (activeChallenge != null) - StopCoroutine(activeChallenge); - EndChallengeUI(); + // Trigger jump in the PlayerPathRunner + playerRunner.TriggerJump(() => { + // This callback runs after jump completes + if (currentHurdle != null) + { + hurdles.Remove(currentHurdle); + if (activeChallenge != null) + StopCoroutine(activeChallenge); + EndChallengeUI(); + } + }); } } - void TriggerJump() - { - Debug.Log("🦘 JUMP! Player successfully jumped over the hurdle!"); - GetComponentInChildren().SetTrigger("Jump"); - } - void EndChallengeUI() { challengeRunning = false; @@ -132,21 +138,12 @@ public class HurdleManager : MonoBehaviour activeChallenge = null; } - void CountTaps() - { - if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)) - { - currentTapCount++; - Debug.Log($"Tap {currentTapCount}/{requiredTaps}"); - } - } - void OnTriggerEnter(Collider other) { if (other.CompareTag("Hurdle")) { // Check if this is the current hurdle and challenge wasn't completed - if (other.transform == currentHurdle && !challengeCompleted) + if (other.transform == currentHurdle && !challengeCompleted && !playerRunner.IsJumping()) { Debug.Log("❌ GAME OVER! Failed to complete tap challenge!"); GameOver(); @@ -173,7 +170,7 @@ public class HurdleManager : MonoBehaviour tapsRequiredText.text = "GAME OVER!"; Debug.Log("💀 GAME OVER - Player failed to clear hurdle!"); - GetComponentInChildren().enabled = false; + // You can add more game over logic here: // - Show game over screen // - Play game over sound @@ -240,6 +237,4 @@ public class HurdleManager : MonoBehaviour float t = Mathf.Clamp01(Vector3.Dot(point - a, ab) / Vector3.Dot(ab, ab)); return a + t * ab; } - - // Removed the old IsBetween method as it's no longer needed } \ No newline at end of file diff --git a/Assets/Scripts/PlayerPathRunner.cs b/Assets/Scripts/PlayerPathRunner.cs index a05bb1e..b74dc2e 100644 --- a/Assets/Scripts/PlayerPathRunner.cs +++ b/Assets/Scripts/PlayerPathRunner.cs @@ -16,6 +16,11 @@ public class PlayerPathRunner : MonoBehaviour public bool useTapToMove = true; public float tapWindowSeconds = 0.5f; + [Header("Jump Settings")] + public float jumpPower = 2f; + public float jumpDuration = 0.5f; + public float jumpDistance = 3f; // Distance in world units + [Header("Path Setup")] public DOTweenPath pathSource; @@ -27,6 +32,8 @@ public class PlayerPathRunner : MonoBehaviour private float pathPosition = 0f; private Vector3[] drawPoints; private Path bakedPath; + private bool isJumping = false; + private System.Action onJumpComplete; private void Awake() { @@ -64,6 +71,9 @@ public class PlayerPathRunner : MonoBehaviour void Update() { + // Don't process normal movement if jumping + if (isJumping) return; + if (useTapToMove) { HandleInput(); @@ -104,7 +114,12 @@ public class PlayerPathRunner : MonoBehaviour pathPosition += speed * Time.deltaTime; pathPosition %= 1f; - float floatIndex = pathPosition * (drawPoints.Length - 1); + UpdatePositionOnPath(pathPosition); + } + + void UpdatePositionOnPath(float normalizedPosition) + { + float floatIndex = normalizedPosition * (drawPoints.Length - 1); int iA = Mathf.FloorToInt(floatIndex); int iB = Mathf.Min(iA + 1, drawPoints.Length - 1); float t = floatIndex - iA; @@ -113,8 +128,67 @@ public class PlayerPathRunner : MonoBehaviour transform.position = pos; } + public void TriggerJump(System.Action onComplete = null) + { + if (isJumping) return; + + Debug.Log("🦘 Jump triggered in PlayerPathRunner!"); + isJumping = true; + onJumpComplete = onComplete; + + // Calculate how much to advance on the path based on world distance + float pathLength = CalculateTotalPathLength(); + float jumpPercentage = jumpDistance / pathLength; + + // Calculate end position on path + float targetPathPosition = pathPosition + jumpPercentage; + targetPathPosition %= 1f; // Wrap around if needed + + // Get world positions for start and end + Vector3 startPos = transform.position; + Vector3 endPos = GetWorldPositionAtPathPosition(targetPathPosition); + + // Do the jump + transform.DOJump(endPos, jumpPower, 1, jumpDuration) + .SetEase(Ease.OutQuad) + .OnComplete(() => { + // Update our path position to match where we jumped to + pathPosition = targetPathPosition; + isJumping = false; + + Debug.Log("Jump completed!"); + onJumpComplete?.Invoke(); + onJumpComplete = null; + }); + } + + float CalculateTotalPathLength() + { + float totalLength = 0f; + for (int i = 0; i < drawPoints.Length - 1; i++) + { + totalLength += Vector3.Distance(drawPoints[i], drawPoints[i + 1]); + } + return totalLength; + } + + Vector3 GetWorldPositionAtPathPosition(float normalizedPos) + { + float floatIndex = normalizedPos * (drawPoints.Length - 1); + int iA = Mathf.FloorToInt(floatIndex); + int iB = Mathf.Min(iA + 1, drawPoints.Length - 1); + float t = floatIndex - iA; + + return Vector3.Lerp(drawPoints[iA], drawPoints[iB], t); + } + public float GetCurrentSpeed() { return currentSpeed; } + + public bool IsJumping() + { + return isJumping; + } } \ No newline at end of file