/// <summary> /// Makes an <see cref="Entity"/> move towards a <paramref name="target"/> using pathfinding. /// </summary> private void StartMoving(Entity entity, Vector2 target) { #region Sanity checks if (entity == null) { throw new ArgumentNullException(nameof(entity)); } #endregion if (Pathfinder == null) { Log.Warn("Pathfinder not initialized"); return; } if (entity.TemplateData.Movement == null) { Log.Warn(entity + " has no Movement component"); return; } if (entity.Position == target) { return; } if (entity.CurrentPath != null && entity.CurrentPath.Target == target && entity.CurrentPath.PathNodes.Count != 0) { return; } using (new TimedLogEvent("Calculating path from " + entity.Position + " to " + target)) { // Get path and cancel if none was found var pathNodes = Pathfinder.FindPath(GetScaledPosition(entity.Position), GetScaledPosition(target)); if (pathNodes == null) { entity.CurrentPath = null; return; } // Store path data in entity var pathLeader = new StoredPath <Vector2> { Target = target }; foreach (var node in pathNodes) { pathLeader.PathNodes.Enqueue(node * Terrain.Size.StretchH); } entity.CurrentPath = pathLeader; } }
/// <summary> /// On New Path subscribed event function. /// </summary> /// <param name="sender"><see cref="AIBaseClient" /> sender.</param> /// <param name="args">Path Data</param> private static void AIHeroClient_OnNewPath(AIBaseClient sender, AIBaseClientNewPathEventArgs args) { if (!(sender is AIHeroClient)) { return; } if (!StoredPaths.ContainsKey(sender.NetworkId)) { StoredPaths.Add(sender.NetworkId, new List <StoredPath>()); } var newPath = new StoredPath { Tick = Variables.TickCount, Path = args.Path.ToList().ToVector2() }; StoredPaths[sender.NetworkId].Add(newPath); if (StoredPaths[sender.NetworkId].Count > 50) { StoredPaths[sender.NetworkId].RemoveRange(0, 40); } }
private static void Obj_AI_Hero_OnNewPath(Obj_AI_Base sender, GameObjectNewPathEventArgs args) { if (!(sender is Obj_AI_Hero)) { return; } if (!StoredPaths.ContainsKey(sender.NetworkId)) { StoredPaths.Add(sender.NetworkId, new List<StoredPath>()); } var newPath = new StoredPath { Tick = Utils.TickCount, Path = args.Path.ToList().To2D() }; StoredPaths[sender.NetworkId].Add(newPath); if (StoredPaths[sender.NetworkId].Count > 50) { StoredPaths[sender.NetworkId].RemoveRange(0, 40); } }