Tetris pieces should be shared_ptr
This commit is contained in:
parent
5d18c3b923
commit
07fb33c2dd
246
tetris.cpp
246
tetris.cpp
@ -58,6 +58,7 @@ public:
|
|||||||
~TetrisBlock() {
|
~TetrisBlock() {
|
||||||
bag[_index]++;
|
bag[_index]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int _index = 0;
|
int _index = 0;
|
||||||
};
|
};
|
||||||
@ -149,8 +150,8 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
std::vector< std::shared_ptr< SDLPP::RectangleRender > > line_coliders;
|
std::vector< std::shared_ptr< SDLPP::RectangleRender > > line_coliders;
|
||||||
TetrisPiece cur_object;
|
std::shared_ptr< TetrisPiece > cur_object;
|
||||||
TetrisPiece next_object;
|
std::shared_ptr< TetrisPiece > next_object;
|
||||||
|
|
||||||
void doInput( std::shared_ptr< SDLPP::Scene > scene );
|
void doInput( std::shared_ptr< SDLPP::Scene > scene );
|
||||||
void doInputPause();
|
void doInputPause();
|
||||||
@ -163,8 +164,8 @@ createTetrisBlock( double x, double y, const std::string &color,
|
|||||||
const std::string &outline, int index,
|
const std::string &outline, int index,
|
||||||
std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
auto ret = std::make_shared< TetrisBlock >(
|
auto ret = std::make_shared< TetrisBlock >( x, y, BLOCK_SIZE, BLOCK_SIZE,
|
||||||
x, y, BLOCK_SIZE, BLOCK_SIZE, renderer, color, true, index );
|
renderer, color, true, index );
|
||||||
ret->setOutlineColor( outline );
|
ret->setOutlineColor( outline );
|
||||||
ret->addCollision( SDLPP::Rect( 0.1, 0.1, 0.8, 0.8 ) );
|
ret->addCollision( SDLPP::Rect( 0.1, 0.1, 0.8, 0.8 ) );
|
||||||
ret->setId( BRICK_ID );
|
ret->setId( BRICK_ID );
|
||||||
@ -173,164 +174,184 @@ createTetrisBlock( double x, double y, const std::string &color,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisBrick( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
|
tetrisBrick( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
TetrisPiece retPiece{};
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#FF0000";
|
auto color = "#FF0000";
|
||||||
auto outline = "#AA0000";
|
auto outline = "#AA0000";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
||||||
|
outline, TETRIS_BRICK, renderer,
|
||||||
|
scene ),
|
||||||
|
0, 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 ),
|
outline, TETRIS_BRICK, renderer, scene ),
|
||||||
0, 0 );
|
0, 0 );
|
||||||
retPiece.addPiece(
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
createTetrisBlock( 0.5, TOP_BORDER, color, outline, TETRIS_BRICK, renderer, scene ),
|
outline, TETRIS_BRICK, renderer,
|
||||||
|
scene ),
|
||||||
0, 0 );
|
0, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
|
||||||
outline, TETRIS_BRICK, renderer, scene ),
|
|
||||||
0, 0 );
|
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
|
||||||
outline, TETRIS_BRICK, renderer, scene ),
|
|
||||||
0, 0 );
|
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisT( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
|
tetrisT( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
TetrisPiece retPiece{};
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#00FF00";
|
auto color = "#00FF00";
|
||||||
auto outline = "#00AA00";
|
auto outline = "#00AA00";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_T, renderer, scene ),
|
outline, TETRIS_T, renderer, scene ),
|
||||||
-1, 0 );
|
-1, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_T, renderer, scene ),
|
outline, TETRIS_T, renderer, scene ),
|
||||||
0, 0 );
|
0, 0 );
|
||||||
retPiece.addPiece(
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
||||||
createTetrisBlock( 0.5, TOP_BORDER, color, outline, TETRIS_T, renderer, scene ),
|
TETRIS_T, renderer, scene ),
|
||||||
0, -1 );
|
0, -1 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE,
|
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE,
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_T, renderer, scene ),
|
outline, TETRIS_T, renderer, scene ),
|
||||||
1, 0 );
|
1, 0 );
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisLRight( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
|
tetrisLRight( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
TetrisPiece retPiece{};
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#0000FF";
|
auto color = "#0000FF";
|
||||||
auto outline = "#0000AA";
|
auto outline = "#0000AA";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
retPiece->addPiece(
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_L_RIGHT, renderer, scene ),
|
outline, TETRIS_L_RIGHT, renderer, scene ),
|
||||||
-2, 0 );
|
-2, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_L_RIGHT, renderer, scene ),
|
outline, TETRIS_L_RIGHT, renderer,
|
||||||
|
scene ),
|
||||||
-1, 0 );
|
-1, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE,
|
retPiece->addPiece(
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_L_RIGHT, renderer, scene ),
|
outline, TETRIS_L_RIGHT, renderer, scene ),
|
||||||
0, 0 );
|
0, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
||||||
outline, TETRIS_L_RIGHT, renderer, scene ),
|
outline, TETRIS_L_RIGHT, renderer,
|
||||||
|
scene ),
|
||||||
0, -1 );
|
0, -1 );
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisZRight( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
|
tetrisZRight( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
TetrisPiece retPiece{};
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#FF00FF";
|
auto color = "#FF00FF";
|
||||||
auto outline = "#AA00AA";
|
auto outline = "#AA00AA";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
retPiece->addPiece(
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_Z_RIGHT, renderer, scene ),
|
outline, TETRIS_Z_RIGHT, renderer, scene ),
|
||||||
-1, 0 );
|
-1, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_Z_RIGHT, renderer, scene ),
|
outline, TETRIS_Z_RIGHT, renderer,
|
||||||
|
scene ),
|
||||||
0, 0 );
|
0, 0 );
|
||||||
retPiece.addPiece(
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
||||||
createTetrisBlock( 0.5, TOP_BORDER, color, outline, TETRIS_Z_RIGHT, renderer, scene ),
|
TETRIS_Z_RIGHT, renderer, scene ),
|
||||||
0, -1 );
|
0, -1 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
||||||
outline, TETRIS_Z_RIGHT, renderer, scene ),
|
outline, TETRIS_Z_RIGHT, renderer,
|
||||||
|
scene ),
|
||||||
1, -1 );
|
1, -1 );
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisLine( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
|
tetrisLine( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
TetrisPiece retPiece{};
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#FFFF00";
|
auto color = "#FFFF00";
|
||||||
auto outline = "#AAAA00";
|
auto outline = "#AAAA00";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - 2 * BLOCK_SIZE, TOP_BORDER,
|
retPiece->addPiece( createTetrisBlock( 0.5 - 2 * BLOCK_SIZE, TOP_BORDER,
|
||||||
color, outline, TETRIS_LINE, renderer, scene ),
|
color, outline, TETRIS_LINE,
|
||||||
|
renderer, scene ),
|
||||||
-1, 0 );
|
-1, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
||||||
outline, TETRIS_LINE, renderer, scene ),
|
outline, TETRIS_LINE, renderer,
|
||||||
|
scene ),
|
||||||
0, 0 );
|
0, 0 );
|
||||||
retPiece.addPiece(
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
||||||
createTetrisBlock( 0.5, TOP_BORDER, color, outline, TETRIS_LINE, renderer, scene ),
|
TETRIS_LINE, renderer, scene ),
|
||||||
1, 0 );
|
1, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
||||||
outline, TETRIS_LINE, renderer, scene ),
|
outline, TETRIS_LINE, renderer,
|
||||||
|
scene ),
|
||||||
2, 0 );
|
2, 0 );
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisLLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
|
tetrisLLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
TetrisPiece retPiece{};
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#00FFFF";
|
auto color = "#00FFFF";
|
||||||
auto outline = "#00AAAA";
|
auto outline = "#00AAAA";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
||||||
outline, TETRIS_L_LEFT, renderer, scene ),
|
outline, TETRIS_L_LEFT, renderer,
|
||||||
|
scene ),
|
||||||
0, -1 );
|
0, -1 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
retPiece->addPiece(
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_L_LEFT, renderer, scene ),
|
outline, TETRIS_L_LEFT, renderer, scene ),
|
||||||
0, 0 );
|
0, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_L_LEFT, renderer, scene ),
|
outline, TETRIS_L_LEFT, renderer,
|
||||||
|
scene ),
|
||||||
1, 0 );
|
1, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE,
|
retPiece->addPiece(
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_L_LEFT, renderer, scene ),
|
outline, TETRIS_L_LEFT, renderer, scene ),
|
||||||
2, 0 );
|
2, 0 );
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
|
tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
TetrisPiece retPiece{};
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#FFFFFF";
|
auto color = "#FFFFFF";
|
||||||
auto outline = "#AAAAAA";
|
auto outline = "#AAAAAA";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
||||||
outline, TETRIS_Z_LEFT, renderer, scene ),
|
outline, TETRIS_Z_LEFT, renderer,
|
||||||
|
scene ),
|
||||||
-1, 0 );
|
-1, 0 );
|
||||||
retPiece.addPiece(
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
||||||
createTetrisBlock( 0.5, TOP_BORDER, color, outline, TETRIS_Z_LEFT, renderer, scene ),
|
TETRIS_Z_LEFT, renderer, scene ),
|
||||||
0, 0 );
|
0, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_Z_LEFT, renderer, scene ),
|
outline, TETRIS_Z_LEFT, renderer,
|
||||||
|
scene ),
|
||||||
0, 1 );
|
0, 1 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE,
|
retPiece->addPiece(
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_Z_LEFT, renderer, scene ),
|
outline, TETRIS_Z_LEFT, renderer, scene ),
|
||||||
1, 1 );
|
1, 1 );
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector< TetrisPiece ( * )( std::shared_ptr< SDLPP::Renderer >,
|
std::vector< std::shared_ptr< TetrisPiece > ( * )(
|
||||||
std::shared_ptr< SDLPP::Scene > ) >
|
std::shared_ptr< SDLPP::Renderer >, std::shared_ptr< SDLPP::Scene > ) >
|
||||||
tetrisFunctions = {
|
tetrisFunctions = {
|
||||||
tetrisBrick, tetrisT, tetrisLRight, tetrisZRight,
|
tetrisBrick, tetrisT, tetrisLRight, tetrisZRight,
|
||||||
tetrisLine, tetrisLLeft, tetrisZLeft,
|
tetrisLine, tetrisLLeft, tetrisZLeft,
|
||||||
@ -432,7 +453,7 @@ void quitGame() {
|
|||||||
quit = true;
|
quit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkRotation( TetrisPiece &piece, SDLPP::Scene &scene ) {
|
void checkRotation( std::shared_ptr<TetrisPiece> piece, SDLPP::Scene &scene ) {
|
||||||
bool crash = true;
|
bool crash = true;
|
||||||
int left = 0x01;
|
int left = 0x01;
|
||||||
int right = 0x02;
|
int right = 0x02;
|
||||||
@ -441,7 +462,7 @@ void checkRotation( TetrisPiece &piece, SDLPP::Scene &scene ) {
|
|||||||
while ( crash ) {
|
while ( crash ) {
|
||||||
crash = false;
|
crash = false;
|
||||||
flags = 0;
|
flags = 0;
|
||||||
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;
|
||||||
@ -459,9 +480,9 @@ void checkRotation( TetrisPiece &piece, SDLPP::Scene &scene ) {
|
|||||||
}
|
}
|
||||||
if ( crash ) {
|
if ( crash ) {
|
||||||
if ( flags == bottom ) {
|
if ( flags == bottom ) {
|
||||||
piece.revert();
|
piece->revert();
|
||||||
} else {
|
} else {
|
||||||
for ( auto &block : piece.getObjects() ) {
|
for ( auto &block : piece->getObjects() ) {
|
||||||
auto pos = block->getPos();
|
auto pos = block->getPos();
|
||||||
switch ( flags ) {
|
switch ( flags ) {
|
||||||
case 1:
|
case 1:
|
||||||
@ -489,20 +510,22 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
} break;
|
} break;
|
||||||
case SDLK_LEFT:
|
case SDLK_LEFT:
|
||||||
case SDLK_a:
|
case SDLK_a:
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
if(!cur_object)
|
||||||
|
break;
|
||||||
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto pos = x->getPos();
|
auto pos = x->getPos();
|
||||||
// 0.01 because doubles
|
// 0.01 because doubles
|
||||||
if ( pos.first < ( LEFT_BORDER + 0.01 ) )
|
if ( pos.first < ( LEFT_BORDER + 0.01 ) )
|
||||||
crash = true;
|
crash = true;
|
||||||
x->setPos( pos.first - BLOCK_SIZE, pos.second );
|
x->setPos( pos.first - BLOCK_SIZE, pos.second );
|
||||||
}
|
}
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto collisions = scene.getCollisions( *x, { BRICK_ID } );
|
auto collisions = scene.getCollisions( *x, { BRICK_ID } );
|
||||||
if ( collisions.size() > 1 )
|
if ( collisions.size() > 1 )
|
||||||
crash = true;
|
crash = true;
|
||||||
}
|
}
|
||||||
if ( crash ) {
|
if ( crash ) {
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto pos = x->getPos();
|
auto pos = x->getPos();
|
||||||
x->setPos( pos.first + BLOCK_SIZE, pos.second );
|
x->setPos( pos.first + BLOCK_SIZE, pos.second );
|
||||||
}
|
}
|
||||||
@ -510,7 +533,9 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
break;
|
break;
|
||||||
case SDLK_RIGHT:
|
case SDLK_RIGHT:
|
||||||
case SDLK_d:
|
case SDLK_d:
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
if(!cur_object)
|
||||||
|
break;
|
||||||
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto pos = x->getPos();
|
auto pos = x->getPos();
|
||||||
// 0.01 because doubles
|
// 0.01 because doubles
|
||||||
if ( pos.first > RIGHT_BORDER - BLOCK_SIZE - 0.01 ) {
|
if ( pos.first > RIGHT_BORDER - BLOCK_SIZE - 0.01 ) {
|
||||||
@ -518,14 +543,14 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
}
|
}
|
||||||
x->setPos( pos.first + BLOCK_SIZE, pos.second );
|
x->setPos( pos.first + BLOCK_SIZE, pos.second );
|
||||||
}
|
}
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto collisions = scene.getCollisions( *x, { BRICK_ID } );
|
auto collisions = scene.getCollisions( *x, { BRICK_ID } );
|
||||||
if ( collisions.size() > 1 ) {
|
if ( collisions.size() > 1 ) {
|
||||||
crash = true;
|
crash = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( crash ) {
|
if ( crash ) {
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto pos = x->getPos();
|
auto pos = x->getPos();
|
||||||
x->setPos( pos.first - BLOCK_SIZE, pos.second );
|
x->setPos( pos.first - BLOCK_SIZE, pos.second );
|
||||||
}
|
}
|
||||||
@ -533,11 +558,15 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
break;
|
break;
|
||||||
case SDLK_DOWN:
|
case SDLK_DOWN:
|
||||||
case SDLK_s:
|
case SDLK_s:
|
||||||
cur_object.startDescend();
|
if(!cur_object)
|
||||||
|
break;
|
||||||
|
cur_object->startDescend();
|
||||||
break;
|
break;
|
||||||
case SDLK_UP:
|
case SDLK_UP:
|
||||||
case SDLK_w:
|
case SDLK_w:
|
||||||
cur_object.rotate();
|
if(!cur_object)
|
||||||
|
break;
|
||||||
|
cur_object->rotate();
|
||||||
checkRotation( cur_object, scene );
|
checkRotation( cur_object, scene );
|
||||||
break;
|
break;
|
||||||
case SDLK_r:
|
case SDLK_r:
|
||||||
@ -550,7 +579,7 @@ 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();
|
cur_object->stopDescend();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -648,10 +677,10 @@ 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 );
|
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;
|
||||||
if ( ticks_till_fall > 0 ) {
|
if ( ticks_till_fall > 0 ) {
|
||||||
if ( cur_object.isDescending() && ticks_till_descend <= 0 ) {
|
if ( cur_object->isDescending() && ticks_till_descend <= 0 ) {
|
||||||
ticks_till_descend = TICKS_TILL_DESCEND;
|
ticks_till_descend = TICKS_TILL_DESCEND;
|
||||||
goto fall;
|
goto fall;
|
||||||
}
|
}
|
||||||
@ -659,12 +688,12 @@ void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
|
|||||||
}
|
}
|
||||||
ticks_till_fall = TICKS_TILL_FALL;
|
ticks_till_fall = TICKS_TILL_FALL;
|
||||||
fall:
|
fall:
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto pos = x->getPos();
|
auto pos = x->getPos();
|
||||||
x->setPos( pos.first, pos.second + BLOCK_SIZE );
|
x->setPos( pos.first, pos.second + BLOCK_SIZE );
|
||||||
}
|
}
|
||||||
bool fell = false;
|
bool fell = false;
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto collisions = scene->getCollisions( *x, { BRICK_ID } );
|
auto collisions = scene->getCollisions( *x, { BRICK_ID } );
|
||||||
if ( collisions.size() > 1 ) {
|
if ( collisions.size() > 1 ) {
|
||||||
fell = true;
|
fell = true;
|
||||||
@ -676,17 +705,17 @@ fall:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( fell ) {
|
if ( fell ) {
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto pos = x->getPos();
|
auto pos = x->getPos();
|
||||||
x->setPos( pos.first, pos.second - BLOCK_SIZE );
|
x->setPos( pos.first, pos.second - BLOCK_SIZE );
|
||||||
}
|
}
|
||||||
for ( auto &block : cur_object.getObjects() ) {
|
for ( auto &block : cur_object->getObjects() ) {
|
||||||
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
|
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
|
||||||
std::cout << "You lost" << std::endl;
|
std::cout << "You lost" << std::endl;
|
||||||
quitGame();
|
quitGame();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cur_object.clear();
|
cur_object->clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -700,7 +729,7 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
|
|||||||
SDL_framerateDelay( &gFPS );
|
SDL_framerateDelay( &gFPS );
|
||||||
pollEvents( *scene );
|
pollEvents( *scene );
|
||||||
scene->movement();
|
scene->movement();
|
||||||
if ( cur_object.getObjects().size() != 0 ) {
|
if ( cur_object && cur_object->getObjects().size() != 0 ) {
|
||||||
moveThem( scene, SDL_GetTicks() - base );
|
moveThem( scene, SDL_GetTicks() - base );
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -771,13 +800,14 @@ int main() {
|
|||||||
inputThread.detach();
|
inputThread.detach();
|
||||||
next_object = tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
|
next_object = tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
|
||||||
renderer, main_scene );
|
renderer, main_scene );
|
||||||
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.getObjects().size() == 0 && checked_line ) {
|
if ( (!cur_object || cur_object->getObjects().size() == 0) && 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 );
|
||||||
int retries = 0;
|
int retries = 0;
|
||||||
while ( bag[rand_index] < 4 ) {
|
while ( bag[rand_index] < 4 ) {
|
||||||
@ -786,8 +816,9 @@ int main() {
|
|||||||
if ( retries == 7 )
|
if ( retries == 7 )
|
||||||
quitGame();
|
quitGame();
|
||||||
}
|
}
|
||||||
|
next_object.reset();
|
||||||
next_object = tetrisFunctions[rand_index]( renderer, main_scene );
|
next_object = tetrisFunctions[rand_index]( renderer, main_scene );
|
||||||
next_object.setPos( 0.9, 0.5 );
|
next_object->setPos( 0.9, 0.5 );
|
||||||
checked_line = false;
|
checked_line = false;
|
||||||
}
|
}
|
||||||
if ( update_score ) {
|
if ( update_score ) {
|
||||||
@ -808,4 +839,5 @@ int main() {
|
|||||||
frames = 0;
|
frames = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user