public void ShiftGridInOppositeDirection(GridManager.Direction shipDirection) { // Deactivate column/row of cells that are going to "leak" after the shift DeactivateLeakingCells(shipDirection); // Shift the grid in the opposite direction of the ship ShiftCells(shipDirection); // Mark the left-over column/row as null EmptyCells(shipDirection); }
void EmptyCells(GridManager.Direction shipDirection) { // Create new column/row of cells that are "empty" after the shift int x = 0, y = 0; if (shipDirection == GridManager.Direction.Right) { // The right column is empty x = size - 1; } else if (shipDirection == GridManager.Direction.Down) { // The bottom row is empty y = size - 1; } // Get direction switch (shipDirection) { case GridManager.Direction.Left: case GridManager.Direction.Right: // Go though a column of cells, and empty cell for (y = 0; y < size; ++y) { //Debug.Log("Empty cell: (" + x + " x " + y + ')'); // Only set the cell to null; do not deactivate it grid[x, y] = null; } break; default: // Go though a row of cells, and empty cell for (x = 0; x < size; ++x) { // Generate a new cell //Debug.Log("Empty cell: (" + x + " x " + y + ')'); // Only set the cell to null; do not deactivate it grid[x, y] = null; } break; } }
void DeactivateLeakingCells(GridManager.Direction shipDirection) { // Deactivate column/row of cells that are going to "leak" after the shift int x = 0, y = 0; if (shipDirection == GridManager.Direction.Left) { // The right column will leak x = size - 1; } else if (shipDirection == GridManager.Direction.Up) { // The bottom row will leak y = size - 1; } switch (shipDirection) { case GridManager.Direction.Left: case GridManager.Direction.Right: // Go though a column of cells for (y = 0; y < size; ++y) { // Deactivate the cells //Debug.Log("Leaking cell: (" + x + " x " + y + ')'); this[x, y] = null; } break; default: // Go though a row of cells for (x = 0; x < size; ++x) { // Deactivate the cells //Debug.Log("Leaking cell: (" + x + " x " + y + ')'); this[x, y] = null; } break; } }
void ShiftCells(GridManager.Direction shipDirection) { // Shift the grid in the opposite direction of the ship int x = 0, y = 0; Vector3 newCenterPosition; switch (shipDirection) { case GridManager.Direction.Left: // Shift all cells to the right newCenterPosition = grid[(sizeHalf - 1), sizeHalf].Position; //Debug.Log("Moving center from " + CenterPosition.ToString() + " to " + newCenterPosition.ToString()); for (x = (size - 1); x > 0; --x) { for (y = 0; y < size; ++y) { //Debug.Log("Shift cell: (" + (x - 1) + " x " + y + ") to (" + x + " x " + y + ')'); grid[x, y] = grid[(x - 1), y]; // Setup the cell to the new position SetupCell(ref grid[x, y], x, y, newCenterPosition); } } break; case GridManager.Direction.Right: // Shift all cells to the left newCenterPosition = grid[(sizeHalf + 1), sizeHalf].Position; //Debug.Log("Moving center from " + CenterPosition.ToString() + " to " + newCenterPosition.ToString()); for (x = 0; x < (size - 1); ++x) { for (y = 0; y < size; ++y) { //Debug.Log("Shift cell: (" + (x + 1) + " x " + y + ") to (" + x + " x " + y + ')'); grid[x, y] = grid[(x + 1), y]; // Setup the cell to the new position SetupCell(ref grid[x, y], x, y, newCenterPosition); } } break; case GridManager.Direction.Up: // Shift all cells to the down newCenterPosition = grid[sizeHalf, (sizeHalf - 1)].Position; //Debug.Log("Moving center from " + CenterPosition.ToString() + " to " + newCenterPosition.ToString()); for (x = 0; x < size; ++x) { for (y = (size - 1); y > 0; --y) { //Debug.Log("Shift cell: (" + x + " x " + (y - 1) + ") to (" + x + " x " + y + ')'); grid[x, y] = grid[x, (y - 1)]; // Setup the cell to the new position SetupCell(ref grid[x, y], x, y, newCenterPosition); } } break; default: // Shift all cells to the up newCenterPosition = grid[sizeHalf, (sizeHalf + 1)].Position; //Debug.Log("Moving center from " + CenterPosition.ToString() + " to " + newCenterPosition.ToString()); for (x = 0; x < size; ++x) { for (y = 0; y < (size - 1); ++y) { //Debug.Log("Shift cell: (" + x + " x " + (y + 1) + ") to (" + x + " x " + y + ')'); grid[x, y] = grid[x, (y + 1)]; // Setup the cell to the new position SetupCell(ref grid[x, y], x, y, newCenterPosition); } } break; } }