From 6e489b2c5c8082795e68b865a79b73ab1cd556d0 Mon Sep 17 00:00:00 2001 From: malak585 Date: Thu, 30 Nov 2023 14:05:10 +0100 Subject: [PATCH] skapat load_to_csv - Alma, Lukas, Malin --- assets/highscore.csv | 10 +++---- src/Menus.cc | 71 ++++++++++++++++++++++++-------------------- src/States.cc | 3 +- src/States.h | 2 ++ 4 files changed, 47 insertions(+), 39 deletions(-) diff --git a/assets/highscore.csv b/assets/highscore.csv index ef57aeb..2d4adab 100644 --- a/assets/highscore.csv +++ b/assets/highscore.csv @@ -1,5 +1,5 @@ -Gorilla,6 -Gorilla,4 -Gorilla,4 -Gorilla,4 -Gorilla,4 +gorilla, 5 +gorilla, 4 +gorilla, 3 +gorilla, 2 +gorilla, 1 diff --git a/src/Menus.cc b/src/Menus.cc index 41a1acc..1c0738b 100644 --- a/src/Menus.cc +++ b/src/Menus.cc @@ -3,6 +3,7 @@ #include #include #include +#include #include "States.h" #include "Context.h" @@ -594,38 +595,6 @@ void GameOver_menu::update(Context& context) // mouse placement mouse_r.setPosition(((S_WIDTH / 2) + 60), ((S_HEIGHT / 2) - 45 + 50*(menu_index - 1))); mouse_l.setPosition(((S_WIDTH / 2) - 58), ((S_HEIGHT / 2) + 2 + 50*(menu_index - 1))); - - //spara namn till fil - std::string current_line{"Gorilla," + std::to_string(points)}; - - std::ifstream highscore_file_r{"assets/highscore.csv"}; - 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)); - - bool added {false}; - for (std::string::size_type line_count{0}; line_count < lines_read.size(); line_count++) - { - if (!added && points > std::stoi(lines_read[line_count].substr(lines_read[line_count].find_first_of(',') + 1))) - { - std::cout << "i loop" << std::endl; - lines_write.push_back(current_line); - added = true; - } - lines_write.push_back(lines_read[line_count]); - } - - if (!added) lines_write.push_back(current_line); - - - - if (lines_write.size() > data["game_constants"]["scoreboard"]) lines_write.pop_back(); - - highscore_file_r.close(); - - std::ofstream highscore_file_w{"assets/highscore.csv"}; - 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(); } @@ -660,6 +629,7 @@ void GameOver_menu::handle_input(sf::Event& event) if(menu_index == 1) { + load_to_csv(); menu = true; menu_index = 1; } @@ -711,4 +681,41 @@ void GameOver_menu::handle_input(sf::Event& event) playerInput += event.text.unicode; playerText.setString(playerInput); } + + } + +//spara namn till fil +void GameOver_menu::load_to_csv() const +{ + std::string current_line{"Gorilla," + std::to_string(points)}; + + std::ifstream highscore_file_r{"assets/highscore.csv"}; + 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)); + + bool added {false}; + for (std::string::size_type line_count{0}; line_count < lines_read.size(); line_count++) + { + if (!added && points > std::stoi(lines_read[line_count].substr(lines_read[line_count].find_first_of(',') + 1))) + { + std::cout << "i loop" << std::endl; + lines_write.push_back(current_line); + added = true; + } + lines_write.push_back(lines_read[line_count]); + } + + if (!added) lines_write.push_back(current_line); + + + + if (lines_write.size() > data["game_constants"]["scoreboard"]) lines_write.pop_back(); + + highscore_file_r.close(); + + std::ofstream highscore_file_w{"assets/highscore.csv"}; + 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/States.cc b/src/States.cc index 77a0b50..10c375d 100644 --- a/src/States.cc +++ b/src/States.cc @@ -59,13 +59,12 @@ void Game_state::update(Context &context) pause_game = false; return; } + game_time += context.time.asSeconds(); int remaining_time {static_cast(data["game_constants"]["game_time"]) - static_cast(game_time)}; if (remaining_time < 0) { - // LÄGG ALLT NEDAN I GAME OVER MENYN. - context.next_state = std::make_unique(points, data["game_constants"]); return; } diff --git a/src/States.h b/src/States.h index d0cdefc..d4e622d 100644 --- a/src/States.h +++ b/src/States.h @@ -154,6 +154,8 @@ public: void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; + void load_to_csv() const; + private: sf::Texture texture; sf::Texture texture2; -- 2.30.2