Restructuring
authorNils Forssén <nilfo359@student.liu.se>
Sat, 11 Nov 2023 17:57:40 +0000 (18:57 +0100)
committerNils Forssén <nilfo359@student.liu.se>
Sat, 11 Nov 2023 17:57:40 +0000 (18:57 +0100)
12 files changed:
src/Game_state.cc [deleted file]
src/Game_state.h [deleted file]
src/Map.h
src/Menus.cc [deleted file]
src/Menus.h [deleted file]
src/Object.h
src/Player.h
src/State.cc [deleted file]
src/State.h [deleted file]
src/States.cc [new file with mode: 0644]
src/States.h [new file with mode: 0644]
src/_main.cc

diff --git a/src/Game_state.cc b/src/Game_state.cc
deleted file mode 100644 (file)
index b9440dc..0000000
+++ /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 (file)
index 587b351..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-#ifndef GAME_STATE_H
-#define GAME_STATE_H
-
-#include <SFML/Graphics.hpp> 
-#include <memory>
-
-#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
index faebec7cf59178979370455e489291fa145ecafa..00ccce1bf659f44dbc4e6469931ff8605518f6f8 100644 (file)
--- a/src/Map.h
+++ b/src/Map.h
@@ -3,7 +3,8 @@
 
 #include <SFML/Graphics.hpp>
 #include "constants.h"
-#include "State.h"
+
+struct Context;
 
 class Map
 {
diff --git a/src/Menus.cc b/src/Menus.cc
deleted file mode 100644 (file)
index dfda650..0000000
+++ /dev/null
@@ -1,94 +0,0 @@
-#include "Menus.h"
-
-#include <iostream> //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 (file)
index 24ea74c..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-#ifndef MENUS_H
-#define MENUS_H
-
-#include <memory>
-
-#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
index f3f0aea540dc51f1665c7e0ea672a3f6ef1df17c..bf5bbe40ad626a5720167eb88b41f5513e90485c 100644 (file)
@@ -2,7 +2,8 @@
 #define OBJECT_H
 
 #include <SFML/Graphics.hpp>
-#include "State.h"
+
+struct Context;
 
 class Object
 {
index e7db3323f2d8aac749f3a43e33ba6b24b4233c9b..255ed515fbb4546d5406a870519843458994c035 100644 (file)
@@ -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 (file)
index e69de29..0000000
diff --git a/src/State.h b/src/State.h
deleted file mode 100644 (file)
index bc8cc22..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-#ifndef STATE_H
-#define STATE_H
-
-#include "SFML/Graphics.hpp"
-#include <memory>
-
-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 (file)
index 0000000..143f2dc
--- /dev/null
@@ -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<Game_state*>(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 (file)
index 0000000..9af2a73
--- /dev/null
@@ -0,0 +1,75 @@
+#ifndef STATES_H
+#define STATES_H
+
+#include <memory>
+#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
index 9c7c7570364120b7becb1c0433d2dbcc673c094d..bb0cf086097850d12b38d41f99669a4189fcdc7d 100644 (file)
@@ -9,9 +9,7 @@
 #include <memory>
 
 #include "constants.h"
-#include "Game_state.h"
-#include "Menus.h"
-#include "State.h"
+#include "States.h"
 
 
 int main ()