Finally fix randomly missing blocks

This commit is contained in:
zvon 2020-08-24 20:37:20 +02:00
parent 14943bf005
commit 194981a3d3

View File

@ -99,6 +99,8 @@ public:
pieces_rel_position.back()[3] = ( y > 0 ) * y; pieces_rel_position.back()[3] = ( y > 0 ) * y;
} }
void rotate() { void rotate() {
if(!rotate_allowed)
return;
for ( unsigned long i = 0; i < pieces.size(); i++ ) { for ( unsigned long i = 0; i < pieces.size(); i++ ) {
auto &piece = pieces[i]; auto &piece = pieces[i];
auto &positions = pieces_rel_position[i]; auto &positions = pieces_rel_position[i];
@ -140,15 +142,14 @@ public:
void setPos( double x, double y ) { void setPos( double x, double y ) {
for ( unsigned long i = 0; i < pieces.size(); i++ ) { for ( unsigned long i = 0; i < pieces.size(); i++ ) {
auto &piece = pieces[i]; auto &piece = pieces[i];
auto pos = piece->getPos(); auto &positions = pieces_rel_position[i];
piece->setPos( x + pos.first - default_x, std::pair<double, double> pos = {x, y};
y + pos.second - default_y ); pos.first -= positions[0] * BLOCK_SIZE;
pos.first += positions[1] * BLOCK_SIZE;
pos.second -= positions[2] * BLOCK_SIZE;
pos.second += positions[3] * BLOCK_SIZE;
piece->setPos( pos.first, pos.second );
} }
setDefPos( x, y );
}
void setDefPos( double x, double y ) {
default_x = x;
default_y = y;
} }
void clear() { void clear() {
pieces.clear(); pieces.clear();
@ -175,6 +176,9 @@ public:
block->setPos( pos.first + x, pos.second + y ); block->setPos( pos.first + x, pos.second + y );
} }
} }
void disableRotation() {
rotate_allowed = false;
}
private: private:
bool isPosition(const SDLPP::RenderObject &block, int pos) const { bool isPosition(const SDLPP::RenderObject &block, int pos) const {
@ -188,9 +192,8 @@ private:
std::vector< std::vector< int > > pieces_rel_position; std::vector< std::vector< int > > pieces_rel_position;
std::vector< std::shared_ptr< TetrisBlock > > pieces; std::vector< std::shared_ptr< TetrisBlock > > pieces;
std::vector< std::pair< double, double > > original_pos; std::vector< std::pair< double, double > > original_pos;
double default_x = 0;
double default_y = 0;
bool descend = false; bool descend = false;
bool rotate_allowed = true;
}; };
std::vector< std::shared_ptr< SDLPP::RectangleRender > > line_coliders; std::vector< std::shared_ptr< SDLPP::RectangleRender > > line_coliders;
@ -228,19 +231,19 @@ tetrisBrick( std::shared_ptr< SDLPP::Renderer > renderer,
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color, retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
outline, TETRIS_BRICK, renderer, outline, TETRIS_BRICK, renderer,
scene ), scene ),
0, 0 ); -1, 0 );
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline, retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
TETRIS_BRICK, renderer, scene ), TETRIS_BRICK, renderer, scene ),
0, 0 ); 0, 0 );
retPiece->addPiece( retPiece->addPiece(
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color, createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_BRICK, renderer, scene ), outline, TETRIS_BRICK, renderer, scene ),
0, 0 ); -1, 1 );
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color, retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_BRICK, renderer, outline, TETRIS_BRICK, renderer,
scene ), scene ),
0, 0 ); 0, 1 );
retPiece->setDefPos( 0.5, TOP_BORDER ); retPiece->disableRotation();
return retPiece; return retPiece;
} }
@ -264,7 +267,6 @@ tetrisT( std::shared_ptr< SDLPP::Renderer > renderer,
TOP_BORDER + BLOCK_SIZE, color, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_T, renderer, scene ), outline, TETRIS_T, renderer, scene ),
1, 0 ); 1, 0 );
retPiece->setDefPos( 0.5, TOP_BORDER );
return retPiece; return retPiece;
} }
@ -290,7 +292,6 @@ tetrisLRight( std::shared_ptr< SDLPP::Renderer > renderer,
outline, TETRIS_L_RIGHT, renderer, outline, TETRIS_L_RIGHT, renderer,
scene ), scene ),
0, -1 ); 0, -1 );
retPiece->setDefPos( 0.5, TOP_BORDER );
return retPiece; return retPiece;
} }
@ -315,7 +316,6 @@ tetrisZRight( std::shared_ptr< SDLPP::Renderer > renderer,
outline, TETRIS_Z_RIGHT, renderer, outline, TETRIS_Z_RIGHT, renderer,
scene ), scene ),
1, -1 ); 1, -1 );
retPiece->setDefPos( 0.5, TOP_BORDER );
return retPiece; return retPiece;
} }
@ -340,7 +340,6 @@ tetrisLine( std::shared_ptr< SDLPP::Renderer > renderer,
outline, TETRIS_LINE, renderer, outline, TETRIS_LINE, renderer,
scene ), scene ),
2, 0 ); 2, 0 );
retPiece->setDefPos( 0.5, TOP_BORDER );
return retPiece; return retPiece;
} }
@ -366,7 +365,6 @@ tetrisLLeft( std::shared_ptr< SDLPP::Renderer > renderer,
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color, createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_L_LEFT, renderer, scene ), outline, TETRIS_L_LEFT, renderer, scene ),
2, 0 ); 2, 0 );
retPiece->setDefPos( 0.5, TOP_BORDER );
return retPiece; return retPiece;
} }
@ -391,7 +389,6 @@ tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color, createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_Z_LEFT, renderer, scene ), outline, TETRIS_Z_LEFT, renderer, scene ),
1, 1 ); 1, 1 );
retPiece->setDefPos( 0.5, TOP_BORDER );
return retPiece; return retPiece;
} }
@ -460,12 +457,11 @@ void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
for ( int i = 0; i < 20; i++ ) { for ( int i = 0; i < 20; i++ ) {
posy -= BLOCK_SIZE; posy -= BLOCK_SIZE;
auto colider = std::make_shared< SDLPP::RectangleRender >( auto colider = std::make_shared< SDLPP::RectangleRender >(
LEFT_BORDER, posy, BLOCK_SIZE, BLOCK_SIZE, r ); LEFT_BORDER, posy, RIGHT_BORDER - LEFT_BORDER, BLOCK_SIZE, r );
auto colider_colider = SDLPP::Rect( -1, 0.1, -1, 0.8 ); colider->addCollision(SDLPP::Rect( 0.01, 0.1, 0.98, 0.8 ));
colider_colider.setInfinite();
colider->addCollision( colider_colider );
colider->setId( COLIDER_ID ); colider->setId( COLIDER_ID );
colider->setStatic(); colider->setStatic();
colider->centerX();
line_coliders.push_back( colider ); line_coliders.push_back( colider );
scene.addObject( colider ); scene.addObject( colider );
} }
@ -936,7 +932,7 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
if ( elem->getId() != BRICK_ID ) if ( elem->getId() != BRICK_ID )
continue; continue;
auto pos = elem->getPos(); auto pos = elem->getPos();
if ( pos.second < colider_y ) { if ( pos.second < colider_y && pos.first >= LEFT_BORDER && pos.first <= RIGHT_BORDER ) {
elem->setPos( pos.first, pos.second + BLOCK_SIZE ); elem->setPos( pos.first, pos.second + BLOCK_SIZE );
} }
} }