public static async ETTask <bool> WaitTillAsync(this TimerComponent self, long tillTime, ETCancellationToken cancellationToken = null) { if (self.timeNow >= tillTime) { return(true); } ETTask <bool> tcs = ETTask <bool> .Create(true); TimerAction timer = self.AddChild <TimerAction, TimerClass, long, int, object>(TimerClass.OnceWaitTimer, tillTime - self.timeNow, 0, tcs, true); self.AddTimer(tillTime, timer); long timerId = timer.Id; void CancelAction() { if (self.Remove(timerId)) { tcs.SetResult(false); } } bool ret; try { cancellationToken?.Add(CancelAction); ret = await tcs; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public async ETTask <bool> WaitTillAsync(long tillTime, ETCancellationToken cancellationToken = null) { if (TimeHelper.ServerNow() >= tillTime) { return(true); } ETTask <bool> tcs = ETTask <bool> .Create(true); TimerAction timer = this.AddChild <TimerAction, TimerClass, long, object>(TimerClass.OnceWaitTimer, 0, tcs, true); this.AddTimer(tillTime, timer); long timerId = timer.Id; void CancelAction() { if (this.Remove(timerId)) { tcs.SetResult(false); } } bool ret; try { cancellationToken?.Add(CancelAction); ret = await tcs; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
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 MoveToAsync(this MoveComponent self, Vector3 target, float speedValue, ETCancellationToken cancellationToken) { Unit unit = self.GetParent <Unit>(); if ((target - self.Target).magnitude < 0.1f) { await ETTask.CompletedTask; } self.Target = target; self.StartPos = unit.Position; self.StartTime = TimeHelper.ClientNow(); float distance = (self.Target - self.StartPos).magnitude; if (Math.Abs(distance) < 0.1f) { await ETTask.CompletedTask; } self.needTime = (long)(distance / speedValue * 1000); self.moveTcs = new ETTaskCompletionSource(); cancellationToken.Add(() => { self.moveTcs = null; }); await self.moveTcs.Task; }
public async ETTask <T> Wait <T>(int timeout, ETCancellationToken cancellationToken = null) where T : struct, IWaitType { long timerId = TimerComponent.Instance.NewOnceTimer(TimeHelper.ServerNow() + timeout, () => { Notify(new T() { Error = WaitTypeError.Timeout }); }); ResultCallback <T> tcs = new ResultCallback <T>(timerId); this.tcss.Add(typeof(T), tcs); void CancelAction() { Notify(new T() { Error = WaitTypeError.Cancel }); } T ret; try { cancellationToken?.Add(CancelAction); ret = await tcs.Task; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public async ETTask <bool> WaitTillAsync(long tillTime, ETCancellationToken cancellationToken = null) { if (TimeHelper.ServerNow() >= tillTime) { return(true); } ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>(); TimerAction timer = EntityFactory.CreateWithParent <TimerAction, TimerClass, long, object>(this, TimerClass.OnceWaitTimer, 0, tcs, true); this.AddTimer(tillTime, timer); long timerId = timer.Id; void CancelAction() { if (this.Remove(timerId)) { tcs.SetResult(false); } } bool ret; try { cancellationToken?.Add(CancelAction); ret = await tcs.Task; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public static async ETTask <bool> MoveToAsync(this DMoveComponent self, Vector3 target, float speedValue, ETCancellationToken cancellationToken = null) { await ETTask.CompletedTask; DUnit unit = self.GetParent <DUnit>(); if ((target - self.Target).magnitude < 0.1f) { return(true); } self.Target = target; self.StartPos = unit.Position; self.StartTime = TimeHelper.ClientNow(); float distance = (self.Target - self.StartPos).magnitude; if (Math.Abs(distance) < 0.1f) { return(true); } self.needTime = (long)(distance / speedValue * 1000); ETTask <bool> moveTcs = ETTask <bool> .Create(); self.Callback = (ret) => { moveTcs.SetResult(ret); }; void CancelAction() { if (self.Callback != null) { Action <bool> callback = self.Callback; self.Callback = null; callback.Invoke(false); } } bool moveRet; try { cancellationToken?.Add(CancelAction); moveRet = await moveTcs; } finally { cancellationToken?.Remove(CancelAction); } return(moveRet); }
// 开启协程移动,每100毫秒移动一次,并且协程取消的时候会计算玩家真实移动 // 比方说玩家移动了2500毫秒,玩家有新的目标,这时旧的移动协程结束,将计算250毫秒移动的位置,而不是300毫秒移动的位置 public async ETTask StartMove(ETCancellationToken cancellationToken) { Unit unit = this.GetParent <Unit>(); this.StartPos = unit.Position; this.StartTime = TimeHelper.ClientNow(); float distance = (this.Target - this.StartPos).magnitude; if (Math.Abs(distance) < 0.1f) { return; } this.needTime = (long)(distance / this.Speed * 1000); TimerComponent timerComponent = Game.Scene.GetComponent <TimerComponent>(); // 协程如果取消,将算出玩家的真实位置,赋值给玩家 cancellationToken?.Add(() => { long timeNow = TimeHelper.ClientNow(); if (timeNow - this.StartTime >= this.needTime) { unit.Position = this.Target; } else { float amount = (timeNow - this.StartTime) * 1f / this.needTime; unit.Position = Vector3.Lerp(this.StartPos, this.Target, amount); } }); while (true) { //新版TimerComponent实现不同于5.0的TimerComponent,需要自己判断是取消还是自然结束,并且return,否则将不会取消任务,并可能会造成cancellationToken泄漏 if (!await timerComponent.WaitAsync(50, cancellationToken)) { return; } long timeNow = TimeHelper.ClientNow(); if (timeNow - this.StartTime >= this.needTime) { unit.Position = this.Target; break; } float amount = (timeNow - this.StartTime) * 1f / this.needTime; unit.Position = Vector3.Lerp(this.StartPos, this.Target, amount); } }
public static async ETTask <bool> MoveToAsync(this MoveComponent self, List <Vector3> target, float speed, int turnTime = 100, ETCancellationToken cancellationToken = null) { self.Stop(); foreach (Vector3 v in target) { self.Targets.Add(v); } self.IsTurnHorizontal = true; self.TurnTime = turnTime; self.Speed = speed; ETTask <bool> tcs = ETTask <bool> .Create(); self.Callback = (ret) => { tcs.SetResult(ret); }; Game.EventSystem.Publish(new EventType.MoveStart() { Unit = self.GetParent <Unit>() }).Coroutine(); self.StartMove(); void CancelAction() { self.Stop(); } bool moveRet; try { cancellationToken?.Add(CancelAction); moveRet = await tcs; } finally { cancellationToken?.Remove(CancelAction); } if (moveRet) { Game.EventSystem.Publish(new EventType.MoveStop() { Unit = self.GetParent <Unit>() }).Coroutine(); } return(moveRet); }
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 async ETTask <IResponse> Call(IRequest request, ETCancellationToken cancellationToken) { int rpcId = ++RpcId; RpcInfo rpcInfo = new RpcInfo(request); this.requestCallbacks[rpcId] = rpcInfo; request.RpcId = rpcId; this.Send(request); void CancelAction() { if (!this.requestCallbacks.TryGetValue(rpcId, out RpcInfo action)) { return; } this.requestCallbacks.Remove(rpcId); Type responseType = OpcodeTypeComponent.Instance.GetResponseType(action.Request.GetType()); IResponse response = (IResponse)Activator.CreateInstance(responseType); response.Error = ErrorCore.ERR_Cancel; action.Tcs.SetResult(response); } IResponse ret; try { cancellationToken?.Add(CancelAction); ret = await rpcInfo.Tcs; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public async ETTask <T> Wait <T>(ETCancellationToken cancellationToken = null) where T : struct, IWaitType { ResultCallback <T> tcs = new ResultCallback <T>(); Type type = typeof(T); this.tcss.Add(type, tcs); void CancelAction() { this.Notify(new T() { Error = WaitTypeError.Cancel }); } T ret; try { if (cancellationToken != null) { if (!cancellationToken.IsCancel()) { cancellationToken?.Add(CancelAction); } else { Log.Error("cancellationToken 已经取消过了"); } } ret = await tcs.Task; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }