The feel is fine

This commit is contained in:
2025-09-24 15:16:31 +02:00
parent 783541a776
commit aeaa977294
19 changed files with 494 additions and 344 deletions

View File

@@ -12,157 +12,139 @@ namespace Minigames.DivingForPictures
{
private bool _isBumping;
private Coroutine _bumpCoroutine;
private bool _bumpInputBlocked; // Tracks bump-specific input blocking
protected override void HandleCollisionResponse(Collider2D obstacle)
{
// Check if the obstacle is on the TrenchTileLayer
if (obstacle.gameObject.layer != _devSettings.TrenchTileLayer)
{
// If not on the trench tile layer, don't process the collision
Debug.Log($"[TileBumpCollision] Ignored collision with object on layer {obstacle.gameObject.layer} (expected {_devSettings.TrenchTileLayer})");
return;
}
// Use bump mode from developer settings
switch (_devSettings.BumpMode)
{
case 0: // Impulse mode
case BumpMode.Impulse:
StartImpulseBump();
break;
case 1: // SmoothToCenter mode
case BumpMode.SmoothToCenter:
StartSmoothMoveToCenter();
break;
}
Debug.Log($"[TileBumpCollision] Collision handled with {(_devSettings.BumpMode == 0 ? "Impulse" : "SmoothToCenter")} mode");
Debug.Log($"[TileBumpCollision] Collision handled with {_devSettings.BumpMode} mode");
}
/// <summary>
/// Applies an impulse force toward center
/// Starts an impulse bump toward the center with force-based distance
/// </summary>
private void StartImpulseBump()
{
if (_isBumping || playerCharacter == null)
return;
if (playerCharacter == null) return;
_isBumping = true;
float currentX = playerCharacter.transform.position.x;
// Block input during bump if configured
if (_gameSettings.EndlessDescenderBlockInputDuringBump && playerController != null)
// Calculate bump distance based on force and current position
float directionToCenter = currentX > 0 ? -1f : 1f; // Direction toward center
// Calculate target position - closer to center based on bump force
float bumpDistance = _gameSettings.BumpForce * 0.2f; // Scale factor for distance
float targetX = currentX + (directionToCenter * bumpDistance);
// Clamp to center if we would overshoot
if ((currentX > 0 && targetX < 0) || (currentX < 0 && targetX > 0))
{
_bumpInputBlocked = true;
// TODO: Implement input blocking
targetX = 0f;
}
// Calculate direction to center (X = 0)
float directionToCenter = Mathf.Sign(-playerCharacter.transform.position.x);
float bumpDuration = 0.5f; // Fixed duration for impulse
// Start impulse bump coroutine
if (_bumpCoroutine != null)
StopCoroutine(_bumpCoroutine);
StartBump(currentX, targetX, bumpDuration);
_bumpCoroutine = StartCoroutine(ImpulseBumpCoroutine(directionToCenter));
Debug.Log($"[TileBumpCollision] Started impulse bump with force {_gameSettings.EndlessDescenderBumpForce} in direction {directionToCenter}");
Debug.Log($"[TileBumpCollision] Starting impulse bump from X={currentX} to X={targetX} (force={_gameSettings.BumpForce})");
}
/// <summary>
/// Smoothly moves the player to center
/// Starts smooth movement to the center
/// </summary>
private void StartSmoothMoveToCenter()
{
if (_isBumping || playerCharacter == null)
return;
if (playerCharacter == null) return;
float currentX = playerCharacter.transform.position.x;
float distanceToCenter = Mathf.Abs(currentX);
float targetX = 0f; // Always move to center
float bumpDuration = distanceToCenter / _gameSettings.SmoothMoveSpeed; // Duration based on distance and speed
StartBump(currentX, targetX, bumpDuration);
Debug.Log($"[TileBumpCollision] Starting smooth move to center from X={currentX} (speed={_gameSettings.SmoothMoveSpeed}, duration={bumpDuration:F2}s)");
}
/// <summary>
/// Common bump initialization using coroutines
/// </summary>
private void StartBump(float startX, float targetX, float duration)
{
// Stop any existing bump
if (_bumpCoroutine != null)
{
StopCoroutine(_bumpCoroutine);
_bumpCoroutine = null;
}
_isBumping = true;
// Block input during bump if configured
if (_gameSettings.EndlessDescenderBlockInputDuringBump && playerController != null)
{
_bumpInputBlocked = true;
// TODO: Implement input blocking
}
// Start smooth move coroutine
if (_bumpCoroutine != null)
StopCoroutine(_bumpCoroutine);
_bumpCoroutine = StartCoroutine(SmoothMoveToCenterCoroutine());
Debug.Log($"[TileBumpCollision] Started smooth move to center with speed {_gameSettings.EndlessDescenderSmoothMoveSpeed}");
// Start bump coroutine
_bumpCoroutine = StartCoroutine(BumpCoroutine(startX, targetX, duration));
}
/// <summary>
/// Coroutine to handle impulse bump movement
/// Coroutine that handles the bump movement over time
/// </summary>
private IEnumerator ImpulseBumpCoroutine(float direction)
private IEnumerator BumpCoroutine(float startX, float targetX, float duration)
{
float elapsedTime = 0f;
float bumpDuration = 0.5f; // Fixed duration for impulse
Vector3 startPosition = playerCharacter.transform.position;
while (elapsedTime < bumpDuration)
while (elapsedTime < duration)
{
elapsedTime += Time.deltaTime;
// Use evaluation time from curve for non-linear movement
float t = elapsedTime / bumpDuration;
float curveValue = _devSettings.BumpCurve.Evaluate(t);
// Calculate progress and apply curve
float progress = elapsedTime / duration;
float curveValue = _devSettings.BumpCurve.Evaluate(progress);
// Calculate movement distance based on force and curve
float distance = _gameSettings.EndlessDescenderBumpForce * curveValue * Time.deltaTime;
// Interpolate position
float currentX = Mathf.Lerp(startX, targetX, curveValue);
// 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;
// Apply the position to the player character
if (playerCharacter != null)
{
Vector3 currentPos = playerCharacter.transform.position;
currentPos.x = Mathf.Clamp(currentX, _gameSettings.ClampXMin, _gameSettings.ClampXMax);
playerCharacter.transform.position = currentPos;
}
yield return null;
}
// Finish bump
_isBumping = false;
_bumpInputBlocked = false;
_bumpCoroutine = null;
}
/// <summary>
/// Coroutine to handle smooth movement to center
/// </summary>
private IEnumerator SmoothMoveToCenterCoroutine()
{
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;
// Move until we reach the center
while (elapsedTime < expectedDuration)
// Ensure we end exactly at target
if (playerCharacter != null)
{
elapsedTime += Time.deltaTime;
// Calculate progress based on time and curve
float t = elapsedTime / expectedDuration;
float curveValue = _devSettings.BumpCurve.Evaluate(t);
// Calculate interpolated position
Vector3 newPosition = Vector3.Lerp(startPosition, targetPosition, curveValue);
// Apply the position
playerCharacter.transform.position = newPosition;
yield return null;
Vector3 currentPos = playerCharacter.transform.position;
float clampedTargetX = Mathf.Clamp(targetX, _gameSettings.ClampXMin, _gameSettings.ClampXMax);
playerCharacter.transform.position = new Vector3(clampedTargetX, currentPos.y, currentPos.z);
}
// Ensure we end at exactly the center
playerCharacter.transform.position = targetPosition;
// Finish bump
// Bump finished
_isBumping = false;
_bumpInputBlocked = false;
_bumpCoroutine = null;
Debug.Log("[TileBumpCollision] Bump movement completed");
}
}
}