A bit wonky but working switching
This commit is contained in:
66
Assets/Scripts/Utils/MovementUtilities.cs
Normal file
66
Assets/Scripts/Utils/MovementUtilities.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System.Threading.Tasks;
|
||||
using Core;
|
||||
using Input;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility methods for character movement operations.
|
||||
/// Extracted from interaction/controller code for reusability.
|
||||
/// </summary>
|
||||
public static class MovementUtilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Moves a character to a target position and waits for arrival.
|
||||
/// Works with any controller implementing IInteractingCharacter.
|
||||
/// </summary>
|
||||
/// <param name="character">The character to move (must implement IInteractingCharacter)</param>
|
||||
/// <param name="targetPosition">World position to move to</param>
|
||||
/// <returns>Task that completes when the character arrives or movement is cancelled</returns>
|
||||
public static async Task<bool> MoveToPositionAsync(IInteractingCharacter character, Vector3 targetPosition)
|
||||
{
|
||||
if (character == null)
|
||||
{
|
||||
Logging.Warning("[MovementUtilities] Cannot move null character");
|
||||
return false;
|
||||
}
|
||||
|
||||
var tcs = new TaskCompletionSource<bool>();
|
||||
|
||||
void OnArrivedLocal()
|
||||
{
|
||||
character.OnArrivedAtTarget -= OnArrivedLocal;
|
||||
character.OnMoveToCancelled -= OnCancelledLocal;
|
||||
tcs.TrySetResult(true);
|
||||
}
|
||||
|
||||
void OnCancelledLocal()
|
||||
{
|
||||
character.OnArrivedAtTarget -= OnArrivedLocal;
|
||||
character.OnMoveToCancelled -= OnCancelledLocal;
|
||||
tcs.TrySetResult(false);
|
||||
}
|
||||
|
||||
character.OnArrivedAtTarget += OnArrivedLocal;
|
||||
character.OnMoveToCancelled += OnCancelledLocal;
|
||||
character.MoveToAndNotify(targetPosition);
|
||||
|
||||
return await tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates a stop position at a given distance from a target position towards a character.
|
||||
/// </summary>
|
||||
/// <param name="targetPosition">The target position</param>
|
||||
/// <param name="characterPosition">The character's current position</param>
|
||||
/// <param name="stopDistance">Distance from target to stop at</param>
|
||||
/// <returns>The calculated stop position</returns>
|
||||
public static Vector3 CalculateStopPosition(Vector3 targetPosition, Vector3 characterPosition, float stopDistance)
|
||||
{
|
||||
Vector3 toCharacter = (characterPosition - targetPosition).normalized;
|
||||
return targetPosition + toCharacter * stopDistance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
3
Assets/Scripts/Utils/MovementUtilities.cs.meta
Normal file
3
Assets/Scripts/Utils/MovementUtilities.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 29f4ca2c743f4890aab59e4ccdda2c79
|
||||
timeCreated: 1765753993
|
||||
Reference in New Issue
Block a user