diff --git a/.gitlab/ci/code-formatting-ci.yml b/.gitlab/ci/code-formatting-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..976dead251fd98b7fb196e1abaee4708799f107f --- /dev/null +++ b/.gitlab/ci/code-formatting-ci.yml @@ -0,0 +1,9 @@ +code-formatting: + stage: formatting + image: alpine:3.21 + script: + - apk add --no-cache clang-extra-tools findutils bash + - find ./game/src ./liblvl/src ./editor/src \( -name "*.cpp" -o -name "*.hpp" -o -name "*.tcc" \) -print0 | xargs -0 clang-format --Werror -n --verbose + rules: + - if: '$CI_COMMIT_BRANCH == "dev" || $CI_COMMIT_BRANCH == "main"' + - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "dev" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main")' diff --git a/editor/src/CenterGrid.cpp b/editor/src/CenterGrid.cpp index 441dc11e0dbcb95436d1df347e9aaac3d19be86a..f2e43a519420cf24ffdd9b4111be39b8a05f7e31 100644 --- a/editor/src/CenterGrid.cpp +++ b/editor/src/CenterGrid.cpp @@ -1,12 +1,10 @@ #include "CenterGrid.hpp" -namespace editor +namespace editor { -CenterGrid::CenterGrid(QWidget* parent) - : QWidget(parent), - scene(new QGraphicsScene(this)), - view(new QGraphicsView(scene, this)) +CenterGrid::CenterGrid(QWidget *parent) + : QWidget(parent), scene(new QGraphicsScene(this)), view(new QGraphicsView(scene, this)) { // Configure view view->setRenderHint(QPainter::Antialiasing); @@ -22,12 +20,14 @@ CenterGrid::CenterGrid(QWidget* parent) m_tileIDs.resize(rows); m_objectIDs.resize(rows); m_mobIDs.resize(rows); - for (int row = 0; row < rows; row++) { + for (int row = 0; row < rows; row++) + { gridItems[row].resize(cols); m_tileIDs[row].resize(cols); m_objectIDs[row].resize(cols); m_mobIDs[row].resize(cols); - for (int col = 0; col < cols; col++) { + for (int col = 0; col < cols; col++) + { m_tileIDs[row][col] = m_currentSpriteID; m_objectIDs[row][col] = m_currentSpriteID; m_mobIDs[row][col] = m_currentSpriteID; @@ -36,11 +36,11 @@ CenterGrid::CenterGrid(QWidget* parent) // Create tiles const qreal tileSize = 20.0; - for (int row = 0; row < rows; row++) + for (int row = 0; row < rows; row++) { - for (int col = 0; col < cols; col++) + for (int col = 0; col < cols; col++) { - GridItem* tile = new GridItem(row, col, tileSize); + GridItem *tile = new GridItem(row, col, tileSize); tile->setPos(col * tileSize, row * tileSize); scene->addItem(tile); gridItems[row][col] = tile; @@ -52,33 +52,32 @@ CenterGrid::CenterGrid(QWidget* parent) scene->setSceneRect(0, 0, 64 * tileSize, 64 * tileSize); // Set Layout - QVBoxLayout* layout = new QVBoxLayout(this); + QVBoxLayout *layout = new QVBoxLayout(this); layout->setContentsMargins(0, 0, 0, 0); layout->addWidget(view); setLayout(layout); - //Resize after initialization is done to fit window size - QTimer::singleShot(0, this, [this]() - { - view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); - }); + // Resize after initialization is done to fit window size + QTimer::singleShot(0, this, [this]() { view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); }); } -void CenterGrid::resizeEvent(QResizeEvent* event) +void CenterGrid::resizeEvent(QResizeEvent *event) { QWidget::resizeEvent(event); view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); } -void CenterGrid::setCurrentSprite(const QPixmap pixmap, const int id) +void CenterGrid::setCurrentSprite(const QPixmap pixmap, const int id) { - if (std::find(m_tileIDList.begin(), m_tileIDList.end(), id) != m_tileIDList.end()) + if (std::find(m_tileIDList.begin(), m_tileIDList.end(), id) != m_tileIDList.end()) { m_spriteType = 0; - } else if (std::find(m_mobIDList.begin(), m_mobIDList.end(), id) != m_mobIDList.end()) + } + else if (std::find(m_mobIDList.begin(), m_mobIDList.end(), id) != m_mobIDList.end()) { m_spriteType = 1; - } else + } + else { m_spriteType = 2; } @@ -86,204 +85,214 @@ void CenterGrid::setCurrentSprite(const QPixmap pixmap, const int id) m_currentSpriteID = id; } -void CenterGrid::onTileClicked(int row, int col) +void CenterGrid::onTileClicked(int row, int col) { - GridItem* tile = getTile(row,col); + GridItem *tile = getTile(row, col); tile->setPixmap(m_currentSprite); - if (m_spriteType == 0) { + if (m_spriteType == 0) + { m_tileIDs[row][col] = m_currentSpriteID; - } else if (m_spriteType == 1) + } + else if (m_spriteType == 1) { - switch(m_currentSpriteID) + switch (m_currentSpriteID) { - case 22: case 23: case 24: case 25: m_mobIDs[row][col] = 0; break; - case 86: case 87: case 88: case 89: m_mobIDs[row][col] = 1; break; - case 146: case 147: case 148: case 149: m_mobIDs[row][col] = 2; break; - } - } else + case 22: + case 23: + case 24: + case 25: + m_mobIDs[row][col] = 0; + break; + case 86: + case 87: + case 88: + case 89: + m_mobIDs[row][col] = 1; + break; + case 146: + case 147: + case 148: + case 149: + m_mobIDs[row][col] = 2; + break; + } + } + else { m_objectIDs[row][col] = m_currentSpriteID; } - } -GridItem* CenterGrid::getTile(int row, int col) const +GridItem *CenterGrid::getTile(int row, int col) const { - if (row >= 0 && row < gridItems.size() && col >= 0 && col < gridItems[row].size()) + if (row >= 0 && row < gridItems.size() && col >= 0 && col < gridItems[row].size()) { return gridItems[row][col]; } return nullptr; } -QVector<QVector<int>> CenterGrid::getTileIDs() +QVector<QVector<int>> CenterGrid::getTileIDs() { return m_tileIDs; } -QVector<QVector<int>> CenterGrid::getMobIDs() +QVector<QVector<int>> CenterGrid::getMobIDs() { return m_mobIDs; } -QVector<QVector<int>> CenterGrid::getObjectIDs() +QVector<QVector<int>> CenterGrid::getObjectIDs() { return m_objectIDs; } void CenterGrid::drawPreset(int index) { - QList<QGraphicsItem*> items = scene->items(); - for (QGraphicsItem* item : scene->items()) - { - GridItem* gridItem = dynamic_cast<GridItem*>(item); - if (gridItem) + QList<QGraphicsItem *> items = scene->items(); + for (QGraphicsItem *item : scene->items()) + { + GridItem *gridItem = dynamic_cast<GridItem *>(item); + if (gridItem) { gridItem->reset(); } } - switch(index) + switch (index) { - case(0): + case (0): { + for (QGraphicsItem *item : items) { - for (QGraphicsItem* item : items) + GridItem *gridItem = dynamic_cast<GridItem *>(item); + if (gridItem) { - GridItem* gridItem = dynamic_cast<GridItem*>(item); - if (gridItem) + if (gridItem->row() == 0 && gridItem->col() == 15 || gridItem->row() == 0 && gridItem->col() == 16 || + gridItem->row() == 31 && gridItem->col() == 15 || gridItem->row() == 31 && gridItem->col() == 16 || + gridItem->row() == 15 && gridItem->col() == 0 || gridItem->row() == 16 && gridItem->col() == 0 || + gridItem->row() == 15 && gridItem->col() == 31 || gridItem->row() == 16 && gridItem->col() == 31) { - if (gridItem->row() == 0 && gridItem->col() == 15 || gridItem->row() == 0 && gridItem->col() == 16 || - gridItem->row() == 31 && gridItem->col() == 15 || gridItem->row() == 31 && gridItem->col() == 16 || - gridItem->row() == 15 && gridItem->col() == 0 || gridItem->row() == 16 && gridItem->col() == 0 || - gridItem->row() == 15 && gridItem->col() == 31 || gridItem->row() == 16 && gridItem->col() == 31) - { - gridItem->setColor(Qt::red); - } - else if (gridItem->row() < 32 && gridItem->col() < 32) - { - gridItem->setColor(Qt::white); - } - else - { - gridItem->setColor(Qt::black); - } + gridItem->setColor(Qt::red); + } + else if (gridItem->row() < 32 && gridItem->col() < 32) + { + gridItem->setColor(Qt::white); + } + else + { + gridItem->setColor(Qt::black); } } - break; } - case(1): + break; + } + case (1): { + for (QGraphicsItem *item : items) { - for (QGraphicsItem* item : items) + GridItem *gridItem = dynamic_cast<GridItem *>(item); + if (gridItem) { - GridItem* gridItem = dynamic_cast<GridItem*>(item); - if (gridItem) + if (gridItem->row() == 0 && gridItem->col() == 31 || gridItem->row() == 0 && gridItem->col() == 32 || + gridItem->row() == 31 && gridItem->col() == 31 || gridItem->row() == 31 && gridItem->col() == 32 || + gridItem->row() == 15 && gridItem->col() == 0 || gridItem->row() == 16 && gridItem->col() == 0 || + gridItem->row() == 15 && gridItem->col() == 63 || gridItem->row() == 16 && gridItem->col() == 63) { - if (gridItem->row() == 0 && gridItem->col() == 31 || gridItem->row() == 0 && gridItem->col() == 32 || - gridItem->row() == 31 && gridItem->col() == 31 || gridItem->row() == 31 && gridItem->col() == 32 || - gridItem->row() == 15 && gridItem->col() == 0 || gridItem->row() == 16 && gridItem->col() == 0 || - gridItem->row() == 15 && gridItem->col() == 63 || gridItem->row() == 16 && gridItem->col() == 63) - { - gridItem->setColor(Qt::red); - } - else if (gridItem->row() < 32) - { - gridItem->setColor(Qt::white); - } - else - { - gridItem->setColor(Qt::black); - } + gridItem->setColor(Qt::red); + } + else if (gridItem->row() < 32) + { + gridItem->setColor(Qt::white); + } + else + { + gridItem->setColor(Qt::black); } } - break; } - case(2): + break; + } + case (2): { + for (QGraphicsItem *item : items) { - for (QGraphicsItem* item : items) + GridItem *gridItem = dynamic_cast<GridItem *>(item); + if (gridItem) { - GridItem* gridItem = dynamic_cast<GridItem*>(item); - if (gridItem) + if (gridItem->row() == 0 && gridItem->col() == 15 || gridItem->row() == 0 && gridItem->col() == 16 || + gridItem->row() == 63 && gridItem->col() == 15 || gridItem->row() == 63 && gridItem->col() == 16 || + gridItem->row() == 31 && gridItem->col() == 0 || gridItem->row() == 32 && gridItem->col() == 0 || + gridItem->row() == 31 && gridItem->col() == 31 || gridItem->row() == 32 && gridItem->col() == 31) + { + gridItem->setColor(Qt::red); + } + else if (gridItem->col() < 32) { - if (gridItem->row() == 0 && gridItem->col() == 15 || gridItem->row() == 0 && gridItem->col() == 16 || - gridItem->row() == 63 && gridItem->col() == 15 || gridItem->row() == 63 && gridItem->col() == 16 || - gridItem->row() == 31 && gridItem->col() == 0 || gridItem->row() == 32 && gridItem->col() == 0 || - gridItem->row() == 31 && gridItem->col() == 31 || gridItem->row() == 32 && gridItem->col() == 31) - { - gridItem->setColor(Qt::red); - } - else if (gridItem->col() < 32) - { - gridItem->setColor(Qt::white); - } - else - { - gridItem->setColor(Qt::black); - } + gridItem->setColor(Qt::white); + } + else + { + gridItem->setColor(Qt::black); } } - break; } - case(3): + break; + } + case (3): { + for (QGraphicsItem *item : items) { - for (QGraphicsItem* item : items) + GridItem *gridItem = dynamic_cast<GridItem *>(item); + if (gridItem) { - GridItem* gridItem = dynamic_cast<GridItem*>(item); - if (gridItem) + if (gridItem->row() == 0 && gridItem->col() == 31 || gridItem->row() == 0 && gridItem->col() == 32 || + gridItem->row() == 63 && gridItem->col() == 31 || gridItem->row() == 63 && gridItem->col() == 32 || + gridItem->row() == 31 && gridItem->col() == 0 || gridItem->row() == 32 && gridItem->col() == 0 || + gridItem->row() == 31 && gridItem->col() == 63 || gridItem->row() == 32 && gridItem->col() == 63) + { + gridItem->setColor(Qt::red); + } + else { - if (gridItem->row() == 0 && gridItem->col() == 31 || gridItem->row() == 0 && gridItem->col() == 32 || - gridItem->row() == 63 && gridItem->col() == 31 || gridItem->row() == 63 && gridItem->col() == 32 || - gridItem->row() == 31 && gridItem->col() == 0 || gridItem->row() == 32 && gridItem->col() == 0 || - gridItem->row() == 31 && gridItem->col() == 63 || gridItem->row() == 32 && gridItem->col() == 63) - { - gridItem->setColor(Qt::red); - } - else - { - gridItem->setColor(Qt::white); - } - + gridItem->setColor(Qt::white); } } - break; } - case(4): + break; + } + case (4): { + for (QGraphicsItem *item : items) { - for (QGraphicsItem* item : items) + GridItem *gridItem = dynamic_cast<GridItem *>(item); + if (gridItem) { - GridItem* gridItem = dynamic_cast<GridItem*>(item); - if (gridItem) + if (gridItem->row() == 0 && gridItem->col() == 31 || gridItem->row() == 0 && gridItem->col() == 32 || + gridItem->row() == 63 && gridItem->col() == 31 || gridItem->row() == 63 && gridItem->col() == 32 || + gridItem->row() == 31 && gridItem->col() == 0 || gridItem->row() == 32 && gridItem->col() == 0 || + gridItem->row() == 31 && gridItem->col() == 63 || gridItem->row() == 32 && gridItem->col() == 63) { - if (gridItem->row() == 0 && gridItem->col() == 31 || gridItem->row() == 0 && gridItem->col() == 32 || - gridItem->row() == 63 && gridItem->col() == 31 || gridItem->row() == 63 && gridItem->col() == 32 || - gridItem->row() == 31 && gridItem->col() == 0 || gridItem->row() == 32 && gridItem->col() == 0 || - gridItem->row() == 31 && gridItem->col() == 63 || gridItem->row() == 32 && gridItem->col() == 63) - { - gridItem->setColor(Qt::red); - } - else if (gridItem->row() < 16 && gridItem->col() < 16 || gridItem->row() > 47 && gridItem->col() > 47 || - gridItem->row() > 47 && gridItem->col() < 16 || gridItem->row() < 16 && gridItem->col() > 47) - { - gridItem->setColor(Qt::black); - } - else - { - gridItem->setColor(Qt::white); - } + gridItem->setColor(Qt::red); + } + else if (gridItem->row() < 16 && gridItem->col() < 16 || gridItem->row() > 47 && gridItem->col() > 47 || + gridItem->row() > 47 && gridItem->col() < 16 || gridItem->row() < 16 && gridItem->col() > 47) + { + gridItem->setColor(Qt::black); + } + else + { + gridItem->setColor(Qt::white); } } - break; } - case(5): + break; + } + case (5): { + for (QGraphicsItem *item : items) { - for (QGraphicsItem* item : items) + GridItem *gridItem = dynamic_cast<GridItem *>(item); + if (gridItem) { - GridItem* gridItem = dynamic_cast<GridItem*>(item); - if (gridItem) - { - gridItem->setColor(Qt::white); - } + gridItem->setColor(Qt::white); } - break; } + break; + } } } diff --git a/editor/src/CenterGrid.hpp b/editor/src/CenterGrid.hpp index 9b12584285f2c4716a6f86a6e8b8fdfb94c1aaad..4c8829332aec1269f9729e806091c9cde3160fb5 100644 --- a/editor/src/CenterGrid.hpp +++ b/editor/src/CenterGrid.hpp @@ -1,144 +1,143 @@ #pragma once -#include <QWidget> #include <QGraphicsScene> #include <QGraphicsView> -#include <QVBoxLayout> #include <QMessageBox> -#include <QTimer> #include <QPixmap> +#include <QTimer> +#include <QVBoxLayout> +#include <QWidget> #include "GridItem.hpp" -namespace editor +namespace editor { /** -* @brief Central widget of the Editor, contains editable grid representing a room -*/ -class CenterGrid : public QWidget + * @brief Central widget of the Editor, contains editable grid representing a room + */ +class CenterGrid : public QWidget { Q_OBJECT -public: + public: /** - * @brief Creates a new CenterGrid - * @param parent parent widget of CenterGrid - */ - CenterGrid(QWidget* parent = nullptr); + * @brief Creates a new CenterGrid + * @param parent parent widget of CenterGrid + */ + CenterGrid(QWidget *parent = nullptr); /** - * @brief Setter function to update which sprite is currently selected and needs to be places when grid clicked - * @param message TEMPORARY message that is shown when tile is clicked replace with sprite data - */ + * @brief Setter function to update which sprite is currently selected and needs to be places when grid clicked + * @param message TEMPORARY message that is shown when tile is clicked replace with sprite data + */ void setCurrentSprite(const QPixmap pixmap, const int id); /** - * @brief Function for drawing the currently selected sprite on a tile when tile is clicked - * @param row row of the clicked tile (y coordiante) - * @param col column of the clicked tile (x coordinate) - */ + * @brief Function for drawing the currently selected sprite on a tile when tile is clicked + * @param row row of the clicked tile (y coordiante) + * @param col column of the clicked tile (x coordinate) + */ void onTileClicked(int row, int col); /** - * @brief draws room presets on the grid - * @param index index of room preset that needs to be drawn - */ + * @brief draws room presets on the grid + * @param index index of room preset that needs to be drawn + */ void drawPreset(int index); /** - * @brief gets GridItem by row and column - * @param row row (y coordinate) of the GridItem to return - * @param col column (x coordinate) of the GridItem to return - * @return GridItem* of the GridItem at row/col, or nullptr when out of bounds - */ - GridItem* getTile(int row, int col) const; + * @brief gets GridItem by row and column + * @param row row (y coordinate) of the GridItem to return + * @param col column (x coordinate) of the GridItem to return + * @return GridItem* of the GridItem at row/col, or nullptr when out of bounds + */ + GridItem *getTile(int row, int col) const; /** - * @brief getter function for Vector of TileIDs - * @return QVector<QVector<int>> of TileIDs - */ + * @brief getter function for Vector of TileIDs + * @return QVector<QVector<int>> of TileIDs + */ QVector<QVector<int>> getTileIDs(); /** - * @brief getter function for Vector of MobIDs - * @return QVector<QVector<int>> of MobIDs - */ + * @brief getter function for Vector of MobIDs + * @return QVector<QVector<int>> of MobIDs + */ QVector<QVector<int>> getMobIDs(); /** - * @brief getter function for Vector of ObjectsIDs - * @return QVector<QVector<int>> of ObjectIDs - */ + * @brief getter function for Vector of ObjectsIDs + * @return QVector<QVector<int>> of ObjectIDs + */ QVector<QVector<int>> getObjectIDs(); -private: - + private: /** - * @brief Event that gets called when the window is resized, adjusts CenterGrid to the new size - * @param event ResizeEvent that informs about the window being resized - */ - void resizeEvent(QResizeEvent* event); + * @brief Event that gets called when the window is resized, adjusts CenterGrid to the new size + * @param event ResizeEvent that informs about the window being resized + */ + void resizeEvent(QResizeEvent *event); /** - * @brief QT element that provides a surface for handling 2D graphics items - */ - QGraphicsScene* scene; + * @brief QT element that provides a surface for handling 2D graphics items + */ + QGraphicsScene *scene; /** - * @brief QT widget for displaying the contents of a QGraphicsScene - */ - QGraphicsView* view; + * @brief QT widget for displaying the contents of a QGraphicsScene + */ + QGraphicsView *view; /** - * @brief Pixmap of the currently selected sprite - */ + * @brief Pixmap of the currently selected sprite + */ QPixmap m_currentSprite; /** - * @brief ID of the currently selected sprite - */ + * @brief ID of the currently selected sprite + */ int m_currentSpriteID; /** - * @brief 2D Vector holding all GridItems, making them accessible by position on grid - */ - QVector<QVector<GridItem*>> gridItems; + * @brief 2D Vector holding all GridItems, making them accessible by position on grid + */ + QVector<QVector<GridItem *>> gridItems; /** - * @brief 2D Vector holding all tileIDs, making them accessible by position on grid - */ + * @brief 2D Vector holding all tileIDs, making them accessible by position on grid + */ QVector<QVector<int>> m_tileIDs; /** - * @brief 2D Vector holding all objectIDs, making them accessible by position on grid - */ + * @brief 2D Vector holding all objectIDs, making them accessible by position on grid + */ QVector<QVector<int>> m_objectIDs; /** - * @brief 2D Vector holding all mobIDs, making them accessible by position on grid - */ + * @brief 2D Vector holding all mobIDs, making them accessible by position on grid + */ QVector<QVector<int>> m_mobIDs; /** - * @brief Integer marking the type of the currently selected sprite - */ + * @brief Integer marking the type of the currently selected sprite + */ int m_spriteType; /** - * @brief List of all spriteIDs that are tiles - */ - std::vector<int> m_tileIDList = {0,1,2,3,4,5,6,7,8,9,32,33,34,35,36,37,38,39, - 40,41,64,65,66,67,68,69,70,71,72,73,96,97,98,99,100,101,128, - 129,130,131,132,133,160,161,162,163,164,165,192,193,194,195, - 201,224,225,226,227,232,233}; + * @brief List of all spriteIDs that are tiles + */ + std::vector<int> m_tileIDList = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 32, 33, 34, 35, 36, + 37, 38, 39, 40, 41, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, + 96, 97, 98, 99, 100, 101, 128, 129, 130, 131, 132, 133, 160, 161, 162, + 163, 164, 165, 192, 193, 194, 195, 201, 224, 225, 226, 227, 232, 233}; /** - * @brief List of all spriteIDs that are mobs - */ - std::vector<int> m_mobIDList = {18,19,20,21,22,23,24,25,50,51,52,53,54,55,56, - 57,82,83,84,85,86,87,88,89,114,115,116,117,118,119,120,121,146, - 147,148,149,150,151,152,153,178,179,180,181,182,183,184,185,210, - 211,212,213,214,215,216,242,243,244,245,246,247,248}; + * @brief List of all spriteIDs that are mobs + */ + std::vector<int> m_mobIDList = {18, 19, 20, 21, 22, 23, 24, 25, 50, 51, 52, 53, 54, 55, 56, 57, + 82, 83, 84, 85, 86, 87, 88, 89, 114, 115, 116, 117, 118, 119, 120, 121, + 146, 147, 148, 149, 150, 151, 152, 153, 178, 179, 180, 181, 182, 183, 184, 185, + 210, 211, 212, 213, 214, 215, 216, 242, 243, 244, 245, 246, 247, 248}; }; } // namespace editor \ No newline at end of file diff --git a/editor/src/Config.cpp b/editor/src/Config.cpp index f09bf98d468ab069fe2d046d26d3ac64fbd0d41b..b84daed2d3f7a65f800a59d1f65c510322e44010 100644 --- a/editor/src/Config.cpp +++ b/editor/src/Config.cpp @@ -5,17 +5,17 @@ namespace editor { - void Config::load(const pt::ptree& tree) - { - // Call load for each section explicitly - TileConfig::load(tree); - ObjectConfig::load(tree); - } +void Config::load(const pt::ptree &tree) +{ + // Call load for each section explicitly + TileConfig::load(tree); + ObjectConfig::load(tree); +} - void Config::save(pt::ptree& tree) - { - // Call save for each section explicitly - TileConfig::save(tree); - ObjectConfig::save(tree); - } -} \ No newline at end of file +void Config::save(pt::ptree &tree) +{ + // Call save for each section explicitly + TileConfig::save(tree); + ObjectConfig::save(tree); +} +} // namespace editor \ No newline at end of file diff --git a/editor/src/ConfigSection.tcc b/editor/src/ConfigSection.tcc index 0d34483855e7564a81ad3799b9fc9bf4769cb766..fa776051f67077ad33d676d9c83a5fd310179e79 100644 --- a/editor/src/ConfigSection.tcc +++ b/editor/src/ConfigSection.tcc @@ -1,16 +1,14 @@ namespace editor { -template<typename Derived> -void ConfigSection<Derived>::load(const pt::ptree &tree) +template <typename Derived> void ConfigSection<Derived>::load(const pt::ptree &tree) { - static_cast<Derived*>(this)->loadImpl(tree); + static_cast<Derived *>(this)->loadImpl(tree); } -template<typename Derived> -void ConfigSection<Derived>::save(pt::ptree &tree) +template <typename Derived> void ConfigSection<Derived>::save(pt::ptree &tree) { - static_cast<Derived*>(this)->saveImpl(tree); + static_cast<Derived *>(this)->saveImpl(tree); } -} +} // namespace editor diff --git a/editor/src/EditorApp.cpp b/editor/src/EditorApp.cpp index adecef5c716197e369a65fec751f56b14ce51a7f..847f9251e0f5b232ef76c0d7a2c58244f9f8b53f 100644 --- a/editor/src/EditorApp.cpp +++ b/editor/src/EditorApp.cpp @@ -11,4 +11,4 @@ EditorApp::EditorApp(int &argc, char **argv) : QApplication(argc, argv) { } -} // editor \ No newline at end of file +} // namespace editor \ No newline at end of file diff --git a/editor/src/EditorApp.hpp b/editor/src/EditorApp.hpp index 96b4c14adea4bf159e753d6e51a3fdfa76c185f9..099b3636ee724a1b890cca353d1ee3c875c07b63 100644 --- a/editor/src/EditorApp.hpp +++ b/editor/src/EditorApp.hpp @@ -8,17 +8,15 @@ namespace editor { /** -* @brief Application Entrypoint. -*/ + * @brief Application Entrypoint. + */ class EditorApp : public QApplication { -public: - - /** - * @brief Create new EditorApp - */ - EditorApp(int &argc, char **argv); + public: + /** + * @brief Create new EditorApp + */ + EditorApp(int &argc, char **argv); }; -} - +} // namespace editor diff --git a/editor/src/GridItem.cpp b/editor/src/GridItem.cpp index 06d051b7712f2d2c29d25a92d85c7736540c5b6d..2a2adb6f1df51d1f478952745bf18ee04de6959f 100644 --- a/editor/src/GridItem.cpp +++ b/editor/src/GridItem.cpp @@ -1,57 +1,59 @@ #include "GridItem.hpp" -namespace editor +namespace editor { -GridItem::GridItem(int row, int col, qreal size, QGraphicsItem* parent) - : QGraphicsObject(parent), m_row(row), m_col(col), m_size(size) +GridItem::GridItem(int row, int col, qreal size, QGraphicsItem *parent) + : QGraphicsObject(parent), m_row(row), m_col(col), m_size(size) { setAcceptHoverEvents(true); } -QRectF GridItem::boundingRect() const +QRectF GridItem::boundingRect() const { return QRectF(0, 0, m_size, m_size); } -void GridItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) +void GridItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { painter->setPen(QPen(Qt::black, 1)); - if (m_usePixmap && !m_pixmap.isNull()) + if (m_usePixmap && !m_pixmap.isNull()) { painter->drawPixmap(boundingRect(), m_pixmap, m_pixmap.rect()); - } else + } + else { painter->setBrush(m_color); painter->drawRect(boundingRect()); } } -void GridItem::setColor(const QColor& color) +void GridItem::setColor(const QColor &color) { - if (m_color != color) + if (m_color != color) { m_color = color; update(); } } -void GridItem::setPixmap(const QPixmap& pixmap) +void GridItem::setPixmap(const QPixmap &pixmap) { m_pixmap = pixmap.scaled(m_size, m_size, Qt::KeepAspectRatio, Qt::SmoothTransformation); m_usePixmap = true; update(); } -void GridItem::reset() { +void GridItem::reset() +{ m_pixmap = QPixmap(); m_usePixmap = false; m_color = Qt::white; update(); } -void GridItem::mousePressEvent(QGraphicsSceneMouseEvent* event) +void GridItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { emit clicked(m_row, m_col); QGraphicsObject::mousePressEvent(event); diff --git a/editor/src/GridItem.hpp b/editor/src/GridItem.hpp index b891b235bb8eb92bd137c349fc37777a2da1f7f8..94c76acf40de1a89a08b20113af8a2ce7a3ce0e2 100644 --- a/editor/src/GridItem.hpp +++ b/editor/src/GridItem.hpp @@ -1,117 +1,121 @@ #pragma once #include <QGraphicsObject> -#include <QPainter> #include <QGraphicsSceneMouseEvent> +#include <QPainter> -namespace editor +namespace editor { /** -* @brief Single element of the CenterGrid, representing the contents of a single tile of the room -*/ -class GridItem : public QGraphicsObject + * @brief Single element of the CenterGrid, representing the contents of a single tile of the room + */ +class GridItem : public QGraphicsObject { Q_OBJECT -public: - + public: /** - * @brief Creates a new GridItem and places it in the CenterGrid - * @param row row to place the new item in (y coordinate) - * @param col column to place the new item in (x coordinate) - * @param size size of the grid item - * @param parent parent of the GridItem - */ - GridItem(int row, int col, qreal size, QGraphicsItem* parent = nullptr); + * @brief Creates a new GridItem and places it in the CenterGrid + * @param row row to place the new item in (y coordinate) + * @param col column to place the new item in (x coordinate) + * @param size size of the grid item + * @param parent parent of the GridItem + */ + GridItem(int row, int col, qreal size, QGraphicsItem *parent = nullptr); /** - * @brief creates a bounding rectangle for drawing the GridItem - * @return QRectF of the bounding rectangle - */ + * @brief creates a bounding rectangle for drawing the GridItem + * @return QRectF of the bounding rectangle + */ QRectF boundingRect() const override; /** - * @brief function for painting the GridItem - * @param painter QPainter responsible for handling the painting - * @param option parameters needed for drawing a QGraphicsItem - * @param widget optional, widget that is painted on, for us always 0 - */ - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override; + * @brief function for painting the GridItem + * @param painter QPainter responsible for handling the painting + * @param option parameters needed for drawing a QGraphicsItem + * @param widget optional, widget that is painted on, for us always 0 + */ + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override; /** - * @brief returns the row (y coordinate) of the GridItem - * @return row (y coordinate) of the GridItem - */ - int row() const { return m_row; } + * @brief returns the row (y coordinate) of the GridItem + * @return row (y coordinate) of the GridItem + */ + int row() const + { + return m_row; + } /** - * @brief returns the column (x coordinate) of the GridItem - * @return column (x coordiante) of the GridItem - */ - int col() const { return m_col; } + * @brief returns the column (x coordinate) of the GridItem + * @return column (x coordiante) of the GridItem + */ + int col() const + { + return m_col; + } /** - * @brief sets the color to paint the GridItem with - * @param color QColor value for the color - */ - void setColor(const QColor& color); + * @brief sets the color to paint the GridItem with + * @param color QColor value for the color + */ + void setColor(const QColor &color); /** - * @brief sets pixmap to draw on the GridItem - * @param pixmap pixmap to draw on the GridItem - */ - void setPixmap(const QPixmap& pixmap); + * @brief sets pixmap to draw on the GridItem + * @param pixmap pixmap to draw on the GridItem + */ + void setPixmap(const QPixmap &pixmap); /** - * @brief reset GridItem to a default state - */ + * @brief reset GridItem to a default state + */ void reset(); -signals: + signals: /** - * @brief signal to be emitted when the GridItem gets clicked - * @param row row (y coordinate) of the GridItem - * @param col column (x coordiante) of the GridItem - */ + * @brief signal to be emitted when the GridItem gets clicked + * @param row row (y coordinate) of the GridItem + * @param col column (x coordiante) of the GridItem + */ void clicked(int row, int col); -protected: + protected: /** - * @brief handles clicks on GridItems, emits clicked()-Signal - * @param event QMouseEvent on a QGraphicsScene - */ - void mousePressEvent(QGraphicsSceneMouseEvent* event) override; - -private: + * @brief handles clicks on GridItems, emits clicked()-Signal + * @param event QMouseEvent on a QGraphicsScene + */ + void mousePressEvent(QGraphicsSceneMouseEvent *event) override; + private: /** - * @brief row (y coordinate) of the GridItem - */ + * @brief row (y coordinate) of the GridItem + */ int m_row; /** - * @brief column (x coordiante) of the GridItem - */ + * @brief column (x coordiante) of the GridItem + */ int m_col; /** - * @brief size of the GridItem - */ + * @brief size of the GridItem + */ qreal m_size; /** - * @brief color of the GridItem - */ + * @brief color of the GridItem + */ QColor m_color; /** - * @brief pixmap of the GridItem - */ + * @brief pixmap of the GridItem + */ QPixmap m_pixmap; /** - * @brief Flag whether solid color or pixmap are to be drawn - */ + * @brief Flag whether solid color or pixmap are to be drawn + */ bool m_usePixmap; }; diff --git a/editor/src/HDF5Handler.cpp b/editor/src/HDF5Handler.cpp index f1c81fe40a6e4f6a3a4997740e616114e828594d..994d32f8136c3c8141be2869bd714bfa49307997 100644 --- a/editor/src/HDF5Handler.cpp +++ b/editor/src/HDF5Handler.cpp @@ -11,9 +11,9 @@ HDF5Handler::HDF5Handler(const std::filesystem::path path) : m_Level(path) { } -void HDF5Handler::loadTilesheet(TileBar* tileBar) const +void HDF5Handler::loadTilesheet(TileBar *tileBar) const { - const unsigned char* tiledata = m_Level.getSpritesheet().getSpritedata().data(); + const unsigned char *tiledata = m_Level.getSpritesheet().getSpritedata().data(); const int width = m_Level.getSpritesheet().getWidth(); const int height = m_Level.getSpritesheet().getHeight(); @@ -37,62 +37,64 @@ void HDF5Handler::loadTilesheet(TileBar* tileBar) const tileBar->populateList(tileNames, tiles); } -void HDF5Handler::saveRoom(int id, QString name, QVector<QVector<int>> tileIDs, QVector<QVector<int>> mobIDs, QVector<QVector<int>> objectIDs) +void HDF5Handler::saveRoom(int id, QString name, QVector<QVector<int>> tileIDs, QVector<QVector<int>> mobIDs, + QVector<QVector<int>> objectIDs) { lvl::Room newRoom(id, name.toStdString(), 64, 64); - for (int row = 0; row < 64; row++) + for (int row = 0; row < 64; row++) { - for (int col = 0; col < 64; col++) + for (int col = 0; col < 64; col++) { - newRoom.placeTile({col,row}, tileIDs[row][col]); - if(mobIDs[row][col] != -1) + newRoom.placeTile({col, row}, tileIDs[row][col]); + if (mobIDs[row][col] != -1) { - newRoom.placeEnemy({col,row}, mobIDs[row][col]); + newRoom.placeEnemy({col, row}, mobIDs[row][col]); } - if(objectIDs[row][col] != -1) + if (objectIDs[row][col] != -1) { - newRoom.placeObject({col,row}, objectIDs[row][col]); + newRoom.placeObject({col, row}, objectIDs[row][col]); } } } - std::vector<lvl::Room>& roomsList = m_Level.getRooms(); + std::vector<lvl::Room> &roomsList = m_Level.getRooms(); roomsList.push_back(newRoom); m_Level.saveChanges("./level.h5"); } -void HDF5Handler::modifyRoom(int id, QVector<QVector<int>> tileIDs, QVector<QVector<int>> mobIDs, QVector<QVector<int>> objectIDs) +void HDF5Handler::modifyRoom(int id, QVector<QVector<int>> tileIDs, QVector<QVector<int>> mobIDs, + QVector<QVector<int>> objectIDs) { lvl::Level level("./level.h5"); - std::vector<lvl::Room>& roomsList = level.getRooms(); - auto it = std::find_if(roomsList.begin(), roomsList.end(), - [id](const lvl::Room& room) { return room.getId() == id; }); - lvl::Room& room = *it; + std::vector<lvl::Room> &roomsList = level.getRooms(); + auto it = + std::find_if(roomsList.begin(), roomsList.end(), [id](const lvl::Room &room) { return room.getId() == id; }); + lvl::Room &room = *it; int width = room.getWidth(); int height = room.getHeight(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { - room.removeTile({i,j}); - room.removeEnemy({i,j}); - room.removeObject({i,j}); + room.removeTile({i, j}); + room.removeEnemy({i, j}); + room.removeObject({i, j}); } } - - for (int row = 0; row < height; row++) + + for (int row = 0; row < height; row++) { - for (int col = 0; col < width; col++) + for (int col = 0; col < width; col++) { - room.placeTile({col,row}, tileIDs[row][col]); + room.placeTile({col, row}, tileIDs[row][col]); std::cout << col << " " << row << " " << tileIDs[row][col] << std::endl; - if(mobIDs[row][col] != -1) + if (mobIDs[row][col] != -1) { - room.placeEnemy({col,row}, mobIDs[row][col]); + room.placeEnemy({col, row}, mobIDs[row][col]); } - if(objectIDs[row][col] != -1) + if (objectIDs[row][col] != -1) { - room.placeObject({col,row}, objectIDs[row][col]); + room.placeObject({col, row}, objectIDs[row][col]); } } } @@ -101,19 +103,17 @@ void HDF5Handler::modifyRoom(int id, QVector<QVector<int>> tileIDs, QVector<QVec int HDF5Handler::getMaxID() { - std::vector<lvl::Room>& roomsList = m_Level.getRooms(); - auto maxIt = std::max_element(roomsList.begin(), roomsList.end(), - [](const lvl::Room& a, const lvl::Room& b) - { - return a.getId() < b.getId(); - }); - if (maxIt != roomsList.end()) + std::vector<lvl::Room> &roomsList = m_Level.getRooms(); + auto maxIt = std::max_element(roomsList.begin(), roomsList.end(), + [](const lvl::Room &a, const lvl::Room &b) { return a.getId() < b.getId(); }); + if (maxIt != roomsList.end()) { return maxIt->getId(); - } else + } + else { return 0; } } -} \ No newline at end of file +} // namespace editor \ No newline at end of file diff --git a/editor/src/HDF5Handler.hpp b/editor/src/HDF5Handler.hpp index 40483d808256f79e3ee9984610285f5bbac455e7..4480b1557d7599576126dd3632e959493638f3cb 100644 --- a/editor/src/HDF5Handler.hpp +++ b/editor/src/HDF5Handler.hpp @@ -1,7 +1,7 @@ #pragma once #include <QImage> -#include <QPixmap> #include <QMessageBox> +#include <QPixmap> #include <algorithm> @@ -16,55 +16,57 @@ namespace editor class HDF5Handler { -public: + public: /** - * @brief Create a new HDF5Handler (main interface with liblvl library) - */ + * @brief Create a new HDF5Handler (main interface with liblvl library) + */ HDF5Handler(); /** - * @brief Create a new HDF5Handler (main interface with liblvl library) - */ + * @brief Create a new HDF5Handler (main interface with liblvl library) + */ HDF5Handler(std::filesystem::path path); /** - * @brief destructor for HDF5Handler, default - */ + * @brief destructor for HDF5Handler, default + */ ~HDF5Handler() = default; /** - * @brief loads the tilesheet and puts tiles into TileBar - * @param tileBar pointer to the TileBar to show the tiles in - */ - void loadTilesheet(TileBar* tileBar) const; + * @brief loads the tilesheet and puts tiles into TileBar + * @param tileBar pointer to the TileBar to show the tiles in + */ + void loadTilesheet(TileBar *tileBar) const; /** - * @brief saves configured room to HDF5 - * @param id ID of the room, has to be unique! - * @param name Name of the room - * @param tileIDs 2D QVector of the tileIDs - * @param mobIDs 2D QVector of the mobIDs - * @param objectIDs 2D QVector of the objectIDs - */ - void saveRoom(int id, QString name, QVector<QVector<int>> tileIDs, QVector<QVector<int>> mobIDs, QVector<QVector<int>> objectIDs); + * @brief saves configured room to HDF5 + * @param id ID of the room, has to be unique! + * @param name Name of the room + * @param tileIDs 2D QVector of the tileIDs + * @param mobIDs 2D QVector of the mobIDs + * @param objectIDs 2D QVector of the objectIDs + */ + void saveRoom(int id, QString name, QVector<QVector<int>> tileIDs, QVector<QVector<int>> mobIDs, + QVector<QVector<int>> objectIDs); /** - * @brief saves configured room to HDF5 - * @param id ID of the room - * @param tileIDs 2D QVector of the tileIDs - * @param mobIDs 2D QVector of the mobIDs - * @param objectIDs 2D QVector of the objectIDs - */ - void modifyRoom(int id, QVector<QVector<int>> tileIDs, QVector<QVector<int>> mobIDs, QVector<QVector<int>> objectIDs); + * @brief saves configured room to HDF5 + * @param id ID of the room + * @param tileIDs 2D QVector of the tileIDs + * @param mobIDs 2D QVector of the mobIDs + * @param objectIDs 2D QVector of the objectIDs + */ + void modifyRoom(int id, QVector<QVector<int>> tileIDs, QVector<QVector<int>> mobIDs, + QVector<QVector<int>> objectIDs); /** - * @brief find maximum room-ID in current HDF5-file - * @return highest room-ID - */ + * @brief find maximum room-ID in current HDF5-file + * @return highest room-ID + */ int getMaxID(); -private: + private: lvl::Level m_Level; }; -} // editor \ No newline at end of file +} // namespace editor \ No newline at end of file diff --git a/editor/src/MainWindow.cpp b/editor/src/MainWindow.cpp index 846d9800dc7c1644c2a7c3a85e73e8329f806006..2f2d5cc8b4806fcfd6170618d5f857e4a1f2a22a 100644 --- a/editor/src/MainWindow.cpp +++ b/editor/src/MainWindow.cpp @@ -6,40 +6,41 @@ namespace editor { -MainWindow::MainWindow(DialogSelection selected, const std::filesystem::path& path, QWidget* parent, int roomID) +MainWindow::MainWindow(DialogSelection selected, const std::filesystem::path &path, QWidget *parent, int roomID) : QMainWindow(parent) { - resize(800,600); - setMinimumSize(300,200); + resize(800, 600); + setMinimumSize(300, 200); setWindowTitle("Editor"); std::string text = ""; - QWidget* centralWidget = new QWidget(this); - QHBoxLayout* mainLayout = new QHBoxLayout; + QWidget *centralWidget = new QWidget(this); + QHBoxLayout *mainLayout = new QHBoxLayout; if (QFile::exists("./level.h5")) { std::filesystem::path path = "./level.h5"; HDF5Handler handler = HDF5Handler(path); m_ID = handler.getMaxID() + 1; - } else + } + else { m_ID = 2; } m_loadID = roomID; // Create the menu bar - QMenuBar* menuBar = new QMenuBar(this); - QMenu* fileMenu = menuBar->addMenu("File"); - QAction* saveRoomAction = new QAction("Save Room", this); + QMenuBar *menuBar = new QMenuBar(this); + QMenu *fileMenu = menuBar->addMenu("File"); + QAction *saveRoomAction = new QAction("Save Room", this); connect(saveRoomAction, &QAction::triggered, this, &MainWindow::saveLevel); fileMenu->addAction(saveRoomAction); - QAction* quitAction = new QAction("Quit", this); + QAction *quitAction = new QAction("Quit", this); connect(quitAction, &QAction::triggered, this, &MainWindow::close); fileMenu->addAction(quitAction); setMenuBar(menuBar); // Left section: Scrollable list - TileBar* leftWidget = new TileBar(0, this); + TileBar *leftWidget = new TileBar(0, this); mainLayout->addWidget(leftWidget); m_leftWidget = leftWidget; connect(leftWidget, &editor::TileBar::itemClicked, this, &MainWindow::handleTileBarClick); @@ -48,16 +49,16 @@ MainWindow::MainWindow(DialogSelection selected, const std::filesystem::path& pa handler.loadTilesheet(m_leftWidget); // Center grid - CenterGrid* middleWidget = new CenterGrid(this); + CenterGrid *middleWidget = new CenterGrid(this); mainLayout->addWidget(middleWidget); m_centerGrid = middleWidget; // Right section: Scrollable list - TileBar* rightWidget = new TileBar(1, this); + TileBar *rightWidget = new TileBar(1, this); mainLayout->addWidget(rightWidget); connect(rightWidget, &editor::TileBar::itemClicked, this, &MainWindow::handleTileBarClick); - //temporary example, replace with list of room layouts + // temporary example, replace with list of room layouts QStringList itemsRight; QList<QPixmap> pixmapsRight; itemsRight << QString("1x1 room"); @@ -78,29 +79,28 @@ MainWindow::MainWindow(DialogSelection selected, const std::filesystem::path& pa centralWidget->setLayout(mainLayout); setCentralWidget(centralWidget); - - //initialize middleWidget as fully custom board + // initialize middleWidget as fully custom board middleWidget->drawPreset(5); - //Load selected level - if(roomID != -1) + // Load selected level + if (roomID != -1) { lvl::Level level("./level.h5"); - std::vector<lvl::Room>& roomsList = level.getRooms(); + std::vector<lvl::Room> &roomsList = level.getRooms(); auto it = std::find_if(roomsList.begin(), roomsList.end(), - [roomID](const lvl::Room& room) { return room.getId() == roomID; }); - lvl::Room& foundRoom = *it; + [roomID](const lvl::Room &room) { return room.getId() == roomID; }); + lvl::Room &foundRoom = *it; int width = foundRoom.getWidth(); for (auto it = foundRoom.tilesBegin(); it != foundRoom.tilesEnd(); it++) { int32_t tileID = *it; - if (tileID != -1) + if (tileID != -1) { std::size_t index = std::distance(foundRoom.tilesBegin(), it++); int x = index % width; int y = index / width; m_centerGrid->setCurrentSprite(m_leftWidget->getPixmapByIndex(tileID), tileID); - m_centerGrid->onTileClicked(x,y); + m_centerGrid->onTileClicked(x, y); } } for (auto it = foundRoom.objectsBegin(); it != foundRoom.objectsEnd(); it++) @@ -110,7 +110,7 @@ MainWindow::MainWindow(DialogSelection selected, const std::filesystem::path& pa int y = gameObject.position.y; int tileID = gameObject.id; m_centerGrid->setCurrentSprite(m_leftWidget->getPixmapByIndex(tileID), tileID); - m_centerGrid->onTileClicked(x,y); + m_centerGrid->onTileClicked(x, y); } for (auto it = foundRoom.enemiesBegin(); it != foundRoom.enemiesEnd(); it++) { @@ -119,35 +119,31 @@ MainWindow::MainWindow(DialogSelection selected, const std::filesystem::path& pa int y = gameObject.position.y; int tileID = gameObject.id; m_centerGrid->setCurrentSprite(m_leftWidget->getPixmapByIndex(tileID), tileID); - m_centerGrid->onTileClicked(x,y); + m_centerGrid->onTileClicked(x, y); } - } - } void MainWindow::handleTileBarClick(int index, int id) { - if (id == 0) + if (id == 0) { m_centerGrid->setCurrentSprite(m_leftWidget->getPixmapByIndex(index), index); - } else + } + else { QMessageBox::StandardButton confirmationPopup; - confirmationPopup = QMessageBox::question( - this, - "Confirmation", - "Changing the preset will delete all placed sprites/tiles! Proceed?", - QMessageBox::Yes | QMessageBox::No - ); - if (confirmationPopup == QMessageBox::Yes) + confirmationPopup = QMessageBox::question(this, "Confirmation", + "Changing the preset will delete all placed sprites/tiles! Proceed?", + QMessageBox::Yes | QMessageBox::No); + if (confirmationPopup == QMessageBox::Yes) { m_centerGrid->drawPreset(index); } } } -void MainWindow::saveLevel() +void MainWindow::saveLevel() { QVector<QVector<int>> tileIDs = m_centerGrid->getTileIDs(); QVector<QVector<int>> mobIDs = m_centerGrid->getMobIDs(); @@ -158,19 +154,22 @@ void MainWindow::saveLevel() HDF5Handler handler = HDF5Handler(path); handler.modifyRoom(m_loadID, tileIDs, mobIDs, objectIDs); m_loadID = -1; - } else + } + else { QString name = QInputDialog::getText(nullptr, "Enter Level Name", "Name:"); - if (name.isEmpty()) { + if (name.isEmpty()) + { name = "defaultName"; } - if (QFile::exists("./level.h5")) + if (QFile::exists("./level.h5")) { std::filesystem::path path = "./level.h5"; HDF5Handler handler = HDF5Handler(path); handler.saveRoom(m_ID, name, tileIDs, mobIDs, objectIDs); - } else + } + else { HDF5Handler handler = HDF5Handler(); handler.saveRoom(m_ID, name, tileIDs, mobIDs, objectIDs); @@ -179,4 +178,4 @@ void MainWindow::saveLevel() } } -} \ No newline at end of file +} // namespace editor \ No newline at end of file diff --git a/editor/src/MainWindow.hpp b/editor/src/MainWindow.hpp index a88c284afd8a1b6b06eef8d1a91b58d5b0c3c5e4..42a23e5d503d0b52ddc9aaf81ae5c7ab231ac840 100644 --- a/editor/src/MainWindow.hpp +++ b/editor/src/MainWindow.hpp @@ -2,81 +2,79 @@ // Created by Leon Lux on 19.01.25. // #pragma once -#include <QMainWindow> +#include <QInputDialog> #include <QLabel> +#include <QMainWindow> #include <QMenuBar> -#include <QWidget> #include <QMessageBox> -#include <QInputDialog> +#include <QWidget> #include <filesystem> -#include "StartupDialog.hpp" #include "CenterGrid.hpp" -#include "TileBar.hpp" #include "HDF5Handler.hpp" - +#include "StartupDialog.hpp" +#include "TileBar.hpp" namespace editor { /** -* @brief Main window and root of ownership -*/ + * @brief Main window and root of ownership + */ class MainWindow : public QMainWindow { - Q_OBJECT -public: - /** - * @brief Create a new MainWindow of the editor - * @param selected selection from StartupDialog (new room/edit room) - * @param path file path for loading levels - * @param parent parent element of MainWindow - * @param roomID ID of the room selected to be loaded in startup (or -1 for new room) - */ - explicit MainWindow(DialogSelection selected, const std::filesystem::path& path, QWidget* parent = nullptr, int roomID = -1); - -private slots: - /** - * @brief MainWindow slot to handle TileBar-clicks - * @param index Index of the item that was clicked in the list of items - * @param id Id identifies the tile bar (left or right) in which an item was clicked - */ - void handleTileBarClick(int index, int id); - -private: + Q_OBJECT + public: + /** + * @brief Create a new MainWindow of the editor + * @param selected selection from StartupDialog (new room/edit room) + * @param path file path for loading levels + * @param parent parent element of MainWindow + * @param roomID ID of the room selected to be loaded in startup (or -1 for new room) + */ + explicit MainWindow(DialogSelection selected, const std::filesystem::path &path, QWidget *parent = nullptr, + int roomID = -1); - /** - * @brief Saves room to HDF5 when called in menu - */ - void saveLevel(); + private slots: + /** + * @brief MainWindow slot to handle TileBar-clicks + * @param index Index of the item that was clicked in the list of items + * @param id Id identifies the tile bar (left or right) in which an item was clicked + */ + void handleTileBarClick(int index, int id); - /** - * @brief Converts 2D QVector to 1D std::vector for saving - * @return std::vector<int> 1D std::vector with tileIDs that can be saved in HDF5 - */ - std::vector<int> flatten(const QVector<QVector<int>>& input); + private: + /** + * @brief Saves room to HDF5 when called in menu + */ + void saveLevel(); - /** - * @brief Pointer to the centerGrid - */ - CenterGrid* m_centerGrid; + /** + * @brief Converts 2D QVector to 1D std::vector for saving + * @return std::vector<int> 1D std::vector with tileIDs that can be saved in HDF5 + */ + std::vector<int> flatten(const QVector<QVector<int>> &input); - /** - * @brief Pointer to leftWidget - */ - TileBar* m_leftWidget; + /** + * @brief Pointer to the centerGrid + */ + CenterGrid *m_centerGrid; - /** - * @brief Room ID to use for saving next room - */ - int m_ID; + /** + * @brief Pointer to leftWidget + */ + TileBar *m_leftWidget; - /** - * @brief ID of loaded room (or -1) - */ - int m_loadID; + /** + * @brief Room ID to use for saving next room + */ + int m_ID; + /** + * @brief ID of loaded room (or -1) + */ + int m_loadID; }; -} // editor +} // namespace editor diff --git a/editor/src/ObjectConfig.cpp b/editor/src/ObjectConfig.cpp index 606d9e2e905c6a12bdb1e5377eade1eee92c6595..05729531cd0a685f9da3f9011f267264d3a6776c 100644 --- a/editor/src/ObjectConfig.cpp +++ b/editor/src/ObjectConfig.cpp @@ -9,33 +9,33 @@ namespace editor { - void ObjectConfig::loadImpl(const pt::ptree &tree) +void ObjectConfig::loadImpl(const pt::ptree &tree) +{ + for (const auto &[fst, snd] : tree.get_child("config.objects")) { - for (const auto&[fst, snd] : tree.get_child("config.objects")) - { - Object obj; - obj.id = snd.get<int32_t>("id"); - obj.name = snd.get<std::string>("name"); - m_Objects.push_back(obj); - } + Object obj; + obj.id = snd.get<int32_t>("id"); + obj.name = snd.get<std::string>("name"); + m_Objects.push_back(obj); } +} - void ObjectConfig::saveImpl(pt::ptree &tree) +void ObjectConfig::saveImpl(pt::ptree &tree) +{ + pt::ptree objectNodes; + for (const auto &tile : m_Objects) { - pt::ptree objectNodes; - for (const auto& tile : m_Objects) - { - pt::ptree objectNode; - objectNode.put("id", tile.id); - objectNode.put("name", tile.name); - objectNodes.push_back(std::make_pair("", objectNode)); - } - tree.add_child("config.objects", objectNodes); + pt::ptree objectNode; + objectNode.put("id", tile.id); + objectNode.put("name", tile.name); + objectNodes.push_back(std::make_pair("", objectNode)); } + tree.add_child("config.objects", objectNodes); +} - const std::vector<ObjectConfig::Object>& ObjectConfig::getObjects() const - { - return m_Objects; - } +const std::vector<ObjectConfig::Object> &ObjectConfig::getObjects() const +{ + return m_Objects; +} -} \ No newline at end of file +} // namespace editor \ No newline at end of file diff --git a/editor/src/StartupDialog.cpp b/editor/src/StartupDialog.cpp index 5419a9af8cf36affc3fc38d5d4adb83a0ee27751..e1bf89a192a75ee85f5c651959b49dfdabf2c4b7 100644 --- a/editor/src/StartupDialog.cpp +++ b/editor/src/StartupDialog.cpp @@ -5,92 +5,85 @@ namespace editor { - StartupDialog::StartupDialog(QWidget* parent) : QDialog(parent), m_Selection(DialogSelection::NEW_LEVEL) - { - setWindowTitle("Der tolle Leveleditor"); +StartupDialog::StartupDialog(QWidget *parent) : QDialog(parent), m_Selection(DialogSelection::NEW_LEVEL) +{ + setWindowTitle("Der tolle Leveleditor"); - auto* layout = new QVBoxLayout(this); + auto *layout = new QVBoxLayout(this); - auto* loadButton = new QPushButton("Load existing Level", this); - auto* newButton = new QPushButton("Create new Level", this); + auto *loadButton = new QPushButton("Load existing Level", this); + auto *newButton = new QPushButton("Create new Level", this); - layout->addWidget(loadButton); - layout->addWidget(newButton); + layout->addWidget(loadButton); + layout->addWidget(newButton); - connect(loadButton, &QPushButton::clicked, this, &StartupDialog::onLoadLevel); - connect(newButton, &QPushButton::clicked, this, &StartupDialog::onNewLevel); - } + connect(loadButton, &QPushButton::clicked, this, &StartupDialog::onLoadLevel); + connect(newButton, &QPushButton::clicked, this, &StartupDialog::onNewLevel); +} - void StartupDialog::onNewLevel() - { - accept(); - } +void StartupDialog::onNewLevel() +{ + accept(); +} - void StartupDialog::onLoadLevel() - { - const std::string path = QFileDialog::getOpenFileName( - this, - "Open Level", - "", - "Level Files (*.h5)" - ) - .toStdString(); - - if (!path.empty()) - { - m_Selection = DialogSelection::LOAD_LEVEL; - m_FilePath = path; - - showSelectionDialog(path); - } - } +void StartupDialog::onLoadLevel() +{ + const std::string path = QFileDialog::getOpenFileName(this, "Open Level", "", "Level Files (*.h5)").toStdString(); - void StartupDialog::showSelectionDialog(const std::string path) + if (!path.empty()) { - QDialog selectionDialog(this); - selectionDialog.setWindowTitle("Select Level"); - - auto* layout = new QVBoxLayout(&selectionDialog); - auto* comboBox = new QComboBox(&selectionDialog); - auto* okButton = new QPushButton("OK", &selectionDialog); - - lvl::Level level(path); - std::vector<lvl::Room>& roomsList = level.getRooms(); - - for (const auto& item : roomsList) - { - comboBox->addItem(QString::fromStdString(item.getName()), QVariant(item.getId())); - } - - layout->addWidget(comboBox); - layout->addWidget(okButton); - - connect(okButton, &QPushButton::clicked, [&]() - { - int selectedID = comboBox->currentData().toInt(); - m_selectedRoomID = selectedID; - selectionDialog.accept(); - }); - - if (selectionDialog.exec() == QDialog::Accepted) - { - accept(); - } - } + m_Selection = DialogSelection::LOAD_LEVEL; + m_FilePath = path; - DialogSelection StartupDialog::getSelection() const - { - return m_Selection; + showSelectionDialog(path); } +} - std::filesystem::path StartupDialog::getPath() const +void StartupDialog::showSelectionDialog(const std::string path) +{ + QDialog selectionDialog(this); + selectionDialog.setWindowTitle("Select Level"); + + auto *layout = new QVBoxLayout(&selectionDialog); + auto *comboBox = new QComboBox(&selectionDialog); + auto *okButton = new QPushButton("OK", &selectionDialog); + + lvl::Level level(path); + std::vector<lvl::Room> &roomsList = level.getRooms(); + + for (const auto &item : roomsList) { - return m_FilePath; + comboBox->addItem(QString::fromStdString(item.getName()), QVariant(item.getId())); } - int StartupDialog::getSelectedRoomID() const + layout->addWidget(comboBox); + layout->addWidget(okButton); + + connect(okButton, &QPushButton::clicked, [&]() { + int selectedID = comboBox->currentData().toInt(); + m_selectedRoomID = selectedID; + selectionDialog.accept(); + }); + + if (selectionDialog.exec() == QDialog::Accepted) { - return m_selectedRoomID; + accept(); } +} +DialogSelection StartupDialog::getSelection() const +{ + return m_Selection; } + +std::filesystem::path StartupDialog::getPath() const +{ + return m_FilePath; +} + +int StartupDialog::getSelectedRoomID() const +{ + return m_selectedRoomID; +} + +} // namespace editor diff --git a/editor/src/StartupDialog.hpp b/editor/src/StartupDialog.hpp index 7b6a311c6a33ccdbd07c06be5b917f383457a223..0d2dd721b4f823f021eac47eefa475966006addf 100644 --- a/editor/src/StartupDialog.hpp +++ b/editor/src/StartupDialog.hpp @@ -2,11 +2,11 @@ // Created by Leon Lux on 19.01.25. // #pragma once +#include <QComboBox> #include <QDialog> -#include <QVBoxLayout> -#include <QPushButton> #include <QFileDialog> -#include <QComboBox> +#include <QPushButton> +#include <QVBoxLayout> #include <filesystem> @@ -17,9 +17,9 @@ namespace editor { /** @brief Possible startup Selection options. -* -* @see StartupDialog -*/ + * + * @see StartupDialog + */ enum class DialogSelection { NEW_LEVEL, @@ -32,8 +32,8 @@ enum class DialogSelection class StartupDialog : public QDialog { Q_OBJECT -public: - explicit StartupDialog(QWidget* parent = nullptr); + public: + explicit StartupDialog(QWidget *parent = nullptr); /** * @brief Return the selection the user made. @@ -58,12 +58,12 @@ public: int getSelectedRoomID() const; /** - * @brief Shows dialog for selecting a room to load/edit - * @param path Path to the .h5 file - */ + * @brief Shows dialog for selecting a room to load/edit + * @param path Path to the .h5 file + */ void showSelectionDialog(const std::string path); -public slots: + public slots: /** * @brief Callback for Load Button. */ @@ -73,11 +73,11 @@ public slots: * @brief Callback for New Button */ void onNewLevel(); -private: + + private: DialogSelection m_Selection; std::filesystem::path m_FilePath; int m_selectedRoomID = -1; }; -} - +} // namespace editor diff --git a/editor/src/TileBar.cpp b/editor/src/TileBar.cpp index ba59550224638d917b6c7fece053ccc9a4de4744..88a3777e23d79b2b04627768311ca4bf89032a6f 100644 --- a/editor/src/TileBar.cpp +++ b/editor/src/TileBar.cpp @@ -7,16 +7,13 @@ namespace editor { -TileBar::TileBar(int id, QWidget *parent) - : QWidget(parent), - m_id(id), - m_ScrollArea(new QScrollArea(this)), - m_List(new QListWidget(this)) +TileBar::TileBar(int id, QWidget *parent) + : QWidget(parent), m_id(id), m_ScrollArea(new QScrollArea(this)), m_List(new QListWidget(this)) { m_ScrollArea->setWidget(m_List); m_ScrollArea->setWidgetResizable(true); m_ScrollArea->setFixedWidth(200); - QVBoxLayout* layout = new QVBoxLayout(this); + QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(m_ScrollArea); setLayout(layout); @@ -26,18 +23,19 @@ TileBar::TileBar(int id, QWidget *parent) }); } -void TileBar::populateList(const QStringList& items, const QList<QPixmap>& pixmaps) +void TileBar::populateList(const QStringList &items, const QList<QPixmap> &pixmaps) { m_List->clear(); const int count = qMin(items.size(), pixmaps.size()); - for (int i = 0; i < count; ++i) { + for (int i = 0; i < count; ++i) + { addListItem(m_List, pixmaps[i], items[i]); } } -void TileBar::addListItem(QListWidget* listWidget, const QPixmap& pixmap, const QString& text) +void TileBar::addListItem(QListWidget *listWidget, const QPixmap &pixmap, const QString &text) { - auto* item = new QListWidgetItem(); + auto *item = new QListWidgetItem(); QIcon icon(pixmap.scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation)); item->setIcon(icon); item->setText(text); @@ -45,16 +43,18 @@ void TileBar::addListItem(QListWidget* listWidget, const QPixmap& pixmap, const listWidget->addItem(item); } -QPixmap TileBar::getPixmapByIndex(int index) +QPixmap TileBar::getPixmapByIndex(int index) { - QListWidgetItem* item = m_List->item(index); - if (item) { + QListWidgetItem *item = m_List->item(index); + if (item) + { QPixmap pixmap = item->data(Qt::UserRole).value<QPixmap>(); - if (!pixmap.isNull()) { + if (!pixmap.isNull()) + { return pixmap; } } return QPixmap(); } -} \ No newline at end of file +} // namespace editor \ No newline at end of file diff --git a/editor/src/TileBar.hpp b/editor/src/TileBar.hpp index 99f37fbf04d7d80046344ccacc0ce86e6da8a87d..c3ab7c057fb502ac86e66af51d989da09e361b4a 100644 --- a/editor/src/TileBar.hpp +++ b/editor/src/TileBar.hpp @@ -3,78 +3,76 @@ // #pragma once -#include <QScrollArea> -#include <QWidget> +#include <QDebug> #include <QListWidget> +#include <QPixmap> +#include <QScrollArea> #include <QString> #include <QVBoxLayout> -#include <QDebug> -#include <QPixmap> +#include <QWidget> namespace editor { /** -* @brief Scrollable list of entries that can vary in type -*/ + * @brief Scrollable list of entries that can vary in type + */ class TileBar : public QWidget { Q_OBJECT -public: - + public: /** - * @brief creates a new TileBar - * @param id allows handling actions from different instances of TileBars differently when created with unique IDs - * @param parent parent widget of TileBar - */ + * @brief creates a new TileBar + * @param id allows handling actions from different instances of TileBars differently when created with unique IDs + * @param parent parent widget of TileBar + */ explicit TileBar(int id, QWidget *parent = nullptr); /** - * @brief populates TileBar with entries - * @param items list of items to add to show in the TileBar - * @param pixmaps list of pixmaps of the images for the items - */ - void populateList(const QStringList& items, const QList<QPixmap>& pixmaps); + * @brief populates TileBar with entries + * @param items list of items to add to show in the TileBar + * @param pixmaps list of pixmaps of the images for the items + */ + void populateList(const QStringList &items, const QList<QPixmap> &pixmaps); /** - * @brief get the pixmap of the entry with the given index - * @param index index of the entry to get the pixmap from - */ + * @brief get the pixmap of the entry with the given index + * @param index index of the entry to get the pixmap from + */ QPixmap getPixmapByIndex(int index); -signals: + signals: /** - * @brief signal when an item in a TileBar gets clicked - * @param index index of the item clicked - * @param id id of the tile bar in which the item got clicked - */ + * @brief signal when an item in a TileBar gets clicked + * @param index index of the item clicked + * @param id id of the tile bar in which the item got clicked + */ void itemClicked(int index, int id); -private: - + private: /** - * @brief ID of the TileBar, allows identifying TileBars when created with unique numbers - */ + * @brief ID of the TileBar, allows identifying TileBars when created with unique numbers + */ int m_id; /** - * @brief QT widget that holds contents and is scrollable - */ + * @brief QT widget that holds contents and is scrollable + */ QScrollArea *m_ScrollArea; /** - * @brief QT widget holding a list of items - */ + * @brief QT widget holding a list of items + */ QListWidget *m_List; /** - * @brief adds items to the list of items shown in the TileBar - * @param listWidget QListWidget to add the items to - * @param pixmap pixmap of image to show for the item - * @param text QString with text to show for the item - */ - void addListItem(QListWidget* listWidget, const QPixmap& pixmap, const QString& text); + * @brief adds items to the list of items shown in the TileBar + * @param listWidget QListWidget to add the items to + * @param pixmap pixmap of image to show for the item + * @param text QString with text to show for the item + */ + void addListItem(QListWidget *listWidget, const QPixmap &pixmap, const QString &text); }; -} \ No newline at end of file +} // namespace editor \ No newline at end of file diff --git a/editor/src/TileConfig.cpp b/editor/src/TileConfig.cpp index 774ad60a650967081dbc23c02cc2300a67f8812d..16121a5136b994fe2455009506b2f4bc41d8e58f 100644 --- a/editor/src/TileConfig.cpp +++ b/editor/src/TileConfig.cpp @@ -8,7 +8,7 @@ namespace editor void TileConfig::loadImpl(const pt::ptree &tree) { - for (const auto&[fst, snd] : tree.get_child("config.tiles")) + for (const auto &[fst, snd] : tree.get_child("config.tiles")) { Tile tile; tile.id = snd.get<int32_t>("id"); @@ -20,7 +20,7 @@ void TileConfig::loadImpl(const pt::ptree &tree) void TileConfig::saveImpl(pt::ptree &tree) { pt::ptree tileNodes; - for (const auto& tile : m_Tiles) + for (const auto &tile : m_Tiles) { pt::ptree tileNode; tileNode.put("id", tile.id); @@ -30,9 +30,9 @@ void TileConfig::saveImpl(pt::ptree &tree) tree.add_child("config.tiles", tileNodes); } -const std::vector<TileConfig::Tile>& TileConfig::getTiles() const +const std::vector<TileConfig::Tile> &TileConfig::getTiles() const { return m_Tiles; } -} \ No newline at end of file +} // namespace editor \ No newline at end of file diff --git a/editor/src/main.cpp b/editor/src/main.cpp index a7712986faec131eeb496ab0f4a2bfd3fe814804..cb1e5b8b6388851425cddd9fabcc32d843b4ed21 100644 --- a/editor/src/main.cpp +++ b/editor/src/main.cpp @@ -4,11 +4,12 @@ #include <QStyleFactory> -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ editor::EditorApp app(argc, argv); editor::StartupDialog startup; - //Set editor to use dark mode + // Set editor to use dark mode app.setStyle(QStyleFactory::create("Fusion")); QPalette darkPalette; darkPalette.setColor(QPalette::Window, QColor(53, 53, 53)); @@ -28,7 +29,7 @@ int main(int argc, char *argv[]) { if (startup.exec() == QDialog::Accepted) { - editor::MainWindow window(startup.getSelection(), startup.getPath(), 0, startup.getSelectedRoomID()); + editor::MainWindow window(startup.getSelection(), startup.getPath(), 0, startup.getSelectedRoomID()); window.show(); return app.exec(); } diff --git a/game/src/BulletManager.hpp b/game/src/BulletManager.hpp index 202760a4d9bd2cc92839750fcc63717134497601..77e10d8e14da0a705bb01735fa033efc0679cabc 100644 --- a/game/src/BulletManager.hpp +++ b/game/src/BulletManager.hpp @@ -86,7 +86,6 @@ class BulletManager */ void render(const Camera &camera) const; - /** * @brief Cleans up the BulletManager resources. * diff --git a/game/src/ContactListener.cpp b/game/src/ContactListener.cpp index 083d01ba0a16b19147891fd29144b373dc0bf8d6..a8d31949f41688083759fe676be15e97ce84e827 100644 --- a/game/src/ContactListener.cpp +++ b/game/src/ContactListener.cpp @@ -51,10 +51,10 @@ void ContactListener::HandleCollision(b2Body *bodyA, b2Body *bodyB) if (bodyA->GetUserData().pointer == 3) { - //if collision with wall or player - //if (bodyB->GetUserData().pointer == 0) + // if collision with wall or player + // if (bodyB->GetUserData().pointer == 0) //{ - //} + // } enemyBulletsToDestroy.push_back(bodyA); if (bodyB->GetUserData().pointer == 2) @@ -68,10 +68,10 @@ void ContactListener::HandleCollision(b2Body *bodyA, b2Body *bodyB) if (bodyB->GetUserData().pointer == 3) { - //if (bodyA->GetUserData().pointer == 0) + // if (bodyA->GetUserData().pointer == 0) //{ - //} - enemyBulletsToDestroy.push_back(bodyB); + // } + enemyBulletsToDestroy.push_back(bodyB); if (bodyA->GetUserData().pointer == 2) { @@ -84,7 +84,7 @@ void ContactListener::HandleCollision(b2Body *bodyA, b2Body *bodyB) // Door Collision if (bodyA->GetUserData().pointer == 5) { - //if collision with player + // if collision with player if (bodyB->GetUserData().pointer == 2) { // Entered Door @@ -94,7 +94,7 @@ void ContactListener::HandleCollision(b2Body *bodyA, b2Body *bodyB) if (bodyB->GetUserData().pointer == 5) { - //if collision with player + // if collision with player if (bodyA->GetUserData().pointer == 2) { // Entered Door diff --git a/game/src/ContactListener.hpp b/game/src/ContactListener.hpp index 2640967864790a2c83e48b37bdd779d7b2fd5080..fac37264f60fa6410ebad27bd3bd21870632478d 100644 --- a/game/src/ContactListener.hpp +++ b/game/src/ContactListener.hpp @@ -11,8 +11,9 @@ * in the Box2D physics world. It processes contact events, such as when two objects begin * or end contact, and organizes them for further handling (e.g., destroying bullets and enemies). */ -class ContactListener : public b2ContactListener { -public: +class ContactListener : public b2ContactListener +{ + public: /** * @brief Default constructor for the ContactListener class. */ @@ -25,54 +26,54 @@ public: /** * @brief Called when two bodies begin to contact. - * + * * This method is invoked by Box2D when two physics bodies start colliding. It allows for custom * behavior, such as detecting specific types of contacts (e.g., between bullets and enemies). * * @param contact A pointer to the contact object representing the collision. */ - void BeginContact(b2Contact* contact) override; + void BeginContact(b2Contact *contact) override; /** * @brief Called when two bodies end contact. - * + * * This method is triggered when two physics bodies stop colliding. Custom behavior can be added * here, such as cleanup actions after the collision ends. * * @param contact A pointer to the contact object representing the collision. */ - void EndContact(b2Contact* contact) override; + void EndContact(b2Contact *contact) override; /** * @brief A vector storing player bullets that should be destroyed after a collision. * * This vector is populated during contact events when bullets need to be removed from the physics world. */ - std::vector<b2Body*> playerBulletsToDestroy; + std::vector<b2Body *> playerBulletsToDestroy; /** * @brief A vector storing enemy bullets that should be destroyed after a collision. * * This vector is populated during contact events when bullets need to be removed from the physics world. */ - std::vector<b2Body*> enemyBulletsToDestroy; + std::vector<b2Body *> enemyBulletsToDestroy; /** * @brief A vector storing enemies that should be destroyed after a collision. * * This vector is populated during contact events when enemies need to be removed from the physics world. */ - std::vector<b2Body*> enemiesToDestroy; + std::vector<b2Body *> enemiesToDestroy; -private: + private: /** * @brief Handles the collision between two bodies. - * + * * This private method processes the collision between two physics bodies and decides whether * any specific action is required (e.g., destroying the bullet or the enemy). * * @param bodyA The first body involved in the collision. * @param bodyB The second body involved in the collision. */ - void HandleCollision(b2Body* bodyA, b2Body* bodyB); + void HandleCollision(b2Body *bodyA, b2Body *bodyB); }; diff --git a/game/src/DynamicObject.cpp b/game/src/DynamicObject.cpp index 60f460bff207edf79ab94eec3ffd286117641480..ccbca68a74dd1eed5d11b9d029f811aa6ddbd105 100644 --- a/game/src/DynamicObject.cpp +++ b/game/src/DynamicObject.cpp @@ -1,7 +1,8 @@ #include "DynamicObject.hpp" -DynamicObject::DynamicObject(b2World* world, const b2Vec2& position, const b2Vec2& size, float density, float scale, float friction, const std::vector<int>& tileIDs) - : GameObject(world, position, size, density, scale, friction, tileIDs) - { - m_body->SetLinearDamping(3.0f); - } \ No newline at end of file +DynamicObject::DynamicObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, + float friction, const std::vector<int> &tileIDs) + : GameObject(world, position, size, density, scale, friction, tileIDs) +{ + m_body->SetLinearDamping(3.0f); +} \ No newline at end of file diff --git a/game/src/DynamicObject.hpp b/game/src/DynamicObject.hpp index d3c4c0a5f31e4302187ee406c41d90d974ddae47..247f01df4cb287b88022f2594143ef276ba60b99 100644 --- a/game/src/DynamicObject.hpp +++ b/game/src/DynamicObject.hpp @@ -6,14 +6,15 @@ * @brief A class representing a dynamic object in the Box2D physics world. * * This class extends the `GameObject` class and adds functionality for objects that are affected by physics. - * It allows dynamic objects to interact with other objects in the physics world, including applying forces and responding to collisions. + * It allows dynamic objects to interact with other objects in the physics world, including applying forces and + * responding to collisions. */ -class DynamicObject : public GameObject +class DynamicObject : public GameObject { -public: + public: /** * @brief Constructs a DynamicObject with the specified parameters. - * + * * This constructor initializes a dynamic object in the provided Box2D world at a specified position, * with a given size, density, and friction. It also links the object to a game window for rendering and * a texture file for visual representation. @@ -26,5 +27,6 @@ public: * @param tilesheetManager The TilesheetManager object used for rendering the object's texture. * @param tileIDs The tile IDs representing the object's texture in the tilesheet. */ - DynamicObject(b2World* world, const b2Vec2& position, const b2Vec2& size, float density, float scale, float friction, const std::vector<int>& tileIDs); + DynamicObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, + float friction, const std::vector<int> &tileIDs); }; diff --git a/game/src/Enemy.cpp b/game/src/Enemy.cpp index 33f7f3d249cdaacaf967674b683cea4dfca8ab84..e61abf896562bc2395a2e84322d5d7a7d7479c26 100644 --- a/game/src/Enemy.cpp +++ b/game/src/Enemy.cpp @@ -2,21 +2,19 @@ #include <box2d/b2_math.h> Enemy::Enemy(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction, - BulletManager &bulletmanager, const std::vector<int>& tileIDs) - : DynamicObject(world, position, size, density, scale, friction, tileIDs), - m_lastFire(1000), m_fireRate(2), m_fireDelay(static_cast<Uint32>(std::round(1000.0 / m_fireRate))) + BulletManager &bulletmanager, const std::vector<int> &tileIDs) + : DynamicObject(world, position, size, density, scale, friction, tileIDs), m_lastFire(1000), m_fireRate(2), + m_fireDelay(static_cast<Uint32>(std::round(1000.0 / m_fireRate))) { m_body->GetUserData().pointer = 4; // 4 = Enemy (idenfier for contact listener) } - -bool Enemy::shoot(const b2Vec2& playerPosition, BulletManager& bulletManager) +bool Enemy::shoot(const b2Vec2 &playerPosition, BulletManager &bulletManager) { Uint32 currentTime = SDL_GetTicks(); if (currentTime - m_lastFire < m_fireDelay) { return false; - } b2Vec2 enemy_position = getPosition(); // offset the enemy position to avoid self collision -> now in BulletManager diff --git a/game/src/Enemy.hpp b/game/src/Enemy.hpp index ed004194228b461de8c6c94de3689968425fb6df..b07b141e2771ed8df98350ef87f1465550155fb1 100644 --- a/game/src/Enemy.hpp +++ b/game/src/Enemy.hpp @@ -1,6 +1,6 @@ #pragma once -#include "DynamicObject.hpp" #include "BulletManager.hpp" +#include "DynamicObject.hpp" /** * @class Enemy @@ -12,10 +12,10 @@ */ class Enemy : public DynamicObject { -public: + public: /** * @brief Constructs an Enemy object with the specified parameters. - * + * * This constructor initializes an enemy in the provided Box2D world at the given position, * with the specified size, density, and friction. Additionally, it links the enemy to projectiles * and a game window for rendering, along with a texture for the enemy's visual representation. @@ -29,40 +29,40 @@ public: * @param tilesheetManager The TilesheetManager object used for rendering the enemy's texture. * @param tileIDs The tile IDs representing the enemy's texture in the tilesheet. */ - Enemy(b2World* world, const b2Vec2& position, const b2Vec2& size, float density, float scale, float friction, BulletManager& bulletmanager, const std::vector<int>& tileIDs); + Enemy(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction, + BulletManager &bulletmanager, const std::vector<int> &tileIDs); /** * @brief Retrieves the Enemy's physics body. - * + * * This method returns the underlying Box2D `b2Body` representing the enemy, allowing interaction * with the physics world (e.g., applying forces or detecting collisions). * * @return The `b2Body` associated with the enemy in the physics world. */ - b2Body* getBody(); + b2Body *getBody(); /** * @brief Shoots a projectile from the enemy towards the player. - * + * * This method creates a new projectile at the enemy's position and fires it towards the player's * current position. The projectile is added to the `BulletManager` object for tracking and rendering. */ - bool shoot(const b2Vec2& playerPosition, BulletManager& bulletManager); + bool shoot(const b2Vec2 &playerPosition, BulletManager &bulletManager); /** * @brief Sets the enemy's fire rate. - * + * * This method adjusts the number of bullets the enemy can fire per second. * * @param rate The fire rate in bullets per second. */ void set_fireRate(int rate); - -private: - int health; /**< The health of the enemy, representing its hit points. */ - int damage; /**< The amount of damage the enemy can inflict on other objects. */ - int m_fireRate; /**< The rate at which the enemy can fire projectiles. */ - Uint32 m_fireDelay; /**< The delay between shots in milliseconds. */ - Uint32 m_lastFire; /**< The timestamp of the last shot fired. */ + private: + int health; /**< The health of the enemy, representing its hit points. */ + int damage; /**< The amount of damage the enemy can inflict on other objects. */ + int m_fireRate; /**< The rate at which the enemy can fire projectiles. */ + Uint32 m_fireDelay; /**< The delay between shots in milliseconds. */ + Uint32 m_lastFire; /**< The timestamp of the last shot fired. */ }; diff --git a/game/src/GameContext.hpp b/game/src/GameContext.hpp index 828e8c40d685a6e0f18fa60ee0fc599d9afa9dd5..8ad4df917ca72d9af75c2757c91f039853987acf 100644 --- a/game/src/GameContext.hpp +++ b/game/src/GameContext.hpp @@ -10,7 +10,8 @@ class GameContext { public: - GameContext(lvl::Level& level, EnemyManager &enemy_manager, BulletManager &bullet_manager, TilesheetManager& tilesheet_manager, ObjectManager& object_manager); + GameContext(lvl::Level &level, EnemyManager &enemy_manager, BulletManager &bullet_manager, + TilesheetManager &tilesheet_manager, ObjectManager &object_manager); ~GameContext(); int32_t w() const; @@ -23,12 +24,11 @@ class GameContext lvl::Room &getCurrentRoom(); private: - lvl::Room m_current_room; - lvl::Level& m_level; - BulletManager& m_bulletManager; - EnemyManager& m_enemyManager; - ObjectManager& m_objectManager; - TilesheetManager& m_tilesheetManager; + lvl::Level &m_level; + BulletManager &m_bulletManager; + EnemyManager &m_enemyManager; + ObjectManager &m_objectManager; + TilesheetManager &m_tilesheetManager; int m_tileSize; }; \ No newline at end of file diff --git a/game/src/ObjectManager.cpp b/game/src/ObjectManager.cpp index 6f7c90c0e88f32dd7bb28646abd3d8e6ec4c7a4d..48217c2794db0bf810ab32ddd5990537f24aba85 100644 --- a/game/src/ObjectManager.cpp +++ b/game/src/ObjectManager.cpp @@ -16,8 +16,8 @@ ObjectManager::~ObjectManager() void ObjectManager::addDoor(const b2Vec2 &position) { - StaticObject *new_object = new StaticObject(m_world, position, b2Vec2(16.0f, 16.0f), m_tilesheetManager.getGameScale(), 0.0f, - std::vector<int>{-1}); + StaticObject *new_object = new StaticObject(m_world, position, b2Vec2(16.0f, 16.0f), + m_tilesheetManager.getGameScale(), 0.0f, std::vector<int>{-1}); new_object->getBody()->GetUserData().pointer = 5; // 5 = Door (idenfier for contact listener) m_doors.emplace_back(new_object); } \ No newline at end of file diff --git a/game/src/ObjectManager.hpp b/game/src/ObjectManager.hpp index 4ddfcc3f86a06824ef80273b7757ea1ed696c936..71b9c30ea5f0f9419097844c0399243bfedd8fb8 100644 --- a/game/src/ObjectManager.hpp +++ b/game/src/ObjectManager.hpp @@ -4,12 +4,11 @@ #include "level.hpp" #include <box2d/box2d.h> - class ObjectManager { private: b2World *m_world; /**< The Box2D world in which the objects exist. */ - std::vector<StaticObject *> m_doors; /**< A collection of all active objects in the game. */ + std::vector<StaticObject *> m_doors; /**< A collection of all active objects in the game. */ TilesheetManager &m_tilesheetManager; /**< The tilesheet manager used for rendering the objects. */ public: @@ -40,5 +39,4 @@ class ObjectManager * @param position The position where the new object will be placed in the world. */ void addDoor(const b2Vec2 &position); - }; \ No newline at end of file diff --git a/game/src/Player.cpp b/game/src/Player.cpp index 7bc331e50da838bead5f7ac7938809b2da6c2052..9f96bf9e150d6814fe2b2e83248a6beb7676a37a 100644 --- a/game/src/Player.cpp +++ b/game/src/Player.cpp @@ -1,19 +1,19 @@ #include "Player.hpp" Player::Player(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction, - BulletManager& bulletmanager, TilesheetManager& tilesheetManager, const std::vector<int>& tileIDs) - : DynamicObject(world, position, size, density, scale, friction, tileIDs), - m_alive(true), m_bulletManager(bulletmanager), m_lastFire(1000), m_fireRate(5), + BulletManager &bulletmanager, TilesheetManager &tilesheetManager, const std::vector<int> &tileIDs) + : DynamicObject(world, position, size, density, scale, friction, tileIDs), m_alive(true), + m_bulletManager(bulletmanager), m_lastFire(1000), m_fireRate(5), m_fireDelay(static_cast<Uint32>(std::round(1000.0 / m_fireRate))) { m_body->GetUserData().pointer = 2; // 2 = Player (idenfier for contact listener) } -Player::~Player() { +Player::~Player() +{ // Empty implementation, but needed for vtable generation } - void Player::handle_input() { // Get the current state of the keyboard @@ -60,28 +60,28 @@ void Player::handle_input() { // Shoot left m_bulletManager.addPlayerProjectile({this->getPosition().x - m_size.x, this->getPosition().y}, - {-10.0f, 0.0f}); + {-10.0f, 0.0f}); m_lastFire = SDL_GetTicks(); } else if (state[SDL_SCANCODE_RIGHT]) { // Shoot right m_bulletManager.addPlayerProjectile({this->getPosition().x + m_size.x, this->getPosition().y}, - {+10.0f, 0.0f}); + {+10.0f, 0.0f}); m_lastFire = SDL_GetTicks(); } else if (state[SDL_SCANCODE_UP]) { // Shoot up m_bulletManager.addPlayerProjectile({this->getPosition().x, this->getPosition().y - m_size.y}, - {0.0f, -10.0f}); + {0.0f, -10.0f}); m_lastFire = SDL_GetTicks(); } else if (state[SDL_SCANCODE_DOWN]) { // Shoot down m_bulletManager.addPlayerProjectile({this->getPosition().x, this->getPosition().y + m_size.y}, - {0.0f, +10.0f}); + {0.0f, +10.0f}); m_lastFire = SDL_GetTicks(); } } diff --git a/game/src/Player.hpp b/game/src/Player.hpp index 453a952de3f9e25e7b86aadb9d186ce0412c4865..9f4a8d9cecb45d5ffb287c58a30d483ad53741fd 100644 --- a/game/src/Player.hpp +++ b/game/src/Player.hpp @@ -1,7 +1,7 @@ #pragma once +#include "BulletManager.hpp" #include "DynamicObject.hpp" #include <SDL2/SDL.h> -#include "BulletManager.hpp" /** * @class Player @@ -11,11 +11,11 @@ * including handling input, tracking health, managing fire rate, and interacting with the game world. */ class Player : public DynamicObject -{ -public: +{ + public: /** * @brief Constructs a Player object with the specified parameters for physics and rendering. - * + * * This constructor initializes the player with the provided Box2D world, position, size, density, * friction, projectiles, game window, and texture file for rendering. * @@ -28,28 +28,30 @@ public: * @param tilesheetManager The TilesheetManager object used for rendering the player's texture. * @param tileIDs The tile IDs representing the player's texture in the tilesheet. */ - Player(b2World* world, const b2Vec2& position, const b2Vec2& size, float density, float scale, float friction, BulletManager& bulletmanager, TilesheetManager& tilesheetManager, const std::vector<int>& tileIDs); + Player(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction, + BulletManager &bulletmanager, TilesheetManager &tilesheetManager, const std::vector<int> &tileIDs); /** * @brief Destructor for the Player class. - * + * * This destructor handles the cleanup of resources associated with the player object. */ virtual ~Player(); /** * @brief Handles user input for player movement and actions. - * + * * This method processes input events (such as keyboard or mouse) to control the player's movement and actions * in the game. It also updates the state of the player based on the input, such as firing projectiles. * - * @param m_running A flag indicating if the game is still running, which can be set to `false` if the game should end. + * @param m_running A flag indicating if the game is still running, which can be set to `false` if the game should + * end. */ void handle_input(); /** * @brief Checks if the player is still alive. - * + * * This method checks whether the player's health is greater than 0, indicating if the player is still alive. * * @return `true` if the player is alive, `false` otherwise. @@ -58,7 +60,7 @@ public: /** * @brief Sets the player's fire rate. - * + * * This method adjusts the number of bullets the player can fire per second, controlling how frequently projectiles * can be fired. * @@ -66,11 +68,11 @@ public: */ void set_fireRate(int rate); -private: - bool m_alive; /**< A flag indicating if the player is alive. */ - int m_health; /**< The player's health value, determines if the player is alive. */ - int m_fireRate; /**< The player's fire rate (bullets per second). */ - Uint32 m_fireDelay; /**< The delay between shots in milliseconds. */ - Uint32 m_lastFire; /**< The timestamp of the last shot fired. */ - BulletManager& m_bulletManager; /**< A reference to the BulletManager object used for managing fired projectiles. */ + private: + bool m_alive; /**< A flag indicating if the player is alive. */ + int m_health; /**< The player's health value, determines if the player is alive. */ + int m_fireRate; /**< The player's fire rate (bullets per second). */ + Uint32 m_fireDelay; /**< The delay between shots in milliseconds. */ + Uint32 m_lastFire; /**< The timestamp of the last shot fired. */ + BulletManager &m_bulletManager; /**< A reference to the BulletManager object used for managing fired projectiles. */ }; diff --git a/game/src/Renderable.cpp b/game/src/Renderable.cpp index 4e0c5cb22bb046c5a566472f173d59cc35575fc2..25ada0702a4fba514678cfa93a4c01fd595a65fb 100644 --- a/game/src/Renderable.cpp +++ b/game/src/Renderable.cpp @@ -4,11 +4,11 @@ Renderable::Renderable(GameWindow *window, const std::string &filePath) : m_window(window), m_renderer(window->renderer()) { - //check if filePath ends with .png (else its animated) + // check if filePath ends with .png (else its animated) if (filePath.rfind(".png") == (filePath.size() - 4)) { - m_texture = LoadTexture(m_renderer, filePath); - computeSourceRect(); + m_texture = LoadTexture(m_renderer, filePath); + computeSourceRect(); } } @@ -23,7 +23,7 @@ Renderable::~Renderable() SDL_Texture *Renderable::LoadTexture(SDL_Renderer *renderer, std::string texFileName) { - //Check if SDL Image Support is available + // Check if SDL Image Support is available if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) { std::cout << "SDL_image could not initialize PNG support! SDL_image Error: " << IMG_GetError() << std::endl; diff --git a/game/src/Renderable.hpp b/game/src/Renderable.hpp index 856c4321537afc7d5337677d778ba780a004380d..d5e3b3dc3aaa664eaa126583d8cc06866300274c 100644 --- a/game/src/Renderable.hpp +++ b/game/src/Renderable.hpp @@ -1,10 +1,10 @@ #pragma once +#include "GameWindow.hpp" #include <SDL2/SDL.h> -#include <string> -#include <iostream> -#include <fstream> #include <cstdint> -#include "GameWindow.hpp" +#include <fstream> +#include <iostream> +#include <string> /** * @class Renderable @@ -15,10 +15,10 @@ */ class Renderable { -public: + public: /** * @brief Constructs a Renderable object with the given window and texture file path. - * + * * This constructor initializes the renderable object, setting up the window and loading the texture for rendering. * * @param window The game window that will be used for rendering. @@ -28,22 +28,22 @@ public: /** * @brief Destructor for the Renderable class. - * + * * This destructor cleans up resources associated with the renderable object, such as the SDL_Texture. */ ~Renderable(); /** * @brief Renders the object to the screen. - * - * This method performs the actual rendering of the object to the screen using the loaded texture and target rectangle. - * The method is virtual to allow for customization in derived classes. + * + * This method performs the actual rendering of the object to the screen using the loaded texture and target + * rectangle. The method is virtual to allow for customization in derived classes. */ virtual void render(); /** * @brief Gets the width of the renderable object. - * + * * This method retrieves the width of the renderable object's texture. * * @return The width of the texture. @@ -52,7 +52,7 @@ public: /** * @brief Gets the height of the renderable object. - * + * * This method retrieves the height of the renderable object's texture. * * @return The height of the texture. @@ -61,7 +61,7 @@ public: /** * @brief Gets the x-coordinate of the renderable object's position. - * + * * This method retrieves the x-coordinate of the object's position, used for rendering placement. * * @return The x-coordinate of the object's position. @@ -70,7 +70,7 @@ public: /** * @brief Gets the y-coordinate of the renderable object's position. - * + * * This method retrieves the y-coordinate of the object's position, used for rendering placement. * * @return The y-coordinate of the object's position. @@ -79,7 +79,7 @@ public: /** * @brief Loads an SDL texture from a file. - * + * * This method loads a texture from the specified file path, with the option to specify a transparency color (RGB). * The loaded texture can then be used for rendering. * @@ -88,21 +88,21 @@ public: * @param key_r Transparency color, red channel. * @param key_g Transparency color, green channel. * @param key_b Transparency color, blue channel. - * + * * @return The loaded SDL_Texture, or NULL if an error occurs. */ SDL_Texture *LoadTexture(SDL_Renderer *renderer, std::string filename); -protected: - GameWindow *m_window; /**< A pointer to the game window used for rendering. */ - SDL_Renderer *m_renderer; /**< A pointer to the SDL renderer used for rendering the texture. */ - SDL_Texture *m_texture; /**< A pointer to the SDL texture used for rendering the object. */ - SDL_Rect m_sourceRect; /**< A rectangle representing the part of the texture to be rendered. */ - SDL_Rect m_targetRect; /**< A rectangle representing where the texture will be rendered on the screen. */ + protected: + GameWindow *m_window; /**< A pointer to the game window used for rendering. */ + SDL_Renderer *m_renderer; /**< A pointer to the SDL renderer used for rendering the texture. */ + SDL_Texture *m_texture; /**< A pointer to the SDL texture used for rendering the object. */ + SDL_Rect m_sourceRect; /**< A rectangle representing the part of the texture to be rendered. */ + SDL_Rect m_targetRect; /**< A rectangle representing where the texture will be rendered on the screen. */ /** * @brief Computes the source rectangle for texture rendering. - * + * * This method calculates the source rectangle, which determines the portion of the texture that will be rendered. * It is typically used when the texture is part of a larger sprite sheet. */ diff --git a/game/src/StaticObject.hpp b/game/src/StaticObject.hpp index 7df98fb4b49d20700558c25d93837fa4ccadf779..e98c59c748ba60c73cf699fa2093516006c3d901 100644 --- a/game/src/StaticObject.hpp +++ b/game/src/StaticObject.hpp @@ -4,17 +4,17 @@ /** * @class StaticObject * @brief A class representing a static object in the game world. - * + * * The `StaticObject` class is derived from the `GameObject` class and represents an object in the game world * that does not move. It is primarily used for creating immovable objects such as walls, obstacles, and boundaries * that interact with dynamic objects but do not change their position. */ -class StaticObject : public GameObject +class StaticObject : public GameObject { -public: + public: /** * @brief Constructs a StaticObject with the specified parameters. - * + * * This constructor initializes the static object by setting up its size, position, and physical properties. * The static object is added to the physics world and its texture is loaded for rendering. * @@ -25,11 +25,12 @@ public: * @param tilesheetManager The tilesheet manager for rendering the static object. * @param tileIDs The list of tile IDs representing the texture of the static object. */ - StaticObject(b2World* world, const b2Vec2& position, const b2Vec2& size, float scale, float friction, const std::vector<int>& tileIDs); + StaticObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float scale, float friction, + const std::vector<int> &tileIDs); void render(); - /** + /** * @brief Retrieves the BulletObject's physics body. * * This method returns the underlying Box2D `b2Body` associated with the bullet, diff --git a/game/src/TilesheetManager.cpp b/game/src/TilesheetManager.cpp index 94505f0b32e5ac3f65b2465f574f056f55e037f8..bb5744b29e46596951b7380d4ab74164d8ae9e40 100644 --- a/game/src/TilesheetManager.cpp +++ b/game/src/TilesheetManager.cpp @@ -20,7 +20,7 @@ void TilesheetManager::loadTexture(SDL_Renderer *renderer) const std::vector<lvl::Spritesheet::Byte> &data = m_Spritesheet.getSpritedata(); - //std::cout << "Data size: " << data.size() << std::endl; + // std::cout << "Data size: " << data.size() << std::endl; int width = m_Spritesheet.getWidth(); int height = m_Spritesheet.getHeight(); diff --git a/game/src/Vector.hpp b/game/src/Vector.hpp index 4acc9003feebc03bf66923953acd6320e5550216..777bef5dc678231db6254bd99be16182e246f93b 100644 --- a/game/src/Vector.hpp +++ b/game/src/Vector.hpp @@ -11,15 +11,12 @@ #pragma once #include <iostream> - /** * @brief A class to represent a Vector */ -template<typename T> -class Vector +template <typename T> class Vector { -public: - + public: /** * Constructor * @param x x coordinate of the Vector @@ -32,7 +29,7 @@ public: * * @param other The Vector that is copied */ - Vector(const Vector<T>& other); + Vector(const Vector<T> &other); /** * Destructor @@ -45,7 +42,7 @@ public: * @param other The Vector that is copied into this Vector * @return The modified instance. */ - Vector<T> operator=(const Vector<T>& other); + Vector<T> operator=(const Vector<T> &other); /** * Adds an offset encoded in the other Vector @@ -53,14 +50,14 @@ public: * @param other A Vector offset added to the current position * @return A modified Vector */ - Vector<T> operator+(const Vector<T> & other) const; + Vector<T> operator+(const Vector<T> &other) const; /** * Adds an offset encoded in the other Vector * * @param other A Vector offset added to the current position */ - void operator+=(const Vector<T> & other); + void operator+=(const Vector<T> &other); /** * Adds an offset encoded in the other Vector @@ -68,15 +65,14 @@ public: * @param other A Vector offset added to the current position * @return A modified Vector */ - Vector operator-(const Vector<T> & other) const; + Vector operator-(const Vector<T> &other) const; /** * Adds an offset encoded in the other Vector * * @param other A Vector offset added to the current position */ - void operator-=(const Vector<T> & other); - + void operator-=(const Vector<T> &other); /** * Scales the Vector @@ -84,13 +80,13 @@ public: * @param other A T with which the Vector should be scaled * @return A modified Vector */ - Vector<T> operator*(const T& other) const; + Vector<T> operator*(const T &other) const; /** * * @param Scalar with which to multiply */ - void operator*=(const T& other); + void operator*=(const T &other); /** * Scales the Vector @@ -98,8 +94,7 @@ public: * @param other Dot product between two vectors * @return A modified Vector */ - Vector<T> operator*(const Vector<T> & other) const; - + Vector<T> operator*(const Vector<T> &other) const; /// Returns the current x value T x() const; @@ -114,16 +109,15 @@ public: void setY(T y); /// Stream output operator - template<typename A> - friend std::ostream& operator<< (std::ostream& stream, const Vector<A> & vec); + template <typename A> friend std::ostream &operator<<(std::ostream &stream, const Vector<A> &vec); /// Unit vector in x direction static Vector UnitX; /// Unit vector in y direction static Vector UnitY; -private: + private: /// x coordinate of the Vector T m_x; @@ -132,10 +126,9 @@ private: }; /// Operator to allow left sided scaling -template<typename T> -Vector<T> operator* (T s, const Vector<T>& v); +template <typename T> Vector<T> operator*(T s, const Vector<T> &v); -typedef Vector<double> Vector2f; -typedef Vector<int> Pixel; +typedef Vector<double> Vector2f; +typedef Vector<int> Pixel; #include "Vector.tcc" \ No newline at end of file diff --git a/game/src/Vector.tcc b/game/src/Vector.tcc index 3c19ca3a4492b868e6bb7eca57f77d165723d92b..3227f5b8754050683fe9ca5839a5a66724b2f840 100644 --- a/game/src/Vector.tcc +++ b/game/src/Vector.tcc @@ -8,48 +8,38 @@ * No unauthorized distribution. */ +template <typename T> Vector<T> Vector<T>::UnitX = Vector<T>(1, 0); -template<typename T> -Vector<T> Vector<T>::UnitX = Vector<T>(1, 0); +template <typename T> Vector<T> Vector<T>::UnitY = Vector<T>(0, 1); -template<typename T> -Vector<T> Vector<T>::UnitY = Vector<T>(0, 1); - -template<typename T> -std::ostream& operator<< (std::ostream& stream, const Vector<T> & vec) +template <typename T> std::ostream &operator<<(std::ostream &stream, const Vector<T> &vec) { return stream << "(" << vec.x() << "," << vec.y() << ")" << std::endl; } -template<typename T> -Vector<T> operator* (T s, const Vector<T>& v) +template <typename T> Vector<T> operator*(T s, const Vector<T> &v) { - return Vector<T>(v * s); + return Vector<T>(v * s); } -template<typename T> -Vector<T>::Vector(T x, T y) - : m_x(x), m_y(y) +template <typename T> Vector<T>::Vector(T x, T y) : m_x(x), m_y(y) { } -template<typename T> -Vector<T>::Vector(const Vector<T> & other) +template <typename T> Vector<T>::Vector(const Vector<T> &other) { m_x = other.m_x; m_y = other.m_y; } -template<typename T> -Vector<T>::~Vector<T>() +template <typename T> Vector<T>::~Vector<T>() { // TODO Auto-generated destructor stub } -template<typename T> -Vector<T> Vector<T>::operator =(const Vector<T> & other) +template <typename T> Vector<T> Vector<T>::operator=(const Vector<T> &other) { - if(this != &other) + if (this != &other) { m_x = other.m_x; m_y = other.m_y; @@ -57,69 +47,59 @@ Vector<T> Vector<T>::operator =(const Vector<T> & other) return *this; } -template<typename T> -Vector<T> Vector<T>::operator +(const Vector<T> & other) const +template <typename T> Vector<T> Vector<T>::operator+(const Vector<T> &other) const { Vector<T> p(*this); p += other; return p; } -template<typename T> -void Vector<T>::operator +=(const Vector<T> & other) +template <typename T> void Vector<T>::operator+=(const Vector<T> &other) { m_x += other.m_x; m_y += other.m_y; } -template<typename T> -Vector<T> Vector<T>::operator *(const T& scalar) const +template <typename T> Vector<T> Vector<T>::operator*(const T &scalar) const { Vector<T> p(this->m_x * scalar, this->m_y * scalar); return p; } -template<typename T> -void Vector<T>::operator *=(const T& scalar) +template <typename T> void Vector<T>::operator*=(const T &scalar) { m_x *= scalar; m_y *= scalar; } -template<typename T> -T Vector<T>::x() const +template <typename T> T Vector<T>::x() const { return m_x; } -template<typename T> -T Vector<T>::y() const +template <typename T> T Vector<T>::y() const { return m_y; } -template<typename T> -void Vector<T>::setX(T x) +template <typename T> void Vector<T>::setX(T x) { m_x = x; } -template<typename T> -void Vector<T>::setY(T y) +template <typename T> void Vector<T>::setY(T y) { m_y = y; } -template<typename T> -Vector<T> Vector<T>::operator-(const Vector<T> &other) const +template <typename T> Vector<T> Vector<T>::operator-(const Vector<T> &other) const { Vector<T> p(*this); p -= other; return p; } -template<typename T> -void Vector<T>::operator-=(const Vector<T> &other) +template <typename T> void Vector<T>::operator-=(const Vector<T> &other) { m_x -= other.m_x; m_y -= other.m_y; diff --git a/liblvl/src/enemy.cpp b/liblvl/src/enemy.cpp index 9136d4510d2161aa248d5c0efe9b8696d67f6b4f..b245bd072d55243ce0aea793bce75b9efc3b8aaa 100644 --- a/liblvl/src/enemy.cpp +++ b/liblvl/src/enemy.cpp @@ -7,7 +7,9 @@ namespace lvl { Enemy::Enemy(EnemyId id, int32_t health, int hitboxWidth, int hitboxHeight, std::vector<int32_t> spriteIndices) - : m_Id(id), m_Health(health), m_Hitbox({hitboxWidth, hitboxHeight}), m_Sprites(std::move(spriteIndices)) {} + : m_Id(id), m_Health(health), m_Hitbox({hitboxWidth, hitboxHeight}), m_Sprites(std::move(spriteIndices)) +{ +} Position Enemy::getHitbox() const { @@ -24,17 +26,15 @@ int32_t Enemy::getHealth() const return m_Health; } -const std::vector<int32_t>& Enemy::getSprites() const +const std::vector<int32_t> &Enemy::getSprites() const { return m_Sprites; } std::vector<Enemy> Enemy::defaultEnemies() { - return {Enemy(0, 30, 16, 16, {22, 23, 24, 25}), - Enemy(1, 50, 16, 16, {86, 87, 88, 89}), - Enemy(2, 70, 16, 16, {146, 147, 148, 149}) - }; + return {Enemy(0, 30, 16, 16, {22, 23, 24, 25}), Enemy(1, 50, 16, 16, {86, 87, 88, 89}), + Enemy(2, 70, 16, 16, {146, 147, 148, 149})}; } } // namespace lvl \ No newline at end of file diff --git a/liblvl/src/formatexception.cpp b/liblvl/src/formatexception.cpp index 6502f8724b52c0d6979d61259bc649765789419f..4dba2900a388ad48c4356254aafcf50115633081 100644 --- a/liblvl/src/formatexception.cpp +++ b/liblvl/src/formatexception.cpp @@ -19,4 +19,4 @@ const char *FormatException::what() const noexcept return m_FullError.c_str(); } -} \ No newline at end of file +} // namespace lvl \ No newline at end of file diff --git a/liblvl/src/include/enemy.hpp b/liblvl/src/include/enemy.hpp index 347fe5970ca88d7d0a96d176b2adc774f1d72f69..9ba87df5d9bd994781fcf41feaf480edeae975da 100644 --- a/liblvl/src/include/enemy.hpp +++ b/liblvl/src/include/enemy.hpp @@ -2,22 +2,22 @@ // Created by Leon Lux on 29.01.25. // #pragma once +#include "spritesheet.hpp" #include <cstdint> -#include <vector> -#include <position.hpp> #include <highfive/H5File.hpp> -#include "spritesheet.hpp" +#include <position.hpp> +#include <vector> namespace lvl { /** - * @brief Represents an Enemy for the Game. - */ + * @brief Represents an Enemy for the Game. + */ class Enemy { -public: + public: /** * @brief Type to uniquely identify an enemy. */ @@ -29,46 +29,47 @@ public: static std::vector<Enemy> defaultEnemies(); /** - * @brief Return the Hitbox of the Enemy. - */ + * @brief Return the Hitbox of the Enemy. + */ Position getHitbox() const; /** - * @brief Return position of enemy-sprites in tilesheet - */ + * @brief Return position of enemy-sprites in tilesheet + */ std::vector<Position> getSpritePositions(const Spritesheet &sheet) const; /** - * @brief Return the Enemy ID. - */ + * @brief Return the Enemy ID. + */ EnemyId getId() const; - + /** - * @brief Return the Enemies Health - */ + * @brief Return the Enemies Health + */ int32_t getHealth() const; - + /** - * @brief Return the Enemies Sprite Data. - */ - const std::vector<int32_t>& getSprites() const; + * @brief Return the Enemies Sprite Data. + */ + const std::vector<int32_t> &getSprites() const; + private: /** - * @brief Unique Identifier for the Enemy. - */ + * @brief Unique Identifier for the Enemy. + */ EnemyId m_Id; /** - * @brief Starting Health of the Enemy. - */ + * @brief Starting Health of the Enemy. + */ int32_t m_Health; /** - * @brief Hitbox of the Enemy. - */ + * @brief Hitbox of the Enemy. + */ Position m_Hitbox; /** - * @brief Animation / Sprite of the Enemy. - */ + * @brief Animation / Sprite of the Enemy. + */ std::vector<int32_t> m_Sprites; }; -} +} // namespace lvl diff --git a/liblvl/src/include/formatexception.hpp b/liblvl/src/include/formatexception.hpp index b22384640887cdda0386d8627f26792fbf106db8..02994afa0b3bfc1047dd3949b3fcdc10afc0b976 100644 --- a/liblvl/src/include/formatexception.hpp +++ b/liblvl/src/include/formatexception.hpp @@ -2,35 +2,36 @@ // Created by Leon Lux on 31.01.25. // #pragma once +#include <levelexception.hpp> +#include <sstream> #include <stdexcept> #include <string> -#include <sstream> -#include <levelexception.hpp> namespace lvl { /** -* @brief FormatException signifies an invalid HDF5 File. -*/ + * @brief FormatException signifies an invalid HDF5 File. + */ class FormatException : public LevelException { -public: - /** - * @brief New exception with an error message and a path in the HDF5 file. - * @param message Error message to display - * @param path Path in the HDF5 file where the error occured - */ - FormatException(const std::string& message, const std::string& path); - /** - * @brief Get the error message with description and the HDF5 Path that caused the error. - * @return The error Message. - */ - [[nodiscard]] - const char* what() const noexcept override; -private: - std::string m_Path; - std::string m_FullError; + public: + /** + * @brief New exception with an error message and a path in the HDF5 file. + * @param message Error message to display + * @param path Path in the HDF5 file where the error occured + */ + FormatException(const std::string &message, const std::string &path); + /** + * @brief Get the error message with description and the HDF5 Path that caused the error. + * @return The error Message. + */ + [[nodiscard]] + const char *what() const noexcept override; + + private: + std::string m_Path; + std::string m_FullError; }; -} +} // namespace lvl diff --git a/liblvl/src/include/hitbox.hpp b/liblvl/src/include/hitbox.hpp index f8b49a18b42b8d3b4b5a93e906e9ee1b10c0b69c..d81d4029389d4a3b5abe493386935aa166015dbf 100644 --- a/liblvl/src/include/hitbox.hpp +++ b/liblvl/src/include/hitbox.hpp @@ -6,8 +6,8 @@ namespace lvl { /** -* @brief Represents a Hitbox -*/ + * @brief Represents a Hitbox + */ struct Hitbox { /** @@ -20,4 +20,4 @@ struct Hitbox int height; }; -} +} // namespace lvl diff --git a/liblvl/src/include/level.hpp b/liblvl/src/include/level.hpp index e1ec93528afd5cd98d75a164c6d348424077c2b6..e45346943147e69e815473ac5125b7276a32621e 100644 --- a/liblvl/src/include/level.hpp +++ b/liblvl/src/include/level.hpp @@ -1,15 +1,15 @@ #pragma once +#include <enemy.hpp> #include <filesystem> +#include <formatexception.hpp> #include <highfive/highfive.hpp> #include <hitbox.hpp> -#include <enemy.hpp> +#include <levelexception.hpp> #include <object.hpp> +#include <player.hpp> #include <position.hpp> -#include <spritesheet.hpp> #include <room.hpp> -#include <player.hpp> -#include <levelexception.hpp> -#include <formatexception.hpp> +#include <spritesheet.hpp> #include <tileindexexception.hpp> namespace lvl @@ -19,32 +19,33 @@ namespace fs = std::filesystem; class Level { -public: - Level(const fs::path& path, std::size_t tileSize); - explicit Level(const fs::path& path); + public: + Level(const fs::path &path, std::size_t tileSize); + explicit Level(const fs::path &path); ~Level() = default; - void saveChanges(const fs::path& path) const; + void saveChanges(const fs::path &path) const; + + const Spritesheet &getSpritesheet() const; + std::vector<Enemy> &getEnemies(); + std::vector<Room> &getRooms(); + Player &getPlayer(); - const Spritesheet& getSpritesheet() const; - std::vector<Enemy>& getEnemies(); - std::vector<Room>& getRooms(); - Player& getPlayer(); -private: + private: std::vector<Enemy> m_Enemies; std::vector<Room> m_Rooms; std::vector<Object> m_Objects; Spritesheet m_Spritesheet; Player m_Player; - void saveEnemies(HighFive::File& file) const; - void loadEnemies(HighFive::File& file); - void saveRooms(HighFive::File& file) const; - void loadRooms(HighFive::File& file); - void savePlayer(HighFive::File& file) const; - void loadPlayer(HighFive::File& file); - void saveObjects(HighFive::File& file) const; - void loadObjects(const HighFive::File& file); + void saveEnemies(HighFive::File &file) const; + void loadEnemies(HighFive::File &file); + void saveRooms(HighFive::File &file) const; + void loadRooms(HighFive::File &file); + void savePlayer(HighFive::File &file) const; + void loadPlayer(HighFive::File &file); + void saveObjects(HighFive::File &file) const; + void loadObjects(const HighFive::File &file); }; -} \ No newline at end of file +} // namespace lvl \ No newline at end of file diff --git a/liblvl/src/include/levelexception.hpp b/liblvl/src/include/levelexception.hpp index 6370437691bbc23abf040eb1be0ddbab10f5734d..80c51a934f9bd7dbb0995e963704a5a372848e23 100644 --- a/liblvl/src/include/levelexception.hpp +++ b/liblvl/src/include/levelexception.hpp @@ -9,28 +9,28 @@ namespace lvl { /** -* @brief Root of the Exception Hierarchy. Can be used to catch all errors related to the Library. -*/ + * @brief Root of the Exception Hierarchy. Can be used to catch all errors related to the Library. + */ class LevelException : public std::exception { -public: - /** - * @brief Create a new Exception with the given message. - * @param msg Message to display. - */ - explicit LevelException(const std::string& msg); - virtual ~LevelException() = default; + public: + /** + * @brief Create a new Exception with the given message. + * @param msg Message to display. + */ + explicit LevelException(const std::string &msg); + virtual ~LevelException() = default; - /** - * @brief Return the underlying error message. - * @return The error message of this exception. - */ - [[nodiscard]] - const char* what() const noexcept override; -private: - std::string m_Message; + /** + * @brief Return the underlying error message. + * @return The error message of this exception. + */ + [[nodiscard]] + const char *what() const noexcept override; + private: + std::string m_Message; }; -} +} // namespace lvl diff --git a/liblvl/src/include/object.hpp b/liblvl/src/include/object.hpp index ce9080b9269f2a095d20d1b5624133e52f7d2389..fb2e1633a43fcc43e0d0a39c558a183f35fbe3a5 100644 --- a/liblvl/src/include/object.hpp +++ b/liblvl/src/include/object.hpp @@ -2,19 +2,19 @@ // Created by Leon Lux on 29.01.25. // #pragma once -#include <vector> -#include <string> #include <hitbox.hpp> +#include <string> +#include <vector> namespace lvl { /** -* @brief Object represents a collidable Object in the level. -*/ + * @brief Object represents a collidable Object in the level. + */ class Object { -public: + public: /** * @brief Initialize an object with all its members * @param id Id of the object. @@ -23,7 +23,7 @@ public: * @param height Height of the Objects Hitbox. * @param spriteIds Vector containing Sprite IDs for the object. */ - Object(int id, const std::string& name, int width, int height, const std::vector<int32_t>& spriteIds); + Object(int id, const std::string &name, int width, int height, const std::vector<int32_t> &spriteIds); ~Object() = default; /** @@ -35,19 +35,19 @@ public: * @brief Get the name of the Object * @return Name of the Object */ - const std::string& getName() const; + const std::string &getName() const; /** * @brief Get the Objects Hitbox * @return Hitbox of the Object */ - const Hitbox& getHitbox() const; + const Hitbox &getHitbox() const; /** * @brief Return the Sprite IDs of the Object * @return Vector containing Sprite IDs */ - const std::vector<int32_t>& getSpriteIds() const; + const std::vector<int32_t> &getSpriteIds() const; /** * @brief Return a small default selection of Objects. @@ -55,7 +55,7 @@ public: */ static std::vector<Object> defaultObjects(); -private: + private: /** * @brief Unique ID of the Object */ @@ -75,4 +75,4 @@ private: std::vector<int32_t> m_SpriteIds; }; -} +} // namespace lvl diff --git a/liblvl/src/include/player.hpp b/liblvl/src/include/player.hpp index d6de8dc223a972fdbf161439b3319f228349b47b..f51ba28e9efbb63209a57a47a75de4df586029ab 100644 --- a/liblvl/src/include/player.hpp +++ b/liblvl/src/include/player.hpp @@ -9,18 +9,19 @@ namespace lvl class Player { -public: + public: Player(); - Player(int health, const std::vector<int>& sprites, const std::vector<int>& bullets); + Player(int health, const std::vector<int> &sprites, const std::vector<int> &bullets); ~Player() = default; int getHealth() const; - const std::vector<int>& getSprites() const; - const std::vector<int>& getBullets() const; -private: - int m_Health; - std::vector<int> m_SpriteIds; - std::vector<int> m_BulletIds; + const std::vector<int> &getSprites() const; + const std::vector<int> &getBullets() const; + + private: + int m_Health; + std::vector<int> m_SpriteIds; + std::vector<int> m_BulletIds; }; -} +} // namespace lvl diff --git a/liblvl/src/include/position.hpp b/liblvl/src/include/position.hpp index a2ca8e6c550d8a2bfa8efb8fd17b76b2ca4861dd..94d9bca1365116093558eb1ddab12bb73773b5a9 100644 --- a/liblvl/src/include/position.hpp +++ b/liblvl/src/include/position.hpp @@ -7,13 +7,13 @@ namespace lvl { /** -* @brief Position represents -*/ + * @brief Position represents + */ struct Position { /** - * @brief X-Component of the position - */ + * @brief X-Component of the position + */ int x; /** * @brief Y-Component of the position @@ -23,4 +23,4 @@ struct Position bool operator==(int _cpp_par_) const; }; -} +} // namespace lvl diff --git a/liblvl/src/include/room.hpp b/liblvl/src/include/room.hpp index bdd57428fa0ab0f6093778fee3fc54c940191a7b..7ce75f18d1125f6c8ee3b056e49bb068ecef3f93 100644 --- a/liblvl/src/include/room.hpp +++ b/liblvl/src/include/room.hpp @@ -13,8 +13,8 @@ namespace lvl struct Placement { - Position position; - int32_t id; + Position position; + int32_t id; }; /** @@ -61,10 +61,8 @@ class Room * @param objects Vector of objects that are inside the room. * @param enemies Vector of enemies that are inside the room. */ - Room(int id, const std::string &name, int width, int height, - const std::vector<TileId>& tiles, - const std::vector<Placement>& objects, - const std::vector<Placement>& enemies); + Room(int id, const std::string &name, int width, int height, const std::vector<TileId> &tiles, + const std::vector<Placement> &objects, const std::vector<Placement> &enemies); ~Room() = default; @@ -148,6 +146,7 @@ class Room * @param pos Position of the Enemy to remove. */ void removeEnemy(Position pos); + private: /** * @brief Unique identifier for the room. diff --git a/liblvl/src/include/spritesheet.hpp b/liblvl/src/include/spritesheet.hpp index 63efc64ba7f84faefeebd3c0c39c95b6d1dc511b..473c8981c89ee455940623485ce8220e1ef9f466 100644 --- a/liblvl/src/include/spritesheet.hpp +++ b/liblvl/src/include/spritesheet.hpp @@ -2,16 +2,16 @@ // Created by Leon Lux on 29.01.25. // #pragma once -#include <stdexcept> -#include <vector> -#include <filesystem> #include <cassert> -#include <position.hpp> +#include <filesystem> #include <formatexception.hpp> -#include <tileindexexception.hpp> -#include <levelexception.hpp> #include <highfive/highfive.hpp> +#include <levelexception.hpp> +#include <position.hpp> #include <stb/stb_image.h> +#include <stdexcept> +#include <tileindexexception.hpp> +#include <vector> namespace lvl { @@ -24,7 +24,7 @@ namespace fs = std::filesystem; class Spritesheet { -public: + public: /** * @brief Type to identify tiles */ @@ -49,11 +49,11 @@ public: ~Spritesheet() = default; /** - * @brief Lookup the Position for a given Tile by its id. - * @throws TileIndexException When the given Tile ID is out of the valid range. - * @param tileId ID of the Tile to lookup. - * @return Position of the upper left corner of the tile. - */ + * @brief Lookup the Position for a given Tile by its id. + * @throws TileIndexException When the given Tile ID is out of the valid range. + * @param tileId ID of the Tile to lookup. + * @return Position of the upper left corner of the tile. + */ [[nodiscard]] Position getTilePosition(TileId tileId) const; @@ -62,7 +62,7 @@ public: * @return Reference to the underlying PNG data. */ [[nodiscard]] - const std::vector<Byte>& getSpritedata() const; + const std::vector<Byte> &getSpritedata() const; /** * @brief Save the sprite sheet definition to HDF5. @@ -70,13 +70,12 @@ public: */ void toHDF5(HighFive::File &file) const; - int getWidth() const; int getHeight() const; int getChannels() const; size_t getTileSize() const; -private: + private: /** * @brief Size of one Tile */ @@ -94,9 +93,9 @@ private: */ int m_Channels; /** - * @brief Raw tile data as .PNG. - */ + * @brief Raw tile data as .PNG. + */ std::vector<Byte> m_TileData; }; -} +} // namespace lvl diff --git a/liblvl/src/include/tileindexexception.hpp b/liblvl/src/include/tileindexexception.hpp index 3bd091618d857aff26f419b4f1c1a0d18734dcbb..07b68aab48add59916aab3b29fd63aaf88d3a13e 100644 --- a/liblvl/src/include/tileindexexception.hpp +++ b/liblvl/src/include/tileindexexception.hpp @@ -2,34 +2,35 @@ // Created by Leon Lux on 31.01.25. // #pragma once -#include <sstream> #include <levelexception.hpp> +#include <sstream> namespace lvl { /** -* @brief Represents an Indexing Error while accessing the tile sheet -*/ + * @brief Represents an Indexing Error while accessing the tile sheet + */ class TileIndexException : public LevelException { -public: + public: /** * @brief Create a new Exception with the given message and the index that caused it. * @param message Message to display for what(). * @param index Index that has caused the exception. * @param length Length of the tile array. */ - TileIndexException(const std::string& message, int32_t index, int32_t length); + TileIndexException(const std::string &message, int32_t index, int32_t length); /** * @brief Print the full error message with the description, the index and the permitted length. * @return The error Message. */ [[nodiscard]] - const char* what() const noexcept override; -private: + const char *what() const noexcept override; + + private: int32_t m_Index; int32_t m_Length; std::string m_FullMessage; }; -} +} // namespace lvl diff --git a/liblvl/src/level.cpp b/liblvl/src/level.cpp index 1781aec3f03826967877cd4b27147148fab5fb0f..acdbb7672479fa7283bfd6d9d549495970ddbbf6 100644 --- a/liblvl/src/level.cpp +++ b/liblvl/src/level.cpp @@ -6,15 +6,13 @@ namespace lvl { -Level::Level(const fs::path& path, const std::size_t tileSize) -: m_Enemies(Enemy::defaultEnemies()), m_Rooms{{0, "Testroom 1", 64, 64}, {1, "Testroom 2", 128, 64}}, - m_Objects(Object::defaultObjects()), - m_Spritesheet(path, tileSize) +Level::Level(const fs::path &path, const std::size_t tileSize) + : m_Enemies(Enemy::defaultEnemies()), m_Rooms{{0, "Testroom 1", 64, 64}, {1, "Testroom 2", 128, 64}}, + m_Objects(Object::defaultObjects()), m_Spritesheet(path, tileSize) { } -Level::Level(const fs::path &path) - : m_Spritesheet(HighFive::File(path, HighFive::File::ReadOnly)) +Level::Level(const fs::path &path) : m_Spritesheet(HighFive::File(path, HighFive::File::ReadOnly)) { HighFive::File file(path, HighFive::File::ReadOnly); loadEnemies(file); @@ -22,8 +20,7 @@ Level::Level(const fs::path &path) loadPlayer(file); } - -void Level::saveChanges(const fs::path& path) const +void Level::saveChanges(const fs::path &path) const { HighFive::File file(path.c_str(), HighFive::File::Create | HighFive::File::Truncate); m_Spritesheet.toHDF5(file); @@ -40,7 +37,7 @@ void Level::saveEnemies(HighFive::File &file) const std::vector<int32_t> ids; std::vector<int32_t> healths; std::vector<int32_t> hitbox_x, hitbox_y; - std::vector<std::vector<int32_t>>sprites; + std::vector<std::vector<int32_t>> sprites; for (const auto &enemy : m_Enemies) { @@ -51,7 +48,6 @@ void Level::saveEnemies(HighFive::File &file) const hitbox_x.push_back(w); hitbox_y.push_back(h); - sprites.push_back(enemy.getSprites()); } @@ -60,7 +56,6 @@ void Level::saveEnemies(HighFive::File &file) const group.createDataSet("hitbox_x", hitbox_x); group.createDataSet("hitbox_y", hitbox_y); - const HighFive::DataSpace spriteSpace({sprites.size(), 4}); auto spriteSet = group.createDataSet<int>("sprites", spriteSpace); @@ -93,13 +88,10 @@ void Level::loadEnemies(HighFive::File &file) spriteSet.read(spriteBuffer); // Reconstruct enemies - for (size_t i = 0; i < ids.size(); ++i) { - Enemy enemy( - ids[i], - healths[i], - hitbox_x[i], - hitbox_y[i], - spriteBuffer[i] // Assuming Enemy constructor takes a vector<int32_t> + for (size_t i = 0; i < ids.size(); ++i) + { + Enemy enemy(ids[i], healths[i], hitbox_x[i], hitbox_y[i], + spriteBuffer[i] // Assuming Enemy constructor takes a vector<int32_t> ); m_Enemies.push_back(std::move(enemy)); @@ -110,7 +102,7 @@ void Level::saveRooms(HighFive::File &file) const { auto group = file.createGroup("rooms"); - for (const auto& room : m_Rooms) + for (const auto &room : m_Rooms) { auto roomGroup = group.createGroup(std::to_string(room.getId())); @@ -182,7 +174,7 @@ void Level::loadRooms(HighFive::File &file) { Placement p{}; p.id = obj_ids[i]; - p.position = { obj_x[i], obj_y[i] }; + p.position = {obj_x[i], obj_y[i]}; objects.push_back(p); } } @@ -199,7 +191,7 @@ void Level::loadRooms(HighFive::File &file) { Placement p{}; p.id = enemy_ids[i]; - p.position = { enemy_x[i], enemy_y[i] }; + p.position = {enemy_x[i], enemy_y[i]}; enemies.push_back(p); } } @@ -228,7 +220,7 @@ void Level::saveObjects(HighFive::File &file) const hitbox_x.push_back(width); hitbox_y.push_back(height); - const auto& sprites = object.getSpriteIds(); + const auto &sprites = object.getSpriteIds(); sprite_ids.insert(sprite_ids.end(), sprites.begin(), sprites.end()); } @@ -241,7 +233,6 @@ void Level::saveObjects(HighFive::File &file) const group.createDataSet("sprite_ids", sprite_ids); } - void Level::loadObjects(const HighFive::File &file) { if (!file.exist("objects")) @@ -265,27 +256,24 @@ void Level::loadObjects(const HighFive::File &file) // Reconstruct objects size_t sprite_index = 0; - for (size_t i = 0; i < ids.size(); ++i) { - // Calculate the number of sprites for each object (assuming it's stored as a single list, not separated by object) + for (size_t i = 0; i < ids.size(); ++i) + { + // Calculate the number of sprites for each object (assuming it's stored as a single list, not separated by + // object) std::vector<int32_t> object_sprites; while (sprite_index < sprite_ids.size() && (object_sprites.size() < hitbox_x[i])) { object_sprites.push_back(sprite_ids[sprite_index++]); } - Object object( - ids[i], - names[i], - hitbox_x[i], - hitbox_y[i], - object_sprites // Assign the corresponding sprite IDs to each object + Object object(ids[i], names[i], hitbox_x[i], hitbox_y[i], + object_sprites // Assign the corresponding sprite IDs to each object ); m_Objects.push_back(std::move(object)); } } - void Level::savePlayer(HighFive::File &file) const { // Create (or overwrite) a group for the player data @@ -316,25 +304,24 @@ void Level::loadPlayer(HighFive::File &file) m_Player = lvl::Player(health, sprites, bullets); } - Player &Level::getPlayer() { return m_Player; } -std::vector<Enemy>& Level::getEnemies() +std::vector<Enemy> &Level::getEnemies() { return m_Enemies; } -const Spritesheet& Level::getSpritesheet() const +const Spritesheet &Level::getSpritesheet() const { return m_Spritesheet; } -std::vector<Room>& Level::getRooms() +std::vector<Room> &Level::getRooms() { return m_Rooms; } -} \ No newline at end of file +} // namespace lvl \ No newline at end of file diff --git a/liblvl/src/levelexception.cpp b/liblvl/src/levelexception.cpp index 3939eee4161b5601d1dc5e1b028d996149193c31..78c6b616912f0b612851a3ef418df95b701ee5db 100644 --- a/liblvl/src/levelexception.cpp +++ b/liblvl/src/levelexception.cpp @@ -10,10 +10,9 @@ LevelException::LevelException(const std::string &msg) : m_Message(msg) { } -const char* LevelException::what() const noexcept +const char *LevelException::what() const noexcept { return m_Message.c_str(); } - -} \ No newline at end of file +} // namespace lvl \ No newline at end of file diff --git a/liblvl/src/object.cpp b/liblvl/src/object.cpp index ec94255c61e2a4147b56e7f667ea58c5b0c8a5e7..16b51eb06ee4a14cf6aa6324e1c65faf359d6ac1 100644 --- a/liblvl/src/object.cpp +++ b/liblvl/src/object.cpp @@ -21,7 +21,7 @@ const std::string &Object::getName() const return m_Name; } -const Hitbox& Object::getHitbox() const +const Hitbox &Object::getHitbox() const { return m_Hitbox; } @@ -33,13 +33,11 @@ const std::vector<int32_t> &Object::getSpriteIds() const std::vector<Object> Object::defaultObjects() { - return - { + return { {0, "bones", 16, 16, {189, 189, 189, 189}}, {1, "candle", 16, 16, {10, 11, 12, 13}}, {2, "flag", 16, 16, {14, 15, 16, 17}}, }; } - -} \ No newline at end of file +} // namespace lvl \ No newline at end of file diff --git a/liblvl/src/player.cpp b/liblvl/src/player.cpp index f13d8fdbe53973ec06e199d439303fe50b67abd4..fddedd700556fbd8ec992389fc001cda6c00a49e 100644 --- a/liblvl/src/player.cpp +++ b/liblvl/src/player.cpp @@ -3,12 +3,16 @@ // #include "player.hpp" - namespace lvl { Player::Player() - : m_Health(100), m_SpriteIds({18, 19, 20, 21}), m_BulletIds({0, 0, 0, 0,}) + : m_Health(100), m_SpriteIds({18, 19, 20, 21}), m_BulletIds({ + 0, + 0, + 0, + 0, + }) { } @@ -32,4 +36,4 @@ const std::vector<int> &Player::getBullets() const return m_BulletIds; } -} \ No newline at end of file +} // namespace lvl \ No newline at end of file diff --git a/liblvl/src/room.cpp b/liblvl/src/room.cpp index 8fe8baf71beeef33ff77a8dbd26137edf9a90d60..0465be4e97c2a79d3226fc7a0ae4c30ab9db0ec0 100644 --- a/liblvl/src/room.cpp +++ b/liblvl/src/room.cpp @@ -5,143 +5,135 @@ namespace lvl { - Room::Room(const int id, const std::string &name, const int width, const int height) - : m_Id(id), m_Name(name), m_Width(width), m_Height(height), m_Tiles(m_Width * m_Height, NULL_ROOM_ID) - { - } +Room::Room(const int id, const std::string &name, const int width, const int height) + : m_Id(id), m_Name(name), m_Width(width), m_Height(height), m_Tiles(m_Width * m_Height, NULL_ROOM_ID) +{ +} - Room::Room(int id, const std::string &name, const int width, const int height, - const std::vector<TileId>& tiles, - const std::vector<Placement>& objects, - const std::vector<Placement>& enemies) - : m_Id(id), - m_Name(name), - m_Width(width), - m_Height(height), - m_Tiles(tiles.empty() ? std::vector<TileId>(width * height, NULL_ROOM_ID) : tiles), - m_Objects(objects), - m_Enemies(enemies) - { - } +Room::Room(int id, const std::string &name, const int width, const int height, const std::vector<TileId> &tiles, + const std::vector<Placement> &objects, const std::vector<Placement> &enemies) + : m_Id(id), m_Name(name), m_Width(width), m_Height(height), + m_Tiles(tiles.empty() ? std::vector<TileId>(width * height, NULL_ROOM_ID) : tiles), m_Objects(objects), + m_Enemies(enemies) +{ +} - const std::string &Room::getName() const - { - return m_Name; - } +const std::string &Room::getName() const +{ + return m_Name; +} - int Room::getWidth() const - { - return m_Width; - } +int Room::getWidth() const +{ + return m_Width; +} - int Room::getHeight() const - { - return m_Height; - } +int Room::getHeight() const +{ + return m_Height; +} - int Room::getId() const - { - return m_Id; - }; +int Room::getId() const +{ + return m_Id; +}; - Room::TileIterator Room::tilesBegin() const - { - return m_Tiles.begin(); - } +Room::TileIterator Room::tilesBegin() const +{ + return m_Tiles.begin(); +} - Room::TileIterator Room::tilesEnd() const - { - return m_Tiles.end(); - } +Room::TileIterator Room::tilesEnd() const +{ + return m_Tiles.end(); +} - Room::ObjectIterator Room::objectsBegin() const - { - return m_Objects.begin(); - } +Room::ObjectIterator Room::objectsBegin() const +{ + return m_Objects.begin(); +} - Room::ObjectIterator Room::objectsEnd() const - { - return m_Objects.end(); - } +Room::ObjectIterator Room::objectsEnd() const +{ + return m_Objects.end(); +} - Room::EnemyIterator Room::enemiesBegin() const - { - return m_Enemies.begin(); - } +Room::EnemyIterator Room::enemiesBegin() const +{ + return m_Enemies.begin(); +} - Room::EnemyIterator Room::enemiesEnd() const - { - return m_Enemies.end(); - } +Room::EnemyIterator Room::enemiesEnd() const +{ + return m_Enemies.end(); +} - void Room::placeTile(const Position pos, const int32_t id) +void Room::placeTile(const Position pos, const int32_t id) +{ + if (pos.x < 0 || pos.x >= m_Width || pos.y < 0 || pos.y >= m_Height) { - if (pos.x < 0 || pos.x >= m_Width || pos.y < 0 || pos.y >= m_Height) - { - throw std::out_of_range("attempt to place tile outside of room"); - } - m_Tiles[pos.y * m_Width + pos.x] = id; + throw std::out_of_range("attempt to place tile outside of room"); } + m_Tiles[pos.y * m_Width + pos.x] = id; +} - void Room::placeEnemy(const Position pos, const int32_t id) +void Room::placeEnemy(const Position pos, const int32_t id) +{ + if (pos.x < 0 || pos.x >= m_Width || pos.y < 0 || pos.y >= m_Height) { - if (pos.x < 0 || pos.x >= m_Width || pos.y < 0 || pos.y >= m_Height) - { - throw std::out_of_range("attempt to enemy outside of room"); - } + throw std::out_of_range("attempt to enemy outside of room"); + } - Placement p{}; - p.id = id; - p.position = pos; + Placement p{}; + p.id = id; + p.position = pos; - m_Enemies.emplace_back(p); - } + m_Enemies.emplace_back(p); +} - void Room::placeObject(const Position pos, const int32_t id) +void Room::placeObject(const Position pos, const int32_t id) +{ + if (pos.x < 0 || pos.x >= m_Width || pos.y < 0 || pos.y >= m_Height) { - if (pos.x < 0 || pos.x >= m_Width || pos.y < 0 || pos.y >= m_Height) - { - throw std::out_of_range("attempt to enemy outside of room"); - } + throw std::out_of_range("attempt to enemy outside of room"); + } - Placement p{}; - p.id = id; - p.position = pos; + Placement p{}; + p.id = id; + p.position = pos; - m_Objects.emplace_back(p); - } + m_Objects.emplace_back(p); +} - void Room::removeTile(const Position pos) +void Room::removeTile(const Position pos) +{ + if (pos.x < 0 || pos.x >= m_Width || pos.y < 0 || pos.y >= m_Height) { - if (pos.x < 0 || pos.x >= m_Width || pos.y < 0 || pos.y >= m_Height) - { - throw std::out_of_range("attempt to remove tile outside of room"); - } - m_Tiles[pos.y * m_Width + pos.x] = NULL_ROOM_ID; + throw std::out_of_range("attempt to remove tile outside of room"); } + m_Tiles[pos.y * m_Width + pos.x] = NULL_ROOM_ID; +} + +void Room::removeEnemy(const Position pos) +{ + const auto it = std::remove_if(m_Enemies.begin(), m_Enemies.end(), + [&](const Placement &e) { return e.position.x == pos.x && e.position.y == pos.y; }); - void Room::removeEnemy(const Position pos) + if (it != m_Enemies.end()) { - const auto it = std::remove_if(m_Enemies.begin(), m_Enemies.end(), - [&](const Placement &e) - { return e.position.x == pos.x && e.position.y == pos.y; }); - - if (it != m_Enemies.end()) - { - m_Enemies.erase(it, m_Enemies.end()); - } + m_Enemies.erase(it, m_Enemies.end()); } +} + +void Room::removeObject(const Position pos) +{ + const auto it = std::remove_if(m_Objects.begin(), m_Objects.end(), + [&](const Placement &o) { return o.position.x == pos.x && o.position.y == pos.y; }); - void Room::removeObject(const Position pos) + if (it != m_Objects.end()) { - const auto it = std::remove_if(m_Objects.begin(), m_Objects.end(), - [&](const Placement &o) - { return o.position.x == pos.x && o.position.y == pos.y; }); - - if (it != m_Objects.end()) - { - m_Objects.erase(it, m_Objects.end()); - } + m_Objects.erase(it, m_Objects.end()); } +} -} \ No newline at end of file +} // namespace lvl \ No newline at end of file diff --git a/liblvl/src/spritesheet.cpp b/liblvl/src/spritesheet.cpp index 53e9e3d0f8f2f58553f47cbc9e243ebfbd35a7a5..c4ad984bb029e904da2b74cd77a12dc26d247c55 100644 --- a/liblvl/src/spritesheet.cpp +++ b/liblvl/src/spritesheet.cpp @@ -21,8 +21,7 @@ Spritesheet::Spritesheet(const HighFive::File &file) const std::vector<std::size_t> dimensions = tileData.getSpace().getDimensions(); if (dimensions.size() != 3) { - throw FormatException("incorrect dimensions for tile data (expected: width, height, channels)", - "tileset/data"); + throw FormatException("incorrect dimensions for tile data (expected: width, height, channels)", "tileset/data"); } std::size_t size = 0; @@ -46,17 +45,19 @@ Spritesheet::Spritesheet(const HighFive::File &file) m_TileData.resize(m_Height * m_Width * m_Channels); size_t index = 0; - for (size_t h = 0; h < m_Height; ++h) { - for (size_t w = 0; w < m_Width; ++w) { - for (size_t c = 0; c < m_Channels; ++c) { + for (size_t h = 0; h < m_Height; ++h) + { + for (size_t w = 0; w < m_Width; ++w) + { + for (size_t c = 0; c < m_Channels; ++c) + { m_TileData[index++] = reshapedData[h][w][c]; } } } } -Spritesheet::Spritesheet(const fs::path &path, const std::size_t dimension) - : m_TileSize(dimension) +Spritesheet::Spritesheet(const fs::path &path, const std::size_t dimension) : m_TileSize(dimension) { int width, height, channels; @@ -75,7 +76,6 @@ Spritesheet::Spritesheet(const fs::path &path, const std::size_t dimension) stbi_image_free(imageData); } - Position Spritesheet::getTilePosition(const TileId tileId) const { const int32_t maxId = (m_Width / m_TileSize) * (m_Height / m_TileSize); @@ -89,7 +89,7 @@ Position Spritesheet::getTilePosition(const TileId tileId) const const std::size_t x = (tileId % tilesPerRow) * m_TileSize; const std::size_t y = (tileId / tilesPerRow) * m_TileSize; - return { static_cast<int>(x), static_cast<int>(y) }; + return {static_cast<int>(x), static_cast<int>(y)}; } const std::vector<Spritesheet::Byte> &Spritesheet::getSpritedata() const @@ -101,7 +101,7 @@ void Spritesheet::toHDF5(HighFive::File &file) const { HighFive::Group group = file.createGroup("tileset"); - std::vector dims = { static_cast<size_t>(m_Height), static_cast<size_t>(m_Width), static_cast<size_t>(m_Channels) }; + std::vector dims = {static_cast<size_t>(m_Height), static_cast<size_t>(m_Width), static_cast<size_t>(m_Channels)}; HighFive::DataSpace dataspace(dims); @@ -113,13 +113,16 @@ void Spritesheet::toHDF5(HighFive::File &file) const std::vector<std::vector<std::vector<Byte>>> reshapedData(m_Height); - for (size_t h = 0; h < m_Height; ++h) { + for (size_t h = 0; h < m_Height; ++h) + { reshapedData[h].resize(m_Width); - for (size_t w = 0; w < m_Width; ++w) { + for (size_t w = 0; w < m_Width; ++w) + { reshapedData[h][w].resize(m_Channels); - for (size_t c = 0; c < m_Channels; ++c) { + for (size_t c = 0; c < m_Channels; ++c) + { // Calculate the index in the 1D vector const size_t index = (h * m_Width * m_Channels) + (w * m_Channels) + c; reshapedData[h][w][c] = m_TileData[index]; @@ -151,4 +154,4 @@ size_t Spritesheet::getTileSize() const return m_TileSize; } -} \ No newline at end of file +} // namespace lvl \ No newline at end of file diff --git a/liblvl/src/tileindexexception.cpp b/liblvl/src/tileindexexception.cpp index 713bbb40b8bfe9019258120d249c3f376cd19109..72edc4a5d8ca63d69e085f1c5e2ae107314b5eb5 100644 --- a/liblvl/src/tileindexexception.cpp +++ b/liblvl/src/tileindexexception.cpp @@ -19,4 +19,4 @@ const char *TileIndexException::what() const noexcept return m_FullMessage.c_str(); } -} \ No newline at end of file +} // namespace lvl \ No newline at end of file