/// <summary> /// Moves to a point right now. Before using this function, check if you cannot use MoveToQueue(Point p). This function may /// impact the FPS in a bad manner if used outside the queue. /// </summary> /// <param name="p">The point to move to</param> public void MoveToNow(Point p) { long ticks = DateTime.UtcNow.Ticks; if (Game1.GetInstance().collision.IsCollisionBetween(new Point((int)this.x, (int)this.y), p)) { Game1 game = Game1.GetInstance(); // Create temp nodes Node start = new Node(game.collision, (int)this.x, (int)this.y, true); Node end = new Node(game.collision, p.X, p.Y, true); LinkedList<PathfindingNode> nodes = new AStar(start, end).FindPath(); if (nodes != null) { // Remove the first node, because that's the node we're currently on .. nodes.RemoveFirst(); // Clear our current waypoints this.waypoints.Clear(); /*PathfindingNode previousNode = null; foreach (Node node in nodes) { node.selected = true; if (previousNode != null) { PathfindingNodeConnection conn = node.IsConnected(previousNode); if (conn != null && ((Node)conn.node1).selected && ((Node)conn.node2).selected) conn.drawColor = Color.Blue; } previousNode = node; }*/ foreach (Node n in nodes) { this.waypoints.AddLast(n.GetLocation()); } } // Nodes can no longer be used start.Destroy(); end.Destroy(); } else { this.waypoints.Clear(); this.waypoints.AddLast(p); } if (this.waypoints.Count > 0) { Point newTarget = this.waypoints.ElementAt(0); SetMoveToTarget(newTarget.X, newTarget.Y); } // Console.Out.WriteLine("Found path in " + ((DateTime.UtcNow.Ticks - ticks) / 10000) + "ms"); }
/// <summary> /// Calculates a path between the current unit and the point. /// </summary> /// <param name="p">The point to calculate to.</param> /// <returns>The list containing all the points that you should visit.</returns> public LinkedList<Point> CalculatePath(Point p) { LinkedList<Point> result = new LinkedList<Point>(); long ticks = DateTime.UtcNow.Ticks; if (Game1.GetInstance().collision.IsCollisionBetween(new Point((int)this.x, (int)this.y), p)) { Game1 game = Game1.GetInstance(); // Create temp nodes Node start = new Node(game.collision, (int)this.x, (int)this.y, true); Node end = new Node(game.collision, p.X, p.Y, true); LinkedList<PathfindingNode> nodes = new AStar(start, end).FindPath(); if (nodes != null) { // Remove the first node, because that's the node we're currently on .. nodes.RemoveFirst(); // Clear our current waypoints this.waypoints.Clear(); foreach (Node n in nodes) { result.AddLast(n.GetLocation()); } } // Nodes can no longer be used start.Destroy(); end.Destroy(); } else { result.AddLast(p); } return result; }