worked on saving points on game over menu - alma, lukas, malin
authormalak585 <malak585@su10-107.ad.liu.se>
Wed, 29 Nov 2023 14:20:30 +0000 (15:20 +0100)
committermalak585 <malak585@su10-107.ad.liu.se>
Wed, 29 Nov 2023 14:20:30 +0000 (15:20 +0100)
assets/highscore.csv
src/Menus.cc
src/States.cc
src/States.h

index 7e7754f2456a6754297f0743ff2fe5b5d36f5702..ef57aeb62832754ceea05fd147bfa26e2ccdf2e6 100644 (file)
@@ -1,5 +1,5 @@
 Gorilla,6
 Gorilla,4
-Gorilla,3
-Stina,2
-Gorilla,2
+Gorilla,4
+Gorilla,4
+Gorilla,4
index dff5ae569efd8b1d09fb978b9d924306f6583b73..41a1acc2aa3043ae7da88339d04baaec62a10994 100644 (file)
@@ -1,9 +1,15 @@
 #include <memory>
 #include <iostream>
+#include <string>
+#include <fstream>
+#include <vector>
 
 #include "States.h"
 #include "Context.h"
 #include "constants.h"
+#include "json.hpp"
+
+using json = nlohmann::json;
 
 
 // Start menu --------------------------------------------
@@ -371,15 +377,15 @@ Scoreboard_menu::Scoreboard_menu() : texture{}, texture2{}, sprite{}, menu{false
     sf::FloatRect gbs {scoreboardtext.getGlobalBounds()};
     scoreboardtext.setOrigin(gbs.width / 2, gbs.height / 2);
     scoreboardtext.setPosition (S_WIDTH / 2 - 15, ((S_HEIGHT / 2) - 120));
-    scoreboardtext.setFillColor(sf::Color::Red);
+    scoreboardtext.setFillColor(sf::Color(153, 0, 0));
 
     // -----------------------------------------------------------------------
     // First place text
-    firstplacetext = sf::Text{ "1. NAME 100p\n 2. NAME 39p" , font, 18 };
+    firstplacetext = sf::Text{ "1. NAME 100p\n2. NAME 39p" , font, 18 };
     sf::FloatRect gbfp {firstplacetext.getGlobalBounds()};
     firstplacetext.setOrigin(gbfp.width / 2, gbfp.height / 2);
     firstplacetext.setPosition (S_WIDTH / 2 - 15, ((S_HEIGHT / 2) - 70));
-    firstplacetext.setFillColor(sf::Color::Red);
+    firstplacetext.setFillColor(sf::Color(153, 0, 0));
  // -----------------------------------------------------------------------
    
     // Start menu text
@@ -407,11 +413,11 @@ void Scoreboard_menu::update(Context& context)
     if( menu_index == 1)
     {
         startmenutext.setFillColor(sf::Color::Black);
-        quittext.setFillColor(sf::Color::Red);
+        quittext.setFillColor(sf::Color(153, 0, 0));
     }
     else if( menu_index == 2)
     {
-        startmenutext.setFillColor(sf::Color::Red);
+        startmenutext.setFillColor(sf::Color(153, 0, 0));
         quittext.setFillColor(sf::Color::Black); 
     }
 
@@ -498,8 +504,8 @@ void Scoreboard_menu::handle_input(sf::Event& event)
 
 
 // GameOver menu --------------------------------------------
-GameOver_menu::GameOver_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, startmenutext{},
-                                 quittext{},  font{}, exit_game{false}, menu{false}, menu_index{1}
+GameOver_menu::GameOver_menu(int const points, json& params) : texture{}, texture2{}, sprite{}, rectangle{},  mouse_l{}, mouse_r{}, pointstext{}, 
+                                                 playerInput{}, data{}, playerText{}, savetext{}, quittext{},  font{}, exit_game{false}, menu{false}, menu_index{1}, points{points}
 {
     texture.loadFromFile("assets/game_over.png");
     sprite.setTexture(texture);;
@@ -507,7 +513,38 @@ GameOver_menu::GameOver_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mou
     sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF);
     sf::FloatRect gb {sprite.getGlobalBounds()};
     sprite.setPosition(0, 0);
-   
+
+    // load textfont
+    if ( !font.loadFromFile ("assets/fonts/Philosopher-Regular.ttf") )
+        throw std::invalid_argument ("Unable to load font");
+
+
+    // transparent box
+    rectangle = sf::CircleShape{200};
+    rectangle.setFillColor(sf::Color(255, 255, 0, 128));
+    rectangle.setOutlineThickness(7);
+    rectangle.setOutlineColor(sf::Color( 0, 0, 0, 200));
+    sf::FloatRect gbrs {rectangle.getGlobalBounds()};
+    rectangle.setOrigin(gbrs.width / 2, gbrs.height / 2);
+    rectangle.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2)));
+
+
+    // Points text
+    pointstext = sf::Text{ "Your points: " + std::to_string(points), font, 30 };
+    sf::FloatRect gbp {pointstext.getGlobalBounds()};
+    pointstext.setOrigin(gbp.width / 2, gbp.height / 2);
+    pointstext.setPosition (S_WIDTH / 2 - 70, ((S_HEIGHT / 2) - 150));
+    pointstext.setFillColor(sf::Color::Black); 
+
+
+    //player text input
+    playerText.setFont( font );
+    playerText.setCharacterSize( 24 );
+    sf::FloatRect gbpt {playerText.getGlobalBounds()};
+    playerText.setOrigin(gbpt.width / 2, gbpt.height / 2);
+    playerText.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2)));
+    playerText.setFillColor(sf::Color::Black); 
+
 
     // mouse
     texture2.loadFromFile("assets/muspekareGul.png");
