public void Connect(WayPoint NewWayPoint) { PathFinderNode currentNode = Path; while (currentNode != null) { currentNode.RemoveFromLayer(); currentNode = currentNode.NextNode; } if (NextWayPoint != null && NextWayPoint.PreviousWayPoint == this) { NextWayPoint.PreviousWayPoint = null; } Path = Map.PathFinder.FindPath(Point, NewWayPoint.Point); if (Path != null) { Path.PathLayer = PathLayer; NextWayPoint = NewWayPoint; NewWayPoint.PreviousWayPoint = this; currentNode = Path; while (currentNode != null) { PathLayer.AddObject(currentNode); currentNode = currentNode.NextNode; } } }
public PathFinderNode FindPath(Point Begin, Point End) { PathFinderNode[,] nodeGrid = new PathFinderNode[Map.Bounds.X, Map.Bounds.Y]; nodeGrid.Function((x, y, v) => new PathFinderNode(Map[new Point(x, y)])); Map.MapObjects.ToArray().Action( (i, mapObject) => { if (mapObject.IsWall) { nodeGrid[mapObject.Point.X, mapObject.Point.Y].Blocked = true; } } ); nodeGrid.Action( (x, y, node) => node.AdjacentNodes = node.MapTile.AdjacentTiles .Select(mapTile => nodeGrid[mapTile.Point.X, mapTile.Point.Y]) .Where(node => !node.Blocked) .ToArray() ); nodeGrid[Begin.X, Begin.Y].Opened = true; IEnumerable <PathFinderNode> openNodeQuery = nodeGrid.Cast <PathFinderNode>().Where(node => node.Opened && !node.Closed); PathFinderNode[] openNodes = openNodeQuery.ToArray(); PathFinderNode endNode = openNodes.FirstOrDefault(node => node.Point == End); while (openNodes.Length > 0) { openNodes.Action( (i, openNode) => { openNode.Closed = true; openNode.AdjacentNodes.Action( (i, adjacentNode) => { if (!adjacentNode.Opened && !adjacentNode.Closed) { adjacentNode.Opened = true; adjacentNode.PreviousNode = openNode; } } ); } ); if ((endNode = openNodes.FirstOrDefault(node => node.Point == End)) != null) { break; } openNodes = openNodeQuery.ToArray(); } if (endNode != null) { return(endNode.BackConnectToFirstNode()); } return(null); }
public PathFinderNode BackConnectToFirstNode() { PathFinderNode node = this; while (node.PreviousNode != null) { node = (node.PreviousNode.NextNode = node).PreviousNode; } return(node); }
public void Disconnect() { PathFinderNode currentNode = Path; while (currentNode != null) { currentNode.RemoveFromLayer(); currentNode = currentNode.NextNode; } }
public PathFinderNode FirstNodeBackwardsWhere(Func <PathFinderNode, bool> func) { PathFinderNode node = this; while (node != null) { if (func(node)) { return(node); } node = node.PreviousNode; } return(null); }
public PathFinderNode FirstNodeWhere(Func <PathFinderNode, bool> func) { PathFinderNode node = this; while (node != null) { if (func(node)) { return(node); } node = node.NextNode; } return(null); }
public NodeEnumerator(PathFinderNode CurrentNode) { FirstNode = this.CurrentNode = CurrentNode; }