public PlacementGrid(int width, int height, bool[] closedSpots) { // Make sure inital parameters make sense if (width * height != closedSpots.Length) { throw new UnityException ("Invalid size of array given width and height."); } this.rotation = 0; this.width = width; this.height = height; // Create internal array internalCells = new PGridCell[closedSpots.Length]; for (int i = 0; i < closedSpots.Length; i++) { internalCells [i] = new PGridCell (i / width, i % width, closedSpots [i]); } }
// Rotate this grid left, recalculate internalCells and swap width and height public void RotateLeft() { PGridCell[] newInternalCells = new PGridCell[width * height]; for (int i = 0; i < internalCells.Length; i++) { // new index is [numCols - 1 - col][row] int newRow = (width - 1) - (i % width); int newCol = i / width; int index = height * newRow + newCol; newInternalCells [index] = new PGridCell (newRow, newCol, internalCells[i].Closed); } internalCells = newInternalCells; int newHeight = width; width = height; height = newHeight; rotation = (rotation + 90) % 360; }
/* * This method returns if the obstacle can be placed at the given cell in the gameworld. * Params - obstacleGrid: the PlacementGrid of the obstacle you are testing * cell: the cell to test placement on */ bool CanPlaceObject(PlacementGrid obstacleGrid, PGridCell cell) { // False if grid bounding box goes outside bounds of level if (obstacleGrid.Height + cell.Row > levelPGrid.Height || obstacleGrid.Width + cell.Col > levelPGrid.Width) return false; // Go through obstacle's placement grid and make sure that each spot // it needs is available, starting at cell for (int r = 0; r < obstacleGrid.Height; r++) { for (int c = 0; c < obstacleGrid.Width; c++) { // If a cell is closed on the obstacle then it is occupied // If a cell is closed on the main grid, it is occpupied or adjacent to an occupied spot if (obstacleGrid.Closed(r, c) && levelPGrid.Closed(cell.Row + r, cell.Col + c)) { return false; } } } return true; }