Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodCoder666 committed Dec 15, 2024
1 parent a860535 commit 4385857
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 15 deletions.
20 changes: 20 additions & 0 deletions src/localGameDialog/localGameDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ LocalGameDialog::~LocalGameDialog() {
delete ui;
}

LocalGameConfig LocalGameDialog::config() const {
LocalGameConfig config;
config.gameSpeed = ui->spinBox_gameSpeed->value();
config.enableSounds = ui->checkBox_enableSounds->isChecked();
config.showAnalysis = ui->checkBox_showAnalysis->isChecked();
config.mapName = ui->comboBox_gameMap->currentText();
config.mapWidth = ui->spinBox_mapWidth->value();
config.mapHeight = ui->spinBox_mapHeight->value();
int numPlayers = ui->spinBox_numPlayers->value();
auto& players = config.players;
players.resize(numPlayers);
QLayout* layout = ui->groupBox_players->layout();
for(int i = 0; i < numPlayers; ++i) {
QWidget* playerWidget = layout->itemAt(i + 1)->widget();
players[i].name = playerWidget->findChild<QComboBox*>()->currentText();
players[i].visible = playerWidget->findChild<QCheckBox*>()->isChecked();
}
return config;
}

void LocalGameDialog::on_btnStartGame_clicked() {
this->done(QDialog::Accepted);
}
Expand Down
14 changes: 14 additions & 0 deletions src/localGameDialog/localGameDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@ namespace Ui {
}
QT_END_NAMESPACE

struct LocalGameConfig {
int gameSpeed;
bool enableSounds, showAnalysis;
QString mapName;
int mapWidth, mapHeight;
struct PlayerConfig {
QString name;
bool visible;
};
QVector<PlayerConfig> players;
};

class LocalGameDialog : public QDialog {
Q_OBJECT

public:
LocalGameDialog(QWidget* parent = nullptr);
~LocalGameDialog();

LocalGameConfig config() const;

public slots:
void on_btnStartGame_clicked();
void on_btnCancel_clicked();
Expand Down
6 changes: 3 additions & 3 deletions src/localGameWindow/localGameWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "localGameWindow.h"
#include <QVBoxLayout>

LocalGameWindow::LocalGameWindow(QWidget* parent) :
QDialog(parent), gameMap(nullptr) {
LocalGameWindow::LocalGameWindow(QWidget* parent, const LocalGameConfig& config) :
QDialog(parent) {
setWindowTitle("Local Game");
gameMap = new MapWidget(this);
gameMap = new MapWidget(this, config.mapWidth, config.mapHeight);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(gameMap);
setLayout(layout);
Expand Down
3 changes: 2 additions & 1 deletion src/localGameWindow/localGameWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

#include <QDialog>
#include "../mapWidget/mapWidget.h"
#include "../localGameDialog/localGameDialog.h"

class LocalGameWindow : public QDialog {
Q_OBJECT

public:
LocalGameWindow(QWidget* parent = nullptr);
explicit LocalGameWindow(QWidget* parent, const LocalGameConfig& config);
~LocalGameWindow();

private:
Expand Down
2 changes: 1 addition & 1 deletion src/mainWindow/mainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void MainWindow::on_btnLocalGame_clicked() {
LocalGameDialog* dialog = new LocalGameDialog(this);
int result = dialog->exec();
if(result == QDialog::Accepted) {
LocalGameWindow* window = new LocalGameWindow(this);
LocalGameWindow* window = new LocalGameWindow(this, dialog->config());
window->exec();
}
}
2 changes: 1 addition & 1 deletion src/mainWindow/mainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MainWindow : public QMainWindow {
Q_OBJECT

public:
MainWindow(QWidget* parent = nullptr);
explicit MainWindow(QWidget* parent = nullptr);
~MainWindow();

public slots:
Expand Down
14 changes: 6 additions & 8 deletions src/mapWidget/mapWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
#include <QRandomGenerator>
#include <QMouseEvent>

MapWidget::MapWidget(QWidget* parent) :
QWidget(parent), scale(1.0), offset(0, 0), isDragging(false) {
// setFixedSize(800, 600);
MapWidget::MapWidget(QWidget* parent, int w, int h) :
QWidget(parent), scale(1.0), offset(0, 0), isDragging(false), width(w), height(h) {
setMouseTracking(true);
}

Expand All @@ -19,12 +18,11 @@ void MapWidget::paintEvent(QPaintEvent* event) {
painter.translate(offset);
painter.scale(scale, scale);

const int gridSize = 20;
const qreal cellSize = 300.0 / gridSize;
const qreal cellWidth = 300.0 / width, cellHeight = 300.0 / height;

for(int i = 0; i < gridSize; ++i) {
for(int j = 0; j < gridSize; ++j) {
QRectF cell(i * cellSize, j * cellSize, cellSize, cellSize);
for(int i = 0; i < width; ++i) {
for(int j = 0; j < height; ++j) {
QRectF cell(i * cellWidth, j * cellHeight, cellWidth, cellHeight);
painter.drawRect(cell);

int randomNumber = QRandomGenerator::global()->bounded(10);
Expand Down
3 changes: 2 additions & 1 deletion src/mapWidget/mapWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ class MapWidget : public QWidget {
Q_OBJECT

public:
explicit MapWidget(QWidget* parent = nullptr);
explicit MapWidget(QWidget* parent, int w, int h);
~MapWidget();
const int width, height;

protected:
void paintEvent(QPaintEvent* event) override;
Expand Down

0 comments on commit 4385857

Please sign in to comment.