public Vector2Int[] Find(Vector2Int start, Vector2Int end) { List <Vector2Int> vector2Ints = m_aStar.FindPath(start, end, OnCalculatePathCostHandler); return(vector2Ints.ToArray()); }
void PathfindHallways() { DungeonPathfinder2D aStar = new DungeonPathfinder2D(size); foreach (var edge in selectedEdges) { var startRoom = (edge.U as Vertex <Room>).Item; var endRoom = (edge.V as Vertex <Room>).Item; var startPosf = startRoom.bounds.center; var endPosf = endRoom.bounds.center; var startPos = new Vector2Int((int)startPosf.x, (int)startPosf.y); var endPos = new Vector2Int((int)endPosf.x, (int)endPosf.y); var path = aStar.FindPath(startPos, endPos, (DungeonPathfinder2D.Node a, DungeonPathfinder2D.Node b) => { var pathCost = new DungeonPathfinder2D.PathCost(); pathCost.cost = Vector2Int.Distance(b.Position, endPos); //heuristic if (grid[b.Position] == CellType.Room) { pathCost.cost += 10; } else if (grid[b.Position] == CellType.None) { pathCost.cost += 5; } else if (grid[b.Position] == CellType.Hallway) { pathCost.cost += 1; } pathCost.traversable = true; return(pathCost); }); if (path != null) { for (int i = 0; i < path.Count; i++) { var current = path[i]; if (grid[current] == CellType.None) { grid[current] = CellType.Hallway; } if (i > 0) { var prev = path[i - 1]; var delta = current - prev; } } foreach (var pos in path) { if (grid[pos] == CellType.Hallway) { PlaceHallway(pos); } } } } }
void PathfindHallways() { DungeonPathfinder2D aStar = new DungeonPathfinder2D(size); foreach (var edge in selectedEdges) { var startRoom = (edge.U as Vertex <Room2D>).Item; var endRoom = (edge.V as Vertex <Room2D>).Item; var startPosf = startRoom.bounds.center; var endPosf = endRoom.bounds.center; var startPos = new Vector2Int((int)startPosf.x, (int)startPosf.y); var endPos = new Vector2Int((int)endPosf.x, (int)endPosf.y); var path = aStar.FindPath(startPos, endPos, (DungeonPathfinder2D.Node a, DungeonPathfinder2D.Node b) => { var pathCost = new DungeonPathfinder2D.PathCost(); pathCost.cost = Vector2Int.Distance(b.Position, endPos); //heuristic if (grid[b.Position] == CellType.Room) { pathCost.cost += 10; } else if (grid[b.Position] == CellType.None) { pathCost.cost += 5; } else if (grid[b.Position] == CellType.Hallway) { pathCost.cost += 1; } pathCost.traversable = true; return(pathCost); }); if (path != null) { for (int i = 0; i < path.Count; i++) { var current = path[i]; if (grid[current] == CellType.None) { grid[current] = CellType.Hallway; } if (i > 0) { var prev = path[i - 1]; var delta = current - prev; //add by csd pathVector tmpPath = new pathVector(); tmpPath.sourVector = prev; tmpPath.destVector = current; pathLst.Add(tmpPath); //add end } } foreach (var pos in path) { if (grid[pos] == CellType.Hallway) { PlaceHallway(pos); //add by csd begin bool isCreate = grid.getDataIsCreate(pos); if (isCreate == false) //不是第一次经过 { //HallWay newHallWay = new HallWay(pos, new Vector3Int(1, 1, 1), planePrefab, wallPrefab, hallWayPlaceMaterial, placeGrid, roomIndex, mazeParent); GameObject newHallWayObj = new GameObject(); newHallWayObj.transform.SetParent(mazeParent.transform); newHallWayObj.name = "hallway" + pos.x.ToString() + "_" + csPosY.ToString() + "_" + pos.y.ToString(); HallWay2D newHallWay = newHallWayObj.AddComponent <HallWay2D>(); newHallWay.initDataHallWay(pos, new Vector2Int(1, 1), wayPrefab, wallPrefab, hallWayPlaceMaterial, placeGrid, roomIndex, newHallWayObj); /* * HallWay newHallWay = mazeParent.AddComponent<HallWay>(); * newHallWay.initData(pos, new Vector3Int(1, 1, 1), planePrefab, wallPrefab, hallWayPlaceMaterial, placeGrid, roomIndex, mazeParent); */ hallways.Add(newHallWay); grid.setGridDataObj(newHallWay, pos); roomIndex++; // grid.setDataIsCreate(pos,true); } //add end } } } } }