[Player] Add dispatcher for cancelled movement and reset pickup correctly when move cancelled during pickup

This commit is contained in:
Michal Pikulski
2025-09-04 11:19:05 +02:00
parent 5d395ba4f4
commit 68592e8514
2 changed files with 14 additions and 3 deletions

View File

@@ -7,6 +7,7 @@ public class Pickup : MonoBehaviour
private Interactable interactable; private Interactable interactable;
private bool pickupInProgress = false; private bool pickupInProgress = false;
private PlayerTouchController cachedPlayerController;
void Awake() void Awake()
{ {
@@ -69,13 +70,11 @@ public class Pickup : MonoBehaviour
private void OnInteracted() private void OnInteracted()
{ {
if (pickupInProgress) return; if (pickupInProgress) return;
pickupInProgress = true;
var playerObj = GameObject.FindGameObjectWithTag("Player"); var playerObj = GameObject.FindGameObjectWithTag("Player");
var followerObj = GameObject.FindGameObjectWithTag("Pulver"); var followerObj = GameObject.FindGameObjectWithTag("Pulver");
if (playerObj == null || followerObj == null) if (playerObj == null || followerObj == null)
{ {
Debug.LogWarning("Pickup: Player or Follower not found."); Debug.LogWarning("Pickup: Player or Follower not found.");
pickupInProgress = false;
return; return;
} }
var playerController = playerObj.GetComponent<PlayerTouchController>(); var playerController = playerObj.GetComponent<PlayerTouchController>();
@@ -83,17 +82,26 @@ public class Pickup : MonoBehaviour
if (playerController == null || followerController == null) if (playerController == null || followerController == null)
{ {
Debug.LogWarning("Pickup: PlayerTouchController or FollowerController missing."); Debug.LogWarning("Pickup: PlayerTouchController or FollowerController missing.");
pickupInProgress = false;
return; return;
} }
// Use GameManager for playerStopDistance and followerPickupDelay // Use GameManager for playerStopDistance and followerPickupDelay
float playerStopDistance = GameManager.Instance.PlayerStopDistance; float playerStopDistance = GameManager.Instance.PlayerStopDistance;
float followerPickupDelay = GameManager.Instance.FollowerPickupDelay; float followerPickupDelay = GameManager.Instance.FollowerPickupDelay;
// Subscribe to cancellation event
cachedPlayerController = playerController;
void OnPlayerArrived() void OnPlayerArrived()
{ {
playerController.OnArrivedAtTarget -= OnPlayerArrived; playerController.OnArrivedAtTarget -= OnPlayerArrived;
playerController.OnMoveToCancelled -= OnPlayerMoveCancelled;
pickupInProgress = true; // Only lock when follower is about to be dispatched
StartCoroutine(DispatchFollower()); StartCoroutine(DispatchFollower());
} }
void OnPlayerMoveCancelled()
{
playerController.OnArrivedAtTarget -= OnPlayerArrived;
playerController.OnMoveToCancelled -= OnPlayerMoveCancelled;
pickupInProgress = false;
}
System.Collections.IEnumerator DispatchFollower() System.Collections.IEnumerator DispatchFollower()
{ {
yield return new WaitForSeconds(followerPickupDelay); yield return new WaitForSeconds(followerPickupDelay);
@@ -111,6 +119,7 @@ public class Pickup : MonoBehaviour
pickupInProgress = false; pickupInProgress = false;
} }
playerController.OnArrivedAtTarget += OnPlayerArrived; playerController.OnArrivedAtTarget += OnPlayerArrived;
playerController.OnMoveToCancelled += OnPlayerMoveCancelled;
Vector3 stopPoint = transform.position + (playerObj.transform.position - transform.position).normalized * playerStopDistance; Vector3 stopPoint = transform.position + (playerObj.transform.position - transform.position).normalized * playerStopDistance;
playerController.MoveToAndNotify(stopPoint); playerController.MoveToAndNotify(stopPoint);
} }

View File

@@ -19,6 +19,7 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
public delegate void ArrivedAtTargetHandler(); public delegate void ArrivedAtTargetHandler();
public event ArrivedAtTargetHandler OnArrivedAtTarget; public event ArrivedAtTargetHandler OnArrivedAtTarget;
public event System.Action OnMoveToCancelled;
private Coroutine moveToCoroutine; private Coroutine moveToCoroutine;
private bool interruptMoveTo = false; private bool interruptMoveTo = false;
@@ -112,6 +113,7 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
public void InterruptMoveTo() public void InterruptMoveTo()
{ {
interruptMoveTo = true; interruptMoveTo = true;
OnMoveToCancelled?.Invoke();
} }
private System.Collections.IEnumerator MoveToTargetCoroutine(Vector3 target) private System.Collections.IEnumerator MoveToTargetCoroutine(Vector3 target)