/// <summary> /// Constructs a Grid instance from the given walkability informations. /// </summary> /// <param name="walkabilityReader">Reference to the object that provides the walkability informations.</param> /// <param name="maxMovingSize">The maximum size of moving agents.</param> public Grid(IWalkabilityReader walkabilityReader, int maxMovingSize) { this.agents = new RCSet <Agent>(); this.cells = new Cell[walkabilityReader.Width, walkabilityReader.Height]; this.width = walkabilityReader.Width; this.height = walkabilityReader.Height; this.maxMovingSize = maxMovingSize; this.obstacleEnvironment = new ObstacleEnvironment(this.maxMovingSize); /// Create the array of sectors int horzSectorCount = this.Width / Sector.SECTOR_SIZE; int vertSectorCount = this.Height / Sector.SECTOR_SIZE; if (this.Width % Sector.SECTOR_SIZE > 0) { horzSectorCount++; } if (this.Height % Sector.SECTOR_SIZE > 0) { vertSectorCount++; } Sector[,] sectors = new Sector[horzSectorCount, vertSectorCount]; /// Create the cells. for (int row = 0; row < this.height; row++) { for (int column = 0; column < this.width; column++) { /// Get the sector of the cell to be created of create the sector if not yet been created. RCIntVector sectorIndex = new RCIntVector(column / Sector.SECTOR_SIZE, row / Sector.SECTOR_SIZE); Sector sectorOfCell = sectors[sectorIndex.X, sectorIndex.Y]; if (sectorOfCell == null) { RCIntRectangle sectorArea = new RCIntRectangle(sectorIndex.X * Sector.SECTOR_SIZE, sectorIndex.Y * Sector.SECTOR_SIZE, Sector.SECTOR_SIZE, Sector.SECTOR_SIZE); sectorOfCell = new Sector(sectorArea, this); sectors[sectorIndex.X, sectorIndex.Y] = sectorOfCell; } /// Create the cell and add it to its sector. this.cells[column, row] = new Cell(new RCIntVector(column, row), walkabilityReader[column, row], this, sectorOfCell); } } /// Calculate the intial subdivisions of the sectors. for (int sectorIndexX = 0; sectorIndexX < horzSectorCount; sectorIndexX++) { for (int sectorIndexY = 0; sectorIndexY < vertSectorCount; sectorIndexY++) { sectors[sectorIndexX, sectorIndexY].CreateInitialSubdivisions(); } } }
/// <see cref="IPathfinder.Initialize"/> public void Initialize(IWalkabilityReader walkabilityReader, int maxMovingSize) { if (walkabilityReader == null) { throw new ArgumentNullException("walkabilityReader"); } if (maxMovingSize < 1) { throw new ArgumentOutOfRangeException("maxMovingSize", "The value of maxMovingSize shall be greater than 0!"); } /// Create the new grid. this.grid = new Grid(walkabilityReader, maxMovingSize); }