private void InitializeSearchNodes(MapBuilder map) { searchNodes = new NodeRecord[levelWidth, levelHeight]; for (int x = 0; x < levelWidth; x++) { for (int y = 0; y < levelHeight; y++) { NodeRecord node = new NodeRecord(); node.position = new Point(x, y); node.movable = map.GetIndex(y, x) == 0; if (node.movable == true) { node.connections = new NodeRecord[4]; searchNodes[x, y] = node; } } } for (int x = 0; x < levelWidth; x++) { for (int y = 0; y < levelHeight; y++) { NodeRecord node = searchNodes[x, y]; if (node == null || node.movable == false) { continue; } // implement diagonal in assignment... Point[] connections = new Point[] { new Point (x, y - 1), // up new Point (x, y + 1), // down new Point (x - 1, y), // left new Point (x + 1, y), // right }; // loop through connections for (int i = 0; i < connections.Length; i++) { Point position = connections[i]; if (position.X < 0 || position.X > levelWidth - 1 || position.Y < 0 || position.Y > levelHeight - 1) { continue; } NodeRecord connection = searchNodes[position.X, position.Y]; if (connection == null || connection.movable == false) { continue; } node.connections[i] = connection; } } } }
private List<Vector2> FindFinalPath(NodeRecord startNode, NodeRecord endNode) { closed.Add(endNode); NodeRecord parentTile = endNode.parentNode; // trace back through parents to find path while (parentTile != startNode) { closed.Add(parentTile); parentTile = parentTile.parentNode; } List<Vector2> localisedPath = new List<Vector2>(); // localise path for (int i = closed.Count - 1; i >= 0; i--) { localisedPath.Add(new Vector2(closed[i].position.X * MapBuilder.TILE_SIZE, closed[i].position.Y * MapBuilder.TILE_SIZE)); } return localisedPath; }