Skip to content

Commit

Permalink
Add pause function to game
Browse files Browse the repository at this point in the history
  • Loading branch information
SmilingPixel committed Nov 18, 2022
1 parent b49cc57 commit 2ecb7cf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
15 changes: 8 additions & 7 deletions include/game_view/GameField.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,17 @@ class GameField: public QGraphicsScene{
void setFps(qreal fps);

/**
* Start the game
* Should be called explicitly
* Start the game.
* Should be called explicitly.
*/
void startGame();

/**
* Pause the game.
* Should be called explicitly.
*/
void pauseGame();

private:

/**
Expand Down Expand Up @@ -221,11 +227,6 @@ class GameField: public QGraphicsScene{
*/
static bool pointFloatEqual(const QPointF& p1, const QPointF& p2);

/**
* Return distance between p1 and p2
*/
static qreal distanceBetween(const QPointF& p1, const QPointF& p2);

/**
* Check if any monster has reached the Protection Objective.
* If so, remove it from the field and minus life_points_ by 1.
Expand Down
2 changes: 2 additions & 0 deletions include/game_view/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ private slots:

void resetGame();

void pauseOrResumeGame(bool is_pause);

void loadLevel();
};

Expand Down
8 changes: 4 additions & 4 deletions source/game_view/GameField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,15 @@ bool GameField::pointFloatEqual(const QPointF& p1, const QPointF& p2){
return qRealEqual(p1.x(), p2.x()) && qRealEqual(p1.y(), p2.y());
}

qreal GameField::distanceBetween(const QPointF &p1, const QPointF &p2) {
return qSqrt(qPow(p1.x() - p2.x(), 2) + qPow(p1.y() - p2.y(), 2));
}


void GameField::startGame() {
timer_.start();
}

void GameField::pauseGame() {
timer_.stop();
}

void GameField::checkReachProtectionObjective() {
auto it = monsters_.begin();
while(it != monsters_.end()){
Expand Down
19 changes: 19 additions & 0 deletions source/game_view/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) {
connect(reset_game_act, &QAction::triggered, this, &MainWindow::resetGame);
auto* load_level_act = new QAction(QIcon(":/icons/plus.svg"), "New Level");
connect(reset_game_act, &QAction::triggered, this, &MainWindow::loadLevel);
auto* pause_game_act = new QAction(QIcon(":/icons/pause.svg"), "Pause");
pause_game_act->setCheckable(true);
connect(pause_game_act, &QAction::toggled, this, &MainWindow::pauseOrResumeGame);

// Set MenuBar
auto* menu_bar = menuBar();
Expand All @@ -37,6 +40,8 @@ MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) {
addToolBar(tool_bar);
tool_bar->addAction(reset_game_act);
tool_bar->addAction(load_level_act);
tool_bar->addSeparator();
tool_bar->addAction(pause_game_act);

}

Expand All @@ -53,6 +58,20 @@ void MainWindow::resetGame() {
startGame();
}

void MainWindow::pauseOrResumeGame(bool is_pause) {
auto* pause_action = qobject_cast<QAction*>(sender());
if(is_pause){
game_field_->pauseGame();
pause_action->setIcon(QIcon(":/icons/play.svg"));
pause_action->setText("Resume");
}
else{
game_field_->startGame();
pause_action->setIcon(QIcon(":/icons/pause.svg"));
pause_action->setText("Pause");
}
}

void MainWindow::loadLevel() {
// TODO: To be implemented
}

0 comments on commit 2ecb7cf

Please sign in to comment.