maze_switching (#82)
Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com> Reviewed-on: #82
This commit is contained in:
@@ -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;
|
||||
@@ -39,89 +34,126 @@ namespace Input
|
||||
_movementSettings = configs.DefaultPlayerMovement;
|
||||
}
|
||||
|
||||
#region ITouchInputConsumer Overrides (Add InterruptMoveTo)
|
||||
|
||||
public override void OnTap(Vector2 worldPosition)
|
||||
internal override void OnManagedStart()
|
||||
{
|
||||
InterruptMoveTo();
|
||||
base.OnTap(worldPosition);
|
||||
base.OnManagedStart();
|
||||
|
||||
// Register with InputManager as default consumer
|
||||
if (InputManager.Instance != null)
|
||||
{
|
||||
InputManager.Instance.RegisterController("trafalgar", this, setAsDefaultConsumer: true);
|
||||
Logging.Debug($"[PlayerTouchController] Registered controller '{gameObject.name}' as default consumer");
|
||||
}
|
||||
}
|
||||
|
||||
#region IInteractingCharacter Override
|
||||
|
||||
public override void OnHoldStart(Vector2 worldPosition)
|
||||
/// <summary>
|
||||
/// PlayerTouchController-specific interaction movement.
|
||||
/// Handles main character movement + follower dispatch based on interactable.characterToInteract setting.
|
||||
/// </summary>
|
||||
public override async System.Threading.Tasks.Task<bool> MoveToInteractableAsync(Interactions.InteractableBase interactable)
|
||||
{
|
||||
InterruptMoveTo();
|
||||
base.OnHoldStart(worldPosition);
|
||||
var characterToInteract = interactable.characterToInteract;
|
||||
|
||||
// If None, skip movement
|
||||
if (characterToInteract == Interactions.CharacterToInteract.None)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Determine stop distance based on interaction type
|
||||
float stopDistance;
|
||||
if (characterToInteract == Interactions.CharacterToInteract.Trafalgar)
|
||||
{
|
||||
// Move ONLY main character directly to item (close distance)
|
||||
stopDistance = Core.GameManager.Instance.PlayerStopDistanceDirectInteraction;
|
||||
}
|
||||
else // Pulver or Both
|
||||
{
|
||||
// Move main character to radius (far distance)
|
||||
stopDistance = Core.GameManager.Instance.PlayerStopDistance;
|
||||
}
|
||||
|
||||
// Calculate stop position for main character
|
||||
Vector3 stopPoint = interactable.transform.position;
|
||||
bool customTargetFound = false;
|
||||
|
||||
// Check for custom CharacterMoveToTarget for main character
|
||||
var moveTargets = interactable.GetComponentsInChildren<Interactions.CharacterMoveToTarget>();
|
||||
foreach (var target in moveTargets)
|
||||
{
|
||||
if (target.characterType == Interactions.CharacterToInteract.Trafalgar ||
|
||||
target.characterType == Interactions.CharacterToInteract.Both)
|
||||
{
|
||||
stopPoint = target.GetTargetPosition();
|
||||
customTargetFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If no custom target, calculate based on distance
|
||||
if (!customTargetFound)
|
||||
{
|
||||
stopPoint = Utils.MovementUtilities.CalculateStopPosition(
|
||||
interactable.transform.position,
|
||||
transform.position,
|
||||
stopDistance
|
||||
);
|
||||
}
|
||||
|
||||
// Move main character
|
||||
bool mainCharacterArrived = await Utils.MovementUtilities.MoveToPositionAsync(this, stopPoint);
|
||||
|
||||
if (!mainCharacterArrived)
|
||||
{
|
||||
return false; // Movement cancelled
|
||||
}
|
||||
|
||||
// Handle follower dispatch based on interaction type
|
||||
if (characterToInteract == Interactions.CharacterToInteract.Pulver ||
|
||||
characterToInteract == Interactions.CharacterToInteract.Both)
|
||||
{
|
||||
// Find follower and dispatch to interactable
|
||||
var followerController = FindFirstObjectByType<FollowerController>();
|
||||
if (followerController != null)
|
||||
{
|
||||
// Determine follower target position
|
||||
Vector3 followerTarget = interactable.transform.position;
|
||||
|
||||
// Check for custom target for Pulver
|
||||
foreach (var target in moveTargets)
|
||||
{
|
||||
if (target.characterType == Interactions.CharacterToInteract.Pulver ||
|
||||
target.characterType == Interactions.CharacterToInteract.Both)
|
||||
{
|
||||
followerTarget = target.GetTargetPosition();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for follower to arrive
|
||||
var tcs = new System.Threading.Tasks.TaskCompletionSource<bool>();
|
||||
|
||||
void OnFollowerArrived()
|
||||
{
|
||||
followerController.OnPickupArrived -= OnFollowerArrived;
|
||||
followerController.ReturnToPlayer(transform);
|
||||
tcs.TrySetResult(true);
|
||||
}
|
||||
|
||||
followerController.OnPickupArrived += OnFollowerArrived;
|
||||
followerController.GoToPoint(followerTarget);
|
||||
|
||||
await tcs.Task;
|
||||
}
|
||||
}
|
||||
|
||||
return true; // Success
|
||||
}
|
||||
|
||||
#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