Skip to content
Snippets Groups Projects
Unverified Commit b66a9086 authored by Julian Dreuth's avatar Julian Dreuth Committed by Leon Lux
Browse files

Add loading/modifying rooms functionality to editor

parent cfb6d00c
No related branches found
No related tags found
1 merge request!55Abgabe des Projektes
......@@ -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;
......
......@@ -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();
......
......@@ -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
......
......@@ -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
......@@ -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
......@@ -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;
}
}
......@@ -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;
};
}
......
......@@ -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();
}
......
......@@ -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})
};
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment