protected override JobHandle OnUpdate(JobHandle inputDeps) { EntityQuery targetQuery = GetEntityQuery(typeof(EnemyTag), 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(); FindTargetJob findTargetJob = new FindTargetJob { targetArray = targetArray, entityCommandBuffer = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(), }; JobHandle jobHandle = findTargetJob.Schedule(this, inputDeps); endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(jobHandle); return(jobHandle); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { var rng = new Random(); rng.InitState((uint)System.DateTime.UtcNow.Ticks); var blockEntityQuery = GetEntityQuery(typeof(BlockComponent), ComponentType.ReadOnly <ObstacleComponent>()); var blockComponentArray = blockEntityQuery.ToComponentDataArray <BlockComponent>(Allocator.TempJob); var blockArray = new NativeArray <BlockEntityWithPosition>(blockComponentArray.Length, Allocator.TempJob); for (var i = 0; i < blockComponentArray.Length; i++) { blockArray[i] = new BlockEntityWithPosition { TranslationX = blockComponentArray[i].X, TranslationY = blockComponentArray[i].Y, }; } blockComponentArray.Dispose(); var findTargetJob = new FindTargetJob { BlockArray = blockArray, EntityCommandBuffer = _endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(), Rng = rng, }; var findTargetJobHandle = findTargetJob.Schedule(this, inputDeps); _endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(findTargetJobHandle); return(findTargetJobHandle); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { // 查询目标entities EntityQuery targetQuery = GetEntityQuery(typeof(Target), ComponentType.ReadOnly <Translation>()); NativeArray <Entity> targetEntityArray = targetQuery.ToEntityArray(Allocator.TempJob); NativeArray <Translation> targetTranslationArray = targetQuery.ToComponentDataArray <Translation>(Allocator.TempJob); // 构造job参数 NativeArray <EntityWithPosition> args = new NativeArray <EntityWithPosition>(targetEntityArray.Length, Allocator.TempJob); for (int i = 0; i < args.Length; ++i) { args[i] = new EntityWithPosition() { entity = targetEntityArray[i], position = targetTranslationArray[i].Value, }; } // 释放临时NativeArray targetEntityArray.Dispose(); targetTranslationArray.Dispose(); // 生成一个Job,然后调用 FindTargetJob job = new FindTargetJob() { targets = args, ecb = endSimulationECBS.CreateCommandBuffer().ToConcurrent(), }; JobHandle handle = job.Schedule(this, inputDeps); endSimulationECBS.AddJobHandleForProducer(handle); return(handle); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { JobHandle findJob = new FindTargetJob { enemyTranslations = Query.ToComponentDataArray <Translation>(Allocator.TempJob), badEntities = Query.ToEntityArray(Allocator.TempJob) }.Schedule(this, inputDeps); return(findJob); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { // First gather the state data we want to operate on. // We want both entity and translation information about the target, so extract this from the target query. var targetEntities = targetsQuery.ToEntityArray(Allocator.TempJob); var targetTranslations = targetsQuery.ToComponentDataArray <LocalToWorld>(Allocator.TempJob); var targetVelocities = targetsQuery.ToComponentDataArray <PhysicsVelocity>(Allocator.TempJob); // Construct a new array that merges the two data types together into simple structs. var targetArray = new NativeArray <TargetingSystemUtils.EntityWithPosition>(targetEntities.Length, Allocator.TempJob); for (int i = 0; i < targetArray.Length; i++) { targetArray[i] = new TargetingSystemUtils.EntityWithPosition { entity = targetEntities[i], position = targetTranslations[i].Position, velocity = targetVelocities[i].Linear, }; } // Clear up the intermediate native arrays; the native arrays passed to jobs are marked to be automatically deallocated. targetEntities.Dispose(); targetTranslations.Dispose(); targetVelocities.Dispose(); // We need to extract the closest target per searching unit, so we need an array whose length matches the unit count. var closestTargetArray = new NativeArray <TargetingSystemUtils.EntityWithPosition>(searchersQuery.CalculateEntityCount(), Allocator.TempJob); var findTargetJob = new FindTargetJob { targetArray = targetArray, closestTargetArray = closestTargetArray, }; // This separation is necessary because command buffers can't be used in Burst compiled structs. // We pass a reference to the same closestTargetArray to this job too, with the intention to read from it. // This requires the sequencing of the jobs to be correct. var assignTargetJob = new AssignTargetJob { closestTargetArray = closestTargetArray, commandBuffer = endSimCommandBufferSystem.CreateCommandBuffer().ToConcurrent(), }; // This makes the "find target" job a prerequisite for the "assign target" job, to ensure the closestTargetArray is initiated. var jobHandle = findTargetJob.Schedule(this, inputDeps); jobHandle = assignTargetJob.Schedule(this, jobHandle); endSimCommandBufferSystem.AddJobHandleForProducer(jobHandle); return(jobHandle); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { EntityQuery targetQuery = GetEntityQuery(new EntityQueryDesc() { All = new ComponentType[] { typeof(EzTarget), ComponentType.ReadOnly <Translation>() }, }); var pArrayEntity = targetQuery.ToEntityArray(Allocator.TempJob); var pArrayTranslation = targetQuery.ToComponentDataArray <Translation>(Allocator.TempJob); var pArrayEztarget = targetQuery.ToComponentDataArray <EzTarget>(Allocator.TempJob); EntityQuery pUnitNontarget = GetEntityQuery(typeof(EzUnit), typeof(Translation), ComponentType.Exclude <EzHasTarget>()); var pClosedFindEntity = new NativeArray <Entity>(pUnitNontarget.CalculateEntityCount(), Allocator.TempJob); NativeArray <EntityWithPosition> targetArray = new NativeArray <EntityWithPosition>(pArrayEntity.Length, Allocator.TempJob); for (int i = 0; i < pArrayEntity.Length; ++i) { targetArray[i] = new EntityWithPosition() { entity = pArrayEntity[i], layer = pArrayEztarget[i].layer.layer, position = pArrayTranslation[i].Value }; } pArrayEntity.Dispose(); pArrayTranslation.Dispose(); pArrayEztarget.Dispose(); FindTargetJob jobFind = new FindTargetJob() { targetArray = targetArray, closestTargetEntityArray = pClosedFindEntity }; var pAddComponentJob = new AddComponentJob() { entityCommandBuffer = commandBufferSystem.CreateCommandBuffer().ToConcurrent(), closestTargetEntityArray = pClosedFindEntity, }; JobHandle pJob = jobFind.Schedule(this, inputDeps); pJob = pAddComponentJob.Schedule(this, pJob); commandBufferSystem.AddJobHandleForProducer(pJob); return(pJob); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { EntityQuery eq = GetEntityQuery(typeof(TargetTag), ComponentType.ReadOnly <Translation>()); NativeArray <Entity> entities = eq.ToEntityArray(Allocator.TempJob); NativeArray <Translation> translations = eq.ToComponentDataArray <Translation>(Allocator.TempJob); NativeArray <EntityWithPosition> targetEntitysWithPosition = new NativeArray <EntityWithPosition>(entities.Length, Allocator.TempJob); for (int i = 0; i < entities.Length; i++) { targetEntitysWithPosition[i] = new EntityWithPosition() { Entity = entities[i], Position = translations[i].Value }; } entities.Dispose(); translations.Dispose(); NativeArray <Entity> foundTargets = new NativeArray <Entity>( GetEntityQuery(typeof(NPCTag), ComponentType.Exclude <HasTarget>()).CalculateEntityCount(), Allocator.TempJob); FindTargetJob findTargetJob = new FindTargetJob() { TargetArray = targetEntitysWithPosition, SelectedTargets = foundTargets }; JobHandle findTargetJobHandle = findTargetJob.Schedule(this, inputDeps); SetTargetJob setTargetJob = new SetTargetJob() { EntityCommandBuffer = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(), TargetArray = foundTargets }; JobHandle setTargetJobHandle = setTargetJob.Schedule(this, findTargetJobHandle); endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(setTargetJobHandle); return(setTargetJobHandle); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { var findTargetJob = new FindTargetJob { commands = _commandsSystem.CreateCommandBuffer().ToConcurrent(), quadrantsAccess = _quadrantsSystem.GetQuadrantAccess() }; var checkDistanceJob = new CheckDistanceToTargetJob { commands = _commandsSystem.CreateCommandBuffer().ToConcurrent() }; var rotateJob = new RotateTowardsTargetJob(); var updateAimJob = new UpdateAimStatusJob(); inputDeps = findTargetJob.Schedule(this, inputDeps); inputDeps = checkDistanceJob.Schedule(this, inputDeps); inputDeps = rotateJob.Schedule(this, inputDeps); inputDeps = updateAimJob.Schedule(this, inputDeps); _commandsSystem.AddJobHandleForProducer(inputDeps); return(inputDeps); }