@@ -521,22 +558,17 @@ GameOver_menu::GameOver_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mou
     mouse_r.setScale(0.1, 0.1);
 
 
-    // load textfont
-    if ( !font.loadFromFile ("assets/fonts/Philosopher-Regular.ttf") )
-        throw std::invalid_argument ("Unable to load font");
-
-
     // Start menu text
-    startmenutext = sf::Text{ "Start menu", font, 24 };
-    sf::FloatRect gbr {startmenutext.getGlobalBounds()};
-    startmenutext.setOrigin(gbr.width / 2, gbr.height / 2);
-    startmenutext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) - 30));
+    savetext = sf::Text{ "Save Game", font, 24 };
+    sf::FloatRect gbr {savetext.getGlobalBounds()};
+    savetext.setOrigin(gbr.width / 2, gbr.height / 2);
+    savetext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) + 100));
 
     // Quit text
     quittext = sf::Text{ "Quit", font, 24 };
     sf::FloatRect gbs {quittext.getGlobalBounds()};
     quittext.setOrigin(gbs.width / 2, gbs.height / 2);
-    quittext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) + 20));
+    quittext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) + 150));
 }
 
 
@@ -550,28 +582,64 @@ void GameOver_menu::update(Context& context)
     // changes color on text depending on selection
     if( menu_index == 1)
     {
-        startmenutext.setFillColor(sf::Color::Yellow);
+        savetext.setFillColor(sf::Color::Yellow);
         quittext.setFillColor(sf::Color::Black);
     }
     else if( menu_index == 2)
     {
-        startmenutext.setFillColor(sf::Color::Black);
+        savetext.setFillColor(sf::Color::Black);
         quittext.setFillColor(sf::Color::Yellow); 
     }
 
     // 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<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();
 }
 
 
 void GameOver_menu::render(sf::RenderWindow& window) const
 {
     window.draw(sprite);
+    
+    window.draw(rectangle);
     window.draw(mouse_l);
     window.draw(mouse_r);
 
-    window.draw(startmenutext);
+    window.draw(playerText);
+    window.draw(pointstext);
+    window.draw(savetext);
     window.draw(quittext);
 
     if (exit_game)
@@ -636,4 +704,11 @@ void GameOver_menu::handle_input(sf::Event& event)
     default:
         break;
     }
+
+    // enter name
+    if (event.type == sf::Event::TextEntered)
+    {
+        playerInput += event.text.unicode;
+        playerText.setString(playerInput);
+    }
 }
index 874c966528d3c39da841941362d4236dcdd04dfe..77a0b50c93a2d0f569b8d5b9db71c453e52fca21 100644 (file)
@@ -65,38 +65,8 @@ void Game_state::update(Context &context)
     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<GameOver_menu>();
+        context.next_state = std::make_unique<GameOver_menu>(points, data["game_constants"]);
         return;
     }
  
index 9e7505c76c3b3cacf22d529b827481bb97a31a28..d0cdefc67d9550da48642df3e091c290cba84698 100644 (file)
@@ -149,7 +149,7 @@ private:
 class GameOver_menu : public State
 {
 public:
-    GameOver_menu();
+    GameOver_menu(int const points, json& params);
     ~GameOver_menu() = default;
     void update(Context& context) override;
     void render(sf::RenderWindow& window) const override;
@@ -158,16 +158,21 @@ private:
     sf::Texture texture;
     sf::Texture texture2;
     sf::Sprite sprite;
+    sf::CircleShape rectangle;
     sf::Sprite mouse_l;
     sf::Sprite mouse_r;
-    sf::Text startmenutext;    
+    sf::String playerInput;
+    sf::Text playerText;
+    sf::Text pointstext;
+    sf::Text savetext;    
     sf::Text quittext;
     sf::Font font;
 
     bool exit_game;
     bool menu;
-
+    json data;
     int menu_index;
+    int points;
 };