protected override JobHandle OnUpdate(JobHandle inputDeps) { //Prepare maps var nodes = new NativeArray <T>(_mapSize, Allocator.TempJob); var prepareNodesJob = new PrepareNodesJob { Nodes = nodes, }; var prepareMapHandle = prepareNodesJob.Schedule(this, inputDeps); var sortNodesHandle = nodes.SortJob(prepareMapHandle); //Path Finding var pathFindingJob = new PathFindingJob { MapSize = _mapSize, Nodes = nodes, ResultECB = resultECB.CreateCommandBuffer().ToConcurrent(), CleanECB = cleanECB.CreateCommandBuffer().ToConcurrent(), IterationLimit = _iterationLimit, PathNodeLimit = _pathNodeLimit }; var pathFindingHandle = pathFindingJob.Schedule(this, sortNodesHandle); resultECB.AddJobHandleForProducer(pathFindingHandle); return(pathFindingHandle); }
protected override void OnUpdate() { NativeList <Node> nodeList = nodes; NativeList <Neighbour> neighbourList = neighbours; NativeList <JobHandle> pathFindDeps = new NativeList <JobHandle>(Allocator.Temp); List <PathFindingJob> pathFindJobs = new List <PathFindingJob>(); Entities.ForEach((Entity entity, ref AgentData agentData) => { if (agentData.navMeshNodeIndex == -1) { FindClosestNodeToAgent closestNode = new FindClosestNodeToAgent { agent = entity, agentComponentData = GetComponentDataFromEntity <AgentData>(), nodes = nodeList }; pathFindDeps.Add(closestNode.Schedule()); } if (!agentData.hasPath && agentData.navMeshNodeIndex != -1) { PathFindingJob PathFindJob = new PathFindingJob { entity = entity, startIndex = agentData.navMeshNodeIndex, endIndex = UnityEngine.Random.Range(0, nodeList.Length - 1), nodes = nodeList, neighbours = neighbourList, parents = new NativeArray <int>(nodeList.Length, Allocator.TempJob) //pathBuffer = GetBufferFromEntity<PathBuffer>() }; pathFindDeps.Add(PathFindJob.Schedule()); pathFindJobs.Add(PathFindJob); } }); JobHandle.CompleteAll(pathFindDeps); foreach (PathFindingJob pfjob in pathFindJobs) { new AddToPathBuffer { endIndex = pfjob.endIndex, entity = pfjob.entity, nodes = nodeList, neighbors = neighbourList, pathBuffer = GetBufferFromEntity <PathBuffer>(), agentComponentData = GetComponentDataFromEntity <AgentData>(), parents = pfjob.parents }.Run(); } pathFindDeps.Dispose(); }
/// <summary> /// process result of PathFinder job - NOTE runs in PathFinder's thread. /// </summary> /// <param name="job"></param> public void PathFinderCallback(PathFindingJob job) { // if no path found -> dont use the job result. if (job != null && job.Result != null && job.Result.First != null) { this.pathPointer = job.Result.First; this.job = job; // NOTE job should be assigned last. This triggers the other (game) thread to use value of job. } }
protected override void OnUpdate( ) { NativeArray <Entity> na_netNodes = group_netNodes.ToEntityArray(Allocator.TempJob); if (!isSystemInitialized) { if (na_netNodes.Length == 0) { return; // Early exit. } isSystemInitialized = true; // nhm_entityIndex.Dispose () ; nhm_entityIndex = new NativeHashMap <Entity, int> (na_netNodes.Length, Allocator.Persistent); // Map node entities to hash map. for (int i = 0; i < na_netNodes.Length; i++) { Entity nodeEntity = na_netNodes [i]; nhm_entityIndex.Add(nodeEntity, i); } // for // na_netNodes.Dispose () ; } EntityCommandBuffer.ParallelWriter ecbp = eecb.CreateCommandBuffer().AsParallelWriter(); Dependency = new PathFindingJob() { na_netNodes = na_netNodes, nhm_entityIndex = nhm_entityIndex, a_pathNodesPosition = GetComponentDataFromEntity <Translation> (true), pathNodeLinksBuffer = GetBufferFromEntity <PathNodeLinksBuffer> (true), pathPlannersHandle = GetComponentTypeHandle <PathPlannerComponent> (false), pathNodesBufferHandle = GetBufferTypeHandle <PathNodesBuffer> (false), }.ScheduleParallel(group_pathPlanners, 1, Dependency); Entities .WithName("PathSearchedJob") .WithAll <IsAliveTag, PathPlannerComponent, CanFindPathTag> () .ForEach((Entity entity, int entityInQueryIndex) => { ecbp.RemoveComponent <CanFindPathTag> (entityInQueryIndex, entity); }).Schedule(); eecb.AddJobHandleForProducer(Dependency); na_netNodes.Dispose(Dependency); }
protected override void OnUpdate() { Dependency = new PathFindingJob { entityType = GetEntityTypeHandle(), collisionStateType = GetComponentDataFromEntity <MapCollisionState>(true), mapDataType = GetComponentDataFromEntity <MapData>(true), mapBodyType = GetComponentTypeHandle <MapBody>(true), mapElementType = GetComponentTypeHandle <MapElement>(true), findingPathInfoType = GetComponentTypeHandle <FindingPathInfo>(true), ecb = commandBufferSystem.CreateCommandBuffer().AsParallelWriter() }.Schedule(query, Dependency); commandBufferSystem.AddJobHandleForProducer(Dependency); }
public override void UpdateSystem() { //We use IJobChunk so that we can share a grid between all pathfinders in a chunk Dependency = new PathFindingJob { pathFindingComponentHandle = GetComponentTypeHandle <PathFinding>(), hasPathComponentHandle = GetComponentTypeHandle <HasPathTag>(), pathNodeBufferHandle = GetBufferTypeHandle <PathNode>(), currentTargetComponentHandle = GetComponentTypeHandle <CurrentTarget>(true), translationComponentHandle = GetComponentTypeHandle <Translation>(true), entityType = GetEntityTypeHandle(), gridRef = m_gridManager.Grid, ecb = m_endSimulationECB.CreateCommandBuffer().AsParallelWriter() }.ScheduleParallel(m_pathFindingQuery, Dependency); m_endSimulationECB.AddJobHandleForProducer(Dependency); }
protected override void OnNextMove() { base.OnNextMove(); try { // follow path finding trail Vector2 vNext = new Vector2(pathPointer.Value.X, pathPointer.Value.Y); Vector2 dif = vNext - ParentThing.Target; // do the move if (dif.Length() > 0f) { dif.Normalize(); } TargetMove = dif; // advance the pointer to path/trail if (pathPointer == job.Result.Last) { // path is done. No next move. job = null; } else { // only advance pointer when position was reached if (ParentThing.Target + TargetMove == vNext) { pathPointer = pathPointer.Next; } } } // if using the path failed for some reason, reset it. catch (Exception) { job = null; } }
protected override void OnUpdate(ref UpdateParams p) { base.OnUpdate(ref p); // some vars Vector2 vTarg = ChaseTarget.Target; Vector2 vMeToTarg = vTarg - ParentThing.Target; float distToTarg = vMeToTarg.Length(); // check if in range to operate the behavior if (distToTarg <= ChaseRange && distToTarg > SatisfiedRange && !ChaseTarget.IsStealthy) { // check if a path is already planned... if (job != null && job.Result != null) { AllowNextMove(); IsTargetMoveDefined = true; } // if not, plan a new path is PathFinder is not busy. else if (job == null && !Level.Current.PathFinder.IsBusy()) { var newJob = new PathFindingJob() { From = ParentThing.Target, To = vTarg, Callback = PathFinderCallback }; Level.Current.PathFinder.AddJob(newJob); } } else { // not in the right range. Delete any previous paths to reset. job = null; } }