94 lines
3.8 KiB
C#
94 lines
3.8 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
namespace Minigames.DivingForPictures
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// Handles endless descender movement in response to tap and hold input events.
|
|||
|
|
/// Moves the character horizontally to follow the finger or tap position.
|
|||
|
|
/// </summary>
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Handles tap input. Moves to the tapped X position.
|
|||
|
|
/// </summary>
|
|||
|
|
public void OnTap(Vector2 worldPosition)
|
|||
|
|
{
|
|||
|
|
Debug.Log($"[EndlessDescenderController] OnTap at {worldPosition}");
|
|||
|
|
_targetFingerX = Mathf.Clamp(worldPosition.x, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
|
|||
|
|
_isTouchActive = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Handles the start of a hold input. Begins tracking the finger.
|
|||
|
|
/// </summary>
|
|||
|
|
public void OnHoldStart(Vector2 worldPosition)
|
|||
|
|
{
|
|||
|
|
Debug.Log($"[EndlessDescenderController] OnHoldStart at {worldPosition}");
|
|||
|
|
_targetFingerX = Mathf.Clamp(worldPosition.x, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
|
|||
|
|
_isTouchActive = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Handles hold move input. Updates the target X position as the finger moves.
|
|||
|
|
/// </summary>
|
|||
|
|
public void OnHoldMove(Vector2 worldPosition)
|
|||
|
|
{
|
|||
|
|
Debug.Log($"[EndlessDescenderController] OnHoldMove at {worldPosition}");
|
|||
|
|
_targetFingerX = Mathf.Clamp(worldPosition.x, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Handles the end of a hold input. Stops tracking.
|
|||
|
|
/// </summary>
|
|||
|
|
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<WobbleBehavior>();
|
|||
|
|
if (wobble != null)
|
|||
|
|
{
|
|||
|
|
newY += wobble.VerticalOffset;
|
|||
|
|
}
|
|||
|
|
transform.position = new Vector3(newX, newY, transform.position.z);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|