public static async ETVoid MoveTo(this UnitPathComponent self, Vector3 target) { if ((self.Target - target).magnitude < 0.1f) { return; } self.Target = target; Unit unit = self.GetParent <Unit>(); PathfindingComponent pathfindingComponent = Game.Scene.GetComponent <PathfindingComponent>(); self.ABPath = ComponentFactory.Create <ETModel.ABPath, Vector3, Vector3>(unit.Position, new Vector3(target.x, target.y, target.z)); pathfindingComponent.Search(self.ABPath); Log.Debug($"find result: {self.ABPath.Result.ListToString()}"); self.CancellationTokenSource?.Cancel(); self.CancellationTokenSource = new CancellationTokenSource(); await self.MoveAsync(self.ABPath.Result); self.CancellationTokenSource.Dispose(); self.CancellationTokenSource = null; }
/// <summary> /// 移动到目标点,随后进入指定状态(如果有的话) /// </summary> /// <param name="self"></param> /// <param name="target"></param> /// <param name="bindState"></param> /// <param name="nextState"></param> /// <param name="targetRange">目标范围,当自身与目标位置小于等于此范围时,则停止寻路,进入NextState</param> /// <returns></returns> private static async ETVoid MoveTodoSomething(this UnitPathComponent self, Vector3 target, AFsmStateBase bindState, AFsmStateBase nextState = null, float targetRange = 0) { self.BindState = bindState; self.NextState = nextState; self.Target = target; self.TargetRange = targetRange; Unit unit = self.GetParent <Unit>(); RecastPathComponent recastPathComponent = Game.Scene.GetComponent <RecastPathComponent>(); RecastPath recastPath = ReferencePool.Acquire <RecastPath>(); recastPath.StartPos = unit.Position; recastPath.EndPos = new Vector3(target.x, target.y, target.z); self.RecastPath = recastPath; //TODO 因为目前阶段只有一张地图,所以默认mapId为10001 recastPathComponent.SearchPath(10001, self.RecastPath); //Log.Debug($"find result: {self.ABPath.Result.ListToString()}"); self.CancellationTokenSource?.Cancel(); self.CancellationTokenSource = new CancellationTokenSource(); await self.MoveAsync(self.RecastPath.Results); self.CancellationTokenSource.Dispose(); self.CancellationTokenSource = null; if (nextState != null) { self.Entity.GetComponent <StackFsmComponent>().ChangeState(nextState); } self.Entity.GetComponent <StackFsmComponent>().RemoveState(bindState.StateName); }
// 从index找接下来3个点,广播 public static void BroadcastPath(this UnitPathComponent self, List <Vector3> path, int index, int offset) { Unit unit = self.GetParent <Unit>(); Vector3 unitPos = unit.Position; M2C_PathfindingResult m2CPathfindingResult = new M2C_PathfindingResult(); m2CPathfindingResult.X = unitPos.x; m2CPathfindingResult.Y = unitPos.y; m2CPathfindingResult.Z = unitPos.z; m2CPathfindingResult.Id = unit.Id; for (int i = 0; i < offset; ++i) { if (index + i >= self.RecastPath.Results.Count) { break; } Vector3 v = self.RecastPath.Results[index + i]; m2CPathfindingResult.Xs.Add(v.x); m2CPathfindingResult.Ys.Add(v.y); m2CPathfindingResult.Zs.Add(v.z); } MessageHelper.Broadcast(m2CPathfindingResult); }
public static async ETVoid MoveTo_InternalWithOutStateChange(this UnitPathComponent self, Vector3 target) { if ((self.Target - target).magnitude < 0.1f) { return; } self.Target = target; Unit unit = self.GetParent <Unit>(); RecastPathComponent recastPathComponent = Game.Scene.GetComponent <RecastPathComponent>(); RecastPath recastPath = ReferencePool.Acquire <RecastPath>(); recastPath.StartPos = unit.Position; recastPath.EndPos = new Vector3(target.x, target.y, target.z); self.RecastPath = recastPath; //TODO 因为目前阶段只有一张地图,所以默认mapId为10001 recastPathComponent.SearchPath(10001, self.RecastPath); //Log.Debug($"find result: {self.ABPath.Result.ListToString()}"); self.ETCancellationTokenSource?.Cancel(); self.ETCancellationTokenSource = ComponentFactory.Create <ETCancellationTokenSource>(); await self.MoveAsync(self.RecastPath.Results); self.ETCancellationTokenSource.Dispose(); }