public static async ETTask <T> Wait <T>(this ObjectWait self, ETCancellationToken cancellationToken = null) where T : struct, IWaitType { ResultCallback <T> tcs = new ResultCallback <T>(); Type type = typeof(T); self.tcss.Add(type, tcs); void CancelAction() { self.Notify(new T() { Error = WaitTypeError.Cancel }); } T ret; try { cancellationToken?.Add(CancelAction); ret = await tcs.Task; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
// 可以多次调用,多次调用的话会取消上一次的协程 public static async ETTask <int> MoveToAsync(this Unit unit, Vector3 targetPos, ETCancellationToken cancellationToken = null) { if (!unit.GetComponent <MoveComponent>().Enable) { Log.Error("暂时无法移动"); return(1); } C2M_PathfindingResult msg = new C2M_PathfindingResult() { X = targetPos.x, Y = targetPos.y, Z = targetPos.z }; unit.ZoneScene().GetComponent <SessionComponent>().Session.Send(msg); ObjectWait objectWait = unit.GetComponent <ObjectWait>(); // 要取消上一次的移动协程 objectWait.Notify(new WaitType.Wait_UnitStop() { Error = WaitTypeError.Cancel }); // 一直等到unit发送stop WaitType.Wait_UnitStop waitUnitStop = await objectWait.Wait <WaitType.Wait_UnitStop>(cancellationToken); return(waitUnitStop.Error); }
public static async ETTask <T> Wait <T>(this ObjectWait self, int timeout, ETCancellationToken cancellationToken = null) where T : struct, IWaitType { ResultCallback <T> tcs = new ResultCallback <T>(); async ETTask WaitTimeout() { bool retV = await TimerComponent.Instance.WaitAsync(timeout, cancellationToken); if (!retV) { return; } if (tcs.IsDisposed) { return; } self.Notify(new T() { Error = WaitTypeError.Timeout }); } WaitTimeout().Coroutine(); self.tcss.Add(typeof(T), tcs); void CancelAction() { self.Notify(new T() { Error = WaitTypeError.Cancel }); } T ret; try { cancellationToken?.Add(CancelAction); ret = await tcs.Task; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public static async ETTask<int> MoveActionImpAsync(DUnit unit, Vector3[] paths, Vector3 serverpos, ETCancellationToken cancellationToken = null) { PathComponent pathComponent = unit.GetComponent<PathComponent>(); if (await pathComponent.StartMove(paths, serverpos, cancellationToken)) { ObjectWait objectWait = unit.GetComponent<ObjectWait>(); objectWait.Notify(new WaitType.Wait_UnitStop() { Error = WaitTypeError.Success }); return WaitTypeError.Success; } else { return WaitTypeError.Cancel; } }
// 可以多次调用,多次调用的话会取消上一次的协程 public static async ETTask <int> MoveToAsync(this Unit unit, Vector3 targetPos, ETCancellationToken cancellationToken = null) { C2M_PathfindingResult msg = new C2M_PathfindingResult(); unit.Domain.GetComponent <SessionComponent>().Session.Send(msg); ObjectWait objectWait = unit.GetComponent <ObjectWait>(); // 要取消上一次的移动协程 objectWait.Notify(new WaitType.Wait_UnitStop() { Error = WaitTypeError.Cancel }); // 一直等到unit发送stop WaitType.Wait_UnitStop waitUnitStop = await objectWait.Wait <WaitType.Wait_UnitStop>(); return(waitUnitStop.Error); }
public static async ETTask<int> MoveActionAsync(DUnit unit, Vector3 targetPos, ETCancellationToken cancellationToken = null) { NavMeshPath meshPath = new NavMeshPath(); NavMesh.CalculatePath(unit.Position, targetPos, 1, meshPath); if (unit.DomainScene().GetComponent<PVPComponent>().bePVP) { if (OperationerComponentSystem.IsOperationer(unit) == false) { return WaitTypeError.Success; } // PVP 发送移动消息,封包发送 C2M_DPathfindingResult msg = new C2M_DPathfindingResult(); msg.Id = unit.Id; msg.X = unit.Position.x; msg.Y = unit.Position.y; msg.Z = unit.Position.z; for (int i = 0; i < meshPath.corners.Length; i++) { msg.Xs.Add(meshPath.corners[i].x); msg.Ys.Add(meshPath.corners[i].y); msg.Zs.Add(meshPath.corners[i].z); } unit.Domain.GetComponent<SessionComponent>().Session.Send(msg); ObjectWait objectWait = unit.GetComponent<ObjectWait>(); objectWait.Notify(new WaitType.Wait_UnitStop() { Error = WaitTypeError.Cancel }); WaitType.Wait_UnitStop waitUnitStop = await objectWait.Wait<WaitType.Wait_UnitStop>(cancellationToken); return waitUnitStop.Error; } else { return await DMoveAction.MoveActionImpAsync(unit, meshPath.corners, unit.Position, cancellationToken); } }