public override void UpdateSystem() { Dependency = JobHandle.CombineDependencies(Dependency, m_raycastSystem.RaycastSystemDependency); if (m_inputManagementSystem.InputData.inputActions.spawn) { NativeArray <RaycastResult> raycastResult = m_raycastSystem.RaycastResult; NativeQueue <SpawnCommand> spawnQueueLocal = spawnQueue; NativeArray2D <MapNode> grid = m_gridManager.Grid; Dependency = Entities.WithReadOnly(grid).WithReadOnly(raycastResult).ForEach((ref RuntimePrefabData runtimePrefabData) => { if (raycastResult[0].raycastTargetType == RaycastTargetType.Ground) { Rotation rotation = GetComponent <Rotation>(runtimePrefabData.aiDrone); Translation translation = new Translation { Value = raycastResult[0].hitPosition + new float3(0, 1, 0) }; LocalToWorld localToWorld = new LocalToWorld { Value = new float4x4(rotation.Value, translation.Value) }; PathFinding pathFinding = new PathFinding { currentNode = PathFindingSystem.FindNearestNode(translation.Value, grid) }; SpawnCommands.SpawnHarvester(spawnQueueLocal, runtimePrefabData.aiDrone, translation, localToWorld, pathFinding); } }).Schedule(Dependency); spawnQueueDependencies = Dependency; } }
/// <summary> /// Iterates through the children of the current node to the target to retrieve the path. /// </summary> private unsafe static void ConstructPath(NativeArray2D <MapNode> gridCopy, ref PathFinding pathfinding, ref DynamicBuffer <PathNode> path) { MapNode node = gridCopy[pathfinding.currentNode.x, pathfinding.currentNode.y]; while (node.child != null) { path.Add(new PathNode { position = node.child->position, gridPosition = node.child->gridPosition }); node = *node.child; } }
public static void SpawnHarvester(NativeQueue <SpawnCommand> spawnQueue, Entity entity, Translation translation, LocalToWorld localToWorld, PathFinding pathFinding) { SpawnCommand spawnCommand = new SpawnCommand(); spawnCommand.spawnCommandType = SpawnCommandType.Harvester; spawnCommand.entity = entity; HarvesterSpawnData *spawnData = spawnCommand.CommandData <HarvesterSpawnData>(); spawnData->translation = translation; spawnData->localToWorld = localToWorld; spawnData->pathFinding = pathFinding; spawnQueue.Enqueue(spawnCommand); }
private static unsafe void CalculateGridH(NativeArray2D <MapNode> gridCopy, ref PathFinding kPathfinding) { //Fill in h values for grid. for (int i = 0; i < gridCopy.Length0; ++i) { for (int j = 0; j < gridCopy.Length1; ++j) { MapNode node = gridCopy[i, j]; node.h = math.distancesq(gridCopy[i, j].position, gridCopy[kPathfinding.targetNode.x, kPathfinding.targetNode.y].position); node.parent = null; node.child = null; node.state = NodeState.Untested; gridCopy[i, j] = node; } } }
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { PathFinding pathFinding = new PathFinding(); dstManager.AddComponentData(entity, pathFinding); }
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) { //Writeable. NativeArray <PathFinding> pathfindingArray = chunk.GetNativeArray(pathFindingComponentHandle); BufferAccessor <PathNode> pathNodeBufferAccessor = chunk.GetBufferAccessor(pathNodeBufferHandle); //Read Only. NativeArray <CurrentTarget> currentTargetArray = chunk.GetNativeArray(currentTargetComponentHandle); NativeArray <Translation> translationArray = chunk.GetNativeArray(translationComponentHandle); NativeArray <Entity> entities = chunk.GetNativeArray(entityType); //Create a copy of the grid for this thread and chunk. NativeArray2D <MapNode> gridCopy = new NativeArray2D <MapNode>(gridRef.Length0, gridRef.Length1, Allocator.Temp, NativeArrayOptions.UninitializedMemory); UnsafeUtility.MemCpy(gridCopy.GetUnsafePtr(), gridRef.GetUnsafePtrReadOnly(), gridRef.Length * sizeof(MapNode)); for (int indexInChunk = 0; indexInChunk < chunk.Count; ++indexInChunk) { //Writable. PathFinding pathfinding = pathfindingArray[indexInChunk]; DynamicBuffer <PathNode> path = pathNodeBufferAccessor[indexInChunk]; //Read Only. Entity entity = entities[indexInChunk]; CurrentTarget currentTarget = currentTargetArray[indexInChunk]; Translation translation = translationArray[indexInChunk]; bool hasPath = chunk.Has(hasPathComponentHandle); if (!pathfinding.requestedPath) { //We only want to remove our has path component if we didn't request a new one, to avoid re-adding later in the job if we find a new path. if (pathfinding.completedPath) { pathfinding.completedPath = false; ecb.RemoveComponent <HasPathTag>(chunkIndex, entity); pathfindingArray[indexInChunk] = pathfinding; } continue; } path.Clear(); pathfinding.currentIndexOnPath = 0; pathfinding.completedPath = false; pathfinding.requestedPath = false; //Calculate the closest nodes to us and our target position. //Don't search for path if we're already at our target node. pathfinding.currentNode = FindNearestNode(translation.Value, gridCopy); pathfinding.targetNode = FindNearestNode(currentTarget.targetData.targetPos, gridCopy); if (pathfinding.targetNode.Equals(pathfinding.currentNode)) { pathfindingArray[indexInChunk] = pathfinding; continue; } CalculateGridH(gridCopy, ref pathfinding); bool pathfound = SearchForPath(pathfinding.currentNode, pathfinding.targetNode, gridCopy); if (pathfound) { ConstructPath(gridCopy, ref pathfinding, ref path); if (!hasPath) { ecb.AddComponent <HasPathTag>(chunkIndex, entity); } } else if (hasPath) { ecb.RemoveComponent <HasPathTag>(chunkIndex, entity); } pathfindingArray[indexInChunk] = pathfinding; } gridCopy.Dispose(); }