public static MapManage Instance() { if (mMapManager == null) { mMapManager = new MapManage(); } return mMapManager; }
public static PathResult GetPath(MapManage manage, Vector2Int startPosition, Vector2Int endPosition) { if (manage.width < startPosition.x || manage.height < startPosition.y || manage.width < endPosition.x || manage.height < endPosition.y) { return(new PathResult() { success = false }); } MapTile start = manage.map[startPosition.x, startPosition.y]; MapTile end = manage.map[endPosition.x, endPosition.y]; bool success = false; Vector2Int[] path = new Vector2Int[0]; if (!start.isWall && !end.isWall) { // Queue<MapTile> openSet = new Queue<MapTile>(); SimplePriorityQueue <MapTile> openSet = new SimplePriorityQueue <MapTile>(); HashSet <MapTile> closedSet = new HashSet <MapTile>(); openSet.Enqueue(start, start.CostF); // openSet.Enqueue(start); while (openSet.Count > 0) { MapTile current = openSet.Dequeue(); if (current == end) { success = true; break; } closedSet.Add(current); // for (int i = 0; i < 8; i++)//按次序取出8个角的格子 // { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { MapTile neighbour = manage.map[current.posX + (i - 1), current.posY - (j + 1)]; if (neighbour == null || neighbour.isWall || closedSet.Contains(neighbour)) { continue; } float neighbourCost = current.costG + Distance(new Vector2Int(current.posX, current.posY), new Vector2Int(neighbour.posX, neighbour.posY)) + neighbour.costH; if (neighbourCost > neighbour.costG || !openSet.Contains(neighbour)) { neighbour.costG = neighbourCost; neighbour.costH = Distance(new Vector2Int(neighbour.posX, neighbour.posY), new Vector2Int(end.posX, end.posY)); // neighbour.parent = current; if (!openSet.Contains(neighbour)) { openSet.Enqueue(neighbour, neighbour.CostF); // openSet.Enqueue(neighbour); } else { openSet.UpdatePriority(neighbour, neighbour.CostF); } } } } } } // } if (success) { path = PathFinder.CalcPath(start, end); success = path.Length > 0; } return(new PathResult(path, success)); }