public void Execute() { DynamicBuffer <PathPosition> pathPositionBuffer = pathPositionBufferFromEntity[entity]; pathPositionBuffer.Clear(); PathfindingParams pathfindingParams = pathfindingParamsComponentDataFromEntity[entity]; int endNodeIndex = CalculateIndex(pathfindingParams.endPosition.x, pathfindingParams.endPosition.y, gridSize.x); OldPathNode endNode = pathNodeArray[endNodeIndex]; if (endNode.cameFromNodeIndex == -1) { // Didn't find a path! //Debug.Log("Didn't find a path!"); pathFollowComponentDataFromEntity[entity] = new PathFollow { pathIndex = -1 }; } else { // Found a path CalculatePath(pathNodeArray, endNode, pathPositionBuffer); pathFollowComponentDataFromEntity[entity] = new PathFollow { pathIndex = pathPositionBuffer.Length - 1 }; } }
private static int GetLowestCostFNodeIndex(NativeList <int> openList, NativeArray <OldPathNode> pathNodeArray) { OldPathNode lowestCostPathNode = pathNodeArray[openList[0]]; for (int i = 1; i < openList.Length; i++) { OldPathNode testPathNode = pathNodeArray[openList[i]]; if (testPathNode.fCost < lowestCostPathNode.fCost) { lowestCostPathNode = testPathNode; } } return(lowestCostPathNode.index); }
//TODO: currently making this public to initialize via the pathfinding system, since we dont have a proper understanding of the size of the rooms yet public void initializePathNodeArray(int width, int height) { pathNodeArray = new NativeArray <OldPathNode>(width * height, Allocator.Persistent); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { OldPathNode pathNode = new OldPathNode(); pathNode.x = x; pathNode.y = y; pathNode.index = CalculateIndex(x, y, width); pathNode.gCost = int.MaxValue; //TODO: this is where we set some stuff with obstacles pathNode.isWalkable = true; pathNode.cameFromNodeIndex = -1; pathNodeArray[pathNode.index] = pathNode; } } }
private static NativeList <int2> CalculatePath(NativeArray <OldPathNode> pathNodeArray, OldPathNode endNode) { if (endNode.cameFromNodeIndex == -1) { // Couldn't find a path! return(new NativeList <int2>(Allocator.Temp)); } else { // Found a path NativeList <int2> path = new NativeList <int2>(Allocator.Temp); path.Add(new int2(endNode.x, endNode.y)); OldPathNode currentNode = endNode; while (currentNode.cameFromNodeIndex != -1) { OldPathNode cameFromNode = pathNodeArray[currentNode.cameFromNodeIndex]; path.Add(new int2(cameFromNode.x, cameFromNode.y)); currentNode = cameFromNode; } return(path); } }
private static void CalculatePath(NativeArray <OldPathNode> pathNodeArray, OldPathNode endNode, DynamicBuffer <PathPosition> pathPositionBuffer) { if (endNode.cameFromNodeIndex == -1) { // Couldn't find a path! } else { // Found a path pathPositionBuffer.Add(new PathPosition { position = new int2(endNode.x, endNode.y) }); OldPathNode currentNode = endNode; while (currentNode.cameFromNodeIndex != -1) { OldPathNode cameFromNode = pathNodeArray[currentNode.cameFromNodeIndex]; pathPositionBuffer.Add(new PathPosition { position = new int2(cameFromNode.x, cameFromNode.y) }); currentNode = cameFromNode; } } }
//public BufferFromEntity<PathPosition> pathPositionBuffer; public void Execute() { for (int i = 0; i < pathNodeArray.Length; i++) { OldPathNode pathNode = pathNodeArray[i]; pathNode.hCost = CalculateDistanceCost(new int2(pathNode.x, pathNode.y), endPosition); pathNode.cameFromNodeIndex = -1; pathNodeArray[i] = pathNode; } int endNodeIndex = CalculateIndex(endPosition.x, endPosition.y, gridSize.x); OldPathNode startNode = pathNodeArray[CalculateIndex(startPosition.x, startPosition.y, gridSize.x)]; startNode.gCost = 0; startNode.CalculateFCost(); pathNodeArray[startNode.index] = startNode; NativeList <int> openList = new NativeList <int>(Allocator.Temp); NativeList <int> closedList = new NativeList <int>(Allocator.Temp); openList.Add(startNode.index); while (openList.Length > 0) { int currentNodeIndex = GetLowestCostFNodeIndex(openList, pathNodeArray); OldPathNode currentNode = pathNodeArray[currentNodeIndex]; if (currentNodeIndex == endNodeIndex) { // Reached our destination! break; } // Remove current node from Open List for (int i = 0; i < openList.Length; i++) { if (openList[i] == currentNodeIndex) { openList.RemoveAtSwapBack(i); break; } } closedList.Add(currentNodeIndex); for (int i = 0; i < neighbourOffsetArray.Length; i++) { int2 neighbourOffset = neighbourOffsetArray[i]; int2 neighbourPosition = new int2(currentNode.x + neighbourOffset.x, currentNode.y + neighbourOffset.y); if (!IsPositionInsideGrid(neighbourPosition, gridSize)) { // Neighbour not valid position continue; } int neighbourNodeIndex = CalculateIndex(neighbourPosition.x, neighbourPosition.y, gridSize.x); if (closedList.Contains(neighbourNodeIndex)) { // Already searched this node continue; } OldPathNode neighbourNode = pathNodeArray[neighbourNodeIndex]; if (!neighbourNode.isWalkable) { // Not walkable continue; } int2 currentNodePosition = new int2(currentNode.x, currentNode.y); int tentativeGCost = currentNode.gCost + CalculateDistanceCost(currentNodePosition, neighbourPosition); if (tentativeGCost < neighbourNode.gCost) { neighbourNode.cameFromNodeIndex = currentNodeIndex; neighbourNode.gCost = tentativeGCost; neighbourNode.CalculateFCost(); pathNodeArray[neighbourNodeIndex] = neighbourNode; if (!openList.Contains(neighbourNode.index)) { openList.Add(neighbourNode.index); } } } } //pathPositionBuffer.Clear(); /* * PathNode endNode = pathNodeArray[endNodeIndex]; * if (endNode.cameFromNodeIndex == -1) { * // Didn't find a path! * //Debug.Log("Didn't find a path!"); * pathFollowComponentDataFromEntity[entity] = new PathFollow { pathIndex = -1 }; * } else { * // Found a path * //CalculatePath(pathNodeArray, endNode, pathPositionBuffer); * //pathFollowComponentDataFromEntity[entity] = new PathFollow { pathIndex = pathPositionBuffer.Length - 1 }; * } */ //neighbourOffsetArray.Dispose(); openList.Dispose(); closedList.Dispose(); }