game/tetris/custom_classes.hpp

302 lines
9.7 KiB
C++
Raw Normal View History

#ifndef TETRIS_CUSTOM_CLASSES_H
#define TETRIS_CUSTOM_CLASSES_H
2020-11-21 19:58:52 +00:00
#include "../sdlpp/sdlpp.hpp"
#include "config.hpp"
class TetrisBlock : public SDLPP::RectangleRender {
public:
TetrisBlock() = delete;
TetrisBlock( double x, double y, double w, double h,
const std::shared_ptr< SDLPP::Renderer > &r,
2020-11-21 19:58:52 +00:00
const std::string &img_or_color, bool is_polygon, int index,
std::shared_ptr< SDLPP::Scene > scene,
std::vector< int > &bag )
: RectangleRender( x, y, w, h, r, img_or_color, is_polygon ),
pieces_bag( bag ) {
_index = index;
pieces_bag[_index]--;
_scene = scene;
2020-09-29 16:22:26 +00:00
setColors();
2020-11-21 19:58:52 +00:00
if ( g_show_3d )
setTexture( "block.png" );
}
TetrisBlock( const TetrisBlock &other )
: TetrisBlock( other.getDoubleRect().first.first,
other.getDoubleRect().first.second,
other.getDoubleRect().second.first,
other.getDoubleRect().second.second,
other.getRenderer(), other.getColor(), true,
other._index, other._scene, other.pieces_bag ) {}
~TetrisBlock() {
2020-11-21 19:58:52 +00:00
if ( _index != PIECE_SHADOW )
pieces_bag[_index]++;
}
2020-11-21 19:58:52 +00:00
virtual std::shared_ptr< RenderObject > copySelf() override {
auto ret = std::make_shared< TetrisBlock >( *this );
copyTo(ret);
return ret;
}
virtual void copyTo(std::shared_ptr<RenderObject> other) override {
RectangleRender::copyTo(other);
}
2020-11-21 19:58:52 +00:00
std::shared_ptr< TetrisBlock > copyInScene() {
auto ret = std::shared_ptr< TetrisBlock >( new TetrisBlock( *this ) );
_scene->addObject( ret );
return ret;
}
2020-11-21 19:58:52 +00:00
bool isSamePos( const SDLPP::RenderObject &other ) const {
auto mypos = getPos();
auto otherpos = other.getPos();
auto diff1 = mypos.first - otherpos.first;
2020-11-21 19:58:52 +00:00
diff1 = ( diff1 < 0 ) * ( -1 ) * diff1 + ( diff1 > 0 ) * diff1;
auto diff2 = mypos.second - otherpos.second;
2020-11-21 19:58:52 +00:00
diff2 = ( diff2 < 0 ) * ( -1 ) * diff2 + ( diff2 > 0 ) * diff2;
return diff1 < 0.0001 && diff2 < 0.0001;
}
virtual void specialAction( int code ) override {
2020-11-21 19:58:52 +00:00
switch ( code ) {
case PIECE_ACTION_UPDATE_COLOR: {
setColors();
if ( g_show_3d )
setTexture( "block.png" );
else
unsetTexture();
}
default:
break;
}
}
2020-09-29 16:22:26 +00:00
void turnIntoShadow() {
2020-11-21 19:58:52 +00:00
setId( SHADOW_ID );
// shadows don't consume pieces from bag
pieces_bag[_index]++;
2020-09-29 16:22:26 +00:00
_index = PIECE_SHADOW;
setColors();
}
private:
std::string getPieceName() {
2020-11-21 19:58:52 +00:00
switch ( _index ) {
case PIECE_BRICK:
return "piece_brick";
case PIECE_T:
return "piece_T";
case PIECE_L_RIGHT:
return "piece_L_right";
case PIECE_Z_RIGHT:
return "piece_Z_right";
case PIECE_LINE:
return "piece_line";
case PIECE_L_LEFT:
return "piece_L_left";
case PIECE_Z_LEFT:
return "piece_Z_left";
case PIECE_SHADOW:
return "shadow";
default:
break;
}
return "";
}
2020-09-29 16:22:26 +00:00
void setColors() {
auto piece_name = getPieceName();
2020-11-21 19:58:52 +00:00
setColor( colors[piece_name] );
setOutlineColor( colors[piece_name + "_out"] );
2020-09-29 16:22:26 +00:00
}
int _index = 0;
2020-11-21 19:58:52 +00:00
std::shared_ptr< SDLPP::Scene > _scene;
std::vector< int > &pieces_bag;
};
class TetrisPiece {
public:
TetrisPiece() {
original_pos.reserve( 4 );
}
2020-11-21 19:58:52 +00:00
void addPiece( std::shared_ptr< TetrisBlock > piece, int x, int y ) {
pieces.push_back( piece );
pieces_rel_position.push_back( { 0, 0, 0, 0 } );
// done this way for SPEEEEEEED
// left
pieces_rel_position.back()[0] = ( x < 0 ) * ( -1 ) * x;
// right
pieces_rel_position.back()[1] = ( x > 0 ) * x;
// top
pieces_rel_position.back()[2] = ( y < 0 ) * ( -1 ) * y;
// bottom
pieces_rel_position.back()[3] = ( y > 0 ) * y;
}
void rotate() {
2020-11-21 19:58:52 +00:00
if ( !rotate_allowed )
return;
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
auto &piece = pieces[i];
auto &positions = pieces_rel_position[i];
auto position = piece->getPos();
original_pos[i] = position;
position.first += positions[0] * BLOCK_SIZE;
position.first -= positions[1] * BLOCK_SIZE;
position.second += positions[2] * BLOCK_SIZE;
position.second -= positions[3] * BLOCK_SIZE;
auto bottom = positions[3];
auto top = positions[2];
positions[3] = positions[1];
positions[2] = positions[0];
positions[1] = top;
positions[0] = bottom;
position.first -= positions[0] * BLOCK_SIZE;
position.first += positions[1] * BLOCK_SIZE;
position.second -= positions[2] * BLOCK_SIZE;
position.second += positions[3] * BLOCK_SIZE;
piece->setPos( position.first, position.second );
}
}
void revert() {
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
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() {
return pieces;
}
void setPos( double x, double y ) {
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
auto &piece = pieces[i];
auto &positions = pieces_rel_position[i];
2020-11-21 19:58:52 +00:00
std::pair< double, double > pos = { x, y };
pos.first -= positions[0] * BLOCK_SIZE;
pos.first += positions[1] * BLOCK_SIZE;
pos.second -= positions[2] * BLOCK_SIZE;
pos.second += positions[3] * BLOCK_SIZE;
piece->setPos( pos.first, pos.second );
}
}
2020-11-21 19:58:52 +00:00
void setPos( const std::pair< double, double > &pos ) {
setPos( pos.first, pos.second );
}
2020-11-21 19:58:52 +00:00
std::pair< double, double > getPos() {
auto &piece = pieces[0];
auto &relpositions = pieces_rel_position[0];
auto pos = piece->getPos();
pos.first += relpositions[0] * BLOCK_SIZE;
pos.first -= relpositions[1] * BLOCK_SIZE;
pos.second += relpositions[2] * BLOCK_SIZE;
pos.second -= relpositions[3] * BLOCK_SIZE;
return pos;
}
void clear() {
pieces.clear();
pieces_rel_position.clear();
}
void startDescend() {
descend = true;
}
void stopDescend() {
descend = false;
}
void startMovement() {
userMovement += 1;
}
void stopMovement() {
userMovement -= 1;
}
bool isDescending() {
return descend;
}
bool isMoving() {
return userMovement > 0;
}
2020-11-21 19:58:52 +00:00
bool isLeft( const SDLPP::RenderObject &block ) const {
return isPosition( block, 0 );
}
2020-11-21 19:58:52 +00:00
bool isRight( const SDLPP::RenderObject &block ) const {
return isPosition( block, 1 );
}
2020-11-21 19:58:52 +00:00
void movePiece( double x, double y ) {
for ( auto &block : getObjects() ) {
auto pos = block->getPos();
block->setPos( pos.first + x, pos.second + y );
}
}
void disableRotation() {
rotate_allowed = false;
}
void turnIntoShadow() {
2020-11-21 19:58:52 +00:00
for ( auto &block : getObjects() )
2020-09-29 16:22:26 +00:00
block->turnIntoShadow();
2020-11-21 19:58:52 +00:00
setHidden( !g_show_shadow );
}
2020-11-21 19:58:52 +00:00
std::shared_ptr< TetrisPiece > copySelf() {
auto ret = std::make_shared< TetrisPiece >();
for ( int i = 0; i < 4; i++ ) {
auto block = pieces[i]->copyInScene();
block->centerX();
2020-11-21 19:58:52 +00:00
ret->addBlockInPos( block, pieces_rel_position[i] );
}
2020-11-21 19:58:52 +00:00
if ( !rotate_allowed )
ret->disableRotation();
2020-11-21 19:58:52 +00:00
ret->setHidden( _hidden );
return ret;
}
void destroy() {
2020-11-21 19:58:52 +00:00
for ( auto &x : getObjects() ) {
x->destroy();
}
}
2020-11-21 19:58:52 +00:00
void addMovement( int x, int y ) {
movement.first += x;
movement.second += y;
}
2020-11-21 19:58:52 +00:00
std::pair< int, int > getMovement() const {
return movement;
}
2020-11-21 19:58:52 +00:00
void setHidden( bool hidden ) {
_hidden = hidden;
2020-11-21 19:58:52 +00:00
for ( auto &x : getObjects() ) {
x->setHidden( hidden );
}
}
bool getHidden() {
return _hidden;
}
private:
2020-11-21 19:58:52 +00:00
bool isPosition( const SDLPP::RenderObject &block, int pos ) const {
for ( int i = 0; i < 4; i++ ) {
if ( pieces[i]->isSamePos( block ) ) {
return pieces_rel_position[i][pos] != 0;
}
}
return false;
}
2020-11-21 19:58:52 +00:00
void resetBlock( int index, std::shared_ptr< TetrisBlock > piece ) {
piece->setPos( pieces[index]->getPos() );
pieces[index] = piece;
}
2020-11-21 19:58:52 +00:00
void addBlockInPos( std::shared_ptr< TetrisBlock > piece,
const std::vector< int > &relpos ) {
pieces.push_back( piece );
2020-11-21 19:58:52 +00:00
pieces_rel_position.push_back( relpos );
}
std::vector< std::vector< int > > pieces_rel_position;
std::vector< std::shared_ptr< TetrisBlock > > pieces;
std::vector< std::pair< double, double > > original_pos;
bool descend = false;
int userMovement = 0;
bool rotate_allowed = true;
2020-11-21 19:58:52 +00:00
std::pair< int, int > movement = { 0, 0 };
bool _hidden = false;
};
#endif