public PathWaypoint(Point location, int cost = 10, PathWaypoint parent = null) { Length = 0; Cost = cost; Location = location; SetParent(parent); }
public Path(PathWaypoint waypoint, Point start, Point end, bool reversed = false) { Waypoints = new List <Point>(); Start = start; End = end; Length = 0; PathWaypoint p = waypoint; while (p != null) { Waypoints.Add(p.Location); p = p.Parent; if (p != null) { if (p.Length > Length) { Length = p.Length; } } } // reverse the list if (reversed) { Waypoints.Reverse(); } }
public void SetParent(PathWaypoint parent) { if (parent == null) { Length = 0; } else { parent.Child = this; Parent = parent; Length = Parent.Length + Cost; } }
public Path Generate(CollisionMapComponent collisionMap, IsometricMapComponent map, Point start, List <Point> end, PathValidationHandler validation) { // reset the lists _openList.Clear(); _closedList.Clear(); foreach (Point p in end) { if (!collisionMap.Map.ContainsKey(p)) { collisionMap.Map.Add(p, PathTypes.UNDEFINED); } } if (!collisionMap.Map.ContainsKey(start)) { collisionMap.Map.Add(start, PathTypes.UNDEFINED); } if (collisionMap.Map[start] == PathTypes.BLOCKED) { return(new Path()); } _openList.Add(start, new PathWaypoint(start)); while (_openList.Count > 0) { int min = Int32.MaxValue; PathWaypoint selectedNode = null; foreach (PathWaypoint node in _openList.Values) { int G = node.Length; int H = MathHelper.Distance(start, node.Location); int length = G + H; if (length < min) { min = length; selectedNode = node; } } _openList.Remove(selectedNode.Location); _closedList.Add(selectedNode.Location, selectedNode); foreach (Point p in end) { if (selectedNode.Location == p) { return(new Path(selectedNode, start, p, true)); } } for (int x = -1; x < 2; x++) { for (int y = -1; y < 2; y++) { if (x != 0 && y != 0) { continue; } // no diags or self if (x == y || x == -y || -x == y) { continue; } if (!map.IsValidIndex(selectedNode.Location.X + x, selectedNode.Location.Y + y)) { continue; } Point p = new Point(selectedNode.Location.X + x, selectedNode.Location.Y + y); if (!collisionMap.Map.ContainsKey(p)) { collisionMap.Map.Add(p, PathTypes.UNDEFINED); } PathWaypoint waypoint = new PathWaypoint( p, (int)collisionMap.Map[p], selectedNode ); if (validation(waypoint.Location) && !_closedList.ContainsKey(waypoint.Location)) { if (_openList.ContainsKey(waypoint.Location)) { if (_openList[waypoint.Location].Length > waypoint.Length) { _openList[waypoint.Location].SetParent(waypoint.Parent); } } else { _openList.Add(waypoint.Location, waypoint); } } } } } return(new Path()); }