2025-09-21 07:32:56 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using System.Collections;
|
2025-09-24 13:31:15 +02:00
|
|
|
|
using AppleHills.Core.Settings;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
|
|
|
|
|
namespace Minigames.DivingForPictures
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Collision behavior that bumps the player toward the center of the trench.
|
|
|
|
|
|
/// Uses trigger-based collision detection with coroutine-based bump timing.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class TileBumpCollision : PlayerCollisionBehavior
|
|
|
|
|
|
{
|
|
|
|
|
|
private bool _isBumping;
|
|
|
|
|
|
private Coroutine _bumpCoroutine;
|
|
|
|
|
|
private bool _bumpInputBlocked; // Tracks bump-specific input blocking
|
|
|
|
|
|
|
|
|
|
|
|
protected override void HandleCollisionResponse(Collider2D obstacle)
|
|
|
|
|
|
{
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Use bump mode from developer settings
|
|
|
|
|
|
switch (_devSettings.BumpMode)
|
2025-09-21 07:32:56 +00:00
|
|
|
|
{
|
2025-09-24 13:31:15 +02:00
|
|
|
|
case 0: // Impulse mode
|
2025-09-21 07:32:56 +00:00
|
|
|
|
StartImpulseBump();
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
case 1: // SmoothToCenter mode
|
2025-09-21 07:32:56 +00:00
|
|
|
|
StartSmoothMoveToCenter();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
Debug.Log($"[TileBumpCollision] Collision handled with {(_devSettings.BumpMode == 0 ? "Impulse" : "SmoothToCenter")} mode");
|
2025-09-21 07:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-24 13:31:15 +02:00
|
|
|
|
/// Applies an impulse force toward center
|
2025-09-21 07:32:56 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void StartImpulseBump()
|
|
|
|
|
|
{
|
2025-09-24 13:31:15 +02:00
|
|
|
|
if (_isBumping || playerCharacter == null)
|
|
|
|
|
|
return;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
_isBumping = true;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Block input during bump if configured
|
|
|
|
|
|
if (_gameSettings.EndlessDescenderBlockInputDuringBump && playerController != null)
|
2025-09-21 07:32:56 +00:00
|
|
|
|
{
|
2025-09-24 13:31:15 +02:00
|
|
|
|
_bumpInputBlocked = true;
|
|
|
|
|
|
// TODO: Implement input blocking
|
2025-09-21 07:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Calculate direction to center (X = 0)
|
|
|
|
|
|
float directionToCenter = Mathf.Sign(-playerCharacter.transform.position.x);
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Start impulse bump coroutine
|
|
|
|
|
|
if (_bumpCoroutine != null)
|
|
|
|
|
|
StopCoroutine(_bumpCoroutine);
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
_bumpCoroutine = StartCoroutine(ImpulseBumpCoroutine(directionToCenter));
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
Debug.Log($"[TileBumpCollision] Started impulse bump with force {_gameSettings.EndlessDescenderBumpForce} in direction {directionToCenter}");
|
2025-09-21 07:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-24 13:31:15 +02:00
|
|
|
|
/// Smoothly moves the player to center
|
2025-09-21 07:32:56 +00:00
|
|
|
|
/// </summary>
|
2025-09-24 13:31:15 +02:00
|
|
|
|
private void StartSmoothMoveToCenter()
|
2025-09-21 07:32:56 +00:00
|
|
|
|
{
|
2025-09-24 13:31:15 +02:00
|
|
|
|
if (_isBumping || playerCharacter == null)
|
|
|
|
|
|
return;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
|
|
|
|
|
_isBumping = true;
|
|
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Block input during bump if configured
|
|
|
|
|
|
if (_gameSettings.EndlessDescenderBlockInputDuringBump && playerController != null)
|
2025-09-21 07:32:56 +00:00
|
|
|
|
{
|
|
|
|
|
|
_bumpInputBlocked = true;
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// TODO: Implement input blocking
|
2025-09-21 07:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Start smooth move coroutine
|
|
|
|
|
|
if (_bumpCoroutine != null)
|
|
|
|
|
|
StopCoroutine(_bumpCoroutine);
|
|
|
|
|
|
|
|
|
|
|
|
_bumpCoroutine = StartCoroutine(SmoothMoveToCenterCoroutine());
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log($"[TileBumpCollision] Started smooth move to center with speed {_gameSettings.EndlessDescenderSmoothMoveSpeed}");
|
2025-09-21 07:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-24 13:31:15 +02:00
|
|
|
|
/// Coroutine to handle impulse bump movement
|
2025-09-21 07:32:56 +00:00
|
|
|
|
/// </summary>
|
2025-09-24 13:31:15 +02:00
|
|
|
|
private IEnumerator ImpulseBumpCoroutine(float direction)
|
2025-09-21 07:32:56 +00:00
|
|
|
|
{
|
|
|
|
|
|
float elapsedTime = 0f;
|
2025-09-24 13:31:15 +02:00
|
|
|
|
float bumpDuration = 0.5f; // Fixed duration for impulse
|
|
|
|
|
|
Vector3 startPosition = playerCharacter.transform.position;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
while (elapsedTime < bumpDuration)
|
2025-09-21 07:32:56 +00:00
|
|
|
|
{
|
|
|
|
|
|
elapsedTime += Time.deltaTime;
|
|
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Use evaluation time from curve for non-linear movement
|
|
|
|
|
|
float t = elapsedTime / bumpDuration;
|
|
|
|
|
|
float curveValue = _devSettings.BumpCurve.Evaluate(t);
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Calculate movement distance based on force and curve
|
|
|
|
|
|
float distance = _gameSettings.EndlessDescenderBumpForce * curveValue * Time.deltaTime;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Move the player toward center
|
|
|
|
|
|
Vector3 newPosition = playerCharacter.transform.position;
|
|
|
|
|
|
newPosition.x += direction * distance;
|
|
|
|
|
|
|
|
|
|
|
|
// Clamp to valid range
|
|
|
|
|
|
newPosition.x = Mathf.Clamp(newPosition.x, _gameSettings.EndlessDescenderClampXMin, _gameSettings.EndlessDescenderClampXMax);
|
|
|
|
|
|
|
|
|
|
|
|
// Apply the position
|
|
|
|
|
|
playerCharacter.transform.position = newPosition;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
|
|
|
|
|
yield return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Finish bump
|
2025-09-21 07:32:56 +00:00
|
|
|
|
_isBumping = false;
|
2025-09-24 13:31:15 +02:00
|
|
|
|
_bumpInputBlocked = false;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
_bumpCoroutine = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-24 13:31:15 +02:00
|
|
|
|
/// Coroutine to handle smooth movement to center
|
2025-09-21 07:32:56 +00:00
|
|
|
|
/// </summary>
|
2025-09-24 13:31:15 +02:00
|
|
|
|
private IEnumerator SmoothMoveToCenterCoroutine()
|
2025-09-21 07:32:56 +00:00
|
|
|
|
{
|
2025-09-24 13:31:15 +02:00
|
|
|
|
Vector3 startPosition = playerCharacter.transform.position;
|
|
|
|
|
|
Vector3 targetPosition = new Vector3(0f, startPosition.y, startPosition.z);
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate distance to center and expected duration
|
|
|
|
|
|
float distanceToCenter = Mathf.Abs(startPosition.x);
|
|
|
|
|
|
float expectedDuration = distanceToCenter / _gameSettings.EndlessDescenderSmoothMoveSpeed;
|
|
|
|
|
|
float elapsedTime = 0f;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Move until we reach the center
|
|
|
|
|
|
while (elapsedTime < expectedDuration)
|
2025-09-21 07:32:56 +00:00
|
|
|
|
{
|
2025-09-24 13:31:15 +02:00
|
|
|
|
elapsedTime += Time.deltaTime;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Calculate progress based on time and curve
|
|
|
|
|
|
float t = elapsedTime / expectedDuration;
|
|
|
|
|
|
float curveValue = _devSettings.BumpCurve.Evaluate(t);
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Calculate interpolated position
|
|
|
|
|
|
Vector3 newPosition = Vector3.Lerp(startPosition, targetPosition, curveValue);
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
// Apply the position
|
|
|
|
|
|
playerCharacter.transform.position = newPosition;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
|
2025-09-24 13:31:15 +02:00
|
|
|
|
yield return null;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
}
|
2025-09-24 13:31:15 +02:00
|
|
|
|
|
|
|
|
|
|
// Ensure we end at exactly the center
|
|
|
|
|
|
playerCharacter.transform.position = targetPosition;
|
|
|
|
|
|
|
|
|
|
|
|
// Finish bump
|
|
|
|
|
|
_isBumping = false;
|
|
|
|
|
|
_bumpInputBlocked = false;
|
|
|
|
|
|
_bumpCoroutine = null;
|
2025-09-21 07:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|