From a83e269bd4868478dbc501c69af0402f9742910e Mon Sep 17 00:00:00 2001 From: stian853 Date: Wed, 6 Dec 2023 15:57:13 +0100 Subject: [PATCH] Massive changes, Nils, Lukas, Stina, Arvid, Alma, Malin --- assets/data.json | 18 +++++++++--------- assets/highscore.csv | 7 ++----- {src => assets/oldsrc}/Menus.cc | 0 src/Bike_enemy.cc | 6 ++---- src/Bike_enemy.h | 4 ++-- src/Bottle.cc | 5 +++-- src/Bottle.h | 1 + src/Context.h | 4 +++- src/GameOver_menu.cc | 30 ++++++++++++------------------ src/Helper.cc | 8 +++----- src/Helper.h | 3 ++- src/Main_enemy.cc | 8 +++----- src/Main_enemy.h | 6 +++--- src/Map.cc | 1 + src/Map.h | 3 ++- src/Pause_menu.cc | 12 +++++------- src/Player.cc | 2 -- src/Player.h | 3 ++- src/Scoreboard_menu.cc | 15 ++++++--------- src/Start_menu.cc | 18 ++++++++---------- src/States.cc | 15 ++++----------- src/States.h | 9 ++------- src/Static_object.h | 3 ++- src/YF.cc | 7 +++---- src/YF.h | 5 +++-- src/_main.cc | 12 ++++++++++-- 26 files changed, 93 insertions(+), 112 deletions(-) rename {src => assets/oldsrc}/Menus.cc (100%) diff --git a/assets/data.json b/assets/data.json index 94064f1..45662d1 100644 --- a/assets/data.json +++ b/assets/data.json @@ -29,11 +29,12 @@ "game_constants": { "scoreboard" : 5, - "bottles_per_second" : 2, - "game_time" : 2, + "bottles_per_second" : 1.5, + "game_time" : 10, "yf_per_second": 0.125, "bikes_per_second": 0.17, - "point_scale_div": 3.0 + "point_scale_div": 3.0, + "highscore_file": "assets/highscore.csv" }, "map": { @@ -66,6 +67,7 @@ "sin_amplitude": 50, "sin_omega": 5 }, + "game_state_assets": { "kir_file": "assets/kir.png", @@ -79,12 +81,11 @@ }, "gameover_menu": { - "scoreboard" : 5, "texture": "assets/game_over.png", - "font" : "assets/fonts/Philosopher-Regular.ttf", - "highscore_file": "assets/highscore.csv" - + "texture_mouse" : "assets/muspekareGul.png", + "font" : "assets/fonts/Philosopher-Regular.ttf" }, + "pause_menu": { "texture": "assets/Pause_bild.png", @@ -95,8 +96,7 @@ { "texture": "assets/scoreboard.png", "texture_mouse" : "assets/muspekareGul.png", - "font": "assets/fonts/Philosopher-Regular.ttf", - "highscore_file": "assets/highscore.csv" + "font": "assets/fonts/Philosopher-Regular.ttf" }, "start_menu": { diff --git a/assets/highscore.csv b/assets/highscore.csv index f5bc581..1def999 100644 --- a/assets/highscore.csv +++ b/assets/highscore.csv @@ -1,5 +1,2 @@ -Gorilla,50 -Gorilla,36 -Gorilla,32 -Gorilla,30 -Gorilla,27 +Nisseboi, 5 +Lukas is KUNG, 5 diff --git a/src/Menus.cc b/assets/oldsrc/Menus.cc similarity index 100% rename from src/Menus.cc rename to assets/oldsrc/Menus.cc diff --git a/src/Bike_enemy.cc b/src/Bike_enemy.cc index ed20f0e..fcaed47 100644 --- a/src/Bike_enemy.cc +++ b/src/Bike_enemy.cc @@ -1,11 +1,9 @@ +#include +#include #include "Bike_enemy.h" #include "constants.h" #include "Context.h" -#include "States.h" -#include -#include - Bike_enemy::Bike_enemy(sf::Texture& txtr, json& params) : max_speed{params["max_speed"]}, elapsed_time{0}, start_x{0}, sin_amplitude{params["sin_amplitude"]}, sin_omega{params["sin_omega"]} { diff --git a/src/Bike_enemy.h b/src/Bike_enemy.h index 58cc348..03b88d5 100644 --- a/src/Bike_enemy.h +++ b/src/Bike_enemy.h @@ -1,9 +1,9 @@ #ifndef BIKE_ENEMY_H #define BIKE_ENEMY_H -#include "Moving_object.h" -#include "Object.h" #include + +#include "Moving_object.h" #include "json.hpp" using json = nlohmann::json; diff --git a/src/Bottle.cc b/src/Bottle.cc index 3e642e8..2ead946 100644 --- a/src/Bottle.cc +++ b/src/Bottle.cc @@ -1,8 +1,9 @@ +#include + #include "Bottle.h" #include "constants.h" #include "Context.h" -#include "States.h" -#include + Bottle::Bottle(sf::Texture& txtr, json& params) { diff --git a/src/Bottle.h b/src/Bottle.h index db00f03..15d6056 100644 --- a/src/Bottle.h +++ b/src/Bottle.h @@ -4,6 +4,7 @@ #include #include "Static_object.h" + #include "json.hpp" using json = nlohmann::json; diff --git a/src/Context.h b/src/Context.h index 327387d..f439b24 100644 --- a/src/Context.h +++ b/src/Context.h @@ -1,8 +1,10 @@ #ifndef CONTEXT_H #define CONTEXT_H -#include "States.h" #include +#include + +#include "States.h" struct Context { diff --git a/src/GameOver_menu.cc b/src/GameOver_menu.cc index 9718f19..181ce75 100644 --- a/src/GameOver_menu.cc +++ b/src/GameOver_menu.cc @@ -1,13 +1,12 @@ #include -#include #include #include #include -#include #include "States.h" #include "Context.h" #include "constants.h" + #include "json.hpp" using json = nlohmann::json; @@ -26,18 +25,17 @@ GameOver_menu::GameOver_menu(int const points, json& params) : pointstext{}, playerInput{}, playerText{}, - data{params}, + data{}, savetext{}, quittext{}, font{}, exit_game{false}, menu{false}, menu_index{1}, - points{points}, - highscore_file{params["gameover_menu"]["highscore_file"]}, - scoreboard_size{params["gameover_menu"]["scoreboard"]} + points{points} { - texture.loadFromFile(params["gameover_menu"]["texture"]); + data = std::move(params); + texture.loadFromFile(data["gameover_menu"]["texture"]); sprite.setTexture(texture); sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); @@ -45,7 +43,7 @@ GameOver_menu::GameOver_menu(int const points, json& params) : sprite.setPosition(0, 0); // load textfont - if ( !font.loadFromFile (params["gameover_menu"]["font"]) ) + if ( !font.loadFromFile (data["gameover_menu"]["font"]) ) throw std::invalid_argument ("Unable to load font"); @@ -99,7 +97,7 @@ GameOver_menu::GameOver_menu(int const points, json& params) : // mouse - texture2.loadFromFile("assets/muspekareRed.png"); + texture2.loadFromFile(data["gameover_menu"]["texture_mouse"]); mouse_l.setTexture(texture2); mouse_l.setScale(-0.1, 0.1); @@ -152,7 +150,7 @@ void GameOver_menu::render(sf::RenderWindow& window) const { window.draw(sprite); - window.draw(rectangle);uniq + window.draw(rectangle); window.draw(mouse_l); window.draw(mouse_r); @@ -168,8 +166,7 @@ void GameOver_menu::render(sf::RenderWindow& window) const { window.close(); } -}uniq - +} void GameOver_menu::handle_input(sf::Event& event) { @@ -262,13 +259,10 @@ void GameOver_menu::handle_input(sf::Event& event) //spara namn till fil void GameOver_menu::load_to_csv(sf::Text playertext) { - - std::cout << "hej" << std::endl; - std::string current_line{playerText.getString() + ", " + std::to_string(points)}; - std::ifstream highscore_file_r{highscore_file}; + std::ifstream highscore_file_r{data["game_constants"]["highscore_file"]}; 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)); @@ -290,11 +284,11 @@ void GameOver_menu::load_to_csv(sf::Text playertext) } - if (lines_write.size() > scoreboard_size) lines_write.pop_back(); + if (lines_write.size() > data["game_constants"]["scoreboard"]) lines_write.pop_back(); highscore_file_r.close(); - std::ofstream highscore_file_w{highscore_file}; + std::ofstream highscore_file_w{data["game_constants"]["highscore_file"]}; 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/Helper.cc b/src/Helper.cc index 41e0d55..7bcd2a7 100644 --- a/src/Helper.cc +++ b/src/Helper.cc @@ -1,11 +1,9 @@ -#include "Helper.h" -#include "constants.h" -#include "States.h" -#include "Context.h" #include -#include +#include "Helper.h" +#include "constants.h" +#include "Context.h" Helper::Helper(sf::Texture& txtr, json& params) : stop_bot{params["stop_bot"]}, stop_top{params["stop_top"]}, max_speed{params["max_speed"]}, movement{} { diff --git a/src/Helper.h b/src/Helper.h index 3b2cb6e..5db3cea 100644 --- a/src/Helper.h +++ b/src/Helper.h @@ -4,11 +4,12 @@ #include #include "Autonomous_object.h" + #include "json.hpp" using json = nlohmann::json; -class Helper : public Object +class Helper : public Autonomous_object { public: Helper(sf::Texture& txtr, json& params); diff --git a/src/Main_enemy.cc b/src/Main_enemy.cc index f62c459..f9f9d9e 100644 --- a/src/Main_enemy.cc +++ b/src/Main_enemy.cc @@ -1,6 +1,5 @@ #include -#include -#include + #include "Main_enemy.h" #include "States.h" #include "Context.h" @@ -78,9 +77,8 @@ sf::Vector2f Main_enemy::find_closest_bottle(std::vector //return {0,0}; unsigned int closest_bottle_index {0}; - float min_dist {10000000.0f}; - - for(unsigned int i {0}; i < bottles.size(); ++i) + float min_dist{100000}; + for (uint i {0}; i < bottles.size(); ++i) { float distance {bottle_dist(bottles[i])}; if(distance < min_dist) diff --git a/src/Main_enemy.h b/src/Main_enemy.h index 8959e39..dad5e8a 100644 --- a/src/Main_enemy.h +++ b/src/Main_enemy.h @@ -1,14 +1,14 @@ #ifndef MAIN_ENEMY_H #define MAIN_ENEMY_H -#include +#include +#include +#include #include "Bottle.h" #include "Moving_object.h" #include "Player.h" -#include - #include "json.hpp" using json = nlohmann::json; diff --git a/src/Map.cc b/src/Map.cc index 1271507..4be2c42 100644 --- a/src/Map.cc +++ b/src/Map.cc @@ -1,5 +1,6 @@ #include "Map.h" #include "constants.h" + #include 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{} diff --git a/src/Map.h b/src/Map.h index 12cdbca..a5e2643 100644 --- a/src/Map.h +++ b/src/Map.h @@ -2,9 +2,10 @@ #define MAP_H #include -#include "json.hpp" #include "Object.h" +#include "json.hpp" + using json = nlohmann::json; class Map diff --git a/src/Pause_menu.cc b/src/Pause_menu.cc index 4252972..3c1afc1 100644 --- a/src/Pause_menu.cc +++ b/src/Pause_menu.cc @@ -1,6 +1,4 @@ #include -#include -#include #include "States.h" #include "Context.h" @@ -11,10 +9,10 @@ using json = nlohmann::json; Pause_menu::Pause_menu(json& params) : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, resumetext{}, startmenutext{}, - quittext{}, font{}, resume_game{false}, exit_game{false}, menu{false}, menu_index{1}, data{params} + quittext{}, font{}, resume_game{false}, exit_game{false}, menu{false}, menu_index{1}, data{} { - - texture.loadFromFile(params["pause_menu"]["texture"]); + data = params; + texture.loadFromFile(data["pause_menu"]["texture"]); sprite.setTexture(texture); sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); @@ -22,7 +20,7 @@ Pause_menu::Pause_menu(json& params) : texture{}, texture2{}, sprite{}, mouse_l{ sprite.setPosition(0, 0); // mouse - texture2.loadFromFile(params["pause_menu"]["texture_mouse"]); + texture2.loadFromFile(data["pause_menu"]["texture_mouse"]); mouse_l.setTexture(texture2); mouse_l.setScale(-0.1, 0.1); @@ -33,7 +31,7 @@ Pause_menu::Pause_menu(json& params) : texture{}, texture2{}, sprite{}, mouse_l{ // load textfont - if ( !font.loadFromFile (params["pause_menu"]["font"]) ) + if ( !font.loadFromFile (data["pause_menu"]["font"]) ) { throw std::invalid_argument ("Unable to load font"); } diff --git a/src/Player.cc b/src/Player.cc index 2a9ee4b..a4fdef0 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -1,8 +1,6 @@ #include -#include // TA BORT! #include "Player.h" -#include "States.h" #include "Context.h" #include "constants.h" diff --git a/src/Player.h b/src/Player.h index 0d2d698..6bfef29 100644 --- a/src/Player.h +++ b/src/Player.h @@ -6,11 +6,12 @@ #include #include "Moving_object.h" -#include "json.hpp" #include "YF.h" #include "Bike_enemy.h" #include "Map.h" +#include "json.hpp" + using json = nlohmann::json; class Player : public Moving_object diff --git a/src/Scoreboard_menu.cc b/src/Scoreboard_menu.cc index 41160c8..d7cfac9 100644 --- a/src/Scoreboard_menu.cc +++ b/src/Scoreboard_menu.cc @@ -1,9 +1,5 @@ #include -#include -#include #include -#include -#include #include "States.h" #include "Context.h" @@ -14,10 +10,11 @@ using json = nlohmann::json; Scoreboard_menu::Scoreboard_menu(json& params) : texture{}, texture2{}, sprite{}, menu{false}, exit_game{false}, - startmenutext{}, quittext{}, mouse_r{}, mouse_l{}, font{}, menu_index{1}, data{params} + startmenutext{}, quittext{}, mouse_r{}, mouse_l{}, font{}, menu_index{1}, data{} { - texture.loadFromFile("assets/scoreboard.png"); + data = std::move(params); + texture.loadFromFile(data["scoreboard_menu"]["texture"]); sprite.setTexture(texture); sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); @@ -26,7 +23,7 @@ Scoreboard_menu::Scoreboard_menu(json& params) : texture{}, texture2{}, sprite{} // mouse - texture2.loadFromFile("assets/muspekare2.png"); + texture2.loadFromFile(data["scoreboard_menu"]["texture_mouse"]); mouse_l.setTexture(texture2); mouse_l.setScale(-0.1, 0.1); @@ -37,7 +34,7 @@ Scoreboard_menu::Scoreboard_menu(json& params) : texture{}, texture2{}, sprite{} // load textfont - if ( !font.loadFromFile ("assets/fonts/Philosopher-Regular.ttf") ) + if ( !font.loadFromFile(data["scoreboard_menu"]["font"])) throw std::invalid_argument ("Unable to load font"); @@ -51,7 +48,7 @@ Scoreboard_menu::Scoreboard_menu(json& params) : texture{}, texture2{}, sprite{} // Scorelist // ----------------------------------------------------------------------- - std::ifstream highscore_file_r{"assets/highscore.csv"}; + std::ifstream highscore_file_r{data["game_constants"]["highscore_file"]}; std::string csv_content{}; int index{0}; diff --git a/src/Start_menu.cc b/src/Start_menu.cc index 263baeb..0bda96a 100644 --- a/src/Start_menu.cc +++ b/src/Start_menu.cc @@ -1,22 +1,20 @@ #include -#include -#include #include "States.h" #include "Context.h" #include "constants.h" #include "json.hpp" - - using json = nlohmann::json; Start_menu::Start_menu(json& params) : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, starttext{}, scoreboardtext{}, - quittext{}, font{}, start_game{false}, exit_game{false}, scoreboard{false}, menu_index{1}, data{params} -{ + quittext{}, font{}, start_game{false}, exit_game{false}, scoreboard{false}, menu_index{1}, data{} +{ + data = std::move(params); + //sprite - texture.loadFromFile(params["start_menu"]["texture"]); + texture.loadFromFile(data["start_menu"]["texture"]); sprite.setTexture(texture); sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); @@ -24,7 +22,7 @@ Start_menu::Start_menu(json& params) : texture{}, texture2{}, sprite{}, mouse_l sprite.setPosition(0, 0); //mouse - texture2.loadFromFile(params["start_menu"]["texture_mouse"]); + texture2.loadFromFile(data["start_menu"]["texture_mouse"]); mouse_l.setTexture(texture2); mouse_l.setScale(-0.1, 0.1); @@ -35,7 +33,7 @@ Start_menu::Start_menu(json& params) : texture{}, texture2{}, sprite{}, mouse_l //load textfont - if ( !font.loadFromFile (params["start_menu"]["font"]) ) + if ( !font.loadFromFile (data["start_menu"]["font"]) ) throw std::invalid_argument ("Unable to load font"); //start @@ -61,7 +59,7 @@ void Start_menu::update(Context& context) { if (start_game) { - context.next_state = std::make_unique(); + context.next_state = std::make_unique(data); } if (scoreboard) diff --git a/src/States.cc b/src/States.cc index 10e7e8a..6063c65 100644 --- a/src/States.cc +++ b/src/States.cc @@ -1,19 +1,15 @@ #include -#include -#include +#include + #include "States.h" #include "Context.h" #include "constants.h" #include "json.hpp" -#include -#include -#include -#include using json = nlohmann::json; -Game_state::Game_state() : +Game_state::Game_state(json& params) : game_map{}, pause_game{false}, player{}, @@ -32,8 +28,7 @@ Game_state::Game_state() : enemy{}, points{0} { - std::ifstream f("assets/data.json"); - data = json::parse(f); + data = std::move(params); sf::Texture main_enemy_texture; sf::Texture sack_texture; sf::Texture player_texture; @@ -56,8 +51,6 @@ Game_state::Game_state() : bike_texture.loadFromFile(data["game_state_assets"]["cyklist_file"]); game_map = std::make_unique(map_texture, bottle_texture, data["map"]); - - f.close(); } void Game_state::update(Context &context) diff --git a/src/States.h b/src/States.h index 4cf7aae..8e97c34 100644 --- a/src/States.h +++ b/src/States.h @@ -2,10 +2,8 @@ #define STATES_H #include -#include "SFML/Graphics.hpp" +#include #include -#include -#include #include "Map.h" #include "Player.h" @@ -32,7 +30,7 @@ public: class Game_state : public State { public: - Game_state(); + Game_state(json& params); ~Game_state() = default; void update(Context& context) override; @@ -171,9 +169,6 @@ private: sf::Text quittext; sf::Font font; - std::string highscore_file; - int scoreboard_size; - bool exit_game; bool menu; json data; diff --git a/src/Static_object.h b/src/Static_object.h index 2b63c0a..2110e6d 100644 --- a/src/Static_object.h +++ b/src/Static_object.h @@ -1,8 +1,9 @@ #ifndef STATIC_OBJECT_H #define STATIC_OBJECT_H -#include "Object.h" #include +#include "Object.h" + class Static_object : public Object { diff --git a/src/YF.cc b/src/YF.cc index fcf3ed6..803ed25 100644 --- a/src/YF.cc +++ b/src/YF.cc @@ -1,9 +1,8 @@ +#include + #include "YF.h" #include "constants.h" #include "Context.h" -#include "States.h" -#include - YF::YF(sf::Texture& txtr, json& params) : max_speed{params["max_speed"]} { @@ -27,7 +26,7 @@ void YF::collision(Object &other) } void YF::update(Context& context) { - Game_state* game = static_cast(context.current_state.get()); + //Game_state* game = static_cast(context.current_state.get()); position += direction*max_speed; sprite.setPosition(position); } diff --git a/src/YF.h b/src/YF.h index 6054da1..97f4e71 100644 --- a/src/YF.h +++ b/src/YF.h @@ -1,9 +1,10 @@ #ifndef YF_H #define YF_H -#include "Moving_object.h" -#include "Object.h" #include + +#include "Moving_object.h" + #include "json.hpp" using json = nlohmann::json; diff --git a/src/_main.cc b/src/_main.cc index 29b2e2d..0204bc0 100644 --- a/src/_main.cc +++ b/src/_main.cc @@ -5,14 +5,18 @@ */ #include -#include #include #include +#include #include "constants.h" #include "States.h" #include "Context.h" +#include "json.hpp" + +using json = nlohmann::json; + int main () { sf::RenderWindow window{ @@ -21,8 +25,12 @@ int main () }; //skapar en start meny Context game_context{}; - game_context.current_state = std::make_unique(); + std::ifstream f("assets/data.json"); + json data = json::parse(f); + game_context.current_state = std::make_unique(data); + f.close(); + sf::Clock game_clock; while (window.isOpen()) { -- 2.30.2