/* * constructors */ /// <summary> /// Default constructor /// </summary> /// <param name="m">The Gameworld.Map to copy dimensional and validity data from</param> public NodeMap(Map m) { this.height = m.GetHeight(); this.width = m.GetWidth(); this.nodes = new Node[width, height]; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { nodes[i, j] = new Node(i, j, m.GetCellAt(i, j).GetTile().Passable() && !(m.GetCellAt(i, j).ContainsActiveEntities)); } } }
/* * public functions */ /// <summary> /// The core function; calls all valid auxiliary functions and returns the path (advanced features can be toggled) /// Includes a boolean toggle for use of advanced pathfinding functions /// </summary> /// <param name="map">The Map</param> /// <param name="start">The starting Cell</param> /// <param name="end">The ending Cell</param> /// <param name="advanced"> A boolean toggle for advanced functions</param> /// <returns>The path as a list of waypoints</returns> public static List <CellComponent> between(Map map, CellComponent start, CellComponent end, bool advanced) { // begin timing the operation DateTime startTime = DateTime.Now; // convert the given Cell-based data to Node-based data NodeMap nodeMap = new NodeMap(map); Node nodeStart = nodeMap.getNode(start.X, start.Y); Node nodeEnd = nodeMap.getNode(end.X, end.Y); // perform advanced pre-calculation tasks if (advanced) { // if the end Node is invalid, replace it with the nearest valid Node if (!nodeEnd.isValid) { nodeEnd = Advanced.nearestValidEnd(nodeMap, nodeStart, nodeEnd); } } // find the path List <Node> nodePath = Basic.findPath(nodeMap, nodeStart, nodeEnd); // convert the path from List<Node> format back to List<Cell> format List <CellComponent> path = new List <CellComponent>(nodePath.Count); for (int i = 0; i < nodePath.Count; i++) { path.Add(map.GetCellAt(nodePath[i].X, nodePath[i].Y)); } // grab and print path data float dist = (float)(nodePath[nodePath.Count - 1].Gscore); span = DateTime.Now - startTime; //printPath(path, dist); return(path); }