A bit wonky but working switching
This commit is contained in:
@@ -9,9 +9,10 @@ namespace Input
|
||||
/// <summary>
|
||||
/// Base class for player movement controllers.
|
||||
/// Handles tap-to-move and hold-to-move input with pathfinding or direct movement.
|
||||
/// Implements IInteractingCharacter to enable interaction with items.
|
||||
/// Derived classes can override to add specialized behavior (e.g., shader updates).
|
||||
/// </summary>
|
||||
public abstract class BasePlayerMovementController : ManagedBehaviour, ITouchInputConsumer
|
||||
public abstract class BasePlayerMovementController : ManagedBehaviour, ITouchInputConsumer, IInteractingCharacter
|
||||
{
|
||||
[Header("Movement")]
|
||||
[SerializeField] protected float moveSpeed = 5f;
|
||||
@@ -42,6 +43,12 @@ namespace Input
|
||||
public event System.Action OnMovementStarted;
|
||||
public event System.Action OnMovementStopped;
|
||||
|
||||
// IInteractingCharacter implementation - scripted movement for interactions
|
||||
private Coroutine _moveToCoroutine;
|
||||
private bool _interruptMoveTo;
|
||||
public event System.Action OnArrivedAtTarget;
|
||||
public event System.Action OnMoveToCancelled;
|
||||
|
||||
// Components
|
||||
protected AIPath _aiPath;
|
||||
protected Animator _animator;
|
||||
@@ -95,6 +102,7 @@ namespace Input
|
||||
|
||||
public virtual void OnTap(Vector2 worldPosition)
|
||||
{
|
||||
InterruptMoveTo(); // Cancel any scripted movement
|
||||
Logging.Debug($"[{GetType().Name}] OnTap at {worldPosition}");
|
||||
if (_aiPath != null)
|
||||
{
|
||||
@@ -109,6 +117,7 @@ namespace Input
|
||||
|
||||
public virtual void OnHoldStart(Vector2 worldPosition)
|
||||
{
|
||||
InterruptMoveTo(); // Cancel any scripted movement
|
||||
Logging.Debug($"[{GetType().Name}] OnHoldStart at {worldPosition}");
|
||||
_lastHoldPosition = worldPosition;
|
||||
_isHolding = true;
|
||||
@@ -318,6 +327,78 @@ namespace Input
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IInteractingCharacter Implementation
|
||||
|
||||
/// <summary>
|
||||
/// Moves the character to a specific target position and notifies via events when arrived or cancelled.
|
||||
/// This is used by systems like interactions to orchestrate scripted movement.
|
||||
/// </summary>
|
||||
public virtual void MoveToAndNotify(Vector3 target)
|
||||
{
|
||||
// Cancel any previous move-to coroutine
|
||||
if (_moveToCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_moveToCoroutine);
|
||||
}
|
||||
|
||||
_interruptMoveTo = false;
|
||||
// Ensure pathfinding is enabled for MoveToAndNotify
|
||||
if (_aiPath != null)
|
||||
{
|
||||
_aiPath.enabled = true;
|
||||
_aiPath.canMove = true;
|
||||
_aiPath.isStopped = false;
|
||||
}
|
||||
_moveToCoroutine = StartCoroutine(MoveToTargetCoroutine(target));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancels any in-progress MoveToAndNotify operation and fires the cancellation event.
|
||||
/// </summary>
|
||||
public virtual void InterruptMoveTo()
|
||||
{
|
||||
_interruptMoveTo = true;
|
||||
_isHolding = false;
|
||||
_directMoveVelocity = Vector3.zero;
|
||||
if (Settings != null && Settings.DefaultHoldMovementMode == HoldMovementMode.Direct && _aiPath != null)
|
||||
_aiPath.enabled = false;
|
||||
OnMoveToCancelled?.Invoke();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine for moving the character to a target position and firing arrival/cancel events.
|
||||
/// </summary>
|
||||
protected virtual System.Collections.IEnumerator MoveToTargetCoroutine(Vector3 target)
|
||||
{
|
||||
if (_aiPath != null)
|
||||
{
|
||||
_aiPath.destination = target;
|
||||
_aiPath.maxSpeed = Settings.MoveSpeed;
|
||||
_aiPath.maxAcceleration = Settings.MaxAcceleration;
|
||||
}
|
||||
|
||||
while (!_interruptMoveTo)
|
||||
{
|
||||
Vector2 current2D = new Vector2(transform.position.x, transform.position.y);
|
||||
Vector2 target2D = new Vector2(target.x, target.y);
|
||||
float dist = Vector2.Distance(current2D, target2D);
|
||||
if (dist <= Settings.StopDistance + 0.2f)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
_moveToCoroutine = null;
|
||||
if (!_interruptMoveTo)
|
||||
{
|
||||
OnArrivedAtTarget?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
38
Assets/Scripts/Input/IInteractingCharacter.cs
Normal file
38
Assets/Scripts/Input/IInteractingCharacter.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Input
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for characters that can participate in scripted interactions.
|
||||
/// Provides movement-to-target with arrival/cancellation notifications.
|
||||
/// Implemented by BasePlayerMovementController to enable all controllers to interact with items.
|
||||
/// </summary>
|
||||
public interface IInteractingCharacter
|
||||
{
|
||||
/// <summary>
|
||||
/// Moves character to target position and notifies when arrived/cancelled
|
||||
/// </summary>
|
||||
void MoveToAndNotify(Vector3 target);
|
||||
|
||||
/// <summary>
|
||||
/// Interrupts any in-progress MoveToAndNotify operation
|
||||
/// </summary>
|
||||
void InterruptMoveTo();
|
||||
|
||||
/// <summary>
|
||||
/// Fired when character arrives at MoveToAndNotify target
|
||||
/// </summary>
|
||||
event System.Action OnArrivedAtTarget;
|
||||
|
||||
/// <summary>
|
||||
/// Fired when MoveToAndNotify is cancelled/interrupted
|
||||
/// </summary>
|
||||
event System.Action OnMoveToCancelled;
|
||||
|
||||
/// <summary>
|
||||
/// Character's transform (for position queries)
|
||||
/// </summary>
|
||||
Transform transform { get; }
|
||||
}
|
||||
}
|
||||
|
||||
3
Assets/Scripts/Input/IInteractingCharacter.cs.meta
Normal file
3
Assets/Scripts/Input/IInteractingCharacter.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0d1b1be281334b9390cc96d7a8ff3132
|
||||
timeCreated: 1765754798
|
||||
@@ -521,6 +521,16 @@ namespace Input
|
||||
return !string.IsNullOrEmpty(controllerName) && _registeredControllers.ContainsKey(controllerName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the currently active controller (the default consumer).
|
||||
/// This is the controller that currently has input control.
|
||||
/// </summary>
|
||||
/// <returns>The active controller, or null if no default consumer is set</returns>
|
||||
public ITouchInputConsumer GetActiveController()
|
||||
{
|
||||
return defaultConsumer;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,17 +16,12 @@ namespace Input
|
||||
|
||||
/// <summary>
|
||||
/// Handles player movement in response to tap and hold input events.
|
||||
/// Supports both direct and pathfinding movement modes, and provides event/callbacks for arrival/cancellation.
|
||||
/// Extends BasePlayerMovementController with save/load and MoveToAndNotify functionality.
|
||||
/// Supports both direct and pathfinding movement modes.
|
||||
/// Extends BasePlayerMovementController with save/load functionality.
|
||||
/// Interaction capability (MoveToAndNotify) is provided by base class.
|
||||
/// </summary>
|
||||
public class PlayerTouchController : BasePlayerMovementController
|
||||
{
|
||||
// --- PlayerTouchController-specific features (MoveToAndNotify) ---
|
||||
public delegate void ArrivedAtTargetHandler();
|
||||
private Coroutine _moveToCoroutine;
|
||||
public event ArrivedAtTargetHandler OnArrivedAtTarget;
|
||||
public event System.Action OnMoveToCancelled;
|
||||
private bool _interruptMoveTo;
|
||||
|
||||
// Save system configuration
|
||||
public override bool AutoRegisterForSave => true;
|
||||
@@ -51,89 +46,6 @@ namespace Input
|
||||
}
|
||||
}
|
||||
|
||||
#region ITouchInputConsumer Overrides (Add InterruptMoveTo)
|
||||
|
||||
public override void OnTap(Vector2 worldPosition)
|
||||
{
|
||||
InterruptMoveTo();
|
||||
base.OnTap(worldPosition);
|
||||
}
|
||||
|
||||
public override void OnHoldStart(Vector2 worldPosition)
|
||||
{
|
||||
InterruptMoveTo();
|
||||
base.OnHoldStart(worldPosition);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Moves the player to a specific target position and notifies via events when arrived or cancelled.
|
||||
/// This is used by systems like Pickup.cs to orchestrate movement.
|
||||
/// </summary>
|
||||
public void MoveToAndNotify(Vector3 target)
|
||||
{
|
||||
// Cancel any previous move-to coroutine
|
||||
if (_moveToCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_moveToCoroutine);
|
||||
}
|
||||
|
||||
_interruptMoveTo = false;
|
||||
// Ensure pathfinding is enabled for MoveToAndNotify
|
||||
if (_aiPath != null)
|
||||
{
|
||||
_aiPath.enabled = true;
|
||||
_aiPath.canMove = true;
|
||||
_aiPath.isStopped = false;
|
||||
}
|
||||
_moveToCoroutine = StartCoroutine(MoveToTargetCoroutine(target));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancels any in-progress MoveToAndNotify operation and fires the cancellation event.
|
||||
/// </summary>
|
||||
public void InterruptMoveTo()
|
||||
{
|
||||
_interruptMoveTo = true;
|
||||
_isHolding = false;
|
||||
_directMoveVelocity = Vector3.zero;
|
||||
if (Settings.DefaultHoldMovementMode == HoldMovementMode.Direct && _aiPath != null)
|
||||
_aiPath.enabled = false;
|
||||
OnMoveToCancelled?.Invoke();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine for moving the player to a target position and firing arrival/cancel events.
|
||||
/// </summary>
|
||||
private System.Collections.IEnumerator MoveToTargetCoroutine(Vector3 target)
|
||||
{
|
||||
if (_aiPath != null)
|
||||
{
|
||||
_aiPath.destination = target;
|
||||
_aiPath.maxSpeed = Settings.MoveSpeed;
|
||||
_aiPath.maxAcceleration = Settings.MaxAcceleration;
|
||||
}
|
||||
|
||||
while (!_interruptMoveTo)
|
||||
{
|
||||
Vector2 current2D = new Vector2(transform.position.x, transform.position.y);
|
||||
Vector2 target2D = new Vector2(target.x, target.y);
|
||||
float dist = Vector2.Distance(current2D, target2D);
|
||||
if (dist <= Settings.StopDistance + 0.2f)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
_moveToCoroutine = null;
|
||||
if (!_interruptMoveTo)
|
||||
{
|
||||
OnArrivedAtTarget?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
#region Save/Load Lifecycle Hooks
|
||||
|
||||
|
||||
Reference in New Issue
Block a user