Trying to switch to more object-oriented

This commit is contained in:
2021-03-12 22:33:46 +01:00
parent fbe122a5b9
commit 258ce51cfe
32 changed files with 693 additions and 249 deletions
+26 -24
View File
@@ -32,10 +32,10 @@ TetrisBlock::TetrisBlock( double x, double y, double w, double h,
}
TetrisBlock::TetrisBlock( const TetrisBlock &other )
: TetrisBlock( other.getDoubleRect().first.first,
other.getDoubleRect().first.second,
other.getDoubleRect().second.first,
other.getDoubleRect().second.second, other.getRenderer(),
: TetrisBlock( other.getDoubleRect().first.getX(),
other.getDoubleRect().first.getY(),
other.getDoubleRect().second.getX(),
other.getDoubleRect().second.getY(), other.getRenderer(),
other.getColor(), true, other._index, other._scene,
other.pieces_bag ) {}
@@ -63,9 +63,9 @@ std::shared_ptr< TetrisBlock > TetrisBlock::copyInScene() {
bool TetrisBlock::isSamePos( const SDLPP::RenderObject &other ) const {
auto mypos = getPos();
auto otherpos = other.getPos();
auto diff1 = mypos.first - otherpos.first;
auto diff1 = mypos.getX() - otherpos.getX();
diff1 = ( diff1 < 0 ) * ( -1 ) * diff1 + ( diff1 > 0 ) * diff1;
auto diff2 = mypos.second - otherpos.second;
auto diff2 = mypos.getY() - otherpos.getY();
diff2 = ( diff2 < 0 ) * ( -1 ) * diff2 + ( diff2 > 0 ) * diff2;
return diff1 < 0.0001 && diff2 < 0.0001;
}
@@ -152,23 +152,23 @@ void TetrisPiece::rotate() {
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 piece_position = piece->getPos();
original_pos[i] = piece_position;
piece_position += { positions[0] * BLOCK_SIZE -
positions[1] * BLOCK_SIZE,
positions[2] * BLOCK_SIZE -
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 );
piece_position += { positions[1] * BLOCK_SIZE -
positions[0] * BLOCK_SIZE,
positions[3] * BLOCK_SIZE -
positions[2] * BLOCK_SIZE };
piece->setPos( piece_position );
}
}
@@ -176,7 +176,7 @@ void TetrisPiece::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 );
piece->setPos( original_pos[i] );
auto top = positions[1];
auto bottom = positions[0];
positions[1] = positions[3];
@@ -207,14 +207,16 @@ void TetrisPiece::setPos( const std::pair< double, double > &pos ) {
setPos( pos.first, pos.second );
}
std::pair< double, double > TetrisPiece::getPos() {
void TetrisPiece::setPos( const SDLPP::Vec2D<double> &vec ) {
setPos( vec.getX(), vec.getY() );
}
SDLPP::Vec2D<double> TetrisPiece::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;
pos += { relpositions[0] * BLOCK_SIZE - relpositions[1] * BLOCK_SIZE,
relpositions[2] * BLOCK_SIZE - relpositions[3] * BLOCK_SIZE };
return pos;
}
@@ -258,7 +260,7 @@ bool TetrisPiece::isRight( const SDLPP::RenderObject &block ) const {
void TetrisPiece::movePiece( double x, double y ) {
for ( auto &block : getObjects() ) {
auto pos = block->getPos();
block->setPos( pos.first + x, pos.second + y );
block->setPos( pos + SDLPP::Vec2D<double>( x, y ) );
}
}