protected override JobHandle OnUpdate(JobHandle lastJobHandle) { // Get component data from the GridPlane var cartesianGridPlane = GetSingleton <CartesianGridOnPlane>(); var rowCount = cartesianGridPlane.Blob.Value.RowCount; var colCount = cartesianGridPlane.Blob.Value.ColCount; var targetDirectionsBufferCapacity = ((colCount + 1) / 2) * rowCount; var gridWalls = (byte *)cartesianGridPlane.Blob.Value.Walls.GetUnsafePtr(); // Initialize the buffer for the target paths with size appropriate to grid. Entities .WithName("InitializeTargets") .WithAll <CartesianGridTarget>() .WithNone <CartesianGridTargetDirection>() .WithStructuralChanges() .ForEach((Entity entity) => { var buffer = EntityManager.AddBuffer <CartesianGridTargetDirection>(entity); buffer.ResizeUninitialized(targetDirectionsBufferCapacity); }).Run(); // Rebuild all the paths to the target *only* when the target's grid position changes. Entities .WithName("UpdateTargetPaths") .WithAll <CartesianGridTarget>() .ForEach((Entity entity, ref CartesianGridTargetCoordinates prevTargetPosition, in CartesianGridCoordinates targetPosition, in DynamicBuffer <CartesianGridTargetDirection> targetDirections) => { if (prevTargetPosition.Equals(targetPosition)) { return; } prevTargetPosition = new CartesianGridTargetCoordinates(targetPosition); CartesianGridOnPlaneShortestPath.CalculateShortestPathsToTarget(targetDirections.Reinterpret <byte>().AsNativeArray(), rowCount, colCount, targetPosition, gridWalls); }).Run();
protected override JobHandle OnUpdate(JobHandle lastJobHandle) { // Get component data from the GridPlane var cartesianGridCube = GetSingleton <CartesianGridOnCube>(); var rowCount = cartesianGridCube.Blob.Value.RowCount; var targetDirectionsBufferCapacity = 6 * (((rowCount + 1) / 2) * rowCount); var targetDistancesBufferCapacity = 6 * (rowCount * rowCount); var gridWalls = (byte *)cartesianGridCube.Blob.Value.Walls.GetUnsafePtr(); var faceLocalToLocal = (float4x4 *)cartesianGridCube.Blob.Value.FaceLocalToLocal.GetUnsafePtr(); Entities .WithName("InitializeTargets") .WithAll <CartesianGridTarget>() .WithAll <CartesianGridOnCubeFace>() .WithNone <CartesianGridTargetDirection>() .WithNone <CartesianGridTargetDistance>() .WithStructuralChanges() .ForEach((Entity entity) => { var directionBuffer = EntityManager.AddBuffer <CartesianGridTargetDirection>(entity); directionBuffer.ResizeUninitialized(targetDirectionsBufferCapacity); var distanceBuffer = EntityManager.AddBuffer <CartesianGridTargetDistance>(entity); distanceBuffer.ResizeUninitialized(targetDistancesBufferCapacity); }).Run(); // Rebuild all the paths to the target *only* when the target's grid position changes. Entities .WithName("UpdateTargetPaths") .WithNativeDisableUnsafePtrRestriction(faceLocalToLocal) .WithAll <CartesianGridTarget>() .ForEach((Entity entity, ref CartesianGridTargetCoordinates prevTargetPosition, in CartesianGridOnCubeFace cubeFace, in CartesianGridCoordinates targetPosition, in DynamicBuffer <CartesianGridTargetDirection> targetDirections, in DynamicBuffer <CartesianGridTargetDistance> targetDistances) => { if (prevTargetPosition.Equals(targetPosition)) { return; } if (targetPosition.OnGrid(rowCount, rowCount)) { prevTargetPosition = new CartesianGridTargetCoordinates(targetPosition); CartesianGridOnCubeShortestPath.CalculateShortestPathsToTarget(targetDirections.Reinterpret <byte>().AsNativeArray(), targetDistances.Reinterpret <int>().AsNativeArray(), rowCount, targetPosition, cubeFace, gridWalls, faceLocalToLocal); } }).Run();