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 = self.Domain.GetComponent <PathfindingComponent>(); self.ABPath = EntityFactory.Create <ABPathWrap, Vector3, Vector3>(self.Domain, 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 = EntityFactory.Create <ETCancellationTokenSource>(self.Domain); await self.MoveAsync(self.ABPath.Result); self.CancellationTokenSource.Dispose(); self.CancellationTokenSource = null; }
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>(); RecastPathComponent recastPathComponent = self.Domain.GetComponent <RecastPathComponent>(); RecastPath recastPath = EntityFactory.Create <RecastPath>((Entity)self.Domain); 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($"------start Pos: {self.RecastPath.StartPos}\n------end Pos: {self.RecastPath.EndPos}\n------find result: {self.RecastPath.Results.ListToString()}"); self.CancellationToken?.Cancel(); self.CancellationToken = new ETCancellationToken(); await self.MoveAsync(self.RecastPath.Results); self.CancellationToken = null; }
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>(); }
// 从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; MessageHelper.Broadcast(unit, m2CPathfindingResult); }
public static async ETTask MoveAsync(this UnitPathComponent self, List <Vector3> path) { if (path.Count == 0) { return; } // 第一个点是unit的当前位置,所以不用发送 for (int i = 1; i < path.Count; ++i) { // 每移动3个点发送下3个点给客户端 if (i % 3 == 1) { self.BroadcastPath(path, i, 3); } Vector3 v3 = path[i]; await self.Parent.GetComponent <MoveComponent>().MoveToAsync(v3, self.CancellationToken); } }
public static async ETVoid StartMove(this UnitPathComponent self, M2C_PathfindingResult message) { // 取消之前的移动协程 self.ETCancellationToken?.Cancel(); self.ETCancellationToken = new ETCancellationToken(); self.Path.Clear(); for (int i = 0; i < message.Xs.Count; ++i) { self.Path.Add(new Vector3(message.Xs[i], message.Ys[i], message.Zs[i])); } self.ServerPos = new Vector3(message.X, message.Y, message.Z); await self.StartMove_Internal(self.ETCancellationToken); self.ETCancellationToken = null; }
protected override async ETTask Run(Session session, M2C_PathfindingResult message) { Unit unit = Game.Scene.GetComponent <UnitComponent>().Get(message.Id); unit.GetComponent <AnimatorComponent>().SetFloatValue("Speed", 5f); UnitPathComponent unitPathComponent = unit.GetComponent <UnitPathComponent>(); unitPathComponent.StartMove(message).Coroutine(); //GizmosDebug.Instance.Path.Clear(); //GizmosDebug.Instance.Path.Add(new Vector3(message.X, message.Y, message.Z)); //for (int i = 0; i < message.Xs.Count; ++i) //{ // GizmosDebug.Instance.Path.Add(new Vector3(message.Xs[i], message.Ys[i], message.Zs[i])); //} await ETTask.CompletedTask; }
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>(); unit.Position = target; M2C_PathfindingResult m2CPathfindingResult = new M2C_PathfindingResult(); m2CPathfindingResult.X = unit.Position.x; m2CPathfindingResult.Y = unit.Position.y; m2CPathfindingResult.Z = unit.Position.z; m2CPathfindingResult.Id = unit.Id; MessageHelper.Broadcast(unit, m2CPathfindingResult); }
// 从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.ABPath.Result.Count) { break; } Vector3 v = self.ABPath.Result[index + i]; m2CPathfindingResult.Xs.Add(v.x); m2CPathfindingResult.Ys.Add(v.y); m2CPathfindingResult.Zs.Add(v.z); } MessageHelper.Broadcast(unit, m2CPathfindingResult); }
private static async ETTask StartMove_Internal(this UnitPathComponent self, ETCancellationToken cancellationToken) { for (int i = 0; i < self.Path.Count; ++i) { Vector3 v = self.Path[i]; float speed = 5; if (i == 0) { // 矫正移动速度 Vector3 clientPos = self.GetParent <Unit>().Position; float serverf = (self.ServerPos - v).magnitude; if (serverf > 0.1f) { float clientf = (clientPos - v).magnitude; speed = clientf / serverf * speed; } } self.Parent.GetComponent <TurnComponent>().Turn(v); await self.Parent.GetComponent <MoveComponent>().MoveToAsync(v, speed, cancellationToken); } }