public static async ETVoid CancelAfter(this ETCancellationToken self, long afterTimeCancel) { if (self.IsCancel()) { return; } await TimerComponent.Instance.WaitAsync(afterTimeCancel); if (self.IsCancel()) { return; } self.Cancel(); }
public async ETVoid PlayThenIdleAsync(AnimationClip animation) { AnimancerComponent.Play(IdleAnimation); AnimancerComponent.Play(animation, 0.25f); if (token != null) { token.Cancel(); } token = new ETCancellationToken(); var isTimeout = await TimerComponent.Instance.WaitAsync((int)(animation.length * 1000), token); if (isTimeout) { AnimancerComponent.Play(IdleAnimation, 0.25f); } }
public override async ETTask Execute(AIComponent aiComponent, AIConfig aiConfig, ETCancellationToken cancellationToken) { Scene clientScene = aiComponent.DomainScene(); Unit myUnit = Client.UnitHelper.GetMyUnitFromClientScene(clientScene); if (myUnit == null) { return; } // 停在当前位置 clientScene.GetComponent <SessionComponent>().Session.Send(new C2M_Stop()); Log.Debug("开始攻击"); for (int i = 0; i < 100000; ++i) { Log.Debug($"攻击: {i}次"); // 因为协程可能被中断,任何协程都要传入cancellationToken,判断如果是中断则要返回 bool timeRet = await TimerComponent.Instance.WaitAsync(1000, cancellationToken); if (!timeRet) { return; } } }
// 可以多次调用,多次调用的话会取消上一次的协程 public static async ETTask FindPathMoveToAsync(this Unit unit, Vector3 target, ETCancellationToken cancellationToken = null) { float speed = unit.GetComponent <NumericComponent>().GetAsFloat(NumericType.Speed); if (speed < 0.01) { unit.SendStop(-1); return; } using var list = ListComponent <Vector3> .Create(); unit.GetComponent <PathfindingComponent>().Find(unit.Position, target, list); List <Vector3> path = list; if (path.Count < 2) { unit.SendStop(0); return; } // 广播寻路路径 M2C_PathfindingResult m2CPathfindingResult = new M2C_PathfindingResult(); m2CPathfindingResult.Id = unit.Id; for (int i = 0; i < list.Count; ++i) { Vector3 vector3 = list[i]; m2CPathfindingResult.Xs.Add(vector3.x); m2CPathfindingResult.Ys.Add(vector3.y); m2CPathfindingResult.Zs.Add(vector3.z); } MessageHelper.Broadcast(unit, m2CPathfindingResult); bool ret = await unit.GetComponent <MoveComponent>().MoveToAsync(path, speed); if (ret) // 如果返回false,说明被其它移动取消了,这时候不需要通知客户端stop { unit.SendStop(0); } }
public override async ETTask Execute(AIComponent aiComponent, AIConfig aiConfig, ETCancellationToken cancellationToken) { Scene clientScene = aiComponent.DomainScene(); Unit myUnit = Client.UnitHelper.GetMyUnitFromClientScene(clientScene); if (myUnit == null) { return; } Log.Debug("开始巡逻"); while (true) { XunLuoPathComponent xunLuoPathComponent = myUnit.GetComponent <XunLuoPathComponent>(); Vector3 nextTarget = xunLuoPathComponent.GetCurrent(); int ret = await myUnit.MoveToAsync(nextTarget, cancellationToken); if (ret != 0) { return; } xunLuoPathComponent.MoveNext(); } }
// 可以多次调用,多次调用的话会取消上一次的协程 public static async ETTask <int> MoveToAsync(this Unit unit, Vector3 targetPos, ETCancellationToken cancellationToken = null) { C2M_PathfindingResult msg = new C2M_PathfindingResult() { X = targetPos.x, Y = targetPos.y, Z = targetPos.z }; unit.ClientScene().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); }