Browse Source

add proper GameState handling

master
noerw 1 year ago
parent
commit
b04767af0b
3 changed files with 36 additions and 25 deletions
  1. 31
    4
      game.cpp
  2. 2
    4
      game.hpp
  3. 3
    17
      main.cpp

+ 31
- 4
game.cpp View File

@@ -15,11 +15,31 @@ namespace Proto4 {

AppState Game::handleKey(const sf::Event &event) {
if (event.key.code == sf::Keyboard::Q)
return AppState::MainMenu;
state = GameState::Finished;

return AppState::Game;
}

AppState Game::update(sf::RenderWindow &window, sf::Time timestep) {
switch (state) {
case GameState::Finished:
reset();
return AppState::MainMenu;

case GameState::Uninitialized:
start();
break;

case GameState::Paused:
break;

case GameState::Running:
break;

default:
break;
}

return AppState::Game;
}

@@ -28,14 +48,21 @@ namespace Proto4 {
window.draw(player);
}

bool Game::start() { return true; }
bool Game::reset() {
state = GameState::Uninitialized;
return true;
}

bool Game::start() {
player.setPosition(mainView.getCenter());
state = GameState::Running;
return true;
}

bool Game::pause() { return true; }

bool Game::resume() { return true; }

bool Game::reset() { return true; }

void Game::resize(sf::Event &resizeEvent) {
mainView.setSize(resizeEvent.size.width, resizeEvent.size.height);
mainView.setCenter(resizeEvent.size.width / 2, resizeEvent.size.height / 2);

+ 2
- 4
game.hpp View File

@@ -3,8 +3,6 @@
#include <SFML/System.hpp>
#include <SFML/Window.hpp>

#include "main.hpp"

#include "player.hpp"

#pragma once
@@ -22,14 +20,14 @@ namespace Proto4 {

bool init();

private:
bool start();
bool pause();
bool resume();
bool reset();

private:
sf::View mainView{};
GameState gamestate = GameState::Uninitialized;
GameState state = GameState::Uninitialized;
Player player{};
uint32_t score = 0;


+ 3
- 17
main.cpp View File

@@ -71,21 +71,10 @@ namespace Proto4 {
switch (event.type) {
case sf::Event::Resized:
background.resize(event);

switch (state) {
case AppState::MainMenu:
menu.resize(event);
break;

case AppState::Game:
game.resize(event);
break;

default:
break;
}

menu.resize(event);
game.resize(event);
break;

case sf::Event::KeyPressed:
switch (state) {
case AppState::MainMenu:
@@ -114,9 +103,6 @@ namespace Proto4 {
break;
case AppState::Game:
state = game.update(window, timestep);
if (state != AppState::Game) {
game.reset();
}
break;
case AppState::Exit:
window.close();

Loading…
Cancel
Save