Fixed random 45 degree turns

This commit is contained in:
zvon 2020-08-24 17:44:34 +02:00
parent e6bf3def99
commit 29b750cf9a

View File

@ -116,7 +116,15 @@ public:
} }
void revert() { void revert() {
for ( unsigned long i = 0; i < pieces.size(); i++ ) { for ( unsigned long i = 0; i < pieces.size(); i++ ) {
pieces[i]->setPos( original_pos[i].first, original_pos[i].second ); auto &piece = pieces[i];
auto &positions = pieces_rel_position[i];
piece->setPos( original_pos[i].first, original_pos[i].second );
auto top = positions[1];
auto bottom = positions[0];
positions[1] = positions[3];
positions[0] = positions[2];
positions[2] = top;
positions[3] = bottom;
} }
} }
std::vector< std::shared_ptr< TetrisBlock > > &getObjects() { std::vector< std::shared_ptr< TetrisBlock > > &getObjects() {
@ -495,26 +503,26 @@ void checkRotation( std::shared_ptr<TetrisPiece> piece, SDLPP::Scene &scene ) {
for ( auto &block : piece->getObjects() ) { for ( auto &block : piece->getObjects() ) {
auto pos = block->getPos(); auto pos = block->getPos();
if ( pos.first < LEFT_BORDER - 0.01 ) { if ( pos.first < LEFT_BORDER - 0.01 ) {
flags = left; flags |= left;
crash = true; crash = true;
break; break;
} else if ( pos.first > RIGHT_BORDER - BLOCK_SIZE + 0.01 ) { } else if ( pos.first > RIGHT_BORDER - BLOCK_SIZE + 0.01 ) {
crash = true; crash = true;
flags = right; flags |= right;
break; break;
} else if ( pos.second >= BOTTOM_BORDER ) { } else if ( pos.second >= BOTTOM_BORDER ) {
crash = true; crash = true;
flags = bottom; flags |= bottom;
break; break;
} }
} }
if ( crash ) { if ( crash ) {
if ( flags == bottom ) { if ( flags & bottom || (flags & left && flags & right) ) {
piece->revert(); piece->revert();
} else { } else {
if( flags == left ) if( flags & left )
piece->movePiece(BLOCK_SIZE, 0); piece->movePiece(BLOCK_SIZE, 0);
else if( flags == right ) else if( flags & right )
piece->movePiece(-BLOCK_SIZE, 0); piece->movePiece(-BLOCK_SIZE, 0);
} }
} }
@ -561,9 +569,9 @@ void checkRotation( std::shared_ptr<TetrisPiece> piece, SDLPP::Scene &scene ) {
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) { void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
bool crash = false; bool crash = false;
std::lock_guard< std::mutex > guard( movement_mutex );
switch ( key ) { switch ( key ) {
case SDLK_ESCAPE: { case SDLK_ESCAPE:
{
pause = true; pause = true;
pause_scene->updateSizeAndPosition(); pause_scene->updateSizeAndPosition();
std::thread pauseThread( doInputPause ); std::thread pauseThread( doInputPause );
@ -623,7 +631,8 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
void handleKeyUp( SDL_Keycode key ) { void handleKeyUp( SDL_Keycode key ) {
if ( key == SDLK_DOWN || key == SDLK_s ) { if ( key == SDLK_DOWN || key == SDLK_s ) {
cur_object->stopDescend(); if(cur_object)
cur_object->stopDescend();
} }
} }
@ -719,7 +728,6 @@ void pollEventsPause() {
} }
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) { void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
std::lock_guard< std::mutex > guard( movement_mutex );
ticks_till_fall -= ticks; ticks_till_fall -= ticks;
if ( cur_object->isDescending() ) if ( cur_object->isDescending() )
ticks_till_descend -= ticks; ticks_till_descend -= ticks;
@ -753,7 +761,7 @@ fall:
quitGame(); quitGame();
} }
} }
cur_object->clear(); cur_object.reset();
} }
} }
@ -765,13 +773,12 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
while ( !quit && !pause ) { while ( !quit && !pause ) {
base = SDL_GetTicks(); base = SDL_GetTicks();
SDL_framerateDelay( &gFPS ); SDL_framerateDelay( &gFPS );
std::lock_guard< std::mutex > guard( movement_mutex );
pollEvents( *scene ); pollEvents( *scene );
scene->movement(); if ( cur_object ) {
if ( cur_object && cur_object->getObjects().size() != 0 ) {
moveThem( scene, SDL_GetTicks() - base ); moveThem( scene, SDL_GetTicks() - base );
continue; continue;
} }
std::lock_guard< std::mutex > guard( movement_mutex );
for ( auto &colider : line_coliders ) { for ( auto &colider : line_coliders ) {
auto collisions = scene->getCollisions( *colider, { BRICK_ID } ); auto collisions = scene->getCollisions( *colider, { BRICK_ID } );
while ( collisions.size() == 10 ) { while ( collisions.size() == 10 ) {
@ -841,9 +848,8 @@ int main() {
next_object->setPos( 0.9, 0.5 ); next_object->setPos( 0.9, 0.5 );
while ( !quit ) { while ( !quit ) {
SDL_framerateDelay( &gFPS ); SDL_framerateDelay( &gFPS );
if ( (!cur_object || cur_object->getObjects().size() == 0) && checked_line ) { if ( !cur_object && checked_line ) {
std::lock_guard< std::mutex > guard( movement_mutex ); std::lock_guard< std::mutex > guard( movement_mutex );
cur_object.reset();
cur_object = next_object; cur_object = next_object;
cur_object->setPos( 0.5, TOP_BORDER ); cur_object->setPos( 0.5, TOP_BORDER );
auto rand_index = std::rand() / ( ( RAND_MAX + 1u ) / 7 ); auto rand_index = std::rand() / ( ( RAND_MAX + 1u ) / 7 );