TETRIS: Code cleanup

This commit is contained in:
2020-09-13 14:14:39 +02:00
parent 896b5d131f
commit 96c54454f7
6 changed files with 66 additions and 45 deletions
+33 -26
View File
@@ -5,15 +5,12 @@
#include <thread>
bool validPos(SDLPP::Scene &scene, std::shared_ptr<TetrisPiece> piece) {
auto ret = true;
for ( auto &x : piece->getObjects() ) {
auto collisions = scene.getCollisions( *x, { BRICK_ID, FLOOR_ID, BORDER_LEFT_ID, BORDER_RIGHT_ID } );
if ( collisions.size() > 1 ) {
ret = false;
break;
}
if ( collisions.size() > 1 )
return false;
}
return ret;
return true;
}
void updateShadow(SDLPP::Scene &scene) {
@@ -22,47 +19,46 @@ void updateShadow(SDLPP::Scene &scene) {
g_cur_shadow.reset();
return;
}
if(!g_cur_shadow)
return;
g_cur_shadow->setPos(g_cur_object->getPos());
double shadow_drop = BOTTOM_BORDER;
auto &invalid_objects = g_cur_object->getObjects();
for( auto &x : g_cur_shadow->getObjects() ) {
if(BOTTOM_BORDER - x->getPos().second < shadow_drop)
shadow_drop = BOTTOM_BORDER - x->getPos().second;
g_shadow_colider->setPos(x->getPos().first, TOP_BORDER);
auto block_pos = x->getPos();
if(BOTTOM_BORDER - block_pos.second < shadow_drop)
shadow_drop = BOTTOM_BORDER - block_pos.second;
// set colider column's position to current block's X position
g_shadow_colider->setPos(block_pos.first, TOP_BORDER);
auto collisions = scene.getCollisions( *g_shadow_colider, { BRICK_ID } );
auto curY = x->getPos().second;
auto curY = block_pos.second;
for(auto &col : collisions) {
auto colY = col->getPos().second;
// if collision with g_cur_object, ignore
if(std::find(invalid_objects.begin(), invalid_objects.end(), col) != invalid_objects.end())
continue;
auto possible_drop = colY - curY;
auto possible_drop = col->getPos().second - curY;
if(possible_drop < shadow_drop && possible_drop >= 0)
shadow_drop = colY - curY;
shadow_drop = possible_drop;
}
}
// we want the shadow to rest on top of the nearest floor
shadow_drop -= BLOCK_SIZE;
g_cur_shadow->setPos(g_cur_shadow->getPos().first, g_cur_shadow->getPos().second + shadow_drop);
auto shadow_pos = g_cur_shadow->getPos();
g_cur_shadow->setPos(shadow_pos.first, shadow_pos.second + shadow_drop);
}
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
if ( !g_cur_object )
return;
// get movement generated by player
auto movement = g_cur_object->getMovement();
g_ticks_till_fall -= ticks;
if ( g_cur_object->isDescending() )
g_ticks_till_descend -= ticks;
if ( g_cur_object->isMoving() )
g_ticks_till_movement -= ticks;
if ( g_ticks_till_fall > 0 ) {
if ( g_cur_object->isDescending() && g_ticks_till_descend <= 0 ) {
g_ticks_till_descend = TICKS_TILL_DESCEND;
g_cur_object->movePiece(0, movement.second * BLOCK_SIZE);
if(!validPos(*scene, g_cur_object)) {
g_cur_object->movePiece(0, movement.second * -BLOCK_SIZE);
return;
} else
goto check_floor;
}
if ( g_cur_object->isMoving() && g_ticks_till_movement <= 0 ) {
g_ticks_till_movement = TICKS_TILL_MOVE;
g_cur_object->movePiece(movement.first * BLOCK_SIZE, 0);
@@ -72,6 +68,15 @@ void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
} else
goto check_floor;
}
if ( g_cur_object->isDescending() && g_ticks_till_descend <= 0 ) {
g_ticks_till_descend = TICKS_TILL_DESCEND;
g_cur_object->movePiece(0, movement.second * BLOCK_SIZE);
if(!validPos(*scene, g_cur_object)) {
g_cur_object->movePiece(0, movement.second * -BLOCK_SIZE);
return;
} else
goto check_floor;
}
return;
}
g_ticks_till_fall = TICKS_TILL_FALL;
@@ -80,6 +85,7 @@ check_floor:
bool fell = false;
for ( auto &x : g_cur_object->getObjects() ) {
auto collisions = scene->getCollisions( *x, { BRICK_ID, FLOOR_ID } );
// we get 1 collision every time with ourselves
if ( collisions.size() > 1 ) {
fell = true;
break;
@@ -89,7 +95,6 @@ check_floor:
g_cur_object->movePiece(0, -BLOCK_SIZE);
for ( auto &block : g_cur_object->getObjects() ) {
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
g_pause = PAUSE_GAME_OVER;
g_game_over_scene->updateSizeAndPosition();
g_active_scenes.push_back( g_game_over_scene );
g_input_functions.push_back(gameOverSceneInput);
@@ -107,7 +112,6 @@ void quitGame() {
}
void resetGame() {
g_pause = 0;
g_cur_object.reset();
g_checked_line = true;
g_next_object.reset();
@@ -116,12 +120,15 @@ void resetGame() {
g_main_scene->resetScene();
g_main_scene->setPrevTicks( SDL_GetTicks() );
// TODO maybe move to main thread
g_next_object = g_tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
g_main_scene->getRendererShared(), g_main_scene );
g_next_object->setPos( 0.9, 0.5 );
g_active_scenes.push_back( g_main_scene );
g_active_scenes = {g_main_scene};
g_input_functions = {mainSceneInput};
for(int i = 0; i < 7; i++)
g_bag[i] = 28;
}
int crashFlags( std::shared_ptr<TetrisPiece> piece, std::shared_ptr<TetrisBlock> block, SDLPP::Scene &scene, int left, int right, int bottom) {
@@ -136,7 +143,7 @@ int crashFlags( std::shared_ptr<TetrisPiece> piece, std::shared_ptr<TetrisBlock>
retFlags |= right;
break;
case FLOOR_ID:
retFlags |= right;
retFlags |= bottom;
default:
break;
}