diff --git a/editor/src/CenterGrid.cpp b/editor/src/CenterGrid.cpp index bb4d1c81232055fbd1ee351521c19170bb50ef47..441dc11e0dbcb95436d1df347e9aaac3d19be86a 100644 --- a/editor/src/CenterGrid.cpp +++ b/editor/src/CenterGrid.cpp @@ -94,7 +94,12 @@ void CenterGrid::onTileClicked(int row, int col) m_tileIDs[row][col] = m_currentSpriteID; } else if (m_spriteType == 1) { - m_mobIDs[row][col] = 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 { m_objectIDs[row][col] = m_currentSpriteID; diff --git a/editor/src/HDF5Handler.cpp b/editor/src/HDF5Handler.cpp index c5404c344483bace63b1f5888d1ee1ee4e55129b..f1c81fe40a6e4f6a3a4997740e616114e828594d 100644 --- a/editor/src/HDF5Handler.cpp +++ b/editor/src/HDF5Handler.cpp @@ -39,10 +39,10 @@ void HDF5Handler::loadTilesheet(TileBar* tileBar) const 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(), 32, 32); - for (int row = 0; row < 32; row++) + lvl::Room newRoom(id, name.toStdString(), 64, 64); + for (int row = 0; row < 64; row++) { - for (int col = 0; col < 32; col++) + for (int col = 0; col < 64; col++) { newRoom.placeTile({col,row}, tileIDs[row][col]); if(mobIDs[row][col] != -1) @@ -61,6 +61,44 @@ void HDF5Handler::saveRoom(int id, QString name, QVector<QVector<int>> tileIDs, m_Level.saveChanges("./level.h5"); } +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; + 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}); + } + } + + for (int row = 0; row < height; row++) + { + for (int col = 0; col < width; col++) + { + room.placeTile({col,row}, tileIDs[row][col]); + std::cout << col << " " << row << " " << tileIDs[row][col] << std::endl; + if(mobIDs[row][col] != -1) + { + room.placeEnemy({col,row}, mobIDs[row][col]); + } + if(objectIDs[row][col] != -1) + { + room.placeObject({col,row}, objectIDs[row][col]); + } + } + } + m_Level.saveChanges("./level.h5"); +} + int HDF5Handler::getMaxID() { std::vector<lvl::Room>& roomsList = m_Level.getRooms(); diff --git a/editor/src/HDF5Handler.hpp b/editor/src/HDF5Handler.hpp index 661ca6c89ba5c2d7cee0e6d30dd2aea1dc96081c..40483d808256f79e3ee9984610285f5bbac455e7 100644 --- a/editor/src/HDF5Handler.hpp +++ b/editor/src/HDF5Handler.hpp @@ -48,6 +48,15 @@ public: */ 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 find maximum room-ID in current HDF5-file * @return highest room-ID diff --git a/editor/src/MainWindow.cpp b/editor/src/MainWindow.cpp index f9e64cb7b08e25d44f0ce00774e264dc6c256c86..846d9800dc7c1644c2a7c3a85e73e8329f806006 100644 --- a/editor/src/MainWindow.cpp +++ b/editor/src/MainWindow.cpp @@ -6,7 +6,7 @@ namespace editor { -MainWindow::MainWindow(DialogSelection selected, const std::filesystem::path& path, QWidget* parent) +MainWindow::MainWindow(DialogSelection selected, const std::filesystem::path& path, QWidget* parent, int roomID) : QMainWindow(parent) { resize(800,600); @@ -21,16 +21,15 @@ MainWindow::MainWindow(DialogSelection selected, const std::filesystem::path& pa std::filesystem::path path = "./level.h5"; HDF5Handler handler = HDF5Handler(path); m_ID = handler.getMaxID() + 1; - std::cout << m_ID << std::endl; } else { m_ID = 2; } + m_loadID = roomID; // Create the menu bar QMenuBar* menuBar = new QMenuBar(this); QMenu* fileMenu = menuBar->addMenu("File"); - //QMenu* settingsMenu = menuBar->addMenu("Settings"); QAction* saveRoomAction = new QAction("Save Room", this); connect(saveRoomAction, &QAction::triggered, this, &MainWindow::saveLevel); fileMenu->addAction(saveRoomAction); @@ -79,8 +78,52 @@ MainWindow::MainWindow(DialogSelection selected, const std::filesystem::path& pa centralWidget->setLayout(mainLayout); setCentralWidget(centralWidget); + //initialize middleWidget as fully custom board middleWidget->drawPreset(5); + + //Load selected level + if(roomID != -1) + { + lvl::Level level("./level.h5"); + 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; + int width = foundRoom.getWidth(); + for (auto it = foundRoom.tilesBegin(); it != foundRoom.tilesEnd(); it++) + { + int32_t tileID = *it; + 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); + } + } + for (auto it = foundRoom.objectsBegin(); it != foundRoom.objectsEnd(); it++) + { + lvl::Placement gameObject = *it; + int x = gameObject.position.x; + int y = gameObject.position.y; + int tileID = gameObject.id; + m_centerGrid->setCurrentSprite(m_leftWidget->getPixmapByIndex(tileID), tileID); + m_centerGrid->onTileClicked(x,y); + } + for (auto it = foundRoom.enemiesBegin(); it != foundRoom.enemiesEnd(); it++) + { + lvl::Placement gameObject = *it; + int x = gameObject.position.x; + int y = gameObject.position.y; + int tileID = gameObject.id; + m_centerGrid->setCurrentSprite(m_leftWidget->getPixmapByIndex(tileID), tileID); + m_centerGrid->onTileClicked(x,y); + } + + } + } void MainWindow::handleTileBarClick(int index, int id) @@ -109,25 +152,31 @@ void MainWindow::saveLevel() QVector<QVector<int>> tileIDs = m_centerGrid->getTileIDs(); QVector<QVector<int>> mobIDs = m_centerGrid->getMobIDs(); QVector<QVector<int>> objectIDs = m_centerGrid->getObjectIDs(); - QString name = QInputDialog::getText(nullptr, "Enter Level Name", "Name:"); - if (name.isEmpty()) { - name = "defaultName"; - } - - if (QFile::exists("./level.h5")) + if (m_loadID != -1) { - std::cout << "using new handler" << std::endl; std::filesystem::path path = "./level.h5"; HDF5Handler handler = HDF5Handler(path); - handler.saveRoom(m_ID, name, tileIDs, mobIDs, objectIDs); - } else + handler.modifyRoom(m_loadID, tileIDs, mobIDs, objectIDs); + m_loadID = -1; + } else { - std::cout << "using default handler" << std::endl; - HDF5Handler handler = HDF5Handler(); - handler.saveRoom(m_ID, name, tileIDs, mobIDs, objectIDs); - } - m_ID++; -} + QString name = QInputDialog::getText(nullptr, "Enter Level Name", "Name:"); + if (name.isEmpty()) { + name = "defaultName"; + } + if (QFile::exists("./level.h5")) + { + std::filesystem::path path = "./level.h5"; + HDF5Handler handler = HDF5Handler(path); + handler.saveRoom(m_ID, name, tileIDs, mobIDs, objectIDs); + } else + { + HDF5Handler handler = HDF5Handler(); + handler.saveRoom(m_ID, name, tileIDs, mobIDs, objectIDs); + } + m_ID++; + } } +} \ No newline at end of file diff --git a/editor/src/MainWindow.hpp b/editor/src/MainWindow.hpp index df6875aaaf2b8df6fb97e1be70d7559d98529c16..a88c284afd8a1b6b06eef8d1a91b58d5b0c3c5e4 100644 --- a/editor/src/MainWindow.hpp +++ b/editor/src/MainWindow.hpp @@ -32,8 +32,9 @@ public: * @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); + explicit MainWindow(DialogSelection selected, const std::filesystem::path& path, QWidget* parent = nullptr, int roomID = -1); private slots: /** @@ -71,6 +72,11 @@ private: */ int m_ID; + /** + * @brief ID of loaded room (or -1) + */ + int m_loadID; + }; } // editor diff --git a/editor/src/StartupDialog.cpp b/editor/src/StartupDialog.cpp index a127a0cddba2849132b7f94f3fd7933bc98fd372..5419a9af8cf36affc3fc38d5d4adb83a0ee27751 100644 --- a/editor/src/StartupDialog.cpp +++ b/editor/src/StartupDialog.cpp @@ -32,7 +32,7 @@ namespace editor this, "Open Level", "", - "Level Files (*.lvl)" + "Level Files (*.h5)" ) .toStdString(); @@ -40,6 +40,40 @@ namespace editor { m_Selection = DialogSelection::LOAD_LEVEL; m_FilePath = path; + + showSelectionDialog(path); + } + } + + 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) + { + 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(); } } @@ -54,4 +88,9 @@ namespace editor return m_FilePath; } + int StartupDialog::getSelectedRoomID() const + { + return m_selectedRoomID; + } + } diff --git a/editor/src/StartupDialog.hpp b/editor/src/StartupDialog.hpp index 41a9f8c303e530466f7d71054e54e5cbd8176b6a..7b6a311c6a33ccdbd07c06be5b917f383457a223 100644 --- a/editor/src/StartupDialog.hpp +++ b/editor/src/StartupDialog.hpp @@ -6,9 +6,13 @@ #include <QVBoxLayout> #include <QPushButton> #include <QFileDialog> +#include <QComboBox> #include <filesystem> +#include <level.hpp> +#include <room.hpp> + namespace editor { @@ -38,12 +42,27 @@ public: */ [[nodiscard]] DialogSelection getSelection() const; + /** * @brief Return the Path to the selected Level File. * @return Path to chosen .lvl file. */ [[nodiscard]] std::filesystem::path getPath() const; + + /** + * @brief Return ID of the selected room for loading rooms. + * @return ID of the selected room (or -1). + */ + [[nodiscard]] + int getSelectedRoomID() const; + + /** + * @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: /** * @brief Callback for Load Button. @@ -57,6 +76,7 @@ public slots: private: DialogSelection m_Selection; std::filesystem::path m_FilePath; + int m_selectedRoomID = -1; }; } diff --git a/editor/src/main.cpp b/editor/src/main.cpp index d0f9dc1d0db95205fb29ee87b0704aeb0144fac5..a7712986faec131eeb496ab0f4a2bfd3fe814804 100644 --- a/editor/src/main.cpp +++ b/editor/src/main.cpp @@ -28,7 +28,7 @@ int main(int argc, char *argv[]) { if (startup.exec() == QDialog::Accepted) { - editor::MainWindow window(startup.getSelection(), startup.getPath()); + editor::MainWindow window(startup.getSelection(), startup.getPath(), 0, startup.getSelectedRoomID()); window.show(); return app.exec(); } diff --git a/liblvl/src/enemy.cpp b/liblvl/src/enemy.cpp index 2d1eb558393cf980b8f07c5a26115dfa58b38d7f..c9743210d896ced95580383239447c275a08d786 100644 --- a/liblvl/src/enemy.cpp +++ b/liblvl/src/enemy.cpp @@ -33,7 +33,7 @@ std::vector<Enemy> Enemy::defaultEnemies() { return {Enemy(0, 30, 16, 16, {22, 23, 24, 25}), Enemy(1, 50, 16, 16, {74, 75, 76, 77}), - Enemy(2, 70, 16, 16, {126, 127, 128, 129}) + Enemy(2, 70, 16, 16, {146, 147, 148, 149}) }; }