From: Nils Forssén Date: Sat, 11 Nov 2023 16:52:32 +0000 (+0100) Subject: Added menus and context, Lukas, Nils, Arvid, Malin, Stina, Alma X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=12de46902deb5d525c24efc9243839df65df8a6a;p=TDDC76_proj.git Added menus and context, Lukas, Nils, Arvid, Malin, Stina, Alma --- diff --git a/assets/kir_flaska2.png b/assets/kir_flaska2.png deleted file mode 100644 index 58a70bb..0000000 Binary files a/assets/kir_flaska2.png and /dev/null differ diff --git "a/assets/s\303\244ck2.png" "b/assets/s\303\244ck2.png" deleted file mode 100644 index 1a76a18..0000000 Binary files "a/assets/s\303\244ck2.png" and /dev/null differ diff --git a/src/Game_state.cc b/src/Game_state.cc index e69de29..b9440dc 100644 --- a/src/Game_state.cc +++ b/src/Game_state.cc @@ -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 diff --git a/src/Game_state.h b/src/Game_state.h index 3bdfc8f..587b351 100644 --- a/src/Game_state.h +++ b/src/Game_state.h @@ -2,20 +2,28 @@ #define GAME_STATE_H #include +#include #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 diff --git a/src/Map.cc b/src/Map.cc index 16ef100..c8dda8b 100644 --- a/src/Map.cc +++ b/src/Map.cc @@ -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 diff --git a/src/Map.h b/src/Map.h index b59816a..faebec7 100644 --- a/src/Map.h +++ b/src/Map.h @@ -3,6 +3,7 @@ #include #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; diff --git a/src/Menus.cc b/src/Menus.cc index e69de29..dfda650 100644 --- a/src/Menus.cc +++ b/src/Menus.cc @@ -0,0 +1,94 @@ +#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 index ac9dbe5..24ea74c 100644 --- a/src/Menus.h +++ b/src/Menus.h @@ -1,19 +1,42 @@ #ifndef MENUS_H #define MENUS_H + +#include + +#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 diff --git a/src/Object.h b/src/Object.h index 91f1b39..f3f0aea 100644 --- a/src/Object.h +++ b/src/Object.h @@ -2,6 +2,7 @@ #define OBJECT_H #include +#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: diff --git a/src/Player.cc b/src/Player.cc index 0f3b2f0..a15848a 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -1,5 +1,7 @@ #include "Player.h" +#include + 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); diff --git a/src/Player.h b/src/Player.h index c76c8bc..e7db332 100644 --- a/src/Player.h +++ b/src/Player.h @@ -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: diff --git a/src/State.h b/src/State.h index fa3d226..bc8cc22 100644 --- a/src/State.h +++ b/src/State.h @@ -1,12 +1,25 @@ #ifndef STATE_H #define STATE_H +#include "SFML/Graphics.hpp" +#include + +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 diff --git a/src/_main.cc b/src/_main.cc index d112351..9c7c757 100644 --- a/src/_main.cc +++ b/src/_main.cc @@ -5,10 +5,14 @@ */ #include +#include +#include #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();