Example #1
0
    private void Update()
    {
        GridMaster gm = GridMaster.Instance;

        CalculateStartEndPosJob calcStartEndJob = new CalculateStartEndPosJob()
        {
            gridBoundsCenter  = gm.Bounds.center,
            gridBoundsExtents = gm.Bounds.extents,
            nodeSize          = GridMaster.NodeSize,
            isGridCreated     = gm.IsGridCreated,
            gridWidth         = gm.GridWidth,

            endPositionsToChooseFrom = endPositionsToChooseFrom,

            startPositionsIndices = new NativeArray <int>(quantity, Allocator.TempJob, NativeArrayOptions.UninitializedMemory),
            endPositionsIndices   = new NativeArray <int>(quantity, Allocator.TempJob, NativeArrayOptions.UninitializedMemory),
            rand = new Random(0x6E624EB7u)
        };

        JobHandle calcStartEndHandle = calcStartEndJob.Schedule(agentsTransAcc);

        FindPathJobParallel findPathsJob    = new FindPathJobParallel(gm.GridWidth * gm.GridDepth, gm.GridWidth, 4, calcStartEndJob.startPositionsIndices, calcStartEndJob.endPositionsIndices, gm.NodesNeighbors, gm.NodesTypes, quantity);
        JobHandle           findPathsHandle = findPathsJob.Schedule(quantity, 4, calcStartEndHandle);

        MoveAgentsJob moveJob = new MoveAgentsJob()
        {
            nodeIndicesDestinations = findPathsJob.nextNodesIndices,
            nodes = gm.NodesTransforms,
            speed = agentsSpeed,
            dt    = Time.deltaTime
        };

        JobHandle moveHandle = moveJob.Schedule(agentsTransAcc, findPathsHandle);

        UpdateCamPivot();

        moveHandle.Complete();

        findPathsJob.Dispose();
        calcStartEndJob.Dispose();

        if (Keyboard.current.escapeKey.wasPressedThisFrame)
        {
            Application.Quit();
        }
    }
Example #2
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            if (Controller == null)
            {
                return(inputDeps);
            }

            MoveAgentsJob moveJob = new MoveAgentsJob
            {
                DeltaTime = Time.deltaTime,
                Query     = Query,
                Random    = Random
            };

            inputDeps = moveJob.ScheduleSingle(this, inputDeps);

            Controller.AddQueryDependency(inputDeps);

            return(inputDeps);
        }
    private void Update()
    {
        float      dt = Time.deltaTime;
        GridMaster gm = GridMaster.Instance;

        CalculateStartEndPosJob calcStartEndJob = new CalculateStartEndPosJob()
        {
            gridBoundsCenter  = gm.Bounds.center,
            gridBoundsExtents = gm.Bounds.extents,
            nodeSize          = GridMaster.NodeSize,
            isGridCreated     = gm.IsGridCreated,
            gridWidth         = gm.GridWidth,
            rand = new Random(0x6E624EB7u),
            endPositionsToChooseFrom = endPositionsToChooseFrom,
            startPositionsIndices    = new NativeArray <int>(quantity, Allocator.TempJob, NativeArrayOptions.UninitializedMemory),
            endPositionsIndices      = new NativeArray <int>(quantity, Allocator.TempJob, NativeArrayOptions.UninitializedMemory)
        };

        JobHandle calcStartEndHandle = calcStartEndJob.Schedule(agentsTransAcc);

        FindPathJobParallel findPathsJob = new FindPathJobParallel()
        {
            numNodes          = gm.GridWidth * gm.GridDepth,
            gridWidth         = gm.GridWidth,
            numNeighbors      = GridMaster.NodeNeighbors,
            startNodesIndices = calcStartEndJob.startPositionsIndices,
            endNodeIndices    = calcStartEndJob.endPositionsIndices,
            nodesNeighbors    = gm.NodesNeighbors,
            nodesTypes        = gm.NodesTypes,
            nextNodesIndices  = new NativeArray <int>(quantity, Allocator.TempJob, NativeArrayOptions.UninitializedMemory),
        };

        JobHandle findPathsHandle = findPathsJob.Schedule(quantity, 4, calcStartEndHandle);

        MoveAgentsJob moveJob = new MoveAgentsJob()
        {
            nodeIndicesDestinations = findPathsJob.nextNodesIndices,
            nodes = gm.NodesTransforms,
            speed = agentsSpeed,
            dt    = dt
        };

        JobHandle moveHandle = moveJob.Schedule(agentsTransAcc, findPathsHandle);

        UpdateCamPivot(dt);

        JobHandle disposeHandle = calcStartEndJob.startPositionsIndices.Dispose(findPathsHandle);

        disposeHandle = JobHandle.CombineDependencies(disposeHandle, calcStartEndJob.endPositionsIndices.Dispose(findPathsHandle));
        disposeHandle = JobHandle.CombineDependencies(disposeHandle, findPathsJob.nextNodesIndices.Dispose(moveHandle));

        JobHandle.CompleteAll(ref moveHandle, ref disposeHandle);

#if !UNITY_EDITOR
        if (Keyboard.current.escapeKey.wasPressedThisFrame)
        {
            Application.Quit();
        }
#else
        if (Keyboard.current.escapeKey.wasPressedThisFrame)
        {
            EditorApplication.isPlaying = false;
        }
#endif
    }
