protected override void OnUpdate() { if (_flowFieldEntity.Equals(Entity.Null)) { return; } float deltaTime = Time.DeltaTime; FlowFieldData flowFieldData = _flowFieldData; int2 destinationCell = _destinationCellData.destinationIndex; JobHandle jobHandle = new JobHandle(); jobHandle = Entities.ForEach((ref PhysicsVelocity physVelocity, ref EntityMovementData entityMovementData, ref Translation translation) => { int2 curCellIndex = FlowFieldHelper.GetCellIndexFromWorldPos(translation.Value, flowFieldData.gridSize, flowFieldData.cellRadius * 2); if (curCellIndex.Equals(destinationCell)) { entityMovementData.destinationReached = true; } int flatCurCellIndex = FlowFieldHelper.ToFlatIndex(curCellIndex, flowFieldData.gridSize.y); float2 moveDirection = _cellDataContainer[flatCurCellIndex].bestDirection; float finalMoveSpeed = (entityMovementData.destinationReached ? entityMovementData.destinationMoveSpeed : entityMovementData.moveSpeed) * deltaTime; physVelocity.Linear.xz = moveDirection * finalMoveSpeed; translation.Value.y = 0f; }).ScheduleParallel(jobHandle); jobHandle.Complete(); }
protected override void OnUpdate() { EntityCommandBuffer commandBuffer = _ecbSystem.CreateCommandBuffer(); Entities.ForEach((Entity entity, in NewFlowFieldData newFlowFieldData, in FlowFieldData flowFieldData) => { commandBuffer.RemoveComponent <NewFlowFieldData>(entity); DynamicBuffer <EntityBufferElement> buffer = newFlowFieldData.isExistingFlowField ? GetBuffer <EntityBufferElement>(entity) : commandBuffer.AddBuffer <EntityBufferElement>(entity); DynamicBuffer <Entity> entityBuffer = buffer.Reinterpret <Entity>(); float cellRadius = flowFieldData.cellRadius; float cellDiameter = cellRadius * 2; int2 gridSize = flowFieldData.gridSize; for (int x = 0; x < gridSize.x; x++) { for (int y = 0; y < gridSize.y; y++) { float3 cellWorldPos = new float3(cellDiameter * x + cellRadius, 0, cellDiameter * y + cellRadius); byte cellCost = CostFieldHelper.instance.EvaluateCost(cellWorldPos, cellRadius); CellData newCellData = new CellData { worldPos = cellWorldPos, gridIndex = new int2(x, y), cost = cellCost, bestCost = ushort.MaxValue, bestDirection = int2.zero }; Entity curCell; if (newFlowFieldData.isExistingFlowField) { int flatIndex = FlowFieldHelper.ToFlatIndex(new int2(x, y), gridSize.y); curCell = entityBuffer[flatIndex]; } else { curCell = commandBuffer.CreateEntity(_cellArchetype); entityBuffer.Add(curCell); } commandBuffer.SetComponent(curCell, newCellData); } } int2 destinationIndex = FlowFieldHelper.GetCellIndexFromWorldPos(flowFieldData.clickedPos, gridSize, cellDiameter); DestinationCellData newDestinationCellData = new DestinationCellData { destinationIndex = destinationIndex }; if (!newFlowFieldData.isExistingFlowField) { commandBuffer.AddComponent <DestinationCellData>(entity); } commandBuffer.SetComponent(entity, newDestinationCellData); commandBuffer.AddComponent <CalculateFlowFieldTag>(entity); }).Run();