summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/GridAdjacencyMapper.cs
blob: bba0a601012d820678c52f1892cfab2da9d12b29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using UnityEngine;
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using Unity.Mathematics;

namespace Pathfinding.Graphs.Grid {
	public interface GridAdjacencyMapper {
		int LayerCount(IntBounds bounds);
		int GetNeighbourIndex(int nodeIndexXZ, int nodeIndex, int direction, NativeArray<ulong> nodeConnections, NativeArray<int> neighbourOffsets, int layerStride);
		bool HasConnection(int nodeIndex, int direction, NativeArray<ulong> nodeConnections);
	}

	public struct FlatGridAdjacencyMapper : GridAdjacencyMapper {
		public int LayerCount (IntBounds bounds) {
			UnityEngine.Assertions.Assert.IsTrue(bounds.size.y == 1);
			return 1;
		}
		public int GetNeighbourIndex (int nodeIndexXZ, int nodeIndex, int direction, NativeArray<ulong> nodeConnections, NativeArray<int> neighbourOffsets, int layerStride) {
			return nodeIndex + neighbourOffsets[direction];
		}
		public bool HasConnection (int nodeIndex, int direction, NativeArray<ulong> nodeConnections) {
			return ((nodeConnections[nodeIndex] >> direction) & 0x1) != 0;
		}
	}

	public struct LayeredGridAdjacencyMapper : GridAdjacencyMapper {
		public int LayerCount(IntBounds bounds) => bounds.size.y;
		public int GetNeighbourIndex (int nodeIndexXZ, int nodeIndex, int direction, NativeArray<ulong> nodeConnections, NativeArray<int> neighbourOffsets, int layerStride) {
			return nodeIndexXZ + neighbourOffsets[direction] + (int)((nodeConnections[nodeIndex] >> LevelGridNode.ConnectionStride*direction) & LevelGridNode.ConnectionMask) * layerStride;
		}
		public bool HasConnection (int nodeIndex, int direction, NativeArray<ulong> nodeConnections) {
			return ((nodeConnections[nodeIndex] >> LevelGridNode.ConnectionStride*direction) & LevelGridNode.ConnectionMask) != LevelGridNode.NoConnection;
		}
	}
}