/* Determine the world coordinates for the Blocks in the Tetromino and * let the Board keep the Block reference in that cell. */ void FixToBoard() { bool[,] grid = BlockMatrix.PatternFor(pattern, rotation); for (int i = 0; i < BlockMatrix.GRID_SIZE; i++) { for (int j = 0; j < BlockMatrix.GRID_SIZE; j++) { if (grid [j, i]) { int x = (int)Coordinates.x + i; int y = (int)Coordinates.y + j; Vector2 target = new Vector2(x, y); Block transfer = blocks [0]; blocks.RemoveAt(0); board.Fix(target, transfer); } } } Destroy(gameObject); board.CompleteLines(); }
/* Take a reading from the board and see if anything would block the Tetromino * from moving to a new location. * * @param direction Target offset from current location (zero for rotation) * @param rotation Desired rotation in new position * @return False if move is impossible */ bool ValidateMove(Vector2 direction, int rotation) { // Specify the location the Tetromino is attempting to move to Vector2 target = Coordinates + direction; // Grab the cells at that location and a copy of the Tetromino pattern bool[,] testGrid = board.CreateTestGrid(target); bool[,] patternGrid = BlockMatrix.PatternFor(pattern, rotation); // Ensure there are gaps in the board whenever there are blocks in the pattern for (int row = 0; row < BlockMatrix.GRID_SIZE; row++) { for (int column = 0; column < BlockMatrix.GRID_SIZE; column++) { // If the testgrid is already occupied where the tetromino is trying to go, fail if (testGrid [row, column] && patternGrid [row, column]) { return(false); } } } // Looks like we're good to go! return(true); }
/** * Position the Blocks within the Tetromino grid after a rotation etc. */ void Draw() { // Retrieve the pattern for drawing bool[,] patternGrid = BlockMatrix.PatternFor(pattern, rotation); int count = 0; // Pass over the grid, placing Blocks whenever a slot is occupied for (int row = 0; row < BlockMatrix.GRID_SIZE; row++) { for (int column = 0; column < BlockMatrix.GRID_SIZE; column++) { // Don't forget: matrices store values (y,x) ... if (patternGrid[column, row]) { // ... but screen coordinates are (x,y)! Vector2 position = gridAlignmentOffset + new Vector2(row, 0 - column); blocks [count++].transform.localPosition = position; } } } }