Example #4
0
    private void Update()
    {
        float      dt = Time.deltaTime;
        GridMaster gm = GridMaster.Instance;

        NativeArray <int> startPositionsIndices = new NativeArray <int>(quantity, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
        NativeArray <int> endPositionsIndices   = new NativeArray <int>(quantity, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

        JobHandle deps = new CalculateStartEndPosJob()
        {
            startPositionsIndices    = startPositionsIndices,
            endPositionsIndices      = endPositionsIndices,
            endPositionsToChooseFrom = endPositionsToChooseFrom,
            gridBoundsCenter         = gm.Bounds.center,
            gridBoundsExtents        = gm.Bounds.extents,
            isGridCreated            = gm.IsGridCreated,
            gridWidth = gm.GridWidth,
            nodeSize  = GridMaster.NodeSize,
            rand      = new Random(0x6E624EB7u),
        }
        .Schedule(agentsTransAcc);

        NativeArray <int> nextNodesIndices = new NativeArray <int>(quantity, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

        const int iterationsPerJob = 8;
        int       loopTimes        = quantity / iterationsPerJob;
        int       remainder        = quantity % iterationsPerJob;

        loopTimes = remainder > 0 ? loopTimes + 1 : loopTimes;

        NativeArray <JobHandle> handles = new NativeArray <JobHandle>(loopTimes, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

        const int scheduleAfterNumOfPaths = 256;
        int       numberOfScheduledPaths  = 0;

        for (int i = 0; i < loopTimes; ++i)
        {
            int iterations = (i < loopTimes - 1 || remainder == 0) ? iterationsPerJob : remainder;
            handles[i] = Pathfinder.ScheduleFindPaths(startPositionsIndices, endPositionsIndices, nextNodesIndices, i * iterationsPerJob, iterations, deps);

            numberOfScheduledPaths += iterations;
            if (numberOfScheduledPaths > scheduleAfterNumOfPaths)
            {
                // force worker threads start resolving paths while the main thread keeps scheduling
                JobHandle.ScheduleBatchedJobs();
                numberOfScheduledPaths %= scheduleAfterNumOfPaths;
            }
        }

        deps = JobHandle.CombineDependencies(handles);

        deps = new MoveAgentsJob()
        {
            nodeIndicesDestinations = nextNodesIndices,
            nodes = gm.NodesTransforms,
            speed = agentsSpeed,
            dt    = dt,
        }
        .Schedule(agentsTransAcc, deps);

        JobHandle disposeHandle = JobHandle.CombineDependencies(startPositionsIndices.Dispose(deps), endPositionsIndices.Dispose(deps), handles.Dispose(deps));

        disposeHandle = JobHandle.CombineDependencies(disposeHandle, nextNodesIndices.Dispose(deps));

        UpdateCamPivot(dt);

        JobHandle.CompleteAll(ref deps, ref disposeHandle);

#if !UNITY_EDITOR
        if (Keyboard.current.escapeKey.wasPressedThisFrame)
        {
            Application.Quit();
        }
#else
        if (Keyboard.current.escapeKey.wasPressedThisFrame)
        {
            EditorApplication.isPlaying = false;
        }
#endif
    }