Skip to content

Refined rotation at wall edge(Apil) #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions inc/GamePlayState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ namespace tetris
int generate_shape_id();
void calc_rotated_pts();
void rotate();
void calc_extreme_x_pos();

int grid[10][20];
float x_rotated_pts[4], y_rotated_pts[4];
float x_largest, x_smallest;
private:
GameDataRef _data;

Expand Down
3 changes: 1 addition & 2 deletions inc/tetromino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace tetris
int _shape_id;

public:
float x_largest, x_smallest;

GameDataRef _data;
Cell cells[4];
Tetromino(){}
Expand Down Expand Up @@ -70,6 +70,5 @@ namespace tetris
float get_rotated_x(Cell);
float get_rotated_y(Cell);

void calc_extreme_x_pos();
};
}
131 changes: 86 additions & 45 deletions src/GamePlayState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,76 +266,117 @@ namespace tetris{
}
}

void GamePlayState::calc_extreme_x_pos()
{
x_smallest = x_rotated_pts[0];
x_largest = x_rotated_pts[0];
for(int i=1; i<4; i++)
{
if(x_largest < x_rotated_pts[i]) x_largest = x_rotated_pts[i];
if(x_smallest > x_rotated_pts[i]) x_smallest = x_rotated_pts[i];
}
}

void GamePlayState::rotate()
{
if (block.get_shape_id() != 6)
{
int X_BOARD_END = X_BOARD + 10 * (CELL_SIZE+1);
int Y_BOARD_END = Y_BOARD + 20 * (CELL_SIZE+1);
calc_rotated_pts();
bool rotation_touching_other = false;
for (int i = 0; i < 4; i++) {
int x_index = static_cast<int>((x_rotated_pts[i] - X_BOARD) / (CELL_SIZE + 1));
int y_index = static_cast<int>((y_rotated_pts[i] - Y_BOARD) / (CELL_SIZE + 1));
if (grid[x_index][y_index] != 7) {
rotation_touching_other = true;
bool is_inside_grid = true;

for (int i=0; i<4; i++)
{
if (!(((x_rotated_pts[i] >= X_BOARD) && x_rotated_pts[i] < X_BOARD_END) && ((y_rotated_pts[i] >= Y_BOARD) && (y_rotated_pts[i] < Y_BOARD_END))))
{
is_inside_grid = false;
break;
}
}
if (!rotation_touching_other) {
block.calc_extreme_x_pos();
if ((block.x_smallest >= X_BOARD) && (block.x_largest <= X_BOARD + (CELL_SIZE + 1) * 9)) {
for (int i = 0; i < 4; i++) {

if(is_inside_grid)
{
std::cout << "Inside " << std::endl;
bool is_touching_other = false;
for (int i=0; i<4; i++)
{
int x_index = static_cast<int>((x_rotated_pts[i] - X_BOARD) / (CELL_SIZE+1));
int y_index = static_cast<int>((y_rotated_pts[i] - Y_BOARD) / (CELL_SIZE+1));
if (grid[x_index][y_index]!=7)
{
is_touching_other = true;
break;
}
}
if (!is_touching_other)
{
for (int i = 0; i < 4; i++)
{
block.cells[i].set_position(x_rotated_pts[i], y_rotated_pts[i]);
}
}
else
}
else
{
std::cout << "Outside " << std::endl;
calc_extreme_x_pos();

if (x_smallest < X_BOARD)
{
if (block.x_smallest < X_BOARD)
std::cout << " Left out" << std::endl;
float diff = X_BOARD - x_smallest;
for(int i=0; i<4; i++)
{
float diff = X_BOARD - block.x_smallest;
for (int i = 0; i < 4; i++)
x_rotated_pts[i] += diff;
}
bool shift_touching_other = false;
for (int i=0; i<4; i++)
{
int x_index = static_cast<int>((x_rotated_pts[i] - X_BOARD) / (CELL_SIZE+1));
int y_index = static_cast<int>((y_rotated_pts[i] - Y_BOARD) / (CELL_SIZE+1));
if (grid[x_index][y_index]!=7)
{
x_rotated_pts[i] += diff;
shift_touching_other = true;
break;
}
bool shift_touching_other = false;
}
if(!shift_touching_other)
{
for (int i = 0; i < 4; i++)
{
int x_index = static_cast<int>((x_rotated_pts[i] - X_BOARD) / (CELL_SIZE + 1));
int y_index = static_cast<int>((y_rotated_pts[i] - Y_BOARD) / (CELL_SIZE + 1));
if (grid[x_index][y_index] != 7) {
shift_touching_other = true;
break;
}
block.cells[i].set_position(x_rotated_pts[i], y_rotated_pts[i]);
}
if (!shift_touching_other)
}
}
if (x_largest >= X_BOARD_END)
{
float diff = x_largest - X_BOARD_END + (CELL_SIZE+1);
for(int i=0; i<4; i++)
{
x_rotated_pts[i] -= diff;
}
bool shift_touching_other = false;
for (int i=0; i<4; i++)
{
int x_index = static_cast<int>((x_rotated_pts[i] - X_BOARD) / (CELL_SIZE+1));
int y_index = static_cast<int>((y_rotated_pts[i] - Y_BOARD) / (CELL_SIZE+1));
if (grid[x_index][y_index]!=7)
{
for (int i = 0; i < 4; i++)
{
block.cells[i].set_position(x_rotated_pts[i], y_rotated_pts[i]);
}
shift_touching_other = true;
break;
}
}
if (block.x_largest > X_BOARD + (CELL_SIZE + 1) * 9) {
float diff = block.x_largest - (X_BOARD + (CELL_SIZE + 1) * 9);
for (int i = 0; i < 4; i++) {
x_rotated_pts[i] -= diff;
}
bool shift_touching_other = false;
for (int i = 0; i < 4; i++) {
int x_index = static_cast<int>((x_rotated_pts[i] - X_BOARD) / (CELL_SIZE + 1));
int y_index = static_cast<int>((y_rotated_pts[i] - Y_BOARD) / (CELL_SIZE + 1));
if (grid[x_index][y_index] != 7) {
shift_touching_other = true;
break;
}
}
if (!shift_touching_other) {
for (int i = 0; i < 4; i++) {
block.cells[i].set_position(x_rotated_pts[i], y_rotated_pts[i]);
}
if(!shift_touching_other)
{
for (int i = 0; i < 4; i++)
{
block.cells[i].set_position(x_rotated_pts[i], y_rotated_pts[i]);
}
}
}
}

}
}
}
11 changes: 0 additions & 11 deletions src/tetromino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,4 @@ Tetromino::Tetromino(GameDataRef& data, int shape_id)
float y_rotated = y_ref - c.get_x() + x_ref;
return y_rotated;
}

void Tetromino::calc_extreme_x_pos()
{
x_largest = cells[0].get_x();
x_smallest = cells[0].get_x();
for (int i=1; i<4; i++)
{
if (x_largest < cells[i].get_x()) x_largest = cells[i].get_x();
if (x_smallest > cells[i].get_x()) x_smallest = cells[i].get_x();
}
}
}