protected override JobHandle OnUpdate(JobHandle inputDeps) { var query = GetEntityQuery(typeof(Tag_Ennemy), ComponentType.ReadOnly <Position>()); var targetEntities = query.ToEntityArray(Allocator.TempJob); var targetPositions = query.ToComponentDataArray <Position>(Allocator.TempJob); var targets = new NativeArray <EntityWithPosition>(targetEntities.Length, Allocator.TempJob); for (var i = 0; i < targetEntities.Length; i++) { targets[i] = new EntityWithPosition() { entity = targetEntities[i], position = targetPositions[i].Value }; } targetEntities.Dispose(); targetPositions.Dispose(); var job = new SetTargetJob { targets = targets, ecb = endSimulationEcbSystem.CreateCommandBuffer().ToConcurrent() }; var handle = job.Schedule(this, inputDeps); endSimulationEcbSystem.AddJobHandleForProducer(handle); return(handle); }
protected override void OnUpdate() { EntityQuery entityQuery = Entities.WithAll <QuadrantEntity, Translation, FindTargetData>().ToEntityQuery(); quadrantDataHashMap.Clear(); if (entityQuery.CalculateEntityCount() > quadrantDataHashMap.Capacity) { quadrantDataHashMap.Capacity = entityQuery.CalculateEntityCount(); } NativeHashMap <Entity, QuadrantData> entityTargetHashMap = new NativeHashMap <Entity, QuadrantData>(entityQuery.CalculateEntityCount(), Allocator.TempJob); // Position Units in HashMap SetQuadrantDataHashMapJob setQuadrantDataHashMapJob = new SetQuadrantDataHashMapJob { nativeMultiHashMap = quadrantDataHashMap.ToConcurrent(), }; JobHandle setEntityHashMapJobHandle = JobForEachExtensions.Schedule(setQuadrantDataHashMapJob, entityQuery); setEntityHashMapJobHandle.Complete(); // Cycle through all Units and FindTarget FindClosestTargetJob findClosestTargetJob = new FindClosestTargetJob { targetHashMap = quadrantDataHashMap, unitTargetHashMap = entityTargetHashMap.ToConcurrent(), }; JobHandle findClosestTargetJobHandle = JobForEachExtensions.Schedule(findClosestTargetJob, entityQuery); findClosestTargetJobHandle.Complete(); SetTargetJob setTargetJob = new SetTargetJob { entityTargetHashMap = entityTargetHashMap, entityCommandBuffer = PostUpdateCommands.ToConcurrent(), }; JobHandle setTargetJobHandle = JobForEachExtensions.Schedule(setTargetJob, entityQuery); setTargetJobHandle.Complete(); entityTargetHashMap.Dispose(); //DebugDrawQuadrant(UtilsClass.GetMouseWorldPosition()); //int hashMapKey = GetPositionHashMapKey(UtilsClass.GetMouseWorldPosition()); //Debug.Log(GetEntityCountInHashMap(quadrantDataHashMap, hashMapKey)); }
protected override void OnUpdate() { var query = GetEntityQuery(typeof(SquadTagSharedComponentData), typeof(SquadComponentData), typeof(LerpShootTargetProvederComponentData), typeof(ArcherTargetPositionComponentData)); var chunkCount = query.CalculateChunkCount(); var sharedIndicesMap = new NativeHashMap <int, int>(chunkCount, Allocator.TempJob); var indicesJobH = new SharedIndicesJob() { indices = sharedIndicesMap.AsParallelWriter(), squadTagType = GetArchetypeChunkSharedComponentType <SquadTagSharedComponentData>() }.Schedule(query); var sharedData = new NativeHashMap <int, SquadFormationData>(chunkCount, Allocator.TempJob); indicesJobH.Complete(); var indices = sharedIndicesMap.GetKeyArray(Allocator.TempJob); for (int i = 0; i < indices.Length; i++) { var data = EntityManager.GetSharedComponentData <SquadTagSharedComponentData>(indices[i]); sharedData.TryAdd(indices[i], new SquadFormationData() { yUnitCount = data.data.heightUnitsCount, xUnitCount = data.unitCount.value / data.data.heightUnitsCount + (data.unitCount.value % data.data.heightUnitsCount == 0 ? 0 : 1), bot2top = data.data.directionBottomToTop, left2right = data.data.directionLeftToRight }); } var setTargetJobH = new SetTargetJob() { sharedData = sharedData, squadTagType = GetArchetypeChunkSharedComponentType <SquadTagSharedComponentData>(), squadType = GetArchetypeChunkComponentType <SquadComponentData>(true), targetProviderType = GetArchetypeChunkComponentType <LerpShootTargetProvederComponentData>(true), targetTypr = GetArchetypeChunkComponentType <ArcherTargetPositionComponentData>(false), rnd = new Unity.Mathematics.Random((uint)UnityEngine.Random.Range(0, int.MaxValue)) }.Schedule(query); setTargetJobH.Complete(); indices.Dispose(); sharedData.Dispose(); sharedIndicesMap.Dispose(); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { var buffer = barrier.CreateCommandBuffer(); int h = 0; while (sendDamage.TryDequeue(out DamageInfo info)) { if (info.Index < data.Length) { var unit = data.units[info.Index]; unit.CurrentHealth -= info.Damage; if (unit.CurrentHealth <= 0) { if (unit.State != UnitState.Dying && unit.State != UnitState.DyingCooldown && unit.State != UnitState.Dead) { unit.State = UnitState.Dying; } } else { unit.State = UnitState.GotHit; } data.units[info.Index] = unit; } h++; if (h > 1000) { break; } } h = 0; while (needsPathfinding.TryDequeue(out PathfindingInfo info)) { navAgentSystem.SetDestination(info.Entity, info.Agent, info.Destination); h++; if (h > 1000) { break; } } var dt = Time.deltaTime; var setTargetJob = new SetTargetJob(dt, Time.time, buffer, sendDamage, needsPathfinding, data.entities, data.agents, data.animations, data.positions, data.units).Schedule(data.Length, 64, inputDeps); return(setTargetJob); }
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) { if (!mapSystem.mapData.IsValid) { return(inputDeps); } generateField.Complete(); var calculateAgentTile = new CalculateNavAgentTile(); var handle = calculateAgentTile.Schedule(this, inputDeps); var clearTileAgentsCount = new ClearTileAgentsCount(); var handle2 = clearTileAgentsCount.Schedule(this, inputDeps); handle = JobHandle.CombineDependencies(handle, handle2); if (!counts.IsCreated) { counts = new NativeMultiHashMap <int2, int>(mapSystem.mapData.width * mapSystem.mapData.height, Allocator.TempJob); } counts.Clear(); handle = new CountJob { hashMap = counts.AsParallelWriter() }.Schedule(this, handle); handle = new UpdateCounts { hashMap = counts }.Schedule(this, handle); var updateJob = new UpdateMapCache { mapTiles = mapSystem.mapData.tiles, width = mapSystem.mapData.width }; handle = updateJob.Schedule(this, handle); if (!flowField.isValid) { var target = new int2(mapSystem.mapData.width / 2 + 1, mapSystem.mapData.height / 2 + 1); flowField = new FlowField(target, mapSystem.mapData); } handle = new SetTargetJob { target = flowField.targetTile, flowField = flowField }.Schedule(handle); var initJob = new InitializeFieldJob { stepField = flowField.stepField, flowField = flowField.flowField, target = flowField.targetTile.x + flowField.targetTile.y * mapSystem.mapData.width }; handle = initJob.Schedule(flowField.flowField.Length, 64, handle); var generateCostFieldJob = new GenerateCostField { map = mapSystem.mapData, blob = mapSystem.mapData.mapDataBlob, stepField = flowField.stepField, openSet = flowField.openSet, nextSet = flowField.nextSet }; handle = generateCostFieldJob.Schedule(handle); var generateFlowFieldJob = new GenerateFlowField { map = mapSystem.mapData, stepField = flowField.stepField, flowField = flowField.flowField, blob = mapSystem.mapData.mapDataBlob }; generateField = generateFlowFieldJob.Schedule(flowField.flowField.Length, 64, handle); return(generateField); }