using Unity.Entities; using Unity.Jobs; [UpdateBefore(typeof(CartesianGridOnPlaneFollowTargetSystem))] public unsafe class CartesianGridOnPlaneTargetSystem : JobComponentSystem { EntityQuery m_GridQuery; protected override void OnCreate() { m_GridQuery = GetEntityQuery(ComponentType.ReadOnly()); RequireForUpdate(m_GridQuery); } protected override JobHandle OnUpdate(JobHandle lastJobHandle) { // Get component data from the GridPlane var cartesianGridPlane = GetSingleton(); 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() .WithNone() .WithStructuralChanges() .ForEach((Entity entity) => { var buffer = EntityManager.AddBuffer(entity); buffer.ResizeUninitialized(targetDirectionsBufferCapacity); }).Run(); // Rebuild all the paths to the target *only* when the target's grid position changes. Entities .WithName("UpdateTargetPaths") .WithAll() .ForEach((Entity entity, ref CartesianGridTargetCoordinates prevTargetPosition, in CartesianGridCoordinates targetPosition, in DynamicBuffer targetDirections) => { if (prevTargetPosition.Equals(targetPosition)) return; prevTargetPosition = new CartesianGridTargetCoordinates(targetPosition); CartesianGridOnPlaneShortestPath.CalculateShortestPathsToTarget(targetDirections.Reinterpret().AsNativeArray(), rowCount, colCount, targetPosition, gridWalls); }).Run(); return lastJobHandle; } }