예제 #1
0
        protected override void OnUpdate()
        {
            GridData grid = Grid;

            var animalBuckets = AnimalBuckets;
            var foodBuckets   = FoodBuckets;

            int animalCount = animalQuery.CalculateEntityCount();
            int foodCount   = foodQuery.CalculateEntityCount();

            // Clear in a job so that it is included in the dependency job handle
            Job.WithCode(() =>
            {
                animalBuckets.Clear();
                foodBuckets.Clear();

                if (animalCount > animalBuckets.Capacity)
                {
                    animalBuckets.Capacity = animalCount;
                }
                if (foodCount > foodBuckets.Capacity)
                {
                    foodBuckets.Capacity = foodCount;
                }
            }).Schedule();

            var animalBucketsWriter = AnimalBuckets.AsParallelWriter();

            Entities
            .WithNone <DeathEvent>()
            .ForEach((Entity entity, in Translation position, in AnimalTypeData animalTypeData, in Rotation rotation) =>
            {
                var data = new BucketAnimalData
                {
                    Entity         = entity,
                    Position       = position.Value,
                    AnimalTypeData = animalTypeData,
                    Rotation       = rotation.Value
                };
                animalBucketsWriter.Add(grid.GetCellKey(position.Value), data);
            }).ScheduleParallel();
예제 #2
0
        /// <summary>
        /// Returns keys to all nearby buckets.
        /// <para/>
        /// Guaranteed to cover all buckets within the specified radius of the specified world position.
        /// </summary>
        public static NativeArray <int> GetNearbyCellsKeys(GridData grid, float3 worldPosition, float radius, Allocator allocator)
        {
            float3 startPos = worldPosition + new float3(-radius, 0, -radius);
            float3 endPos   = worldPosition + new float3(radius, 0, radius);
            int2   start    = grid.GetGridPosition(startPos);
            int2   end      = grid.GetGridPosition(endPos);
            int    length   = (1 + end.x - start.x) * (1 + end.y - start.y);

            NativeArray <int> nearbyCellKeys = new NativeArray <int>(length, allocator);

            int index = 0;

            for (int i = start.x; i <= end.x; i++)
            {
                for (int j = start.y; j <= end.y; j++)
                {
                    nearbyCellKeys[index] = grid.GetCellKey(new int2(i, j));
                    index++;
                }
            }

            return(nearbyCellKeys);
        }