/// <summary> /// Create a new instance of the grid, instantiating the corresponding active algorithms nodes. /// </summary> public void CreateGrid() { CheckExistingGrid(); grid = new Node[gridSizeX, gridSizeY]; Vector2 bottomLeft = (Vector2)transform.position - Vector2.right * gridWorldSize.x / 2 - Vector2.up * gridWorldSize.y / 2; // Calculate the bottom left position of the grid by starting from the centre (transform.position) and subtracting half the length and height of the grid. // Cycle through every position of the grid. for (int y = 0; y < gridSizeY; y++) { for (int x = 0; x < gridSizeX; x++) { Vector2 worldPoint = bottomLeft + Vector2.right * (x * nodeDiameter + nodeRadius) + Vector2.up * (y * nodeDiameter + nodeRadius); // Calculate position the node is in the world based on position in the grid and node size. bool walkable = true; // Flag as not being obstructed by default. // CircleCast at the current position of the grid to check if there are any obstacles, useful for adapting to level design changes. if (Physics2D.CircleCast(worldPoint, nodeRadius, Vector2.zero, 0.5f, unwalkableMask)) { walkable = false; // Flag as being obstructed if an obstacle was overalpping the circlecast. } // Instantiate node game object and scale to set size. GameObject _nodeGO = Instantiate(node, worldPoint, Quaternion.identity); _nodeGO.transform.localScale = Vector3.one * (nodeDiameter - distanceBetweenNodes); // Assign active algorithms node class to the node game object and store at the current grid position. switch (PathfinderFactory.ActiveAlgorithm) { case PathfinderFactory.Pathfinding.AStar: AStarNode _aStarNode = _nodeGO.AddComponent <AStarNode>(); _aStarNode.SetValues(walkable, worldPoint, x, y); grid[x, y] = _aStarNode; break; case PathfinderFactory.Pathfinding.Dijkstra: DijkstraNode _dijkstraNode = _nodeGO.AddComponent <DijkstraNode>(); _dijkstraNode.SetValues(walkable, worldPoint, x, y); grid[x, y] = _dijkstraNode; break; case PathfinderFactory.Pathfinding.BFS: BFSNode _BFSNode = _nodeGO.AddComponent <BFSNode>(); _BFSNode.SetValues(walkable, worldPoint, x, y); grid[x, y] = _BFSNode; break; case PathfinderFactory.Pathfinding.DFS: DFSNode _DFSNode = _nodeGO.AddComponent <DFSNode>(); _DFSNode.SetValues(walkable, worldPoint, x, y); grid[x, y] = _DFSNode; break; } } } if (previousObstaclePoints != null) // If any previous obstacles are recorded... { // Loop over each, calculate the node that has taken their position and flag them as not walkable. foreach (Vector2 point in previousObstaclePoints) { Node node = NodeFromWorldPoint(point); node.Walkable = false; } } if (previousStartPosition != Vector2.zero) // If a previous start position exists... { // Calculate the node that has taken its position and flag as start node. Node node = NodeFromWorldPoint(previousStartPosition); node.IsStartNode = true; PathfindingManager.instance.startNode = node.transform; } if (previousEndPosition != Vector2.zero) // If a previous end position exists... { // Calculate the node that has taken its position and flag as end node. Node node = NodeFromWorldPoint(previousEndPosition); node.IsEndNode = true; PathfindingManager.instance.endNode = node.transform; } UpdateNodeColours(); }