MoveHelper.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. using System.Collections.Generic;
  2. using System.Numerics;
  3. using Unity.Mathematics;
  4. namespace ET.Client
  5. {
  6. public static class MoveHelper
  7. {
  8. // 可以多次调用,多次调用的话会取消上一次的协程
  9. public static async ETTask<int> MoveToAsync(this Unit unit, float3 targetPos, ETCancellationToken cancellationToken = null)
  10. {
  11. C2M_PathfindingResult msg = new C2M_PathfindingResult() { Position = targetPos };
  12. unit.ClientScene().GetComponent<SessionComponent>().Session.Send(msg);
  13. ObjectWait objectWait = unit.GetComponent<ObjectWait>();
  14. // 要取消上一次的移动协程
  15. objectWait.Notify(new Wait_UnitStop() { Error = WaitTypeError.Cancel });
  16. // 一直等到unit发送stop
  17. Wait_UnitStop waitUnitStop = await objectWait.Wait<Wait_UnitStop>(cancellationToken);
  18. return waitUnitStop.Error;
  19. }
  20. public static async ETTask MoveToAsync(this Unit unit, List<float3> path)
  21. {
  22. float speed = unit.GetComponent<NumericComponent>().GetAsFloat(NumericType.Speed);
  23. MoveComponent moveComponent = unit.GetComponent<MoveComponent>();
  24. await moveComponent.MoveToAsync(path, speed);
  25. }
  26. }
  27. }