summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobAllocateNodes.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-05-23 10:08:29 +0800
committerchai <215380520@qq.com>2024-05-23 10:08:29 +0800
commit8722a9920c1f6119bf6e769cba270e63097f8e25 (patch)
tree2eaf9865de7fb1404546de4a4296553d8f68cc3b /Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobAllocateNodes.cs
parent3ba4020b69e5971bb0df7ee08b31d10ea4d01937 (diff)
+ astar project
Diffstat (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobAllocateNodes.cs')
-rw-r--r--Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobAllocateNodes.cs57
1 files changed, 57 insertions, 0 deletions
diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobAllocateNodes.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobAllocateNodes.cs
new file mode 100644
index 0000000..d301d44
--- /dev/null
+++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobAllocateNodes.cs
@@ -0,0 +1,57 @@
+using Pathfinding.Util;
+using Unity.Collections;
+using Unity.Jobs;
+using Unity.Mathematics;
+
+namespace Pathfinding.Graphs.Grid.Jobs {
+ /// <summary>
+ /// Allocates and deallocates nodes in a grid graph.
+ ///
+ /// This will inspect every cell in the dataBounds and allocate or deallocate the node depending on if that slot should have a node or not according to the nodeNormals array (pure zeroes means no node).
+ ///
+ /// This is only used for incremental updates of grid graphs.
+ /// The initial layer of the grid graph (which is always filled with nodes) is allocated in the <see cref="GridGraph.AllocateNodesJob"/> method.
+ /// </summary>
+ public struct JobAllocateNodes : IJob {
+ public AstarPath active;
+ [ReadOnly]
+ public NativeArray<float4> nodeNormals;
+ public IntBounds dataBounds;
+ public int3 nodeArrayBounds;
+ public GridNodeBase[] nodes;
+ public System.Func<GridNodeBase> newGridNodeDelegate;
+
+ public void Execute () {
+ var size = dataBounds.size;
+
+ // Start at y=1 because all nodes at y=0 are guaranteed to already be allocated (they are always allocated in a layered grid graph).
+ var nodeNormalsSpan = nodeNormals.AsUnsafeReadOnlySpan();
+ for (int y = 1; y < size.y; y++) {
+ for (int z = 0; z < size.z; z++) {
+ var rowOffset = ((y + dataBounds.min.y) * nodeArrayBounds.z + (z + dataBounds.min.z)) * nodeArrayBounds.x + dataBounds.min.x;
+ for (int x = 0; x < size.x; x++) {
+ var nodeIndex = rowOffset + x;
+ var shouldHaveNode = math.any(nodeNormalsSpan[nodeIndex]);
+ var node = nodes[nodeIndex];
+ var hasNode = node != null;
+ if (shouldHaveNode != hasNode) {
+ if (shouldHaveNode) {
+ node = nodes[nodeIndex] = newGridNodeDelegate();
+ active.InitializeNode(node);
+ } else {
+ // Clear custom connections first and clear connections from other nodes to this one
+ node.ClearCustomConnections(true);
+ // Clear grid connections without clearing the connections from other nodes to this one (a bit slow)
+ // Since this is inside a graph update we guarantee that the grid connections will be correct at the end
+ // of the update anyway
+ node.ResetConnectionsInternal();
+ node.Destroy();
+ nodes[nodeIndex] = null;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}