From: Nils Forssén Date: Sat, 11 Nov 2023 17:57:40 +0000 (+0100) Subject: Restructuring X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=8685a0c146b172a32aec63f24cf2ff0c479325cb;p=TDDC76_proj.git Restructuring --- 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.cc b/src/Menus.cc deleted file mode 100644 index dfda650..0000000 --- a/src/Menus.cc +++ /dev/null @@ -1,94 +0,0 @@ -#include "Menus.h" - -#include //TA BORT! - -Start_menu::Start_menu() : texture{}, sprite{}, exit_menu{false} -{ - texture.loadFromFile("assets/sack.png"); - sprite.setTexture(texture); - - sf::FloatRect gb {sprite.getGlobalBounds()}; - sprite.setOrigin(gb.width / 2, gb.height / 2); - sprite.setPosition(S_WIDTH / 2, S_HEIGHT / 2); -} - -void Start_menu::update(Context& context) -{ - if (exit_menu) - { - context.next_state = new Game_state{}; - } -} - -void Start_menu::render(sf::RenderWindow& window) const -{ - window.draw(sprite); -} - -void Start_menu::handle_input(sf::Event& event) -{ - switch (event.type) - { - case sf::Event::KeyPressed: - switch(event.key.code) - { - case sf::Keyboard::Enter: - exit_menu = true; - break; - default: - break; - } - break; - default: - break; - } -} - -Pause_menu::Pause_menu() : texture{}, sprite{}, exit_game{false}, resume_game{false} -{ - texture.loadFromFile("assets/kir.png"); - sprite.setTexture(texture); - - sf::FloatRect gb {sprite.getGlobalBounds()}; - sprite.setOrigin(gb.width / 2, gb.height / 2); - sprite.setPosition(S_WIDTH / 2, S_HEIGHT / 2); -} - -void Pause_menu::update(Context& context) -{ - if (resume_game) - { - context.next_state = std::move(context.saved_game); - } - else if (exit_game) - { - context.next_state = new Start_menu{}; - } -} - -void Pause_menu::render(sf::RenderWindow& window) const -{ - window.draw(sprite); -} - -void Pause_menu::handle_input(sf::Event& event) -{ - switch (event.type) - { - case sf::Event::KeyPressed: - switch(event.key.code) - { - case sf::Keyboard::Enter: - resume_game = true; - break; - case sf::Keyboard::Escape: - exit_game = true; - break; - default: - break; - } - break; - default: - break; - } -} 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/States.cc b/src/States.cc new file mode 100644 index 0000000..143f2dc --- /dev/null +++ b/src/States.cc @@ -0,0 +1,137 @@ +#include "States.h" + +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} +{ + texture.loadFromFile("assets/sack.png"); + sprite.setTexture(texture); + + sf::FloatRect gb {sprite.getGlobalBounds()}; + sprite.setOrigin(gb.width / 2, gb.height / 2); + sprite.setPosition(S_WIDTH / 2, S_HEIGHT / 2); +} + +void Start_menu::update(Context& context) +{ + if (exit_menu) + { + context.next_state = new Game_state{}; + } +} + +void Start_menu::render(sf::RenderWindow& window) const +{ + window.draw(sprite); +} + +void Start_menu::handle_input(sf::Event& event) +{ + switch (event.type) + { + case sf::Event::KeyPressed: + switch(event.key.code) + { + case sf::Keyboard::Enter: + exit_menu = true; + break; + default: + break; + } + break; + default: + break; + } +} + +Pause_menu::Pause_menu() : texture{}, sprite{}, exit_game{false}, resume_game{false} +{ + texture.loadFromFile("assets/kir.png"); + sprite.setTexture(texture); + + sf::FloatRect gb {sprite.getGlobalBounds()}; + sprite.setOrigin(gb.width / 2, gb.height / 2); + sprite.setPosition(S_WIDTH / 2, S_HEIGHT / 2); +} + +void Pause_menu::update(Context& context) +{ + if (resume_game) + { + context.next_state = std::move(context.running_game); + } + else if (exit_game) + { + context.next_state = new Start_menu{}; + } +} + +void Pause_menu::render(sf::RenderWindow& window) const +{ + window.draw(sprite); +} + +void Pause_menu::handle_input(sf::Event& event) +{ + switch (event.type) + { + case sf::Event::KeyPressed: + switch(event.key.code) + { + case sf::Keyboard::Enter: + resume_game = true; + break; + case sf::Keyboard::Escape: + exit_game = true; + break; + default: + break; + } + break; + default: + break; + } +} 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 ()