Added menus and context, Lukas, Nils, Arvid, Malin, Stina, Alma
authorNils Forssén <nilfo359@student.liu.se>
Sat, 11 Nov 2023 16:52:32 +0000 (17:52 +0100)
committerNils Forssén <nilfo359@student.liu.se>
Sat, 11 Nov 2023 16:52:32 +0000 (17:52 +0100)
13 files changed:
assets/kir_flaska2.png [deleted file]
assets/säck2.png [deleted file]
src/Game_state.cc
src/Game_state.h
src/Map.cc
src/Map.h
src/Menus.cc
src/Menus.h
src/Object.h
src/Player.cc
src/Player.h
src/State.h
src/_main.cc

diff --git a/assets/kir_flaska2.png b/assets/kir_flaska2.png
deleted file mode 100644 (file)
index 58a70bb..0000000
Binary files a/assets/kir_flaska2.png and /dev/null differ
diff --git a/assets/säck2.png b/assets/säck2.png
deleted file mode 100644 (file)
index 1a76a18..0000000
Binary files "a/assets/s\303\244ck2.png" and /dev/null differ
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..b9440dc58325e87a1b519e6ccaf4f3c185beee56 100644 (file)
@@ -0,0 +1,46 @@
+#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
index 3bdfc8fcd2d88cbd65643b44d7f44ae39023974a..587b35104bf95e3462475c2ce7afd0dae98ea901 100644 (file)
@@ -2,20 +2,28 @@
 #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:
-        void update() override;
-        void render(sf::RenderWindow& window) override;
-        void handle_input();
-
-    protected:
-
-    private:
+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 16ef1002ee588fa1d8a0ed428d6153261baf7a09..c8dda8be9adc9dc0218a17e9bf647801b91bba7a 100644 (file)
@@ -9,7 +9,7 @@ Map::Map() : texture{}, sprite{}
     sprite.setPosition(0,0);
 }
 
-void Map::update()
+void Map::update(Context& context)
 {}
 
 void Map::render(sf::RenderWindow& window) const
index b59816a610b6bf2e24b17d4662443de225895023..faebec7cf59178979370455e489291fa145ecafa 100644 (file)
--- a/src/Map.h
+++ b/src/Map.h
@@ -3,6 +3,7 @@
 
 #include <SFML/Graphics.hpp>
 #include "constants.h"
+#include "State.h"
 
 class Map
 {
@@ -10,8 +11,9 @@ public:
     Map();
     ~Map() = default;
 
-    void update();
+    void update(Context& context);
     void render(sf::RenderWindow& window) const;
+    int value {69};
 protected:
 private:
     sf::Texture texture;
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..dfda6501c7d82331d6bbda660ba25b9b7c58601d 100644 (file)
@@ -0,0 +1,94 @@
+#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;
+    }
+}
index ac9dbe5727d797d01ae6037e8b49761c16a82ea0..24ea74c150d2f62b68ee5c451e7a85efeb443746 100644 (file)
@@ -1,19 +1,42 @@
 #ifndef MENUS_H
 #define MENUS_H
+
+#include <memory>
+
+#include "SFML/Graphics.hpp"
 #include "State.h"
-class Start_menu
+#include "constants.h"
+#include "Game_state.h"
+
+class Start_menu : public State
 {
 public:
-    void update(Context) override;
-    void rednder(Window) override;
-    void handle_input() override;
+    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 Paus_menu
+
+
+class Pause_menu : public State
 {
 public:
-    void update(Context) override;
-    void rednder(Window) override;
-    void handle_input() override;
+    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 91f1b39114eba21b4d73e460c4b1dd3f418828de..f3f0aea540dc51f1665c7e0ea672a3f6ef1df17c 100644 (file)
@@ -2,6 +2,7 @@
 #define OBJECT_H
 
 #include <SFML/Graphics.hpp>
+#include "State.h"
 
 class Object
 {
@@ -11,7 +12,7 @@ public:
 
     virtual bool collides(Object& other) = 0;
     virtual void collision(Object& other) = 0;
-    virtual void update() = 0;
+    virtual void update(Context& context) = 0;
     virtual void render(sf::RenderWindow& window) const = 0;
   
 protected:
index 0f3b2f0f00e5b9eb69205d3e3207d88d23c354da..a15848a44fc4cd20f9aa741cc8abae9de60afe5f 100644 (file)
@@ -1,5 +1,7 @@
 #include "Player.h"
 
+#include <iostream>
+
 Player::Player() : collected{0}, max_speed{10}
 {
     texture.loadFromFile("assets/4V_figur.png");
@@ -21,8 +23,9 @@ void Player::collision(Object& other)
     return;
 }
 
-void Player::update()
+void Player::update(Context& context)
 {
+    //std::cout << context.current_state->game_map.value << std::endl;
     position += max_speed * direction;
     
     sprite.setPosition(position);
index c76c8bc9091718797f55a445cfda7cdc55e61ddb..e7db3323f2d8aac749f3a43e33ba6b24b4233c9b 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "Moving_object.h"
 #include "constants.h"
+#include "State.h"
 
 class Player : public Moving_object
 {
@@ -16,7 +17,7 @@ public:
     ~Player() = default;
     bool collides(Object& other) override;
     void collision(Object& other) override;
-    void update() override;
+    void update(Context& context) override;
     void render(sf::RenderWindow& window) const override;
     void handle_input(sf::Event& event);
 protected:
index fa3d2261c297b4bfb341248cb518a7dcf7a8d187..bc8cc224f6fca76d433caf7d4c3a3b72dd1b9f2a 100644 (file)
@@ -1,12 +1,25 @@
 #ifndef STATE_H
 #define STATE_H
 
+#include "SFML/Graphics.hpp"
+#include <memory>
+
+struct Context;
+
 class State
 {
 public:
-    virtual void update(Context) = 0;
-    virtual void render(Window) = 0;
-    virtual void hande_input() = 0;
+    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
index d1123518e94e1a022e6d05735d9159cc04499c56..9c7c7570364120b7becb1c0433d2dbcc673c094d 100644 (file)
@@ -5,10 +5,14 @@
 */
 
 #include <SFML/Graphics.hpp>
+#include <iostream>
+#include <memory>
 
 #include "constants.h"
-#include "Player.h"
-#include "Map.h"
+#include "Game_state.h"
+#include "Menus.h"
+#include "State.h"
+
 
 int main ()
 {
@@ -17,8 +21,8 @@ int main ()
         "DespYrat"
     };
 
-    Map game_map{};
-    Player player{};
+    Context game_context{};
+    game_context.current_state = new Start_menu{};
     
     sf::Clock game_clock;
     while (window.isOpen())
@@ -34,26 +38,27 @@ int main ()
                 break;
             default:
                 break;
-            }
+            } 
 
-            player.handle_input(event);
+            game_context.current_state->handle_input(event);
         }
 
-
         // Update
-        game_map.update();
-        player.update();
+        game_context.current_state->update(game_context);
+
+        if (game_context.next_state != nullptr)
+        {
+            game_context.current_state = std::move(game_context.next_state);
+            game_context.next_state = nullptr;
+        }
 
         //Draw
         window.clear();
-
-        game_map.render(window);
-        player.render(window);
-
-
+        game_context.current_state->render(window);
         window.display();
 
         // Make game run at constant fps
+        // std::cout << "frametime: " << game_clock.getElapsedTime().asSeconds() << std::endl;
         sf::sleep(sf::milliseconds(1000.0 / FPS) - game_clock.getElapsedTime());
         game_clock.restart();