/// <summary> /// Create an empty grid /// </summary> public Griding(Game game) : base(game) { this.Components = new List<IGridable>(); this.range = new Rectangle(); Grid = new Cell[0, 0]; }
/// <summary> /// Create grid /// </summary> /// <param name="range">Range</param> /// <param name="nRows">Number of rows</param> /// <param name="nColumns">Number of columns</param> public Griding(Game game, Rectangle range, int nRows, int nColumns) : base(game) { this.Components = new List<IGridable>(); this.range = range; Grid = new Cell[nRows, nColumns]; this.nRows = nRows; this.nColumns = nColumns; this.Divide(); }
protected virtual void Divide() { this.cellWidth = (int)(this.Range.Width / this.nColumns); this.cellHeight = (int)(this.Range.Height / this.nRows); this.range.Width -= this.range.Width % cellWidth; this.range.Height -= this.range.Height % cellHeight; for (int i = 0; i < nRows; ++i) { for (int j = 0; j < nColumns; ++j) { Grid[i, j] = new Cell(); Grid[i, j].Grid = this; Grid[i, j].Index = new Vector2(i, j); Grid[i, j].Range = new Rectangle(this.range.X + j * cellWidth, this.range.Y + i * cellHeight, cellWidth, cellHeight); Grid[i, j].Range = new Rectangle(j * cellWidth + this.range.X, i * cellHeight + this.range.Y, cellWidth, cellHeight); } } }