public void SetMovementValues() { _flowFieldQuery = GetEntityQuery(typeof(FlowFieldData)); _flowFieldEntity = _flowFieldQuery.GetSingletonEntity(); _flowFieldData = EntityManager.GetComponentData <FlowFieldData>(_flowFieldEntity); _destinationCellData = EntityManager.GetComponentData <DestinationCellData>(_flowFieldEntity); _entityBuffer = EntityManager.GetBuffer <EntityBufferElement>(_flowFieldEntity); _gridEntities = _entityBuffer.Reinterpret <Entity>(); if (_cellDataContainer.IsCreated) { _cellDataContainer.Dispose(); } _cellDataContainer = new NativeArray <CellData>(_gridEntities.Length, Allocator.Persistent); for (int i = 0; i < _entityBuffer.Length; i++) { _cellDataContainer[i] = GetComponent <CellData>(_entityBuffer[i]); } Entities.ForEach((ref EntityMovementData entityMovementData) => { entityMovementData.destinationReached = false; }).Run(); }
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();