Changed to softcoding, Nils, Arvid, Stina
authorstian853 <stian853@su15-110.ad.liu.se>
Wed, 6 Dec 2023 13:40:20 +0000 (14:40 +0100)
committerstian853 <stian853@su15-110.ad.liu.se>
Wed, 6 Dec 2023 13:40:20 +0000 (14:40 +0100)
assets/data.json
assets/fighter.png [deleted file]
src/Bottle.cc
src/Helper.cc
src/Helper.h
src/Map.cc
src/Map.h
src/Player.cc
src/Player.h
src/States.cc
src/States.h

index c7a2d2a19e83c629d02b68b55a9ebf73dc45038b..635963fe688e164b1c2ca5b92e160afdbb0e60d0 100644 (file)
         "direction": [0, 1],
         "sin_amplitude": 50,
         "sin_omega": 5
+    },
+    "game_state_assets":
+    {
+        "kir_file": "assets/kir.png",
+        "YF_file": "assets/YF.png",
+        "cyklist_file": "assets/cyklist.png",
+        "ling_file": "assets/6Ling_figur2.png",
+        "sack_file": "assets/sack.png",
+        "player_file": "assets/4V_figur.png",
+        "helper_file": "assets/ysex.png",
+        "map_file": "assets/bakgrund.png"
     }
 
 }
\ No newline at end of file
diff --git a/assets/fighter.png b/assets/fighter.png
deleted file mode 100644 (file)
index 6a36a83..0000000
Binary files a/assets/fighter.png and /dev/null differ
index e414dce7bc18f7a6678eb575304c5fa8c784a272..3e642e8456a3af39bdb177d2f866b20170199036 100644 (file)
@@ -4,7 +4,7 @@
 #include "States.h"
 #include <random>
 
