TETRIS: use new sdlpp library
This commit is contained in:
+98
-85
@@ -4,47 +4,50 @@
|
||||
#include "scenes.hpp"
|
||||
#include <thread>
|
||||
|
||||
bool validPos(SDLPP::Scene &scene, std::shared_ptr<TetrisPiece> piece) {
|
||||
bool validPos( SDLPP::Scene &scene, std::shared_ptr< TetrisPiece > piece ) {
|
||||
for ( auto &x : piece->getObjects() ) {
|
||||
auto collisions = scene.getCollisions( *x, { BRICK_ID, FLOOR_ID, BORDER_LEFT_ID, BORDER_RIGHT_ID } );
|
||||
auto collisions = scene.getCollisions(
|
||||
*x, { BRICK_ID, FLOOR_ID, BORDER_LEFT_ID, BORDER_RIGHT_ID } );
|
||||
if ( collisions.size() > 1 )
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void updateShadow(SDLPP::Scene &scene) {
|
||||
if(!g_cur_object) {
|
||||
void updateShadow( SDLPP::Scene &scene ) {
|
||||
if ( !g_cur_object ) {
|
||||
g_cur_shadow->destroy();
|
||||
g_cur_shadow.reset();
|
||||
return;
|
||||
}
|
||||
if(!g_cur_shadow)
|
||||
if ( !g_cur_shadow )
|
||||
return;
|
||||
g_cur_shadow->setPos(g_cur_object->getPos());
|
||||
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() ) {
|
||||
for ( auto &x : g_cur_shadow->getObjects() ) {
|
||||
auto block_pos = x->getPos();
|
||||
if(BOTTOM_BORDER - block_pos.second < shadow_drop)
|
||||
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 } );
|
||||
g_shadow_colider->setPos( block_pos.first, TOP_BORDER );
|
||||
auto collisions =
|
||||
scene.getCollisions( *g_shadow_colider, { BRICK_ID } );
|
||||
auto curY = block_pos.second;
|
||||
for(auto &col : collisions) {
|
||||
for ( auto &col : collisions ) {
|
||||
// if collision with g_cur_object, ignore
|
||||
if(std::find(invalid_objects.begin(), invalid_objects.end(), col) != invalid_objects.end())
|
||||
if ( std::find( invalid_objects.begin(), invalid_objects.end(),
|
||||
col ) != invalid_objects.end() )
|
||||
continue;
|
||||
auto possible_drop = col->getPos().second - curY;
|
||||
if(possible_drop < shadow_drop && possible_drop >= 0)
|
||||
if ( possible_drop < shadow_drop && possible_drop >= 0 )
|
||||
shadow_drop = possible_drop;
|
||||
}
|
||||
}
|
||||
// we want the shadow to rest on top of the nearest floor
|
||||
shadow_drop -= BLOCK_SIZE;
|
||||
auto shadow_pos = g_cur_shadow->getPos();
|
||||
g_cur_shadow->setPos(shadow_pos.first, shadow_pos.second + shadow_drop);
|
||||
g_cur_shadow->setPos( shadow_pos.first, shadow_pos.second + shadow_drop );
|
||||
}
|
||||
|
||||
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
|
||||
@@ -61,18 +64,18 @@ void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
|
||||
if ( g_ticks_till_fall > 0 ) {
|
||||
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);
|
||||
if(!validPos(*scene, g_cur_object)) {
|
||||
g_cur_object->movePiece(movement.first * -BLOCK_SIZE, 0);
|
||||
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;
|
||||
}
|
||||
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);
|
||||
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;
|
||||
@@ -80,7 +83,7 @@ void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
|
||||
return;
|
||||
}
|
||||
g_ticks_till_fall = TICKS_TILL_FALL;
|
||||
g_cur_object->movePiece(0, BLOCK_SIZE);
|
||||
g_cur_object->movePiece( 0, BLOCK_SIZE );
|
||||
check_floor:
|
||||
bool fell = false;
|
||||
for ( auto &x : g_cur_object->getObjects() ) {
|
||||
@@ -92,18 +95,18 @@ check_floor:
|
||||
}
|
||||
}
|
||||
if ( fell ) {
|
||||
g_cur_object->movePiece(0, -BLOCK_SIZE);
|
||||
g_cur_object->movePiece( 0, -BLOCK_SIZE );
|
||||
for ( auto &block : g_cur_object->getObjects() ) {
|
||||
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
|
||||
g_game_over_scene->updateSizeAndPosition();
|
||||
g_active_scenes.push_back( g_game_over_scene );
|
||||
g_input_functions.push_back(gameOverSceneInput);
|
||||
g_input_functions.push_back( gameOverSceneInput );
|
||||
break;
|
||||
}
|
||||
}
|
||||
g_cur_object.reset();
|
||||
}
|
||||
updateShadow(*scene);
|
||||
updateShadow( *scene );
|
||||
}
|
||||
|
||||
void quitGame() {
|
||||
@@ -120,35 +123,38 @@ void resetGame() {
|
||||
g_main_scene->resetScene();
|
||||
g_main_scene->setPrevTicks( SDL_GetTicks() );
|
||||
|
||||
g_active_scenes = {g_main_scene};
|
||||
g_input_functions = {mainSceneInput};
|
||||
for(int i = 0; i < 7; i++)
|
||||
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) {
|
||||
int crashFlags( std::shared_ptr< TetrisPiece > piece,
|
||||
std::shared_ptr< TetrisBlock > block, SDLPP::Scene &scene,
|
||||
int left, int right, int bottom ) {
|
||||
int retFlags = 0;
|
||||
auto collisions = scene.getCollisions(*block, {BORDER_LEFT_ID, BORDER_RIGHT_ID, FLOOR_ID});
|
||||
for(auto &col : collisions) {
|
||||
switch(col->getId()) {
|
||||
case BORDER_LEFT_ID:
|
||||
retFlags |= left;
|
||||
break;
|
||||
case BORDER_RIGHT_ID:
|
||||
retFlags |= right;
|
||||
break;
|
||||
case FLOOR_ID:
|
||||
retFlags |= bottom;
|
||||
default:
|
||||
break;
|
||||
auto collisions = scene.getCollisions(
|
||||
*block, { BORDER_LEFT_ID, BORDER_RIGHT_ID, FLOOR_ID } );
|
||||
for ( auto &col : collisions ) {
|
||||
switch ( col->getId() ) {
|
||||
case BORDER_LEFT_ID:
|
||||
retFlags |= left;
|
||||
break;
|
||||
case BORDER_RIGHT_ID:
|
||||
retFlags |= right;
|
||||
break;
|
||||
case FLOOR_ID:
|
||||
retFlags |= bottom;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
collisions = scene.getCollisions(*block, {BRICK_ID});
|
||||
if(collisions.size() > 1) {
|
||||
for(auto &col : collisions) {
|
||||
if(piece->isLeft(*col))
|
||||
collisions = scene.getCollisions( *block, { BRICK_ID } );
|
||||
if ( collisions.size() > 1 ) {
|
||||
for ( auto &col : collisions ) {
|
||||
if ( piece->isLeft( *col ) )
|
||||
retFlags |= left;
|
||||
else if(piece->isRight(*col))
|
||||
else if ( piece->isRight( *col ) )
|
||||
retFlags |= right;
|
||||
else
|
||||
retFlags |= bottom;
|
||||
@@ -157,7 +163,8 @@ int crashFlags( std::shared_ptr<TetrisPiece> piece, std::shared_ptr<TetrisBlock>
|
||||
return retFlags;
|
||||
}
|
||||
|
||||
bool checkRotation( std::shared_ptr<TetrisPiece> piece, SDLPP::Scene &scene ) {
|
||||
bool checkRotation( std::shared_ptr< TetrisPiece > piece,
|
||||
SDLPP::Scene &scene ) {
|
||||
int crash = 0;
|
||||
int cur_left = 0x01;
|
||||
int cur_right = 0x02;
|
||||
@@ -167,31 +174,33 @@ bool checkRotation( std::shared_ptr<TetrisPiece> piece, SDLPP::Scene &scene ) {
|
||||
int counter = 0;
|
||||
do {
|
||||
counter++;
|
||||
if(counter > 5) {
|
||||
if ( counter > 5 ) {
|
||||
piece->revert();
|
||||
return false;
|
||||
}
|
||||
crash = 0;
|
||||
for(auto &block : piece->getObjects()) {
|
||||
crash |= crashFlags(piece, block, scene, cur_left, cur_right, bottom);
|
||||
for ( auto &block : piece->getObjects() ) {
|
||||
crash |=
|
||||
crashFlags( piece, block, scene, cur_left, cur_right, bottom );
|
||||
}
|
||||
if(crash & bottom || (crash & cur_left && crash & cur_right) ||
|
||||
(crash & cur_left && crash & was_right) || (crash & cur_right && crash & was_left)) {
|
||||
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;
|
||||
}
|
||||
crash &= ~(was_left | was_right);
|
||||
if(crash & cur_left) {
|
||||
piece->movePiece(BLOCK_SIZE, 0);
|
||||
crash &= ~( was_left | was_right );
|
||||
if ( crash & cur_left ) {
|
||||
piece->movePiece( BLOCK_SIZE, 0 );
|
||||
crash &= ~cur_left;
|
||||
crash |= was_left;
|
||||
}
|
||||
if(crash & cur_right) {
|
||||
piece->movePiece(-BLOCK_SIZE, 0);
|
||||
if ( crash & cur_right ) {
|
||||
piece->movePiece( -BLOCK_SIZE, 0 );
|
||||
crash &= ~cur_right;
|
||||
crash |= was_right;
|
||||
}
|
||||
} while(crash);
|
||||
} while ( crash );
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -200,10 +209,11 @@ 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 ) {
|
||||
auto ret = std::make_shared< TetrisBlock >( x, y, BLOCK_SIZE, BLOCK_SIZE,
|
||||
renderer, color, true, index, scene, g_bag );
|
||||
auto ret =
|
||||
std::make_shared< TetrisBlock >( x, y, BLOCK_SIZE, BLOCK_SIZE, renderer,
|
||||
color, true, index, scene, g_bag );
|
||||
ret->setOutlineColor( outline );
|
||||
ret->addCollision( SDLPP::Rect( 0.1, 0.1, 0.8, 0.8 ) );
|
||||
ret->addCollision( SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) );
|
||||
ret->setId( BRICK_ID );
|
||||
ret->centerX();
|
||||
scene->addObject( ret );
|
||||
@@ -381,38 +391,41 @@ tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||
}
|
||||
|
||||
void updateColors() {
|
||||
for(auto &x : g_main_scene->getObjects({BRICK_ID, SHADOW_ID})) {
|
||||
x->specialAction(PIECE_ACTION_UPDATE_COLOR);
|
||||
for ( auto &x : g_main_scene->getObjects( { BRICK_ID, SHADOW_ID } ) ) {
|
||||
x->specialAction( PIECE_ACTION_UPDATE_COLOR );
|
||||
}
|
||||
for(auto &x : g_main_scene->getObjects({BARRIER_ID})) {
|
||||
x->setColor(colors["barrier"]);
|
||||
for ( auto &x : g_main_scene->getObjects( { BARRIER_ID } ) ) {
|
||||
x->setColor( colors["barrier"] );
|
||||
}
|
||||
for(auto &x : g_main_scene->getObjects({BACKGROUND_ID})) {
|
||||
x->setColor(colors["background"]);
|
||||
for ( auto &x : g_main_scene->getObjects( { BACKGROUND_ID } ) ) {
|
||||
x->setColor( colors["background"] );
|
||||
}
|
||||
for(auto &x : g_main_scene->getObjects({SHADOW_ID})) {
|
||||
x->setColor(colors["shadow"]);
|
||||
for ( auto &x : g_main_scene->getObjects( { SHADOW_ID } ) ) {
|
||||
x->setColor( colors["shadow"] );
|
||||
}
|
||||
for(auto &x : g_main_scene->getObjects({LINE_ID})) {
|
||||
x->setColor(colors["line"]);
|
||||
for ( auto &x : g_main_scene->getObjects( { LINE_ID } ) ) {
|
||||
x->setColor( colors["line"] );
|
||||
}
|
||||
for(auto &x : g_main_scene->getObjects({TEXT_ID})) {
|
||||
std::dynamic_pointer_cast<SDLPP::TextRenderer>(x)->setTextColor(*g_font, colors["text"], colors["text_out"], 5);
|
||||
for ( auto &x : g_main_scene->getObjects( { TEXT_ID } ) ) {
|
||||
std::dynamic_pointer_cast< SDLPP::TextRenderer >( x )->setTextColor(
|
||||
*g_font, colors["text"], colors["text_out"], 5 );
|
||||
}
|
||||
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"]);
|
||||
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"] );
|
||||
}
|
||||
for(auto &x : g_game_over_scene->getObjects({MENU_BACKGROUND_ID})) {
|
||||
x->setColor(colors["menu_background"]);
|
||||
for ( auto &x : g_game_over_scene->getObjects( { MENU_BACKGROUND_ID } ) ) {
|
||||
x->setColor( colors["menu_background"] );
|
||||
}
|
||||
for(auto &x : g_options_scene->getObjects({MENU_BACKGROUND_ID})) {
|
||||
x->setColor(colors["menu_background"]);
|
||||
for ( auto &x : g_options_scene->getObjects( { MENU_BACKGROUND_ID } ) ) {
|
||||
x->setColor( colors["menu_background"] );
|
||||
}
|
||||
for(auto &x : g_options_scene->getObjects({MENU_ITEM_ID})) {
|
||||
std::dynamic_pointer_cast<SDLPP::TextRenderer>(x)->setTextColor(*g_font, colors["text"], colors["text_out"]);
|
||||
for ( auto &x : g_options_scene->getObjects( { MENU_ITEM_ID } ) ) {
|
||||
std::dynamic_pointer_cast< SDLPP::TextRenderer >( x )->setTextColor(
|
||||
*g_font, colors["text"], colors["text_out"] );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user