Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit upon pressing the close button #71

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/states/GameOverState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ void GameOverState::handleEvents(std::vector<Event> events) {
for(Event event: events) {
if (event == Event::CONFIRM) {
this->done = true;
} else if (event == Event::QUIT) {
this->next_state = State::EXIT;
this->done = true;
}
}
}
Expand All @@ -34,5 +37,5 @@ void GameOverState::draw(SDL_Renderer *renderer) {
}

State GameOverState::getNextState() {
return State::MENU;
return this->next_state;
}
2 changes: 2 additions & 0 deletions src/states/GameOverState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class GameOverState : public BaseState {
PauseScreen screen_text;

bool sound_played = false;

State next_state = State::MENU;
public:
GameOverState(SDL_Renderer *renderer, FontManager * fonts, SoundManager * sounds, OptionManager * options);
~GameOverState();
Expand Down
22 changes: 14 additions & 8 deletions src/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,23 @@ void GameState::handleEvents(std::vector<Event> events) {
}

for(Event event: events) {
if (event == Event::QUIT) {
if (this->completed) {
this->options->setStandardModeLevel(this->level+1);
} else {
this->options->setStandardModeLevel(this->level);
}
this->next_state = State::EXIT;
this->done = true;
return;
}
if (this->completed) {
if (event == Event::CONFIRM) {
this->completed = false;
int next_level = this->level+1;
if (next_level > this->total_levels ) {
this->options->resetStandardMode();
this->finished = true;
this->next_state = State::WON;
this->done = true;
} else {
this->loadLevel(next_level);
Expand All @@ -77,12 +87,12 @@ void GameState::handleEvents(std::vector<Event> events) {
this->failed = false;
}
} else if (this->paused) {
if (event == Event::CONFIRM || event == Event::QUIT) {
if (event == Event::CONFIRM) {
this->done = true;
} else if (event == Event::CANCEL || event == Event::MENU) {
this->paused = false;
}
} else if (event == Event::MENU || event == Event::QUIT) {
} else if (event == Event::MENU) {
this->paused = true;
return;
} else if(event == Event::NEXT) {
Expand Down Expand Up @@ -243,9 +253,5 @@ SDL_Point GameState::calculatePosition(int width, int height) {
}

State GameState::getNextState() {
if(this->finished) {
return State::WON;
} else {
return State::MENU;
}
return this->next_state;
}
2 changes: 2 additions & 0 deletions src/states/GameState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class GameState : public BaseState {
std::vector<std::vector<ShellType>> shells;
SDL_Point position;

State next_state = State::MENU;

SDL_Point calculatePosition(int width, int height);
int getTotalLevels();
void loadLevel(int level);
Expand Down
19 changes: 11 additions & 8 deletions src/states/GameStateChallenge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void GameStateChallenge::update() {
this->options->setChallengeModeHighscore(this->level);
}
this->options->resetChallengeMode();
this->game_over = true;
this->next_state = State::GAMEOVER;
this->done = true;
} else if (this->board->isCompleted() && !this->completed) {
this->theme.pause();
Expand All @@ -58,6 +58,13 @@ void GameStateChallenge::handleEvents(std::vector<Event> events) {
}

for(Event event: events) {
if (event == Event::QUIT) {
this->options->setChallengeModeLevel(this->level);
this->options->setChallengeModeLives(this->attempts);
this->next_state = State::EXIT;
this->done = true;
return;
}
if (this->completed) {
if (event == Event::CONFIRM) {
this->completed = false;
Expand All @@ -71,14 +78,14 @@ void GameStateChallenge::handleEvents(std::vector<Event> events) {
this->theme.unpause();
}
} else if (this->paused) {
if (event == Event::CONFIRM || event == Event::QUIT) {
if (event == Event::CONFIRM) {
this->options->setChallengeModeLevel(this->level);
this->options->setChallengeModeLives(this->attempts);
this->done = true;
} else if (event == Event::CANCEL || event == Event::MENU) {
this->paused = false;
}
} else if (event == Event::MENU || event == Event::QUIT) {
} else if (event == Event::MENU) {
this->paused = true;
return;
}
Expand Down Expand Up @@ -165,9 +172,5 @@ SDL_Point GameStateChallenge::calculatePosition(int width, int height) {
}

State GameStateChallenge::getNextState() {
if (this->game_over) {
return State::GAMEOVER;
} else {
return State::MENU;
}
return this->next_state;
}
3 changes: 2 additions & 1 deletion src/states/GameStateChallenge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class GameStateChallenge : public BaseState {
bool paused = false;
bool completed = false;
bool failed = false;
bool game_over = false;

int level;
int seed;
Expand All @@ -45,6 +44,8 @@ class GameStateChallenge : public BaseState {
int attempts;
bool attempts_changed;

State next_state = State::MENU;

SDL_Point calculatePosition(int width, int height);
void loadLevel();

Expand Down
9 changes: 6 additions & 3 deletions src/states/GameStateRelaxed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ void GameStateRelaxed::handleEvents(std::vector<Event> events) {
this->paused = !(this->paused);
return;
} else if (event == Event::QUIT) {
this->paused = true;
return;
this->options->setRelaxedModeScore(this->board->getMatches());
this->options->setRelaxedModeShells(this->board->getCurrentShells());
this->options->setRelaxedModeSeed(this->board->getCurrentSeed());
this->next_state = State::EXIT;
this->done = true;
}

if (this->paused) {
Expand Down Expand Up @@ -113,5 +116,5 @@ SDL_Point GameStateRelaxed::calculatePosition(int width, int height) {
}

State GameStateRelaxed::getNextState() {
return State::MENU;
return this->next_state;
}
2 changes: 2 additions & 0 deletions src/states/GameStateRelaxed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class GameStateRelaxed : public BaseState {
int height;
SDL_Point position;

State next_state = State::MENU;

SDL_Point calculatePosition(int width, int height);
void loadLevel();
public:
Expand Down
5 changes: 4 additions & 1 deletion src/states/HighscoreState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ void HighscoreState::handleEvents(std::vector<Event> events) {
for(Event event :events) {
switch (event) {
case Event::QUIT:
this->next_state = State::EXIT;
this->done = true;
break;
case Event::MENU:
case Event::CANCEL:
case Event::CONFIRM:
Expand Down Expand Up @@ -79,5 +82,5 @@ int HighscoreState::getTextY(int number) {
}

State HighscoreState::getNextState() {
return State::MENU;
return this->next_state;
}
2 changes: 2 additions & 0 deletions src/states/HighscoreState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class HighscoreState : public BaseState {
int text_offset = 2;
int text_start_y;

State next_state = State::MENU;

int getTextY(int number);
public:
HighscoreState(SDL_Renderer * renderer, FontManager * fonts, SoundManager * sounds, OptionManager * options);
Expand Down
7 changes: 5 additions & 2 deletions src/states/NotImplementedState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ void NotImplementedState::update() {

void NotImplementedState::handleEvents(std::vector<Event> events) {
for(Event event: events) {
if (event == Event::CONFIRM || event == Event::CANCEL || event == Event::MENU) {
if (event == Event::QUIT) {
this->next_state = State::EXIT;
this->done = true;
} else if (event == Event::CONFIRM || event == Event::CANCEL || event == Event::MENU) {
this->done = true;
}
}
Expand All @@ -31,5 +34,5 @@ void NotImplementedState::draw(SDL_Renderer *renderer) {
}

State NotImplementedState::getNextState() {
return State::MENU;
return this->next_state;
}
2 changes: 2 additions & 0 deletions src/states/NotImplementedState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class NotImplementedState : public BaseState {
ThemeManager theme;

PauseScreen screen_text;

State next_state = State::MENU;
public:
NotImplementedState(SDL_Renderer *renderer, FontManager * fonts, SoundManager * sounds, OptionManager * options);
~NotImplementedState();
Expand Down
5 changes: 4 additions & 1 deletion src/states/OptionsState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ void OptionsState::handleEvents(std::vector<Event> events) {
}
break;
case Event::QUIT:
this->next_state = State::EXIT;
this->done = true;
break;
case Event::MENU:
case Event::CANCEL:
this->done = true;
Expand Down Expand Up @@ -309,5 +312,5 @@ int OptionsState::getTextY(int number) {
}

State OptionsState::getNextState() {
return State::MENU;
return this->next_state;
}
2 changes: 2 additions & 0 deletions src/states/OptionsState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class OptionsState : public BaseState {
SDL_Texture * text_title;
std::vector<SDL_Texture*> texts;

State next_state = State::MENU;

int text_offset = 2;
int text_start_y;

Expand Down
5 changes: 4 additions & 1 deletion src/states/WonState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ void WonState::handleEvents(std::vector<Event> events) {
for(Event event: events) {
if (event == Event::CONFIRM) {
this->done = true;
} else if (event == Event::QUIT) {
this->next_state = State::EXIT;
this->done = true;
}
}
}
Expand All @@ -34,5 +37,5 @@ void WonState::draw(SDL_Renderer *renderer) {
}

State WonState::getNextState() {
return State::MENU;
return this->next_state;
}
2 changes: 2 additions & 0 deletions src/states/WonState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class WonState : public BaseState {
PauseScreen screen_text;

bool sound_played = false;

State next_state = State::EXIT;
public:
WonState(SDL_Renderer *renderer, FontManager * fonts, SoundManager * sounds, OptionManager * options);
~WonState();
Expand Down
Loading