using UnityEngine;
namespace Minigames.DivingForPictures
{
///
/// Handles endless descender movement in response to tap and hold input events.
/// Moves the character horizontally to follow the finger or tap position.
///
public class PlayerController : MonoBehaviour, ITouchInputConsumer
{
private float _targetFingerX;
private bool _isTouchActive;
private float _originY;
void Awake()
{
_originY = transform.position.y;
}
void Start()
{
// Register as default consumer for input
InputManager.Instance?.SetDefaultConsumer(this);
// Initialize target to current position
_targetFingerX = transform.position.x;
_isTouchActive = false;
}
///
/// Handles tap input. Moves to the tapped X position.
///
public void OnTap(Vector2 worldPosition)
{
// Debug.Log($"[EndlessDescenderController] OnTap at {worldPosition}");
_targetFingerX = Mathf.Clamp(worldPosition.x, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
_isTouchActive = true;
}
///
/// Handles the start of a hold input. Begins tracking the finger.
///
public void OnHoldStart(Vector2 worldPosition)
{
// Debug.Log($"[EndlessDescenderController] OnHoldStart at {worldPosition}");
_targetFingerX = Mathf.Clamp(worldPosition.x, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
_isTouchActive = true;
}
///
/// Handles hold move input. Updates the target X position as the finger moves.
///
public void OnHoldMove(Vector2 worldPosition)
{
// Debug.Log($"[EndlessDescenderController] OnHoldMove at {worldPosition}");
_targetFingerX = Mathf.Clamp(worldPosition.x, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
}
///
/// Handles the end of a hold input. Stops tracking.
///
public void OnHoldEnd(Vector2 worldPosition)
{
// Debug.Log($"[EndlessDescenderController] OnHoldEnd at {worldPosition}");
_isTouchActive = false;
}
void Update()
{
if (!_isTouchActive) return;
float currentX = transform.position.x;
float lerpSpeed = GameManager.Instance.EndlessDescenderLerpSpeed;
float maxOffset = GameManager.Instance.EndlessDescenderMaxOffset;
float exponent = GameManager.Instance.EndlessDescenderSpeedExponent;
float targetX = _targetFingerX;
float offset = targetX - currentX;
offset = Mathf.Clamp(offset, -maxOffset, maxOffset);
float absOffset = Mathf.Abs(offset);
float t = Mathf.Pow(absOffset / maxOffset, exponent); // Non-linear drop-off
float moveStep = Mathf.Sign(offset) * maxOffset * t * Time.deltaTime * lerpSpeed;
// Prevent overshooting
moveStep = Mathf.Clamp(moveStep, -absOffset, absOffset);
float newX = currentX + moveStep;
newX = Mathf.Clamp(newX, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
float newY = _originY;
// Add vertical offset from WobbleBehavior if present
WobbleBehavior wobble = GetComponent();
if (wobble != null)
{
newY += wobble.VerticalOffset;
}
transform.position = new Vector3(newX, newY, transform.position.z);
}
}
}