added scoreboard and etc
authorlukel495 <lukel495@su15-110.ad.liu.se>
Mon, 27 Nov 2023 10:18:34 +0000 (11:18 +0100)
committerlukel495 <lukel495@su15-110.ad.liu.se>
Mon, 27 Nov 2023 10:18:34 +0000 (11:18 +0100)
assets/data.json
assets/highscore.csv
src/Map.cc
src/Map.h
src/States.cc
src/_main.cc

index ecbd6def66d8727e10e97ece2682617d6ff419c5..63de909889547f521bf950ce4611f9b3b6cc93b7 100644 (file)
@@ -28,8 +28,9 @@
     "game_constants":
     {
         "bottles_per_second" : 2,
-        "game_time" : 20,
-        "yf_per_second": 0.125
+        "game_time" : 3,
+        "yf_per_second": 0.125,
+        "scoreboard" : 5
         
     },
     "map":
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..16aabef13ad3fb9ac1e91cdea70899d077246e55 100644 (file)
@@ -0,0 +1,5 @@
+Gorilla,4
+Gorilla,3
+Stina,2
+Gorilla,2
+Gorilla,0
index 0251acae9e5a30db410cf7426af764400c75946a..de8879f0f4f42b2f62d2873ded2158f9f7b1c321 100644 (file)
@@ -21,9 +21,9 @@ Map::Map(json& params) : texture{}, sprite{}, point_text{}, font{}
 
 }
 
-void Map::update(int const points, int const game_time)
+void Map::update(int const collected, int const points, int const time_left)
 {
-    point_text.setString("Points: " + std::to_string(points) + "\n" + "Time left: " + std::to_string(game_time) + " s");
+    point_text.setString("Collected: " + std::to_string(collected) + "\n" + "Points: " + std::to_string(points) + "\n" + "Time left: " + std::to_string(time_left) + " s");
 }
 
 void Map::render(sf::RenderWindow& window) const
index 5d2c9c1866b628a06a4be911d059ff32e3b53b54..f7a5b06c7a73431e055edcb7bb6f6b8746feabaa 100644 (file)
--- a/src/Map.h
+++ b/src/Map.h
@@ -12,7 +12,7 @@ public:
     Map(json& params);
     ~Map() = default;
 
-    void update(int const points, int const game_state);
+    void update(int const collected, int const points, int const time_left);
     void render(sf::RenderWindow& window) const;
 protected:
 private:
index 48ac63fe4a8598cfaeb94c9f5e735d5b946c5074..364841499708c9bf968b69fd2644fdee626227dc 100644 (file)
@@ -7,6 +7,8 @@
 
 #include "json.hpp"
 #include <fstream>
+#include <string>
+#include <vector>
 #include <cmath>
 
 using json = nlohmann::json;
@@ -40,6 +42,7 @@ Game_state::Game_state() :
 
     bottle_texture.loadFromFile("assets/kir.png");
     YF_texture.loadFromFile("assets/YF.png");
+    f.close();
 }
 
 void Game_state::update(Context &context)
@@ -57,6 +60,38 @@ 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)
     {
+        // LÄGG ALLT NEDAN I GAME OVER MENYN.
+        std::string current_line{"Gorilla," + std::to_string(points)};
+
+        std::ifstream highscore_file_r{"assets/highscore.csv"};
+        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));
+
+        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();
+
         context.next_state = std::make_unique<Start_menu>();
         return;
     }
@@ -116,7 +151,7 @@ void Game_state::update(Context &context)
 
     }
     enemy->update(context);
-    game_map->update(points, remaining_time);
+    game_map->update(player->get_collected(), points, remaining_time);
     if (yf.size()>0)
     {
         for(unsigned int i {0}; i < yf.size(); ++i)
@@ -381,7 +416,7 @@ void Pause_menu::update(Context &context)
     else if (menu)
     {
         context.next_state = std::make_unique<Start_menu>();
-        context.saved_game.release();
+        context.saved_game.reset();
     }
  
     // changes color on text depending on selection
index 913a5bbfc158e4e71b13586d69259739d1e2d64c..29b2e2dd3561f1139a8919db6dc7e46009dc34dd 100644 (file)
@@ -48,7 +48,6 @@ int main ()
         //om vi har bytt så ska vi byta state, ex Esc hopp till paus meny
         if (game_context.next_state != nullptr)
         {
-            game_context.current_state.release();
             game_context.current_state = std::move(game_context.next_state);
             game_context.next_state = nullptr;