restructuring
authorNils Forssén <nilfo359@student.liu.se>
Mon, 13 Nov 2023 07:22:18 +0000 (08:22 +0100)
committerNils Forssén <nilfo359@student.liu.se>
Mon, 13 Nov 2023 07:22:18 +0000 (08:22 +0100)
src/Context.h [new file with mode: 0644]
src/Map.cc
src/Map.h
src/Player.cc
src/Player.h
src/States.cc
src/States.h
src/_main.cc

diff --git a/src/Context.h b/src/Context.h
new file mode 100644 (file)
index 0000000..6f99ae2
--- /dev/null
@@ -0,0 +1,13 @@
+#ifndef CONTEXT_H
+#define CONTEXT_H
+
+#include "States.h"
+
+struct Context 
+{
+    std::unique_ptr<State> current_state {nullptr};
+    std::unique_ptr<State> next_state {nullptr};
+    std::unique_ptr<Game_state> saved_game {nullptr};
+};
+
+#endif
\ No newline at end of file
index c8dda8be9adc9dc0218a17e9bf647801b91bba7a..10e3a848d9c8c04456dd8706ce329fc7d5d001a3 100644 (file)
@@ -1,4 +1,5 @@
 #include "Map.h"
+#include "constants.h"
 
 Map::Map() : texture{}, sprite{}
 {
index 00ccce1bf659f44dbc4e6469931ff8605518f6f8..afa8c697c640567ebc33d80c9f5c00695b00d3e3 100644 (file)
--- a/src/Map.h
+++ b/src/Map.h
@@ -2,7 +2,6 @@
 #define MAP_H
 
 #include <SFML/Graphics.hpp>
-#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;
index a15848a44fc4cd20f9aa741cc8abae9de60afe5f..dd0be279b4e5010111a816b12e3fedbb1f864a7b 100644 (file)
@@ -1,6 +1,11 @@
+#include <cmath>
+#include <iostream> // TA BORT!
+
 #include "Player.h"
+#include "States.h"
+#include "Context.h"
+#include "constants.h"
 
-#include <iostream>
 
 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<Game_state*>(context.current_state.get());
+
     position += max_speed * direction;
-    
+
     sprite.setPosition(position);
     return;
 }
index 255ed515fbb4546d5406a870519843458994c035..17334a6512ac3928c02e5b821e287534d51f95da 100644 (file)
@@ -4,10 +4,7 @@
 #define _USE_MATH_DEFINES
 
 #include <SFML/Graphics.hpp>
-#include <cmath>
-
 #include "Moving_object.h"
-#include "constants.h"
 
 class Player : public Moving_object
 {
index e79e9e170ea4458c26bc42c7e1efc22fd158ba88..97faf60e81df22edc1805aecdc36704ae77554e5 100644 (file)
@@ -1,4 +1,8 @@
+#include <memory>
+
 #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<Game_state*>(context.current_state.release()));
+        context.saved_game.reset(static_cast<Game_state*>(context.current_state.release()));
         
         context.next_state = std::make_unique<Pause_menu>();
         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<Start_menu>();
-        context.running_game.release();
+        context.saved_game.release();
     }
 }
 
index fc36a5ba7151a42ae91668452585a32f4d987cbd..cb366fabbaec1d873ba85aeb8a0dcd0c1fbd6c51 100644 (file)
@@ -1,10 +1,8 @@
 #ifndef STATES_H
 #define STATES_H
 
-#include <memory>
 #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<State> current_state {nullptr};
-    std::unique_ptr<State> next_state {nullptr};
-    std::unique_ptr<Game_state> running_game {nullptr};
-};
-
 #endif
\ No newline at end of file
index 630aa2bde6bdb2dd2b611120c5effc636b2125fa..a0f92ca705e235523d124d582ad01befe9fafad0 100644 (file)
@@ -10,7 +10,7 @@
 
 #include "constants.h"
 #include "States.h"
-
+#include "Context.h"
 
 int main ()
 {