67 lines
2.6 KiB
C#
67 lines
2.6 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|
|
|