Finally fix randomly missing blocks
This commit is contained in:
parent
14943bf005
commit
194981a3d3
46
tetris.cpp
46
tetris.cpp
@ -99,6 +99,8 @@ public:
|
||||
pieces_rel_position.back()[3] = ( y > 0 ) * y;
|
||||
}
|
||||
void rotate() {
|
||||
if(!rotate_allowed)
|
||||
return;
|
||||
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
|
||||
auto &piece = pieces[i];
|
||||
auto &positions = pieces_rel_position[i];
|
||||
@ -140,15 +142,14 @@ public:
|
||||
void setPos( double x, double y ) {
|
||||
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
|
||||
auto &piece = pieces[i];
|
||||
auto pos = piece->getPos();
|
||||
piece->setPos( x + pos.first - default_x,
|
||||
y + pos.second - default_y );
|
||||
auto &positions = pieces_rel_position[i];
|
||||
std::pair<double, double> pos = {x, 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() {
|
||||
pieces.clear();
|
||||
@ -175,6 +176,9 @@ public:
|
||||
block->setPos( pos.first + x, pos.second + y );
|
||||
}
|
||||
}
|
||||
void disableRotation() {
|
||||
rotate_allowed = false;
|
||||
}
|
||||
|
||||
private:
|
||||
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::shared_ptr< TetrisBlock > > pieces;
|
||||
std::vector< std::pair< double, double > > original_pos;
|
||||
double default_x = 0;
|
||||
double default_y = 0;
|
||||
bool descend = false;
|
||||
bool rotate_allowed = true;
|
||||
};
|
||||
|
||||
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,
|
||||
outline, TETRIS_BRICK, renderer,
|
||||
scene ),
|
||||
0, 0 );
|
||||
-1, 0 );
|
||||
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
||||
TETRIS_BRICK, renderer, scene ),
|
||||
0, 0 );
|
||||
retPiece->addPiece(
|
||||
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||
outline, TETRIS_BRICK, renderer, scene ),
|
||||
0, 0 );
|
||||
-1, 1 );
|
||||
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||
outline, TETRIS_BRICK, renderer,
|
||||
scene ),
|
||||
0, 0 );
|
||||
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||
0, 1 );
|
||||
retPiece->disableRotation();
|
||||
return retPiece;
|
||||
}
|
||||
|
||||
@ -264,7 +267,6 @@ tetrisT( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||
TOP_BORDER + BLOCK_SIZE, color,
|
||||
outline, TETRIS_T, renderer, scene ),
|
||||
1, 0 );
|
||||
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||
return retPiece;
|
||||
}
|
||||
|
||||
@ -290,7 +292,6 @@ tetrisLRight( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||
outline, TETRIS_L_RIGHT, renderer,
|
||||
scene ),
|
||||
0, -1 );
|
||||
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||
return retPiece;
|
||||
}
|
||||
|
||||
@ -315,7 +316,6 @@ tetrisZRight( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||
outline, TETRIS_Z_RIGHT, renderer,
|
||||
scene ),
|
||||
1, -1 );
|
||||
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||
return retPiece;
|
||||
}
|
||||
|
||||
@ -340,7 +340,6 @@ tetrisLine( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||
outline, TETRIS_LINE, renderer,
|
||||
scene ),
|
||||
2, 0 );
|
||||
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||
return retPiece;
|
||||
}
|
||||
|
||||
@ -366,7 +365,6 @@ tetrisLLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||
outline, TETRIS_L_LEFT, renderer, scene ),
|
||||
2, 0 );
|
||||
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||
return retPiece;
|
||||
}
|
||||
|
||||
@ -391,7 +389,6 @@ tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||
outline, TETRIS_Z_LEFT, renderer, scene ),
|
||||
1, 1 );
|
||||
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||
return retPiece;
|
||||
}
|
||||
|
||||
@ -460,12 +457,11 @@ void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
|
||||
for ( int i = 0; i < 20; i++ ) {
|
||||
posy -= BLOCK_SIZE;
|
||||
auto colider = std::make_shared< SDLPP::RectangleRender >(
|
||||
LEFT_BORDER, posy, BLOCK_SIZE, BLOCK_SIZE, r );
|
||||
auto colider_colider = SDLPP::Rect( -1, 0.1, -1, 0.8 );
|
||||
colider_colider.setInfinite();
|
||||
colider->addCollision( colider_colider );
|
||||
LEFT_BORDER, posy, RIGHT_BORDER - LEFT_BORDER, BLOCK_SIZE, r );
|
||||
colider->addCollision(SDLPP::Rect( 0.01, 0.1, 0.98, 0.8 ));
|
||||
colider->setId( COLIDER_ID );
|
||||
colider->setStatic();
|
||||
colider->centerX();
|
||||
line_coliders.push_back( colider );
|
||||
scene.addObject( colider );
|
||||
}
|
||||
@ -936,7 +932,7 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
|
||||
if ( elem->getId() != BRICK_ID )
|
||||
continue;
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user