public override async ETVoid Execute(AIComponent aiComponent, AIConfig aiConfig, ETCancellationToken cancellationToken) { Scene zoneScene = aiComponent.DomainScene(); Unit myUnit = zoneScene.GetComponent <UnitComponent>().MyUnit; if (myUnit == null) { return; } // 停在当前位置 zoneScene.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 override async ETTask Execute(AIComponent aiComponent, AIConfig aiConfig, ETCancellationToken cancellationToken) { Unit myUnit = aiComponent.GetParent <Unit>(); if (myUnit == null) { return; } Log.Info("开始追逐"); ZhuiZhuAimComponent zhuiZhuAimPathComponent = myUnit.GetComponent <ZhuiZhuAimComponent>(); while (zhuiZhuAimPathComponent?.Aim != null) { Vector3 nextTarget = zhuiZhuAimPathComponent.Aim.Position; #if SERVER myUnit.FindPathMoveToAsync(nextTarget, cancellationToken).Coroutine(); await TimerComponent.Instance.WaitAsync(100); #else myUnit.MoveToAsync(nextTarget, cancellationToken).Coroutine(); await TimerComponent.Instance.WaitAsync(10); #endif if (Vector3.Distance(nextTarget, myUnit.Position) < 0.1f) { zhuiZhuAimPathComponent.Arrived(); } } }
public override async ETTask Execute(AIComponent aiComponent, AIConfig aiConfig, ETCancellationToken cancellationToken) { Scene zoneScene = aiComponent.DomainScene(); Unit myUnit = UnitHelper.GetMyUnitFromZoneScene(zoneScene); 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 override int Check(AIComponent aiComponent, AIConfig aiConfig) { long sec = TimeHelper.ClientNow() / 1000 % 15; if (sec >= 10) { return(0); } return(1); }
public override int Check(AIComponent aiComponent, AIConfig aiConfig) { Unit myUnit = aiComponent.GetParent <Unit>(); Log.Info("Check"); if (myUnit == null) { Log.Info("myUnit == null"); return(1); } ZhuiZhuAimComponent zhuiZhuAimPathComponent = myUnit.GetComponent <ZhuiZhuAimComponent>(); if (zhuiZhuAimPathComponent == null || zhuiZhuAimPathComponent.Aim == null) { Log.Info("zhuiZhuAimPathComponent == null||zhuiZhuAimPathComponent.Aim==null"); return(2); } return(0); }
public static void Check(this AIComponent self) { if (self.Parent == null) { TimerComponent.Instance.Remove(ref self.Timer); return; } SortedDictionary <int, AIConfig> oneAI = AIConfigCategory.Instance.AIConfigs[self.AIConfigId]; foreach (AIConfig aiConfig in oneAI.Values) { AIDispatcherComponent.Instance.AIHandlers.TryGetValue(aiConfig.Name, out AAIHandler aaiHandler); if (aaiHandler == null) { Log.Error($"not found aihandler: {aiConfig.Name}"); continue; } int ret = aaiHandler.Check(self, aiConfig); if (ret != 0) { continue; } if (self.Current == aiConfig.Id) { break; } self.Cancel(); // 取消之前的行为 ETCancellationToken cancellationToken = new ETCancellationToken(); self.CancellationToken = cancellationToken; self.Current = aiConfig.Id; aaiHandler.Execute(self, aiConfig, cancellationToken).Coroutine(); return; } }
private static void Cancel(this AIComponent self) { self.CancellationToken?.Cancel(); self.Current = 0; self.CancellationToken = null; }
// 协程编写必须可以取消 public abstract ETTask Execute(AIComponent aiComponent, AIConfig aiConfig, ETCancellationToken cancellationToken);
// 检查是否满足条件 public abstract int Check(AIComponent aiComponent, AIConfig aiConfig);