public List <PathPoint> FindPath(Vector2 start, Vector2 target) { PathPoint startPoint = pathfindingNodeManager.GetPathPoint(start); PathPoint targetPoint = pathfindingNodeManager.GetPathPoint(target); List <PathPoint> openList = new List <PathPoint>(); HashSet <PathPoint> closedList = new HashSet <PathPoint>(); openList.Add(startPoint); while (openList.Count > 0) { PathPoint currentPoint = openList[0]; //Create a point and set it to the first item in the open list for (int i = 1; i < openList.Count; i++) //Loop through the open list starting from the second object { //If the f cost of that object is less than or equal to the f cost of the current node if (openList[i].FCost < currentPoint.FCost || openList[i].FCost == currentPoint.FCost && openList[i].ihCost < currentPoint.ihCost) { currentPoint = openList[i]; //Set the current point to that object } } openList.Remove(currentPoint); //Remove that from the open list closedList.Add(currentPoint); //And add it to the closed list if (currentPoint == targetPoint) //If the current point is the same as the target node { Debug.Log("found"); return(GetFinalPath(startPoint, targetPoint)); //Calculate the final path } //Loop through each neighbor of the current point foreach (PathPoint NeighborNode in currentPoint.neighbours) { //If the neighbor is nonWalkable or has already been checked if (NeighborNode.GetNode == PathfindNode.Nonwalkable || closedList.Contains(NeighborNode)) { continue; //Skip it } float MoveCost = currentPoint.igCost + GetManhattenDistance(currentPoint, NeighborNode); //Get the F cost of that neighbor //If the f cost is greater than the g cost or it is not in the open list if (MoveCost < NeighborNode.igCost || !openList.Contains(NeighborNode)) { NeighborNode.igCost = MoveCost; //Set the g cost to the f cost NeighborNode.ihCost = GetManhattenDistance(NeighborNode, targetPoint); //Set the h cost NeighborNode.parent = currentPoint; //Set the parent of the point for retracing steps //If the neighbor is not in the openlist if (!openList.Contains(NeighborNode)) { openList.Add(NeighborNode); //Add it to the list } } } } return(null); }
private void SetFurnitureHorizontalWall(List <PathPoint> wall, GameObject[] furniture, int storeNumber) { float stepsSize = (1 / tiles.gridSize); for (int xx = 0; xx < wall.Count; xx++) { //If own position iteration is taken go to next iteration. if (!TestPosition(wall[xx].GetPosition, storeNumber)) { continue; } //try for lenght furniture; for (int fur = 0; fur < storeFurniture.Length; fur++) { //Get Furniture and the x size. int g = Random.Range(0, furniture.Length); GameObject furniturePiece = furniture[g]; int blocksX = (int)furniturePiece.GetComponent <FurnitureSpace>().GetSize.x; blocksX -= 1; Vector2 testPosition = new Vector2(wall[xx].GetPosition.x + (stepsSize * blocksX), wall[xx].GetPosition.y); //If furniture fits, place it. if (TestPosition(testPosition, storeNumber) && TestPosition(wall[xx].GetPosition, storeNumber)) { //If point behind furniture = wall if (!TestPosition(new Vector2(wall[xx].GetPosition.x + (stepsSize), wall[xx].GetPosition.y - (stepsSize)), storeNumber)) { objectSpawner.SpawnIndividualObjects(furniture[g], new Vector3(wall[xx].GetPosition.x + (stepsSize * blocksX), 0, wall[xx].GetPosition.y), Quaternion.Euler(0, 180, 0), mallGenerator.gameObject.transform); } else { objectSpawner.SpawnIndividualObjects(furniture[g], new Vector3(wall[xx].GetPosition.x, 0, wall[xx].GetPosition.y), Quaternion.Euler(0, 0, 0), mallGenerator.gameObject.transform); } for (int yy = 0; yy <= blocksX; yy++) { PathPoint temp = pathfindingNodeManager.GetPathPoint(new Vector2(wall[xx].GetPosition.x + (stepsSize * yy), wall[xx].GetPosition.y)); temp.SetNode = PathfindNode.Nonwalkable; } //Furniture fits and we placed it. No need for further checking. break; } } } }
public void InitPathfindGrid() { float gridPercentage = (1 / gridSize); //Stores int roomNumber = 0; foreach (MallSpace baseSpace in storeSpaces) { List <Vector2> grid = GetPlazaGrid(baseSpace, true, roomNumber); foreach (Vector2 x in grid) { Vector2 position = new Vector2(x.x - gridPercentage, x.y - gridPercentage); for (int z = 0; z < gridSize; z++) { for (int q = 0; q < gridSize; q++) { if (doors[(int)x.x, (int)x.y] == Tile.Door) { pathfindingNodeManager.AddNavPoint(new PathPoint(roomNumber, new Vector2(position[0], position[1]), 1, PathfindNode.Door)); } else if (stores[roomNumber][(int)x.x, (int)x.y] != Tile.None) { pathfindingNodeManager.AddNavPoint(new PathPoint(roomNumber, new Vector2(position[0], position[1]), 1, PathfindNode.Walkable)); } position[0] += gridPercentage; } position[0] = (x.x - gridPercentage); position[1] += gridPercentage; } } roomNumber++; } mallGenerator.hallNumber = roomNumber; //Hallways and plaza foreach (MallSpace i in hallwaySpaces) { PathPoint existTester; List <Vector2> hallGrid = GetPlazaGrid(i, false, 0); foreach (Vector2 x in hallGrid) { Vector2 position = new Vector2(x.x - gridPercentage, x.y - gridPercentage); for (int z = 0; z < gridSize; z++) { for (int q = 0; q < gridSize; q++) { existTester = pathfindingNodeManager.GetPathPoint(new Vector2(position[0], position[1])); if (existTester == null) //if the point does not exist. { if (doors[(int)x.x, (int)x.y] == Tile.Door) { pathfindingNodeManager.AddNavPoint(new PathPoint(roomNumber, new Vector2(position[0], position[1]), 1, PathfindNode.Door)); } else { pathfindingNodeManager.AddNavPoint(new PathPoint(roomNumber, new Vector2(position[0], position[1]), 1, PathfindNode.Walkable)); } } position[0] += gridPercentage; } position[0] = (x.x - gridPercentage); position[1] += gridPercentage; } } } }