Пример #1
0
        private CellBehavior[,] CreateAggregatedGrid()
        {
            var aggregatedGrid = new CellBehavior[rowCount, columnCount];

            void CaclulateRow(int row)
            {
                var minRow = Math.Max(0, row - 1);
                var maxRow = Math.Min(rowCount - 1, row + 1);

                for (int column = 0; column < columnCount; column++)
                {
                    var minColumn = Math.Max(0, column - 1);
                    var maxColumn = Math.Min(columnCount - 1, column + 1);

                    aggregatedGrid[row, column] = AggregateCellBehaviors(minRow, maxRow, minColumn, maxColumn);
                }
            }

            Parallel.For(0, rowCount, parallelOptions, CaclulateRow);

            return(aggregatedGrid);
        }
Пример #2
0
        private void CalculateCellBehavior()
        {
            void CaclulateRow(int row)
            {
                for (int column = 0; column < columnCount; column++)
                {
                    // Get boids in the cell and aggregate
                    var center    = Vector2.Zero;
                    var direction = Vector2.Zero;
                    int count     = 0;

                    var boundingBox = new RectangleF(column * cellSize, row * cellSize, cellSize, cellSize);
                    var neighbors   = Physics.BoxcastBroadphase(boundingBox);

                    foreach (var neighbor in neighbors)
                    {
                        center    += neighbor.GetComponent <DrawInfo>().Position;
                        direction += neighbor.GetComponent <Velocity>().Value;
                        count++;

                        var cellId = neighbor.Entity.GetComponent <CellId>();
                        cellId.X = column;
                        cellId.Y = row;
                    }

                    grid[row, column] = new CellBehavior(center, direction, count);
                }
            }

            var singleThreadedOptions = new ParallelOptions()
            {
                MaxDegreeOfParallelism = 1
            };

            Parallel.For(0, rowCount, singleThreadedOptions, CaclulateRow);
        }