From ffdf5fa226b0f2649badcf2be6bdbbae9a7660c9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Mon, 13 Nov 2023 08:22:18 +0100 Subject: [PATCH] restructuring --- src/Context.h | 13 +++++++++++++ src/Map.cc | 1 + src/Map.h | 2 -- src/Player.cc | 14 +++++++++++--- src/Player.h | 3 --- src/States.cc | 10 +++++++--- src/States.h | 11 +---------- src/_main.cc | 2 +- 8 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 src/Context.h diff --git a/src/Context.h b/src/Context.h new file mode 100644 index 0000000..6f99ae2 --- /dev/null +++ b/src/Context.h @@ -0,0 +1,13 @@ +#ifndef CONTEXT_H +#define CONTEXT_H + +#include "States.h" + +struct Context +{ + std::unique_ptr current_state {nullptr}; + std::unique_ptr next_state {nullptr}; + std::unique_ptr saved_game {nullptr}; +}; + +#endif \ No newline at end of file diff --git a/src/Map.cc b/src/Map.cc index c8dda8b..10e3a84 100644 --- a/src/Map.cc +++ b/src/Map.cc @@ -1,4 +1,5 @@ #include "Map.h" +#include "constants.h" Map::Map() : texture{}, sprite{} { diff --git a/src/Map.h b/src/Map.h index 00ccce1..afa8c69 100644 --- a/src/Map.h +++ b/src/Map.h @@ -2,7 +2,6 @@ #define MAP_H #include -#include "constants.h" struct Context; @@ -14,7 +13,6 @@ public: void update(Context& context); void render(sf::RenderWindow& window) const; - int value {69}; protected: private: sf::Texture texture; diff --git a/src/Player.cc b/src/Player.cc index a15848a..dd0be27 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -1,6 +1,11 @@ +#include +#include // TA BORT! + #include "Player.h" +#include "States.h" +#include "Context.h" +#include "constants.h" -#include Player::Player() : collected{0}, max_speed{10} { @@ -25,9 +30,12 @@ void Player::collision(Object& other) void Player::update(Context& context) { - //std::cout << context.current_state->game_map.value << std::endl; + // Get game_state from context, static_cast since we know that game is currently running + // game_state is still managed by context + Game_state* game = static_cast(context.current_state.get()); + position += max_speed * direction; - + sprite.setPosition(position); return; } diff --git a/src/Player.h b/src/Player.h index 255ed51..17334a6 100644 --- a/src/Player.h +++ b/src/Player.h @@ -4,10 +4,7 @@ #define _USE_MATH_DEFINES #include -#include - #include "Moving_object.h" -#include "constants.h" class Player : public Moving_object { diff --git a/src/States.cc b/src/States.cc index e79e9e1..97faf60 100644 --- a/src/States.cc +++ b/src/States.cc @@ -1,4 +1,8 @@ +#include + #include "States.h" +#include "Context.h" +#include "constants.h" Game_state::Game_state() : game_map{}, player{}, pause_game{false} { @@ -8,7 +12,7 @@ void Game_state::update(Context& context) { if (pause_game) { - context.running_game.reset(static_cast(context.current_state.release())); + context.saved_game.reset(static_cast(context.current_state.release())); context.next_state = std::make_unique(); pause_game = false; @@ -102,12 +106,12 @@ void Pause_menu::update(Context& context) { if (resume_game) { - context.next_state = std::move(context.running_game); + context.next_state = std::move(context.saved_game); } else if (exit_game) { context.next_state = std::make_unique(); - context.running_game.release(); + context.saved_game.release(); } } diff --git a/src/States.h b/src/States.h index fc36a5b..cb366fa 100644 --- a/src/States.h +++ b/src/States.h @@ -1,10 +1,8 @@ #ifndef STATES_H #define STATES_H -#include #include "SFML/Graphics.hpp" -#include "constants.h" #include "Map.h" #include "Player.h" @@ -28,9 +26,9 @@ public: void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; - Map game_map; protected: private: + Map game_map; Player player; bool pause_game; }; @@ -65,11 +63,4 @@ private: bool exit_game; }; -struct Context -{ - std::unique_ptr current_state {nullptr}; - std::unique_ptr next_state {nullptr}; - std::unique_ptr running_game {nullptr}; -}; - #endif \ No newline at end of file diff --git a/src/_main.cc b/src/_main.cc index 630aa2b..a0f92ca 100644 --- a/src/_main.cc +++ b/src/_main.cc @@ -10,7 +10,7 @@ #include "constants.h" #include "States.h" - +#include "Context.h" int main () { -- 2.30.2