Big script cleanup. Remove the examples from Ropes' external package
This commit is contained in:
91
Assets/Scripts/Movement/EndlessDescenderController.cs
Normal file
91
Assets/Scripts/Movement/EndlessDescenderController.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <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 EndlessDescenderController : 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user