game/tetris/functions.cpp

480 lines
18 KiB
C++
Raw Normal View History

#include "functions.hpp"
#include "custom_classes.hpp"
#include "global_vars.hpp"
#include "scenes.hpp"
#include <thread>
2020-11-21 19:58:52 +00:00
bool validPos( SDLPP::Scene &scene, std::shared_ptr< TetrisPiece > piece ) {
for ( auto &x : piece->getObjects() ) {
2020-11-21 19:58:52 +00:00
auto collisions = scene.getCollisions(
*x, { BRICK_ID, FLOOR_ID, BORDER_LEFT_ID, BORDER_RIGHT_ID } );
2020-09-13 12:14:39 +00:00
if ( collisions.size() > 1 )
return false;
}
2020-09-13 12:14:39 +00:00
return true;
}
2020-11-21 19:58:52 +00:00
void updateShadow( SDLPP::Scene &scene ) {
if ( !g_cur_object ) {
g_cur_shadow->destroy();
g_cur_shadow.reset();
return;
}
2020-11-21 19:58:52 +00:00
if ( !g_cur_shadow )
2020-09-13 12:14:39 +00:00
return;
2020-11-21 19:58:52 +00:00
g_cur_shadow->setPos( g_cur_object->getPos() );
double shadow_drop = BOTTOM_BORDER;
auto &invalid_objects = g_cur_object->getObjects();
2020-11-21 19:58:52 +00:00
for ( auto &x : g_cur_shadow->getObjects() ) {
2020-09-13 12:14:39 +00:00
auto block_pos = x->getPos();
if ( BOTTOM_BORDER - block_pos.getY() < shadow_drop )
shadow_drop = BOTTOM_BORDER - block_pos.getY();
2020-09-13 12:14:39 +00:00
// set colider column's position to current block's X position
g_shadow_colider->setPos( block_pos.getX(), TOP_BORDER );
2020-11-21 19:58:52 +00:00
auto collisions =
scene.getCollisions( *g_shadow_colider, { BRICK_ID } );
auto curY = block_pos.getY();
2020-11-21 19:58:52 +00:00
for ( auto &col : collisions ) {
2020-09-13 12:14:39 +00:00
// if collision with g_cur_object, ignore
2020-11-21 19:58:52 +00:00
if ( std::find( invalid_objects.begin(), invalid_objects.end(),
2021-04-29 11:25:55 +00:00
col.second ) != invalid_objects.end() )
continue;
2021-04-29 11:25:55 +00:00
auto possible_drop = col.second->getPos().getY() - curY;
2020-11-21 19:58:52 +00:00
if ( possible_drop < shadow_drop && possible_drop >= 0 )
2020-09-13 12:14:39 +00:00
shadow_drop = possible_drop;
}
}
2020-09-13 12:14:39 +00:00
// we want the shadow to rest on top of the nearest floor
shadow_drop -= BLOCK_SIZE;
2020-09-13 12:14:39 +00:00
auto shadow_pos = g_cur_shadow->getPos();
g_cur_shadow->setPos( shadow_pos.getX(), shadow_pos.getY() + shadow_drop );
}
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
if ( !g_cur_object )
return;
2020-09-13 12:14:39 +00:00
// get movement generated by player
auto movement = g_cur_object->getMovement();
2020-09-13 12:14:39 +00:00
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 ) {
2020-09-13 12:14:39 +00:00
if ( g_cur_object->isMoving() && g_ticks_till_movement <= 0 ) {
g_ticks_till_movement = TICKS_TILL_MOVE;
2020-11-21 19:58:52 +00:00
g_cur_object->movePiece( movement.first * BLOCK_SIZE, 0 );
if ( !validPos( *scene, g_cur_object ) ) {
g_cur_object->movePiece( movement.first * -BLOCK_SIZE, 0 );
return;
} else
goto check_floor;
}
2020-09-13 12:14:39 +00:00
if ( g_cur_object->isDescending() && g_ticks_till_descend <= 0 ) {
g_ticks_till_descend = TICKS_TILL_DESCEND;
2020-11-21 19:58:52 +00:00
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;
2020-11-21 19:58:52 +00:00
g_cur_object->movePiece( 0, BLOCK_SIZE );
check_floor:
bool fell = false;
for ( auto &x : g_cur_object->getObjects() ) {
auto collisions = scene->getCollisions( *x, { BRICK_ID, FLOOR_ID } );
2020-09-13 12:14:39 +00:00
// we get 1 collision every time with ourselves
if ( collisions.size() > 1 ) {
fell = true;
break;
}
}
if ( fell ) {
2020-11-21 19:58:52 +00:00
g_cur_object->movePiece( 0, -BLOCK_SIZE );
for ( auto &block : g_cur_object->getObjects() ) {
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
2021-03-13 17:37:23 +00:00
g_update_scenes.push_back( g_game_over_scene );
g_active_scenes.push_back( g_game_over_scene );
2020-11-21 19:58:52 +00:00
g_input_functions.push_back( gameOverSceneInput );
#ifdef FEATURE
pauseBlocks();
#endif
break;
}
}
g_cur_object.reset();
}
2020-11-21 19:58:52 +00:00
updateShadow( *scene );
}
void quitGame() {
std::cout << "Quitting!" << std::endl;
g_quit = true;
}
void resetGame() {
2021-03-14 16:02:41 +00:00
std::lock_guard< std::mutex > guard( g_render_mutex );
std::lock_guard< std::mutex > guard2( g_movement_mutex );
g_cur_object.reset();
g_checked_line = true;
g_next_object.reset();
g_score = 0;
g_update_score = true;
g_main_scene->resetScene();
g_main_scene->setPrevTicks( SDL_GetTicks() );
2020-11-21 19:58:52 +00:00
g_active_scenes = { g_main_scene };
g_input_functions = { mainSceneInput };
for ( int i = 0; i < 7; i++ )
2020-09-13 12:14:39 +00:00
g_bag[i] = 28;
}
2020-11-21 19:58:52 +00:00
int crashFlags( std::shared_ptr< TetrisPiece > piece,
std::shared_ptr< TetrisBlock > block, SDLPP::Scene &scene,
int left, int right, int bottom ) {
int retFlags = 0;
2020-11-21 19:58:52 +00:00
auto collisions = scene.getCollisions(
*block, { BORDER_LEFT_ID, BORDER_RIGHT_ID, FLOOR_ID } );
for ( auto &col : collisions ) {
2021-04-29 11:25:55 +00:00
switch ( col.second->getId() ) {
2020-11-21 19:58:52 +00:00
case BORDER_LEFT_ID:
retFlags |= left;
break;
case BORDER_RIGHT_ID:
retFlags |= right;
break;
case FLOOR_ID:
retFlags |= bottom;
default:
break;
}
}
2020-11-21 19:58:52 +00:00
collisions = scene.getCollisions( *block, { BRICK_ID } );
if ( collisions.size() > 1 ) {
for ( auto &col : collisions ) {
2021-04-29 11:25:55 +00:00
if ( piece->isLeft( *col.second ) )
retFlags |= left;
2021-04-29 11:25:55 +00:00
else if ( piece->isRight( *col.second ) )
retFlags |= right;
else
retFlags |= bottom;
}
}
return retFlags;
}
2020-11-21 19:58:52 +00:00
bool checkRotation( std::shared_ptr< TetrisPiece > piece,
SDLPP::Scene &scene ) {
int crash = 0;
int cur_left = 0x01;
int cur_right = 0x02;
int was_left = 0x04;
int was_right = 0x08;
int bottom = 0x10;
int counter = 0;
do {
counter++;
2020-11-21 19:58:52 +00:00
if ( counter > 5 ) {
piece->revert();
return false;
}
crash = 0;
2020-11-21 19:58:52 +00:00
for ( auto &block : piece->getObjects() ) {
crash |=
crashFlags( piece, block, scene, cur_left, cur_right, bottom );
}
2020-11-21 19:58:52 +00:00
if ( crash & bottom || ( crash & cur_left && crash & cur_right ) ||
( crash & cur_left && crash & was_right ) ||
( crash & cur_right && crash & was_left ) ) {
piece->revert();
return false;
}
2020-11-21 19:58:52 +00:00
crash &= ~( was_left | was_right );
if ( crash & cur_left ) {
piece->movePiece( BLOCK_SIZE, 0 );
crash &= ~cur_left;
crash |= was_left;
}
2020-11-21 19:58:52 +00:00
if ( crash & cur_right ) {
piece->movePiece( -BLOCK_SIZE, 0 );
crash &= ~cur_right;
crash |= was_right;
}
2020-11-21 19:58:52 +00:00
} while ( crash );
return true;
}
std::shared_ptr< TetrisBlock >
createTetrisBlock( double x, double y, const std::string &color,
const std::string &outline, int index,
std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
2020-11-21 19:58:52 +00:00
auto ret =
std::make_shared< TetrisBlock >( x, y, BLOCK_SIZE, BLOCK_SIZE, renderer,
color, true, index, scene, g_bag );
ret->setOutlineColor( outline );
2020-11-21 19:58:52 +00:00
ret->addCollision( SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) );
ret->setId( BRICK_ID );
2021-04-29 11:25:55 +00:00
ret->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
scene->addObject( ret );
return ret;
}
std::shared_ptr< TetrisPiece >
tetrisBrick( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = colors["piece_brick"];
auto outline = colors["piece_brick_out"];
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
outline, TETRIS_BRICK, renderer,
scene ),
-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 ),
-1, 1 );
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_BRICK, renderer,
scene ),
0, 1 );
retPiece->disableRotation();
return retPiece;
}
std::shared_ptr< TetrisPiece >
tetrisT( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = colors["piece_T"];
auto outline = colors["piece_T_out"];
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_T, renderer, scene ),
-1, 0 );
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_T, renderer, scene ),
0, 0 );
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
TETRIS_T, renderer, scene ),
0, -1 );
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE,
TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_T, renderer, scene ),
1, 0 );
return retPiece;
}
std::shared_ptr< TetrisPiece >
tetrisLRight( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = colors["piece_L_right"];
auto outline = colors["piece_L_right_out"];
retPiece->addPiece(
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_L_RIGHT, renderer, scene ),
-2, 0 );
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_L_RIGHT, renderer,
scene ),
-1, 0 );
retPiece->addPiece(
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_L_RIGHT, renderer, scene ),
0, 0 );
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
outline, TETRIS_L_RIGHT, renderer,
scene ),
0, -1 );
return retPiece;
}
std::shared_ptr< TetrisPiece >
tetrisZRight( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = colors["piece_Z_right"];
auto outline = colors["piece_Z_right_out"];
retPiece->addPiece(
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_Z_RIGHT, renderer, scene ),
-1, 0 );
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_Z_RIGHT, renderer,
scene ),
0, 0 );
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
TETRIS_Z_RIGHT, renderer, scene ),
0, -1 );
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
outline, TETRIS_Z_RIGHT, renderer,
scene ),
1, -1 );
return retPiece;
}
std::shared_ptr< TetrisPiece >
tetrisLine( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = colors["piece_line"];
auto outline = colors["piece_line_out"];
retPiece->addPiece( createTetrisBlock( 0.5 - 2 * BLOCK_SIZE, TOP_BORDER,
color, outline, TETRIS_LINE,
renderer, scene ),
-1, 0 );
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
outline, TETRIS_LINE, renderer,
scene ),
0, 0 );
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
TETRIS_LINE, renderer, scene ),
1, 0 );
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
outline, TETRIS_LINE, renderer,
scene ),
2, 0 );
return retPiece;
}
std::shared_ptr< TetrisPiece >
tetrisLLeft( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = colors["piece_L_left"];
auto outline = colors["piece_L_left_out"];
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
outline, TETRIS_L_LEFT, renderer,
scene ),
0, -1 );
retPiece->addPiece(
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_L_LEFT, renderer, scene ),
0, 0 );
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_L_LEFT, renderer,
scene ),
1, 0 );
retPiece->addPiece(
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_L_LEFT, renderer, scene ),
2, 0 );
return retPiece;
}
std::shared_ptr< TetrisPiece >
tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = colors["piece_Z_left"];
auto outline = colors["piece_Z_left_out"];
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
outline, TETRIS_Z_LEFT, renderer,
scene ),
-1, 0 );
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
TETRIS_Z_LEFT, renderer, scene ),
0, 0 );
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_Z_LEFT, renderer,
scene ),
0, 1 );
retPiece->addPiece(
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_Z_LEFT, renderer, scene ),
1, 1 );
return retPiece;
}
2020-12-18 14:14:08 +00:00
void updateSize() {
// have to resize rectangles for the text to be resized correctly
g_main_scene->updateSizeAndPosition();
g_menu_scene->updateSizeAndPosition();
g_game_over_scene->updateSizeAndPosition();
g_options_scene->updateSizeAndPosition();
2021-01-30 15:48:40 +00:00
std::unordered_set< int > textObjects = { TEXT_ID, MENU_TEXT_ID,
MENU_ITEM_ID };
for ( auto &x : g_main_scene->getObjects( textObjects ) ) {
x->updateSizeAndPosition();
2020-12-18 14:14:08 +00:00
}
2021-01-30 15:48:40 +00:00
g_score_texture->updateSizeAndPosition();
for ( auto &x : g_menu_scene->getObjects( textObjects ) ) {
x->updateSizeAndPosition();
2020-12-18 14:14:08 +00:00
}
2021-01-30 15:48:40 +00:00
for ( auto &x : g_game_over_scene->getObjects( textObjects ) ) {
x->updateSizeAndPosition();
2020-12-18 14:14:08 +00:00
}
2021-01-30 15:48:40 +00:00
for ( auto &x : g_options_scene->getObjects( textObjects ) ) {
x->updateSizeAndPosition();
2020-12-18 14:14:08 +00:00
}
}
2021-01-30 20:35:25 +00:00
void updateBlocks() {
for ( auto &x : g_main_scene->getObjects( { BRICK_ID, SHADOW_ID } ) ) {
x->specialAction( PIECE_ACTION_UPDATE_BLOCK );
}
}
#ifdef FEATURE
void pauseBlocks() {
for ( auto &x : g_main_scene->getObjects( { BRICK_ID, SHADOW_ID } ) ) {
x->pauseAnimation();
}
}
void resumeBlocks() {
for ( auto &x : g_main_scene->getObjects( { BRICK_ID, SHADOW_ID } ) ) {
x->resumeAnimation();
}
}
#endif
2021-01-30 20:35:25 +00:00
void updateColors() {
2020-11-21 19:58:52 +00:00
for ( auto &x : g_main_scene->getObjects( { BRICK_ID, SHADOW_ID } ) ) {
x->specialAction( PIECE_ACTION_UPDATE_COLOR );
}
2020-11-21 19:58:52 +00:00
for ( auto &x : g_main_scene->getObjects( { BARRIER_ID } ) ) {
x->setColor( colors["barrier"] );
}
2020-11-21 19:58:52 +00:00
for ( auto &x : g_main_scene->getObjects( { BACKGROUND_ID } ) ) {
x->setColor( colors["background"] );
}
2020-11-21 19:58:52 +00:00
for ( auto &x : g_main_scene->getObjects( { SHADOW_ID } ) ) {
x->setColor( colors["shadow"] );
}
2020-11-21 19:58:52 +00:00
for ( auto &x : g_main_scene->getObjects( { LINE_ID } ) ) {
x->setColor( colors["line"] );
}
2021-01-30 20:35:25 +00:00
if ( g_font_config != nullptr ) {
g_font_config->setColor( colors["text"] );
g_font_config->setOutlineColor( colors["text_out"] );
for ( auto &x : g_main_scene->getObjects( { TEXT_ID } ) ) {
x->updateSizeAndPosition();
}
}
2020-11-21 19:58:52 +00:00
g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] );
g_game_over_options[g_game_over_select]->setColor(
colors["menu_item_background"] );
g_options_options[g_options_select]->setColor(
colors["menu_item_background"] );
for ( auto &x : g_menu_scene->getObjects( { MENU_BACKGROUND_ID } ) ) {
x->setColor( colors["menu_background"] );
}
2020-11-21 19:58:52 +00:00
for ( auto &x : g_game_over_scene->getObjects( { MENU_BACKGROUND_ID } ) ) {
x->setColor( colors["menu_background"] );
}
2020-11-21 19:58:52 +00:00
for ( auto &x : g_options_scene->getObjects( { MENU_BACKGROUND_ID } ) ) {
x->setColor( colors["menu_background"] );
}
2020-12-18 14:14:08 +00:00
updateSize();
}