예제 #1
0
    private void initializePersistentArrays(int numberThreads, int2 gridParams)
    {
        neighbourOffsetArray    = new NativeArray <int2>(8, Allocator.Persistent);
        neighbourOffsetArray[0] = new int2(-1, 0);  // Left
        neighbourOffsetArray[1] = new int2(+1, 0);  // Right
        neighbourOffsetArray[2] = new int2(0, +1);  // Up
        neighbourOffsetArray[3] = new int2(0, -1);  // Down
        neighbourOffsetArray[4] = new int2(-1, -1); // Left Down
        neighbourOffsetArray[5] = new int2(-1, +1); // Left Up
        neighbourOffsetArray[6] = new int2(+1, -1); // Right Down
        neighbourOffsetArray[7] = new int2(+1, +1); // Right Up


        //Initialize the arrays for the worker threads to use in parrallel
        jobCollections = new List <jobCollection>(numberThreads);

        for (int i = 0; i < numberThreads; i++)
        {
            jobCollection collection = new jobCollection
            {
                CostSoFar = new NativeArray <float>(gridParams.x * gridParams.y, Allocator.Persistent),
                CameFrom  = new NativeArray <PathNode>(gridParams.x * gridParams.y, Allocator.Persistent),
                OpenSet   = new NativeMinHeap(gridParams.x * gridParams.y, Allocator.Persistent),
                foundPath = true
            };

            jobCollections.Add(collection);
        }
    }
예제 #2
0
    private void disposeArrays()
    {
        neighbourOffsetArray.Dispose();

        for (int i = 0; i < jobCollections.Count; i++)
        {
            jobCollection collection = jobCollections[i];
            collection.CameFrom.Dispose();
            collection.CostSoFar.Dispose();
            collection.OpenSet.Dispose();
        }

        //obstacleArray.Dispose();
    }
예제 #3
0
    protected override JobHandle OnUpdate(JobHandle jobHandle)
    {
        if (!initialized)
        {
            initializePersistentArrays(numberWorkerThreads, gridParams);
            initialized = true;
        }

        //QuadTree.ClearAndBulkInsert(obstacleArray);

        //NativeList<ObstacleStruct> queryResult = new NativeList<ObstacleStruct>(Allocator.Temp);

        //QuadTree.RangeQuery(new NativeQuadTree.AABB2D(new float2(500, 500), new float2(1000, 1000)), queryResult);

        //Debug.Log(queryResult);



        EntityCommandBuffer concurrentCommandBuffer = entityCommandBufferSystem.CreateCommandBuffer();

        EntityQuery entityQuery = GetEntityQuery(ComponentType.ReadOnly <PathfindingParams>());

        NativeArray <PathfindingParams> pathFindingArray = entityQuery.ToComponentDataArray <PathfindingParams>(Allocator.TempJob);
        NativeArray <Entity>            entityArray      = entityQuery.ToEntityArray(Allocator.TempJob);



        NativeArray <JobHandle> jobHandles = new NativeArray <JobHandle>(jobCollections.Count, Allocator.TempJob);

        for (int index = 0; index < jobCollections.Count; index++)
        {
            jobHandles[index] = jobHandle;
        }

        for (int r = 0; r < entityArray.Length; r++)
        {
            int               index             = r % jobCollections.Count;
            jobCollection     data              = jobCollections[index];
            PathfindingParams pathfindingParams = pathFindingArray[r];

            FindPathJob findPathJob = new FindPathJob
            {
                DimX            = gridParams.x,
                DimY            = gridParams.y,
                Neighbours      = neighbourOffsetArray,
                startPosition   = pathfindingParams.startPosition,
                endPosition     = pathfindingParams.endPosition,
                itterationLimit = itterationLimit,
                Grid            = ObstacleController.instance.getNativeMap(),
                CostSoFar       = data.CostSoFar,
                CameFrom        = data.CameFrom,
                entity          = entityArray[r],
                OpenSet         = data.OpenSet
            };

            JobHandle findPathHandle = findPathJob.Schedule(jobHandles[index]);



            ResetJob resetJob = new ResetJob
            {
                CostSoFar = data.CostSoFar,
                OpenSet   = data.OpenSet,
                CameFrom  = data.CameFrom,
                DimX      = gridParams.x,
                entity    = entityArray[r],
                pathFound = data.foundPath,
                pathfindingParamsComponentDataFromEntity = GetComponentDataFromEntity <PathfindingParams>(),
                pathFollowComponentDataFromEntity        = GetComponentDataFromEntity <PathFollow>(),
                pathPositionBufferFromEntity             = GetBufferFromEntity <PathPosition>(),
                startPosition = pathfindingParams.startPosition,
                endPosition   = pathfindingParams.endPosition,
            };

            jobHandles[index] = resetJob.Schedule(findPathHandle);
            //PostUpdateCommands.RemoveComponent<PathfindingParams>(entity);
            concurrentCommandBuffer.RemoveComponent <PathfindingParams>(entityArray[r]);
        }

        jobHandle = JobHandle.CombineDependencies(jobHandles);
        entityCommandBufferSystem.AddJobHandleForProducer(jobHandle);

        entityArray.Dispose();
        pathFindingArray.Dispose();

        jobHandles.Dispose();


        return(jobHandle);
    }