From 8685a0c146b172a32aec63f24cf2ff0c479325cb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Sat, 11 Nov 2023 18:57:40 +0100 Subject: [PATCH] Restructuring --- src/Game_state.cc | 46 ----------------------- src/Game_state.h | 29 -------------- src/Map.h | 3 +- src/Menus.h | 42 --------------------- src/Object.h | 3 +- src/Player.h | 1 - src/State.cc | 0 src/State.h | 25 ------------- src/{Menus.cc => States.cc} | 49 ++++++++++++++++++++++-- src/States.h | 75 +++++++++++++++++++++++++++++++++++++ src/_main.cc | 4 +- 11 files changed, 126 insertions(+), 151 deletions(-) delete mode 100644 src/Game_state.cc delete mode 100644 src/Game_state.h delete mode 100644 src/Menus.h delete mode 100644 src/State.cc delete mode 100644 src/State.h rename src/{Menus.cc => States.cc} (65%) create mode 100644 src/States.h diff --git a/src/Game_state.cc b/src/Game_state.cc deleted file mode 100644 index b9440dc..0000000 --- a/src/Game_state.cc +++ /dev/null @@ -1,46 +0,0 @@ -#include "Game_state.h" - -Game_state::Game_state() : game_map{}, player{}, pause_game{false} -{ -} - -void Game_state::update(Context& context) -{ - if (pause_game) - { - context.saved_game = std::move(context.current_state); - context.next_state = new Pause_menu{}; - pause_game = false; - return; - } - game_map.update(context); - player.update(context); -} - -void Game_state::render(sf::RenderWindow& window) const -{ - game_map.render(window); - player.render(window); -} - -void Game_state::handle_input(sf::Event& event) -{ - switch (event.type) - { - case sf::Event::KeyPressed: - switch(event.key.code) - { - case sf::Keyboard::Escape: - pause_game = true; - return; - break; - default: - break; - } - break; - default: - break; - } - - player.handle_input(event); -} \ No newline at end of file diff --git a/src/Game_state.h b/src/Game_state.h deleted file mode 100644 index 587b351..0000000 --- a/src/Game_state.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef GAME_STATE_H -#define GAME_STATE_H - -#include -#include - -#include "State.h" -#include "Map.h" -#include "Player.h" -#include "Menus.h" - -class Game_state : public State -{ -public: - Game_state(); - ~Game_state() = default; - - void update(Context& context) override; - void render(sf::RenderWindow& window) const override; - void handle_input(sf::Event& event) override; - Map game_map; -protected: -private: - - Player player; - bool pause_game; -}; - -#endif \ No newline at end of file diff --git a/src/Map.h b/src/Map.h index faebec7..00ccce1 100644 --- a/src/Map.h +++ b/src/Map.h @@ -3,7 +3,8 @@ #include #include "constants.h" -#include "State.h" + +struct Context; class Map { diff --git a/src/Menus.h b/src/Menus.h deleted file mode 100644 index 24ea74c..0000000 --- a/src/Menus.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef MENUS_H -#define MENUS_H - -#include - -#include "SFML/Graphics.hpp" -#include "State.h" -#include "constants.h" -#include "Game_state.h" - -class Start_menu : public State -{ -public: - Start_menu(); - ~Start_menu() = default; - void update(Context& context) override; - void render(sf::RenderWindow& window) const override; - void handle_input(sf::Event& event) override; - -private: - sf::Texture texture; - sf::Sprite sprite; - bool exit_menu; -}; - - -class Pause_menu : public State -{ -public: - Pause_menu(); - ~Pause_menu() = default; - void update(Context& context) override; - void render(sf::RenderWindow& window) const override; - void handle_input(sf::Event& event) override; -private: - sf::Texture texture; - sf::Sprite sprite; - bool resume_game; - bool exit_game; -}; - -#endif \ No newline at end of file diff --git a/src/Object.h b/src/Object.h index f3f0aea..bf5bbe4 100644 --- a/src/Object.h +++ b/src/Object.h @@ -2,7 +2,8 @@ #define OBJECT_H #include -#include "State.h" + +struct Context; class Object { diff --git a/src/Player.h b/src/Player.h index e7db332..255ed51 100644 --- a/src/Player.h +++ b/src/Player.h @@ -8,7 +8,6 @@ #include "Moving_object.h" #include "constants.h" -#include "State.h" class Player : public Moving_object { diff --git a/src/State.cc b/src/State.cc deleted file mode 100644 index e69de29..0000000 diff --git a/src/State.h b/src/State.h deleted file mode 100644 index bc8cc22..0000000 --- a/src/State.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef STATE_H -#define STATE_H - -#include "SFML/Graphics.hpp" -#include - -struct Context; - -class State -{ -public: - virtual void update(Context& context) = 0; - virtual void render(sf::RenderWindow& window) const = 0; - virtual void handle_input(sf::Event& event) = 0; - virtual ~State() = default; -}; - -struct Context -{ - State* current_state {nullptr}; - State* next_state {nullptr}; - State* saved_game {nullptr}; -}; - -#endif \ No newline at end of file diff --git a/src/Menus.cc b/src/States.cc similarity index 65% rename from src/Menus.cc rename to src/States.cc index dfda650..143f2dc 100644 --- a/src/Menus.cc +++ b/src/States.cc @@ -1,6 +1,49 @@ -#include "Menus.h" +#include "States.h" -#include //TA BORT! +Game_state::Game_state() : game_map{}, player{}, pause_game{false} +{ +} + +void Game_state::update(Context& context) +{ + if (pause_game) + { + context.running_game = std::move(static_cast(context.current_state)); + context.next_state = new Pause_menu{}; + pause_game = false; + return; + } + game_map.update(context); + player.update(context); +} + +void Game_state::render(sf::RenderWindow& window) const +{ + game_map.render(window); + player.render(window); +} + +void Game_state::handle_input(sf::Event& event) +{ + switch (event.type) + { + case sf::Event::KeyPressed: + switch(event.key.code) + { + case sf::Keyboard::Escape: + pause_game = true; + return; + break; + default: + break; + } + break; + default: + break; + } + + player.handle_input(event); +} Start_menu::Start_menu() : texture{}, sprite{}, exit_menu{false} { @@ -58,7 +101,7 @@ void Pause_menu::update(Context& context) { if (resume_game) { - context.next_state = std::move(context.saved_game); + context.next_state = std::move(context.running_game); } else if (exit_game) { diff --git a/src/States.h b/src/States.h new file mode 100644 index 0000000..9af2a73 --- /dev/null +++ b/src/States.h @@ -0,0 +1,75 @@ +#ifndef STATES_H +#define STATES_H + +#include +#include "SFML/Graphics.hpp" + +#include "constants.h" +#include "Map.h" +#include "Player.h" + +struct Context; + +class State +{ +public: + virtual void update(Context& context) = 0; + virtual void render(sf::RenderWindow& window) const = 0; + virtual void handle_input(sf::Event& event) = 0; + virtual ~State() = default; +}; + +class Game_state : public State +{ +public: + Game_state(); + ~Game_state() = default; + + void update(Context& context) override; + void render(sf::RenderWindow& window) const override; + void handle_input(sf::Event& event) override; + Map game_map; +protected: +private: + Player player; + bool pause_game; +}; + +class Start_menu : public State +{ +public: + Start_menu(); + ~Start_menu() = default; + void update(Context& context) override; + void render(sf::RenderWindow& window) const override; + void handle_input(sf::Event& event) override; + +private: + sf::Texture texture; + sf::Sprite sprite; + bool exit_menu; +}; + +class Pause_menu : public State +{ +public: + Pause_menu(); + ~Pause_menu() = default; + void update(Context& context) override; + void render(sf::RenderWindow& window) const override; + void handle_input(sf::Event& event) override; +private: + sf::Texture texture; + sf::Sprite sprite; + bool resume_game; + bool exit_game; +}; + +struct Context +{ + State* current_state {nullptr}; + State* next_state {nullptr}; + Game_state* running_game {nullptr}; +}; + +#endif \ No newline at end of file diff --git a/src/_main.cc b/src/_main.cc index 9c7c757..bb0cf08 100644 --- a/src/_main.cc +++ b/src/_main.cc @@ -9,9 +9,7 @@ #include #include "constants.h" -#include "Game_state.h" -#include "Menus.h" -#include "State.h" +#include "States.h" int main () -- 2.30.2