protected override JobHandle OnUpdate(JobHandle inputDependencies)
    {
        var damageDict = new NativeMultiHashMap <int, int>(bulletGroup.CalculateEntityCount(), Allocator.TempJob);

        var nextCells = new PrevCells {
            damageDict = damageDict
        };

        if (prevCells.Count == 0)
        {
            prevCells.Add(nextCells);
        }
        else
        {
            prevCells[0].damageDict.Dispose();
        }
        prevCells[0] = nextCells;

        ref PhysicsWorld physicsWorld = ref Unity.Entities.World.Active.GetExistingSystem <BuildPhysicsWorld>().PhysicsWorld;
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            EntityManager.GetAllUniqueSharedComponentData(m_UniqueTypes);

            var obstacleCount = m_ObstacleGroup.CalculateLength();
            var targetCount   = m_TargetGroup.CalculateLength();

            // Ignore typeIndex 0, can't use the default for anything meaningful.
            for (int typeIndex = 1; typeIndex < m_UniqueTypes.Count; typeIndex++)
            {
                var settings = m_UniqueTypes[typeIndex];
                m_BoidGroup.SetFilter(settings);

                var boidCount = m_BoidGroup.CalculateLength();

                var cacheIndex                = typeIndex - 1;
                var cellIndices               = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                var hashMap                   = new NativeMultiHashMap <int, int>(boidCount, Allocator.TempJob);
                var cellObstacleDistance      = new NativeArray <float>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                var cellObstaclePositionIndex = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                var cellTargetPositionIndex   = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                var cellCount                 = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);


                var cellAlignment = new NativeArray <float3>(boidCount, Allocator.TempJob,
                                                             NativeArrayOptions.UninitializedMemory);
                var cellSeparation = new NativeArray <float3>(boidCount, Allocator.TempJob,
                                                              NativeArrayOptions.UninitializedMemory);
                var copyTargetPositions = new NativeArray <float3>(targetCount, Allocator.TempJob,
                                                                   NativeArrayOptions.UninitializedMemory);
                var copyObstaclePositions = new NativeArray <float3>(obstacleCount, Allocator.TempJob,
                                                                     NativeArrayOptions.UninitializedMemory);

                var initialCellAlignmentJob = new CopyHeadings
                {
                    headings = cellAlignment
                };
                var initialCellAlignmentJobHandle = initialCellAlignmentJob.Schedule(m_BoidGroup, inputDeps);

                var initialCellSeparationJob = new CopyPositions
                {
                    positions = cellSeparation
                };
                var initialCellSeparationJobHandle = initialCellSeparationJob.Schedule(m_BoidGroup, inputDeps);

                var copyTargetPositionsJob = new CopyPositions
                {
                    positions = copyTargetPositions
                };
                var copyTargetPositionsJobHandle = copyTargetPositionsJob.Schedule(m_TargetGroup, inputDeps);

                var copyObstaclePositionsJob = new CopyPositions
                {
                    positions = copyObstaclePositions
                };
                var copyObstaclePositionsJobHandle = copyObstaclePositionsJob.Schedule(m_ObstacleGroup, inputDeps);

                var nextCells = new PrevCells
                {
                    cellIndices               = cellIndices,
                    hashMap                   = hashMap,
                    copyObstaclePositions     = copyObstaclePositions,
                    copyTargetPositions       = copyTargetPositions,
                    cellAlignment             = cellAlignment,
                    cellSeparation            = cellSeparation,
                    cellObstacleDistance      = cellObstacleDistance,
                    cellObstaclePositionIndex = cellObstaclePositionIndex,
                    cellTargetPositionIndex   = cellTargetPositionIndex,
                    cellCount                 = cellCount
                };

                if (cacheIndex > (m_PrevCells.Count - 1))
                {
                    m_PrevCells.Add(nextCells);
                }
                else
                {
                    m_PrevCells[cacheIndex].hashMap.Dispose();
                    m_PrevCells[cacheIndex].cellIndices.Dispose();
                    m_PrevCells[cacheIndex].cellObstaclePositionIndex.Dispose();
                    m_PrevCells[cacheIndex].cellTargetPositionIndex.Dispose();
                    m_PrevCells[cacheIndex].copyTargetPositions.Dispose();
                    m_PrevCells[cacheIndex].copyObstaclePositions.Dispose();
                    m_PrevCells[cacheIndex].cellAlignment.Dispose();
                    m_PrevCells[cacheIndex].cellSeparation.Dispose();
                    m_PrevCells[cacheIndex].cellObstacleDistance.Dispose();
                    m_PrevCells[cacheIndex].cellCount.Dispose();
                }
                m_PrevCells[cacheIndex] = nextCells;

                var hashPositionsJob = new HashPositions
                {
                    hashMap    = hashMap.ToConcurrent(),
                    cellRadius = settings.CellRadius
                };
                var hashPositionsJobHandle = hashPositionsJob.Schedule(m_BoidGroup, inputDeps);

                var initialCellCountJob = new MemsetNativeArray <int>
                {
                    Source = cellCount,
                    Value  = 1
                };
                var initialCellCountJobHandle = initialCellCountJob.Schedule(boidCount, 64, inputDeps);

                var initialCellBarrierJobHandle        = JobHandle.CombineDependencies(initialCellAlignmentJobHandle, initialCellSeparationJobHandle, initialCellCountJobHandle);
                var copyTargetObstacleBarrierJobHandle = JobHandle.CombineDependencies(copyTargetPositionsJobHandle, copyObstaclePositionsJobHandle);
                var mergeCellsBarrierJobHandle         = JobHandle.CombineDependencies(hashPositionsJobHandle, initialCellBarrierJobHandle, copyTargetObstacleBarrierJobHandle);

                var mergeCellsJob = new MergeCells
                {
                    cellIndices               = cellIndices,
                    cellAlignment             = cellAlignment,
                    cellSeparation            = cellSeparation,
                    cellObstacleDistance      = cellObstacleDistance,
                    cellObstaclePositionIndex = cellObstaclePositionIndex,
                    cellTargetPositionIndex   = cellTargetPositionIndex,
                    cellCount         = cellCount,
                    targetPositions   = copyTargetPositions,
                    obstaclePositions = copyObstaclePositions
                };
                var mergeCellsJobHandle = mergeCellsJob.Schedule(hashMap, 64, mergeCellsBarrierJobHandle);

                var steerJob = new Steer
                {
                    cellIndices               = nextCells.cellIndices,
                    settings                  = settings,
                    cellAlignment             = cellAlignment,
                    cellSeparation            = cellSeparation,
                    cellObstacleDistance      = cellObstacleDistance,
                    cellObstaclePositionIndex = cellObstaclePositionIndex,
                    cellTargetPositionIndex   = cellTargetPositionIndex,
                    cellCount                 = cellCount,
                    targetPositions           = copyTargetPositions,
                    obstaclePositions         = copyObstaclePositions,
                    dt = Time.deltaTime,
                };
                var steerJobHandle = steerJob.Schedule(m_BoidGroup, mergeCellsJobHandle);

                inputDeps = steerJobHandle;
                m_BoidGroup.AddDependency(inputDeps);
            }
            m_UniqueTypes.Clear();

            return(inputDeps);
        }
