Update touch input settings

This commit is contained in:
Michal Pikulski
2025-09-05 14:10:42 +02:00
parent 32f09f4755
commit 5d3a587b5e
9 changed files with 235 additions and 56 deletions

View File

@@ -17,12 +17,21 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
private Animator animator;
private Transform artTransform;
// For direct movement mode
private Vector3 directMoveVelocity = Vector3.zero;
public delegate void ArrivedAtTargetHandler();
public event ArrivedAtTargetHandler OnArrivedAtTarget;
public event System.Action OnMoveToCancelled;
private Coroutine moveToCoroutine;
private bool interruptMoveTo = false;
private bool isDragging = false;
private Coroutine pathfindingDragCoroutine; // For pathfinding drag updates
private Vector2 lastDragPosition; // Store last drag position
private float pathfindingDragUpdateInterval = 0.1f; // Interval in seconds
private bool pendingTap = false; // Track if OnHoldEnd is following a tap
void Awake()
{
rb3d = GetComponent<Rigidbody>();
@@ -49,37 +58,90 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
InputManager.Instance?.SetDefaultConsumer(this);
}
void OnEnable()
// Implement new ITouchInputConsumer contract
public void OnTap(Vector2 worldPosition)
{
// No longer register here
Debug.Log($"[PlayerTouchController] OnTap at {worldPosition}");
pendingTap = true;
if (aiPath != null)
{
aiPath.enabled = true;
aiPath.canMove = true;
aiPath.isStopped = false;
SetTargetPosition(worldPosition);
directMoveVelocity = Vector3.zero;
isDragging = false;
}
}
// Remove Update and HandleInput
public void OnTouchPress(Vector2 worldPosition)
// --- Hold-based tracking ---
public void OnHoldStart(Vector2 worldPosition)
{
// If moving to pickup, interrupt
InterruptMoveTo();
Debug.Log($"PlayerTouchController.OnTouchPress received worldPosition: {worldPosition}");
SetTargetPosition(worldPosition);
pendingTap = false;
lastDragPosition = worldPosition;
isDragging = true;
if (GameManager.Instance.DefaultHoldMovementMode == GameSettings.HoldMovementMode.Pathfinding && aiPath != null)
{
aiPath.enabled = true;
if (pathfindingDragCoroutine != null) StopCoroutine(pathfindingDragCoroutine);
pathfindingDragCoroutine = StartCoroutine(PathfindingDragUpdateCoroutine());
}
else // Direct movement
{
if (aiPath != null) aiPath.enabled = false;
directMoveVelocity = Vector3.zero;
}
}
public void OnTouchPosition(Vector2 screenPosition)
public void OnHold(Vector2 worldPosition)
{
Debug.Log($"PlayerTouchController.OnTouchPosition called with screenPosition: {screenPosition}");
// Optionally handle drag/move here
if (!isDragging) return;
lastDragPosition = worldPosition;
if (GameManager.Instance.DefaultHoldMovementMode == GameSettings.HoldMovementMode.Direct)
{
if (aiPath != null && aiPath.enabled) aiPath.enabled = false;
MoveDirectlyTo(worldPosition);
}
// If pathfinding, coroutine will update destination
}
public void OnHoldEnd(Vector2 worldPosition)
{
isDragging = false;
directMoveVelocity = Vector3.zero;
if (aiPath != null && GameManager.Instance.DefaultHoldMovementMode == GameSettings.HoldMovementMode.Pathfinding)
{
if (pathfindingDragCoroutine != null)
{
StopCoroutine(pathfindingDragCoroutine);
pathfindingDragCoroutine = null;
}
}
// Only disable aiPath in direct mode if this was a hold/drag, not a tap
if (aiPath != null && GameManager.Instance.DefaultHoldMovementMode == GameSettings.HoldMovementMode.Direct && !pendingTap)
{
aiPath.enabled = false;
}
pendingTap = false; // Reset after handling
}
// --- Drag methods are now no-ops for movement tracking ---
public void OnDragStart(Vector2 worldPosition) { }
public void OnDrag(Vector2 worldPosition) { }
public void OnDragEnd(Vector2 worldPosition) { }
void SetTargetPosition(Vector2 worldPosition)
{
Debug.Log($"PlayerTouchController.SetTargetPosition: worldPosition={worldPosition}");
Debug.Log($"[PlayerTouchController] SetTargetPosition: worldPosition={worldPosition}");
targetPosition = new Vector3(worldPosition.x, worldPosition.y, transform.position.z);
hasTarget = true;
if (aiPath != null)
{
aiPath.destination = targetPosition;
aiPath.maxSpeed = GameManager.Instance.MoveSpeed;
Debug.Log($"AIPath destination set to {targetPosition}");
aiPath.canMove = true;
aiPath.isStopped = false;
Debug.Log($"[PlayerTouchController] AIPath destination set to {targetPosition}, canMove={aiPath.canMove}, isStopped={aiPath.isStopped}, enabled={aiPath.enabled}");
}
else
{
@@ -87,12 +149,41 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
}
}
void MoveDirectlyTo(Vector2 worldPosition)
{
if (aiPath == null) return;
Vector3 current = transform.position;
Vector3 target = new Vector3(worldPosition.x, worldPosition.y, current.z);
Vector3 toTarget = (target - current);
Vector3 direction = toTarget.normalized;
float maxSpeed = aiPath.maxSpeed;
float acceleration = aiPath.maxAcceleration;
// Accelerate velocity toward target direction
directMoveVelocity = Vector3.MoveTowards(directMoveVelocity, direction * maxSpeed, acceleration * Time.deltaTime);
// Clamp velocity to max speed
if (directMoveVelocity.magnitude > maxSpeed)
directMoveVelocity = directMoveVelocity.normalized * maxSpeed;
// Move the player
Vector3 move = directMoveVelocity * Time.deltaTime;
// Don't overshoot the target
if (move.magnitude > toTarget.magnitude)
move = toTarget;
transform.position += move;
}
void Update()
{
// Update animator speed parameter
if (animator != null && aiPath != null)
{
float normalizedSpeed = aiPath.velocity.magnitude / aiPath.maxSpeed;
float normalizedSpeed = 0f;
if (isDragging)
{
normalizedSpeed = directMoveVelocity.magnitude / aiPath.maxSpeed;
}
else if (aiPath.enabled)
{
normalizedSpeed = aiPath.velocity.magnitude / aiPath.maxSpeed;
}
animator.SetFloat("Speed", Mathf.Clamp01(normalizedSpeed));
}
}
@@ -113,6 +204,9 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
public void InterruptMoveTo()
{
interruptMoveTo = true;
isDragging = false;
directMoveVelocity = Vector3.zero;
if (GameManager.Instance.DefaultHoldMovementMode == GameSettings.HoldMovementMode.Direct && aiPath != null) aiPath.enabled = false;
OnMoveToCancelled?.Invoke();
}
@@ -143,4 +237,16 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
OnArrivedAtTarget?.Invoke();
}
}
private System.Collections.IEnumerator PathfindingDragUpdateCoroutine()
{
Debug.Log("[PlayerTouchController] PathfindingDragUpdateCoroutine started");
while (isDragging && aiPath != null)
{
aiPath.destination = new Vector3(lastDragPosition.x, lastDragPosition.y, transform.position.z);
Debug.Log($"[PlayerTouchController] Updating aiPath.destination to {aiPath.destination}");
yield return new WaitForSeconds(pathfindingDragUpdateInterval);
}
Debug.Log("[PlayerTouchController] PathfindingDragUpdateCoroutine stopped");
}
}