Esempio n. 1
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);
    }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var AlignmentAction = new BoidAction
            {
                actionType     = BoidActionType.Alignment,
                range          = 4,
                weight         = 1,
                divideByNearby = false,
                viewangle      = 20,
            };
            var cohesionAction = new BoidAction
            {
                actionType     = BoidActionType.Cohesion,
                range          = 10,
                weight         = 2,
                divideByNearby = false,
                viewangle      = 20,
            };
            var seperationAction = new BoidAction
            {
                actionType     = BoidActionType.Seperation,
                range          = 0.2f,
                weight         = 3,
                divideByNearby = false,
                viewangle      = 20,
            };
            var fleeAction = new NonBoidAction
            {
                actionType     = NonBoidActionType.Fleeing,
                range          = 10f,
                weight         = 4,
                divideByNearby = false,
                viewangle      = 50,
            };

            var aBoid = new MainBoid
            {
                boidActions = new BoidAction[]
                {
                    AlignmentAction,
                    cohesionAction,
                    seperationAction,
                },
                nonBoidActions = new NonBoidAction[]
                {
                    fleeAction
                }
            };
            var bBoid = new MainBoid
            {
                boidActions = new BoidAction[]
                {
                    AlignmentAction,
                    cohesionAction,
                    seperationAction,
                },
                nonBoidActions = new NonBoidAction[]
                {
                    fleeAction
                }
            };
            var a = new int[] { 10, 1 };
            var b = new int[] { 10, 1 };

            Console.WriteLine(((IStructuralEquatable)a).GetHashCode(EqualityComparer <int> .Default) + " : " + ((IStructuralEquatable)b).GetHashCode(EqualityComparer <int> .Default));
        }