//Set path indexes public void Execute() { DynamicBuffer <PathPosition> pathPositionBuffer = pathPositionBufferFromEntity[entity]; pathPositionBuffer.Clear(); PathfindingComponentData pathfindingComponentData = pathfindingComponentDataFromEntity[entity]; int endNodeIndex = CalculateIndex(pathfindingComponentData.endPosition.x, pathfindingComponentData.endPosition.y, gridSize.x); PathNode endNode = pathNodeArray[endNodeIndex]; if (endNode.cameFromNodeIndex == -1) { //Did not find path pathFollowComponentDataFromEntity[entity] = new PathFollowComponent { pathIndex = -1 }; } else { CalculatePath(pathNodeArray, endNode, pathPositionBuffer); pathFollowComponentDataFromEntity[entity] = new PathFollowComponent { pathIndex = pathPositionBuffer.Length - 1 }; } }
//Function for moving protected void Move() { //Entity data getting Entity entity = convertedEntityHolder.GetEntity(); EntityManager entityManager = convertedEntityHolder.GetEntityManager(); if (entityManager == null) { return; } PathFollowComponent pathFollow = entityManager.GetComponentData <PathFollowComponent>(entity); DynamicBuffer <PathPosition> pathPositionBuffer = entityManager.GetBuffer <PathPosition>(entity); //If still following path if (pathFollow.pathIndex >= 0) { //Get path position PathPosition pathPosition = pathPositionBuffer[pathFollow.pathIndex]; //Get world position for each node float3 targetPosition = new float3(pathPosition.position.x, 0, pathPosition.position.y) + Vector3.one * new float3(gridCellSize, 0, gridCellSize) * 0.5f; //Calculate move direction float3 moveDir = math.normalizesafe(targetPosition - (float3)transform.position); //Move the ai to the next node transform.position += (Vector3)(moveDir * (3f + unitSpeed) * Time.deltaTime); //Check if within radius of next node if (math.distance(transform.position, targetPosition) < movementStopDist) { //If reached destination if (pathFollow.pathIndex == 0) { hasCompletedCurrentTask = true; } //Next node pathFollow.pathIndex--; //Update pathfollow array entityManager.SetComponentData(entity, pathFollow); } } //If current task completed if (hasCompletedCurrentTask == true) { //Null pointer check for next task if (nextTask != null) { //Invoke next task nextTask.Invoke(); nextTask = null; } } }
private static void WonderingFollow(ref PathFollowComponent pathFollow, ref PhysicsWorld physicsWorld, ref NativeArray <Random> RandomArray, in Translation translation, in float deltaTime, in int naticeThreadIndex)