From ad2833fabce8324d280227120af9b9c4718a56a1 Mon Sep 17 00:00:00 2001 From: malak585 Date: Wed, 6 Dec 2023 14:43:18 +0100 Subject: [PATCH] incomplete json chaos --- src/GameOver_menu.cc | 50 +++++++++++++++++++++++++++++------------- src/Pause_menu.cc | 17 ++++++++------ src/Scoreboard_menu.cc | 9 ++++---- src/Start_menu.cc | 14 +++++++----- src/States.cc | 4 ++-- src/States.h | 16 +++++++++----- 6 files changed, 70 insertions(+), 40 deletions(-) diff --git a/src/GameOver_menu.cc b/src/GameOver_menu.cc index d034a8f..9718f19 100644 --- a/src/GameOver_menu.cc +++ b/src/GameOver_menu.cc @@ -13,12 +13,31 @@ using json = nlohmann::json; -GameOver_menu::GameOver_menu(int const points, json& params) : texture{}, texture2{}, sprite{}, rectangle{}, mouse_l{}, mouse_r{}, linetext{}, funfacttext{}, entertext{}, pointstext{}, - playerInput{}, data{}, playerText{}, savetext{}, quittext{}, font{}, exit_game{false}, menu{false}, menu_index{1}, points{points} +GameOver_menu::GameOver_menu(int const points, json& params) : + texture{}, + texture2{}, + sprite{}, + rectangle{}, + mouse_l{}, + mouse_r{}, + linetext{}, + funfacttext{}, + entertext{}, + pointstext{}, + playerInput{}, + playerText{}, + data{params}, + savetext{}, + quittext{}, + font{}, + exit_game{false}, + menu{false}, + menu_index{1}, + points{points}, + highscore_file{params["gameover_menu"]["highscore_file"]}, + scoreboard_size{params["gameover_menu"]["scoreboard"]} { - std::cout << params["texture"] << "hejsan" << std::endl; - - texture.loadFromFile(params["texture"]); + texture.loadFromFile(params["gameover_menu"]["texture"]); sprite.setTexture(texture); sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); @@ -26,7 +45,7 @@ GameOver_menu::GameOver_menu(int const points, json& params) : texture{}, textur sprite.setPosition(0, 0); // load textfont - if ( !font.loadFromFile (params["font"]) ) + if ( !font.loadFromFile (params["gameover_menu"]["font"]) ) throw std::invalid_argument ("Unable to load font"); @@ -108,7 +127,7 @@ void GameOver_menu::update(Context& context) { if (menu) { - context.next_state = std::make_unique(); + context.next_state = std::make_unique(data); } // changes color on text depending on selection @@ -133,7 +152,7 @@ void GameOver_menu::render(sf::RenderWindow& window) const { window.draw(sprite); - window.draw(rectangle); + window.draw(rectangle);uniq window.draw(mouse_l); window.draw(mouse_r); @@ -149,7 +168,7 @@ void GameOver_menu::render(sf::RenderWindow& window) const { window.close(); } -} +}uniq void GameOver_menu::handle_input(sf::Event& event) @@ -228,7 +247,7 @@ void GameOver_menu::handle_input(sf::Event& event) } // if not backspace, print letters - else if(playerInput.getSize() < 15) + else if(playerInput.getSize() < 20) { playerInput += event.text.unicode; } @@ -243,12 +262,13 @@ void GameOver_menu::handle_input(sf::Event& event) //spara namn till fil void GameOver_menu::load_to_csv(sf::Text playertext) { - std::cout << data["highscore_file"] << "hej" << std::endl; - std::cout << data["game_constants"]["scoreboard"] << "hejdå" << std::endl; + + std::cout << "hej" << std::endl; + std::string current_line{playerText.getString() + ", " + std::to_string(points)}; - std::ifstream highscore_file_r{"assets/highscore.csv"}; + std::ifstream highscore_file_r{highscore_file}; std::vector lines_read; std::vector lines_write; for (std::string one_line; std::getline(highscore_file_r, one_line);lines_read.push_back(one_line)); @@ -270,11 +290,11 @@ void GameOver_menu::load_to_csv(sf::Text playertext) } - if (lines_write.size() > data["scoreboard"]) lines_write.pop_back(); + if (lines_write.size() > scoreboard_size) lines_write.pop_back(); highscore_file_r.close(); - std::ofstream highscore_file_w{"assets/highscore.csv"}; + std::ofstream highscore_file_w{highscore_file}; for (std::string::size_type line_count{0}; line_count < lines_write.size(); highscore_file_w << lines_write[line_count++] << std::endl); highscore_file_w.close(); } \ No newline at end of file diff --git a/src/Pause_menu.cc b/src/Pause_menu.cc index 95e1cb8..4252972 100644 --- a/src/Pause_menu.cc +++ b/src/Pause_menu.cc @@ -5,21 +5,24 @@ #include "States.h" #include "Context.h" #include "constants.h" +#include "json.hpp" +using json = nlohmann::json; -Pause_menu::Pause_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, resumetext{}, startmenutext{}, - quittext{}, font{}, resume_game{false}, exit_game{false}, menu{false}, menu_index{1} +Pause_menu::Pause_menu(json& params) : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, resumetext{}, startmenutext{}, + quittext{}, font{}, resume_game{false}, exit_game{false}, menu{false}, menu_index{1}, data{params} { - texture.loadFromFile("assets/Pause_bild.png"); - sprite.setTexture(texture);; + + texture.loadFromFile(params["pause_menu"]["texture"]); + sprite.setTexture(texture); sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); sf::FloatRect gb {sprite.getGlobalBounds()}; sprite.setPosition(0, 0); // mouse - texture2.loadFromFile("assets/muspekareGul.png"); + texture2.loadFromFile(params["pause_menu"]["texture_mouse"]); mouse_l.setTexture(texture2); mouse_l.setScale(-0.1, 0.1); @@ -30,7 +33,7 @@ Pause_menu::Pause_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{} // load textfont - if ( !font.loadFromFile ("assets/fonts/Philosopher-Regular.ttf") ) + if ( !font.loadFromFile (params["pause_menu"]["font"]) ) { throw std::invalid_argument ("Unable to load font"); } @@ -63,7 +66,7 @@ void Pause_menu::update(Context& context) } else if (menu) { - context.next_state = std::make_unique(); + context.next_state = std::make_unique(data); context.saved_game.release(); } diff --git a/src/Scoreboard_menu.cc b/src/Scoreboard_menu.cc index 04f1e06..41160c8 100644 --- a/src/Scoreboard_menu.cc +++ b/src/Scoreboard_menu.cc @@ -13,11 +13,12 @@ using json = nlohmann::json; -Scoreboard_menu::Scoreboard_menu() : texture{}, texture2{}, sprite{}, menu{false}, exit_game{false}, - startmenutext{}, quittext{}, mouse_r{}, mouse_l{}, font{}, menu_index{1} +Scoreboard_menu::Scoreboard_menu(json& params) : texture{}, texture2{}, sprite{}, menu{false}, exit_game{false}, + startmenutext{}, quittext{}, mouse_r{}, mouse_l{}, font{}, menu_index{1}, data{params} + { texture.loadFromFile("assets/scoreboard.png"); - sprite.setTexture(texture);; + sprite.setTexture(texture); sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); sf::FloatRect gb {sprite.getGlobalBounds()}; @@ -85,7 +86,7 @@ void Scoreboard_menu::update(Context& context) { if (menu) { - context.next_state = std::make_unique(); + context.next_state = std::make_unique(data); } // changes color on text depending on selection diff --git a/src/Start_menu.cc b/src/Start_menu.cc index 3b29b65..263baeb 100644 --- a/src/Start_menu.cc +++ b/src/Start_menu.cc @@ -5,16 +5,18 @@ #include "States.h" #include "Context.h" #include "constants.h" +#include "json.hpp" +using json = nlohmann::json; -Start_menu::Start_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, starttext{}, scoreboardtext{}, - quittext{}, font{}, start_game{false}, exit_game{false}, scoreboard{false}, menu_index{1} +Start_menu::Start_menu(json& params) : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, starttext{}, scoreboardtext{}, + quittext{}, font{}, start_game{false}, exit_game{false}, scoreboard{false}, menu_index{1}, data{params} { //sprite - texture.loadFromFile("assets/meny_bild.png"); + texture.loadFromFile(params["start_menu"]["texture"]); sprite.setTexture(texture); sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); @@ -22,7 +24,7 @@ Start_menu::Start_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{ sprite.setPosition(0, 0); //mouse - texture2.loadFromFile("assets/muspekareGul.png"); + texture2.loadFromFile(params["start_menu"]["texture_mouse"]); mouse_l.setTexture(texture2); mouse_l.setScale(-0.1, 0.1); @@ -33,7 +35,7 @@ Start_menu::Start_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{ //load textfont - if ( !font.loadFromFile ("assets/fonts/Philosopher-Regular.ttf") ) + if ( !font.loadFromFile (params["start_menu"]["font"]) ) throw std::invalid_argument ("Unable to load font"); //start @@ -64,7 +66,7 @@ void Start_menu::update(Context& context) if (scoreboard) { - context.next_state = std::make_unique(); + context.next_state = std::make_unique(data); scoreboard = false; return; } diff --git a/src/States.cc b/src/States.cc index 5fabb60..d7a7021 100644 --- a/src/States.cc +++ b/src/States.cc @@ -55,7 +55,7 @@ void Game_state::update(Context &context) { context.saved_game.reset(static_cast(context.current_state.release())); - context.next_state = std::make_unique(); + context.next_state = std::make_unique(data); pause_game = false; return; } @@ -65,7 +65,7 @@ void Game_state::update(Context &context) int remaining_time {static_cast(data["game_constants"]["game_time"]) - static_cast(game_time)}; if (remaining_time < 0) { - context.next_state = std::make_unique(points, data["gameover_menu"]); + context.next_state = std::make_unique(points, data); return; } diff --git a/src/States.h b/src/States.h index 2cb0b45..63737fe 100644 --- a/src/States.h +++ b/src/States.h @@ -5,6 +5,7 @@ #include "SFML/Graphics.hpp" #include #include +#include #include "Map.h" #include "Player.h" @@ -59,8 +60,6 @@ private: sf::Texture main_enemy_texture; sf::Texture YF_texture; sf::Texture bike_texture; - //std::vector> yf; - json data; int points; }; @@ -68,7 +67,7 @@ private: class Start_menu : public State { public: - Start_menu(); + Start_menu(json& params); ~Start_menu() = default; void update(Context& context) override; void render(sf::RenderWindow& window) const override; @@ -88,6 +87,7 @@ private: bool start_game; bool exit_game; bool scoreboard; + json data; int menu_index; }; @@ -95,7 +95,7 @@ private: class Pause_menu : public State { public: - Pause_menu(); + Pause_menu(json& params); ~Pause_menu() = default; void update(Context& context) override; void render(sf::RenderWindow& window) const override; @@ -114,6 +114,7 @@ private: bool resume_game; bool exit_game; bool menu; + json data; int menu_index; }; @@ -122,7 +123,7 @@ private: class Scoreboard_menu : public State { public: - Scoreboard_menu(); + Scoreboard_menu(json& params); ~Scoreboard_menu() = default; void update(Context& context) override; void render(sf::RenderWindow& window) const override; @@ -141,7 +142,7 @@ private: bool exit_game; bool menu; - + json data; int menu_index; }; @@ -173,6 +174,9 @@ private: sf::Text quittext; sf::Font font; + std::string highscore_file; + int scoreboard_size; + bool exit_game; bool menu; json data; -- 2.30.2