incomplete json chaos
authormalak585 <malak585@su15-111.ad.liu.se>
Wed, 6 Dec 2023 13:43:18 +0000 (14:43 +0100)
committermalak585 <malak585@su15-111.ad.liu.se>
Wed, 6 Dec 2023 13:43:18 +0000 (14:43 +0100)
src/GameOver_menu.cc
src/Pause_menu.cc
src/Scoreboard_menu.cc
src/Start_menu.cc
src/States.cc
src/States.h

index d034a8fc5f165a80793d0e8dd04d8a3f625681e3..9718f194d1a1b257f1efa7f2557f6810fb7a2c75 100644 (file)
 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<Start_menu>();
+        context.next_state = std::make_unique<Start_menu>(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<std::string> lines_read;
     std::vector<std::string> 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
index 95e1cb814bbb39695dca938a87524a459943651b..42529727b1f2b466d407d2e291f17ab68584564d 100644 (file)
@@ -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<Start_menu>();
+        context.next_state = std::make_unique<Start_menu>(data);
         context.saved_game.release();
     }
  
index 04f1e0683635bb5d296fc952d568b47fcf5e28d6..41160c8230a2fb2490750e9a32c542cdcbf4b4b5 100644 (file)
 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<Start_menu>();
+        context.next_state = std::make_unique<Start_menu>(data);
     }
  
     // changes color on text depending on selection
index 3b29b659625050d1d3b8c04b6706ac3cc93e6cb6..263baebca6e8fbd4fb6d330c239a11a4d7654abf 100644 (file)
@@ -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<Scoreboard_menu>();
+        context.next_state = std::make_unique<Scoreboard_menu>(data);
         scoreboard = false;
         return;
     }
index 5fabb60235051abf18b9dd05e8da76a161fc1b67..d7a702187ae3f815bc1ce900119bc8e848661062 100644 (file)
@@ -55,7 +55,7 @@ void Game_state::update(Context &context)
     {
         context.saved_game.reset(static_cast<Game_state *>(context.current_state.release()));
 
-        context.next_state = std::make_unique<Pause_menu>();
+        context.next_state = std::make_unique<Pause_menu>(data);
         pause_game = false;
         return;
     }
@@ -65,7 +65,7 @@ void Game_state::update(Context &context)
     int remaining_time {static_cast<int>(data["game_constants"]["game_time"]) - static_cast<int>(game_time)};
     if (remaining_time < 0)
     {
-        context.next_state = std::make_unique<GameOver_menu>(points, data["gameover_menu"]);
+        context.next_state = std::make_unique<GameOver_menu>(points, data);
         return;
     }
  
index 2cb0b45a9158eb674a73cf1d4cdb8b2ef9f45f40..63737fea781cad484e7338843e05d2489c15246b 100644 (file)
@@ -5,6 +5,7 @@
 #include "SFML/Graphics.hpp"
 #include <vector>
 #include <memory>
+#include <fstream>
 
 #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<std::unique_ptr<YF>> 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;