// protected override JobHandle OnUpdate(JobHandle inputDeps) {
    protected override void OnUpdate()
    {
        //set the query for temp and translation
        // EntityQuery targetQuery = GetEntityQuery(typeof(Temperature), ComponentType.ReadOnly<Translation>());

        //convert to native arrays of entities and components
        NativeArray <Entity>      targetEntityArray      = targetQuery.ToEntityArray(Allocator.TempJob);
        NativeArray <Translation> targetTranslationArray = targetQuery.ToComponentDataArray <Translation>(Allocator.TempJob);
        NativeArray <Temperature> targetTemperatureArray = targetQuery.ToComponentDataArray <Temperature>(Allocator.TempJob);

        //allocate a EntityWithPosition struct array
        NativeArray <EntityWithPosition> targetArray = new NativeArray <EntityWithPosition>(targetEntityArray.Length, Allocator.TempJob);

        //fill struct array
        for (int i = 0; i < targetEntityArray.Length; i++)
        {
            targetArray[i] = new EntityWithPosition {
                entity      = targetEntityArray[i],
                position    = targetTranslationArray[i].Value,
                temperature = targetTemperatureArray[i].Value
            };
        }

        //clean up
        targetEntityArray.Dispose();
        targetTranslationArray.Dispose();
        targetTemperatureArray.Dispose();

        // EntityQuery unitQuery = GetEntityQuery(typeof(Unit), ComponentType.Exclude<HasTarget>());
        // NativeArray<Entity> closestTargetEntityArray = new NativeArray<Entity>(unitQuery.CalculateLength(), Allocator.TempJob);

        //do work son
        FindTargetBurstJob findTargetBurstJob = new FindTargetBurstJob {
            targetArray     = targetArray,
            deltaTime       = .01f,
            TranslationType = GetComponentTypeHandle <Translation>(true),
            TemperatureType = GetComponentTypeHandle <Temperature>(false)
                              // closestTargetEntityArray = closestTargetEntityArray
        };

        // targetArray.Dispose();

        // state.Dependency = findTargetBurstJob.ScheduleSingle(targetQuery, state.Dependency);
        // JobHandle jobHandle = findTargetBurstJob.Schedule(this, inputDeps);
        Dependency = findTargetBurstJob.ScheduleParallel(targetQuery, 1, Dependency);
        // AddComponentJob addComponentJob = new AddComponentJob {
        //     closestTargetEntityArray = closestTargetEntityArray,
        //     entityCommandBuffer = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(),
        // };
        // jobHandle = addComponentJob.Schedule(this, jobHandle);

        // endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(jobHandle);

        // return jobHandle;
    }
Exemplo n.º 2
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        EntityQuery targetQuery = GetEntityQuery(typeof(Target), ComponentType.ReadOnly <Translation>());

        NativeArray <Entity>      targetEntityArray      = targetQuery.ToEntityArray(Allocator.TempJob);
        NativeArray <Translation> targetTranslationArray = targetQuery.ToComponentDataArray <Translation>(Allocator.TempJob);

        NativeArray <EntityWithPosition> targetArray = new NativeArray <EntityWithPosition>(targetEntityArray.Length, Allocator.TempJob);

        for (int i = 0; i < targetEntityArray.Length; i++)
        {
            targetArray[i] = new EntityWithPosition {
                entity   = targetEntityArray[i],
                position = targetTranslationArray[i].Value,
            };
        }

        targetEntityArray.Dispose();
        targetTranslationArray.Dispose();

        EntityQuery          unitQuery = GetEntityQuery(typeof(Unit), ComponentType.Exclude <HasTarget>());
        NativeArray <Entity> closestTargetEntityArray = new NativeArray <Entity>(unitQuery.CalculateEntityCount(), Allocator.TempJob);


        FindTargetBurstJob findTargetBurstJob = new FindTargetBurstJob {
            targetArray = targetArray,
            closestTargetEntityArray = closestTargetEntityArray
        };
        JobHandle jobHandle = findTargetBurstJob.Schedule(this, inputDeps);

        AddComponentJob addComponentJob = new AddComponentJob {
            closestTargetEntityArray = closestTargetEntityArray,
            entityCommandBuffer      = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(),
        };

        jobHandle = addComponentJob.Schedule(this, jobHandle);

        endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(jobHandle);

        return(jobHandle);
    }