From: stian853 Date: Wed, 6 Dec 2023 13:40:20 +0000 (+0100) Subject: Changed to softcoding, Nils, Arvid, Stina X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=fc60db0b5397f70484a972f1109f7c4b1a47b355;p=TDDC76_proj.git Changed to softcoding, Nils, Arvid, Stina --- diff --git a/assets/data.json b/assets/data.json index c7a2d2a..635963f 100644 --- a/assets/data.json +++ b/assets/data.json @@ -66,6 +66,17 @@ "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 index 6a36a83..0000000 Binary files a/assets/fighter.png and /dev/null differ diff --git a/src/Bottle.cc b/src/Bottle.cc index e414dce..3e642e8 100644 --- a/src/Bottle.cc +++ b/src/Bottle.cc @@ -4,7 +4,7 @@ #include "States.h" #include -Bottle::Bottle(sf::Texture& txtr, json& params) +Bottle::Bottle(sf::Texture& txtr, json& params) { texture = txtr; sprite.setTexture(texture); diff --git a/src/Helper.cc b/src/Helper.cc index 9280f94..41e0d55 100644 --- a/src/Helper.cc +++ b/src/Helper.cc @@ -7,13 +7,13 @@ #include -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]); diff --git a/src/Helper.h b/src/Helper.h index 0eb246b..3b2cb6e 100644 --- a/src/Helper.h +++ b/src/Helper.h @@ -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; diff --git a/src/Map.cc b/src/Map.cc index 7e62975..1271507 100644 --- a/src/Map.cc +++ b/src/Map.cc @@ -2,12 +2,10 @@ #include "constants.h" #include -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); diff --git a/src/Map.h b/src/Map.h index 41daf14..12cdbca 100644 --- 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); diff --git a/src/Player.cc b/src/Player.cc index 1f131ce..2a9ee4b 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -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"); diff --git a/src/Player.h b/src/Player.h index 5303611..0d2d698 100644 --- a/src/Player.h +++ b/src/Player.h @@ -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; diff --git a/src/States.cc b/src/States.cc index 9c769f6..9129afd 100644 --- a/src/States.cc +++ b/src/States.cc @@ -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_texture, sack_texture, data["player"]); + helper = std::make_unique(helper_texture, data["helper"]); - player = std::make_unique(data["player"]); - helper = std::make_unique(data["helper"]); - game_map = std::make_unique(data["map"]); enemy = std::make_unique(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_texture, bottle_texture, data["map"]); + f.close(); } @@ -64,38 +75,6 @@ void Game_state::update(Context &context) 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. - 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(); - context.next_state = std::make_unique(); return; } diff --git a/src/States.h b/src/States.h index 9e7505c..5e4a754 100644 --- a/src/States.h +++ b/src/States.h @@ -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> yf;