-Bottle::Bottle(sf::Texture& txtr, json& params) 
+Bottle::Bottle(sf::Texture& txtr, json& params)
 {
     texture = txtr;
     sprite.setTexture(texture);
index 9280f9449c781afff8af9bfbca4c6e55a5723da9..41e0d554997d5983252c3dc797402292fd7b4677 100644 (file)
@@ -7,13 +7,13 @@
 
 #include <iostream>
 
-Helper::Helper(json& params) : stop_bot{params["stop_bot"]}, stop_top{params["stop_top"]}, max_speed{params["max_speed"]}, movement{}
+Helper::Helper(sf::Texture& txtr, json& params) : stop_bot{params["stop_bot"]}, stop_top{params["stop_top"]}, max_speed{params["max_speed"]}, movement{}
 {
+    texture = txtr;
     position = {params["start_pos"][0], params["start_pos"][1]};
     direction = {params["direction"][0], params["direction"][1]};
     movement = direction;
 
-    texture.loadFromFile("assets/ysex.png");
     sprite.setTexture(texture);
 
     sprite.setScale(params["scale"][0], params["scale"][1]);
index 0eb246b4bbbcbbd4c3369e2e8823263ff271f084..3b2cb6e4b2af1ad05251eecebc907516d4027d97 100644 (file)
@@ -11,7 +11,7 @@ using json = nlohmann::json;
 class Helper : public Object
 {
 public:
-    Helper(json& params);
+    Helper(sf::Texture& txtr, json& params);
     ~Helper() = default;
 
     void collision(Object& other) override;
index 7e62975d0cb0d1448c793c1646368a5a2e497519..1271507f2c0b8c71917168ecf84b2eb34fed5c86 100644 (file)
@@ -2,12 +2,10 @@
 #include "constants.h"
 #include <string>
 
-Map::Map(json& params) : texture{}, sprite{}, point_text{}, collected_text{}, time_text{}, bottle_texture{}, bottle_sprite{}, font{}
+Map::Map(sf::Texture& txtr, sf::Texture& bottle_txtr, json& params) : texture{txtr}, sprite{}, point_text{}, collected_text{}, time_text{}, bottle_texture{bottle_txtr}, bottle_sprite{}, font{}
 {
-    texture.loadFromFile("assets/bakgrund.png");
     sprite.setTexture(texture);
 
-    bottle_texture.loadFromFile("assets/kir.png");
     bottle_sprite.setTexture(bottle_texture);
     sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF);
     sprite.setPosition(0,0);
index 41daf1481b3a2a844a14c54f171707fd3c5e6cba..12cdbca895d20be620dd59fb45f416b71d97dc13 100644 (file)
--- a/src/Map.h
+++ b/src/Map.h
@@ -10,7 +10,7 @@ using json = nlohmann::json;
 class Map
 {
 public:
-    Map(json& params);
+    Map(sf::Texture& txtr, sf::Texture& bottle_txtr, json& params);
     ~Map() = default;
 
     void update(int const collected, int const points, int const time_left);
index 1f131ce99754c6e920328274c93003ddecff0567..2a9ee4b3a33c3b3d12c98b862470cd67f1146dfb 100644 (file)
@@ -6,8 +6,9 @@
 #include "Context.h"
 #include "constants.h"
 
-Player::Player(json& params) : collected{0}, max_speed{params["max_speed"]}, tumbling{false}, tumble_degrees{0}, rotation{params["degrees_per_second"]}
+Player::Player(sf::Texture& player_txtr, sf::Texture& sack_txtr, json& params) : sack_texture{sack_txtr}, collected{0}, max_speed{params["max_speed"]}, tumbling{false}, tumble_degrees{0}, rotation{params["degrees_per_second"]}
 {
+    texture = player_txtr;
     position = {params["start_pos"][0], params["start_pos"][1]};
 
     texture.loadFromFile("assets/4V_figur.png");
index 53036119eb36c7e73623ff11687ee134e6185ebc..0d2d698c5e58737843474d6d93a2ca06dd420fdd 100644 (file)
@@ -16,7 +16,7 @@ using json = nlohmann::json;
 class Player : public Moving_object
 {
 public:
-    Player(json& params);
+    Player(sf::Texture& player_txtr, sf::Texture& sack_txtr, json& params);
     ~Player() = default;
     
     void collision(Object& other) override;
index 9c769f6d3a4fed8d722767d475865a86cb9234b5..9129afd0401f50878928f987ebe9c984d7cbd697 100644 (file)
@@ -20,7 +20,6 @@ Game_state::Game_state() :
     bottles{}, 
     helper{},
     bike{},
-    main_enemy_texture{},
     time_since_last_bottle{0.0f},
     game_time{0},
     time_since_last_yf{0.0f},
@@ -35,17 +34,29 @@ Game_state::Game_state() :
 {
     std::ifstream f("assets/data.json");
     data = json::parse(f);
+    sf::Texture main_enemy_texture;
+    sf::Texture sack_texture;
+    sf::Texture player_texture;
+    sf::Texture helper_texture;
+    sf::Texture map_texture;
 
-    main_enemy_texture.loadFromFile("assets/6Ling_figur2.png");
+    main_enemy_texture.loadFromFile(data["game_state_assets"]["ling_file"]);
+    sack_texture.loadFromFile(data["game_state_assets"]["sack_file"]);
+    player_texture.loadFromFile(data["game_state_assets"]["player_file"]);
+    helper_texture.loadFromFile(data["game_state_assets"]["helper_file"]);
+    map_texture.loadFromFile(data["game_state_assets"]["map_file"]);
+
+    player = std::make_unique<Player>(player_texture, sack_texture, data["player"]);
+    helper = std::make_unique<Helper>(helper_texture, data["helper"]);
 
-    player = std::make_unique<Player>(data["player"]);
-    helper = std::make_unique<Helper>(data["helper"]);
-    game_map = std::make_unique<Map>(data["map"]);
     enemy = std::make_unique<Main_enemy>(main_enemy_texture, data["main_enemy"]);
 
-    bottle_texture.loadFromFile("assets/kir.png");
-    YF_texture.loadFromFile("assets/YF.png");
-    bike_texture.loadFromFile("assets/cyklist.png");
+    bottle_texture.loadFromFile(data["game_state_assets"]["kir_file"]);
+    YF_texture.loadFromFile(data["game_state_assets"]["YF_file"]);
+    bike_texture.loadFromFile(data["game_state_assets"]["cyklist_file"]);
+    
+    game_map = std::make_unique<Map>(map_texture, bottle_texture, data["map"]);
+    
     f.close();
 }
 
@@ -64,38 +75,6 @@ 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<GameOver_menu>();
         return;
     }
index 9e7505c76c3b3cacf22d529b827481bb97a31a28..5e4a7543957b7b294c1e6a734c06193e0332b805 100644 (file)
@@ -17,8 +17,6 @@
 
 using json = nlohmann::json;
 
-
-
 struct Context;
 
 class State
@@ -56,7 +54,6 @@ private:
 
     bool pause_game;
     sf::Texture bottle_texture;
-    sf::Texture main_enemy_texture;
     sf::Texture YF_texture;
     sf::Texture bike_texture;
     //std::vector<std::unique_ptr<YF>> yf;