one should be in the correct folder when initializing the git project...

This commit is contained in:
Rene Kievits
2024-10-14 04:28:43 +02:00
commit 730df043f7
20 changed files with 799 additions and 0 deletions

112
src/Tetromino.cpp Normal file
View File

@@ -0,0 +1,112 @@
#include "Tetromino.hpp"
#include "GameBoard.hpp"
Tetromino::Tetromino(TetrominoShape shape) : x(0), y(0) {
initializeShape(shape);
currentRotationState = 1;
color.a = 255;
color.r = 0;
color.g = 0;
color.b = 0;
random_device dev;
mt19937 rng(dev( ));
uniform_int_distribution<mt19937::result_type> dist6(0, 5);
switch (dist6(rng)) {
case 0:
color.r = 255;
break;
case 1:
color.g = 255;
break;
case 2:
color.b = 255;
break;
case 3:
color.r = 255;
color.g = 255;
break;
case 4:
color.g = 255;
color.b = 255;
break;
case 5:
color.b = 255;
color.r = 255;
break;
default:
break;
}
}
void Tetromino::initializeShape(TetrominoShape s) {
switch (s) {
case TetrominoShape::I:
shape = { {1, 1, 1, 1} };
break;
case TetrominoShape::O:
shape = { {1, 1}, {1, 1} };
break;
case TetrominoShape::S:
shape = { {0, 1, 1}, {1, 1, 0} };
break;
case TetrominoShape::Z:
shape = { {1, 1, 0}, {0, 1, 1} };
break;
case TetrominoShape::J:
shape = { {1, 0, 0}, {1, 1, 1} };
break;
case TetrominoShape::L:
shape = { {0, 0, 1}, {1, 1, 1} };
break;
default:
shape = { {0} };
break;
}
}
void Tetromino::rotate(GameBoard& gameBoard) {
vector<vector<int>> rotated(shape[0].size( ), vector<int>(shape.size( )));
for (int row = 0; row < shape.size( ); ++row)
for (int col = 0; col < shape[0].size( ); ++col)
rotated[col][shape.size( ) - 1 - row] = shape[row][col];
if (gameBoard.isValidPosition(rotated, x, y)) {
shape = rotated;
currentRotationState = (currentRotationState + 1) % 4;
} else {
int dx = x - 1;
while (!gameBoard.isValidPosition(rotated, dx, y)) {
//If the shape before rotation wont have a valid position then rotation is not possible and we abort
if (!gameBoard.isValidPosition(shape, dx, y)) {
shape = rotated;
return;
}
dx--;
}
x = dx;
shape = rotated;
currentRotationState = (currentRotationState + 1) % 4;
}
}
void Tetromino::move(int dx, int dy) {
x += dx;
y += dy;
}
double Tetromino::getRotationAngle( ) const {
return currentRotationState * 90.0;
}
const vector<vector<int>>& Tetromino::getShape( ) const { return shape; }
int Tetromino::getX( ) const { return x; }
int Tetromino::getY( ) const { return y; }
void Tetromino::setTexture(const TetrominoShape shape) { this->textureShape = shape; }
const TetrominoShape Tetromino::getTexture( ) const { return textureShape; }
SDL_Color Tetromino::getColor( ) const { return color; }