private void Update() { int i = 0; //if there are current jobs in the list then check if they're done and remove while (i < currentJobs.Count) { if (currentJobs[i].jobDone) { currentJobs.RemoveAt(i); } else { i++; } } //Check if there is jobs to do in list and the max number of threads arent all currently in use then create thread and do job if (todoJobs.Count > 0 && currentJobs.Count < MaxThreads) { PathThread job = todoJobs[0]; todoJobs.RemoveAt(0); currentJobs.Add(job); //Start a new thread ThreadStart newThread = delegate { job.path = PathFinding.CalculatePath(job.startPos, job.targetPos, out string output); job.jobDone = true; }; Thread jobThread = new Thread(newThread); jobThread.Start(); } }
//KS - Handle setting a new target for the agent. void SetTarget(GameObject target, bool isPatrolTarget = false) { //KS - If the target is different, we will need to recalculate a path. If the new target is null then we should path to the last valid target, it is to simulate a sense of memory. if (target != targetObject) { lastValidTargetObject = target != null && target.GetComponent <Controller>() ? target : lastValidTargetObject; pathfindingDebugOutput = ""; Node pathfindingNode = target == null ? null : PathFinding.CalculatePath(transform.position, target.transform.position, out pathfindingDebugOutput); targetNode = pathfindingNode != null ? pathfindingNode.parent : pathfindingNode; } //KS - If our target has not changed, then we need to check if we have a reached a sub node in the path that was calculated, if so we need to re-target the target node to it's parent which is just effectively traversing the path. else if (targetNode != null && Vector3.Distance(transform.position, targetNode.GetVector3Position()) < 0.1f) { targetNode = targetNode != null ? targetNode.parent : targetNode; } targetObject = target; targetNode = target == null ? null : targetNode; patrolling = isPatrolTarget; }