MoveHelper.cs 1.3 KB

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