Beispiel #3
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        BoidActionSort    boidActionsSort   = new BoidActionSort();
        NonBoidActionSort nonBoidActionSort = new NonBoidActionSort();

        EntityManager.GetAllUniqueSharedComponentData(m_UniqueTypes);

        int obstacleCount = m_ObstacleGroup.CalculateLength();
        int targetCount   = m_TargetGroup.CalculateLength();

        // Ingore typeIndex 0, can't use the default for anything meaningful.
        for (int typeIndex = 1; typeIndex < m_UniqueTypes.Count; typeIndex++)
        {
            MainBoid uniqueBoidConfig = m_UniqueTypes[typeIndex];
            m_BoidGroup.SetFilter(uniqueBoidConfig);

            int boidCount = m_BoidGroup.CalculateLength();
            //some of this can be cached from last time to reduce component data calls.
            int cacheIndex = typeIndex - 1;
            //this was causing an undisposed allocation error
            //NativeArray<JobHandle> initializationJobHandles = new NativeArray<JobHandle>(5, Allocator.Temp);

            NativeArray <Heading> boidHeadings = m_BoidGroup.ToComponentDataArray <Heading>(Allocator.TempJob, out JobHandle initialCellAlignmentJobHandle);
            //TODO: make this into a 2d array so that the 2d positions doesnt have to be calculated all the time.
            NativeArray <Translation> boidPositions         = m_BoidGroup.ToComponentDataArray <Translation>(Allocator.TempJob, out JobHandle initialCellSeparationJobHandle);
            NativeArray <Translation> copyTargetPositions   = m_TargetGroup.ToComponentDataArray <Translation>(Allocator.TempJob, out JobHandle copyTargetPositionsJobHandle);
            NativeArray <Translation> copyObstaclePositions = m_ObstacleGroup.ToComponentDataArray <Translation>(Allocator.TempJob, out JobHandle copyObstaclePositionsJobHandle);
            //  initializationJobHandles[0] = initialCellAlignmentJobHandle;
            //    initializationJobHandles[1] = initialCellSeparationJobHandle;
            //   initializationJobHandles[2] = copyTargetPositionsJobHandle;
            //    initializationJobHandles[3] = copyObstaclePositionsJobHandle;

            NativeArray <BoidAction> orderedBoidActions = new NativeArray <BoidAction>(uniqueBoidConfig.boidActions.Length, Allocator.TempJob);
            orderedBoidActions.CopyFrom(uniqueBoidConfig.boidActions);

            NativeArray <NonBoidAction> orderedNonBoidActions = new NativeArray <NonBoidAction>(uniqueBoidConfig.boidActions.Length, Allocator.TempJob);
            orderedBoidActions.CopyFrom(uniqueBoidConfig.boidActions);

            orderedNonBoidActions.Sort(nonBoidActionSort);
            orderedBoidActions.Sort(boidActionsSort);

            var hashMap = new NativeHashMap <float3, int>(boidCount, Allocator.TempJob);

            var hashPositionsJob = new HashPositions
            {
                hashMap = hashMap.ToConcurrent()
            };
            var hashPositionsJobHandle = hashPositionsJob.Schedule(m_BoidGroup, inputDeps);

            //  initializationJobHandles[4] = hashPositionsJobHandle;

            var nextCells = new PrevCells
            {
                hashMap = hashMap,

                boidHeadings  = boidHeadings,
                boidPositions = boidPositions,

                copyObstaclePositions = copyObstaclePositions,
                copyTargetPositions   = copyTargetPositions,

                orderedBoidActions    = orderedBoidActions,
                orderedNonBoidActions = orderedNonBoidActions,
            };
            if (cacheIndex > (m_PrevCells.Count - 1))
            {
                m_PrevCells.Add(nextCells);
            }
            else
            {
                m_PrevCells[cacheIndex].hashMap.Dispose();

                m_PrevCells[cacheIndex].copyTargetPositions.Dispose();
                m_PrevCells[cacheIndex].copyObstaclePositions.Dispose();

                m_PrevCells[cacheIndex].boidHeadings.Dispose();
                m_PrevCells[cacheIndex].boidPositions.Dispose();

                m_PrevCells[cacheIndex].orderedBoidActions.Dispose();
                m_PrevCells[cacheIndex].orderedNonBoidActions.Dispose();
            }
            m_PrevCells[cacheIndex] = nextCells;

            JobHandle initialCellBarrierJobHandle        = JobHandle.CombineDependencies(initialCellAlignmentJobHandle, initialCellSeparationJobHandle, copyTargetPositionsJobHandle);
            JobHandle copyTargetObstacleBarrierJobHandle = JobHandle.CombineDependencies(initialCellBarrierJobHandle, copyObstaclePositionsJobHandle, hashPositionsJobHandle);

            Steer steerJob = new Steer
            {
                //                boidActionFunctions = boidActionFunctions,
                //boidConfig = uniqueBoidConfig,
                boidIndexs            = hashMap,
                boidHeadings          = boidHeadings,
                boidPositions         = boidPositions,
                orderedBoidActions    = orderedBoidActions,
                orderedNonBoidActions = orderedNonBoidActions,
                targetPositions       = copyTargetPositions,
                obstaclePositions     = copyObstaclePositions,
                dt = Time.deltaTime
            };
            JobHandle steerJobHandle = steerJob.Schedule(m_BoidGroup, copyTargetObstacleBarrierJobHandle);

            inputDeps = steerJobHandle;
            m_BoidGroup.AddDependency(inputDeps);
        }
        m_UniqueTypes.Clear();

        return(inputDeps);
    }