From: stian853 Date: Wed, 6 Dec 2023 14:57:13 +0000 (+0100) Subject: Massive changes, Nils, Lukas, Stina, Arvid, Alma, Malin X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=a83e269bd4868478dbc501c69af0402f9742910e;p=TDDC76_proj.git Massive changes, Nils, Lukas, Stina, Arvid, Alma, Malin --- 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/assets/oldsrc/Menus.cc b/assets/oldsrc/Menus.cc new file mode 100644 index 0000000..09f1d5c --- /dev/null +++ b/assets/oldsrc/Menus.cc @@ -0,0 +1,765 @@ +#include +#include + +#include "States.h" +#include "Context.h" +#include "constants.h" + + +// Start menu -------------------------------------------- +Start_menu::Start_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, starttext{}, scoreboardtext{}, + quittext{}, font{}, start_game{false}, exit_game{false}, scoreboard{false}, menu_index{1} +{ + //sprite + texture.loadFromFile("assets/meny_bild.png"); + sprite.setTexture(texture); + + sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); + sf::FloatRect gb {sprite.getGlobalBounds()}; + sprite.setPosition(0, 0); + + //mouse + texture2.loadFromFile("assets/muspekareGul.png"); + mouse_l.setTexture(texture2); + + mouse_l.setScale(0.1, 0.1); + mouse_l.setRotation(180); + sf::FloatRect gbm {mouse_l.getGlobalBounds()}; + + mouse_r.setTexture(texture2); + 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 + starttext = sf::Text{ "Start Game", font, 24 }; + sf::FloatRect gbts {starttext.getGlobalBounds()}; + starttext.setOrigin(gbts.width / 2, gbts.height / 2); + starttext.setPosition ((S_WIDTH) / 2, ((S_HEIGHT / 2) - 100)); + + //score + scoreboardtext = sf::Text{ "Scoreboard", font, 24 }; + sf::FloatRect gbtb {scoreboardtext.getGlobalBounds()}; + scoreboardtext.setOrigin(gbtb.width / 2, gbtb.height / 2); + scoreboardtext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) - 50)); + + //quit + quittext = sf::Text{ "Quit", font, 24 }; + sf::FloatRect gbtq {quittext.getGlobalBounds()}; + quittext.setOrigin(gbtq.width / 2, gbtq.height / 2); + quittext.setPosition (S_WIDTH / 2, S_HEIGHT / 2); +} + +void Start_menu::update(Context& context) +{ + if (start_game) + { + context.next_state = std::make_unique(); + } + + if (scoreboard) + { + context.next_state = std::make_unique(); + scoreboard = false; + return; + } + + // changes color on text depending on selection + if( menu_index == 1) + { + starttext.setFillColor(sf::Color::Yellow); + scoreboardtext.setFillColor(sf::Color::Black); + quittext.setFillColor(sf::Color::Black); + } + else if( menu_index == 2) + { + starttext.setFillColor(sf::Color::Black); + scoreboardtext.setFillColor(sf::Color::Yellow); + quittext.setFillColor(sf::Color::Black); + } + else if( menu_index == 3) + { + starttext.setFillColor(sf::Color::Black); + scoreboardtext.setFillColor(sf::Color::Black); + quittext.setFillColor(sf::Color::Yellow); + } + + // mouse placement + mouse_r.setPosition(((S_WIDTH / 2) + 60), ((S_HEIGHT / 2) - 115 + 50*(menu_index - 1))); + mouse_l.setPosition(((S_WIDTH / 2) - 58), ((S_HEIGHT / 2) - 68 + 50*(menu_index - 1))); + +} + +void Start_menu::render(sf::RenderWindow& window) const +{ + window.draw(sprite); + window.draw(mouse_l); + window.draw(mouse_r); + + window.draw(starttext); + window.draw(scoreboardtext); + window.draw(quittext); + + if (exit_game) + { + window.close(); + } +} + +void Start_menu::handle_input(sf::Event& event) +{ + switch (event.type) + { + case sf::Event::JoystickButtonPressed: + switch (event.joystickButton.button) + { + case 0: // A + if(menu_index == 1) + { + start_game = true; + menu_index = 1; + } + + else if(menu_index == 2) + { + scoreboard = true; + menu_index = 1; + } + + else if(menu_index == 3) + { + exit_game = true; + menu_index = 1; + } + break; + default: + + break; + } + + break; + case sf::Event::JoystickMoved: + switch(event.joystickMove.axis) + { + case sf::Joystick::Y: + case sf::Joystick::PovY: + if (event.joystickMove.position <= -50) + { + if(menu_index == 1) + { + break; + } + + else + { + menu_index -= 1; + } + break; + } + else if (event.joystickMove.position >= 50) + { + if(menu_index == 3) + { + break; + } + + else + { + menu_index += 1; + } + break; + } + break; + } + break; + + case sf::Event::KeyPressed: + + switch(event.key.code) + { + case sf::Keyboard::Enter: + if(menu_index == 1) + { + start_game = true; + menu_index = 1; + } + + else if(menu_index == 2) + { + scoreboard = true; + menu_index = 1; + } + + else if(menu_index == 3) + { + exit_game = true; + menu_index = 1; + } + break; + + + case sf::Keyboard::Up: + if(menu_index == 1) + { + break; + } + + else + { + menu_index -= 1; + } + break; + + case sf::Keyboard::Down: + if(menu_index == 3) + { + break; + } + + else + { + menu_index += 1; + } + break; + + default: + break; + } + + break; + default: + break; + } +} + + +// Pause menu -------------------------------------------- +Pause_menu::Pause_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, resumetext{}, startmenutext{}, + quittext{}, font{}, resume_game{false}, exit_game{false}, menu{false}, menu_index{1} +{ + texture.loadFromFile("assets/Pause_bild.png"); + sprite.setTexture(texture);; + + sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); + sf::FloatRect gb {sprite.getGlobalBounds()}; + sprite.setPosition(0, 0); + + // mouse + texture2.loadFromFile("assets/muspekareGul.png"); + mouse_l.setTexture(texture2); + + mouse_l.setScale(0.1, 0.1); + mouse_l.setRotation(180); + sf::FloatRect gbm {mouse_l.getGlobalBounds()}; + + mouse_r.setTexture(texture2); + 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"); + } + + + // Resume text + resumetext = sf::Text{ "Resume", font, 24 }; + sf::FloatRect gbr {resumetext.getGlobalBounds()}; + resumetext.setOrigin(gbr.width / 2, gbr.height / 2); + resumetext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) - 30)); + + // Start menu text + startmenutext = sf::Text{ "Start menu", font, 24 }; + sf::FloatRect gbs {startmenutext.getGlobalBounds()}; + startmenutext.setOrigin(gbs.width / 2, gbs.height / 2); + startmenutext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) + 20)); + + //quit text + quittext = sf::Text{ "Quit", font, 24 }; + sf::FloatRect gbq {quittext.getGlobalBounds()}; + quittext.setOrigin(gbq.width / 2, gbq.height / 2 ); + quittext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) + 70)); +} + +void Pause_menu::update(Context& context) +{ + if (resume_game) + { + context.next_state = std::move(context.saved_game); + } + else if (menu) + { + context.next_state = std::make_unique(); + context.saved_game.release(); + } + + // changes color on text depending on selection + if( menu_index == 1) + { + resumetext.setFillColor(sf::Color::Yellow); + startmenutext.setFillColor(sf::Color::Black); + quittext.setFillColor(sf::Color::Black); + } + else if( menu_index == 2) + { + resumetext.setFillColor(sf::Color::Black); + startmenutext.setFillColor(sf::Color::Yellow); + quittext.setFillColor(sf::Color::Black); + } + else if( menu_index == 3) + { + resumetext.setFillColor(sf::Color::Black); + startmenutext.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))); +} + +void Pause_menu::render(sf::RenderWindow& window) const +{ + window.draw(sprite); + window.draw(mouse_l); + window.draw(mouse_r); + + window.draw(resumetext); + window.draw(startmenutext); + window.draw(quittext); + + if (exit_game) + { + window.close(); + } +} + +void Pause_menu::handle_input(sf::Event& event) +{ + switch (event.type) + { + case sf::Event::JoystickButtonPressed: + switch (event.joystickButton.button) + { + case 0: // A + if(menu_index == 1) + { + resume_game = true; + menu_index = 1; + } + + else if(menu_index == 2) + { + menu = true; + menu_index = 1; + } + + else if(menu_index == 3) + { + exit_game = true; + menu_index = 1; + } + break; + default: + + break; + } + + break; + case sf::Event::JoystickMoved: + switch(event.joystickMove.axis) + { + case sf::Joystick::Y: + case sf::Joystick::PovY: + if (event.joystickMove.position <= -50) + { + if(menu_index == 1) + { + break; + } + + else + { + menu_index -= 1; + } + break; + } + else if (event.joystickMove.position >= 50) + { + if(menu_index == 3) + { + break; + } + + else + { + menu_index += 1; + } + break; + } + break; + } + break; + case sf::Event::KeyPressed: + switch(event.key.code) + { + case sf::Keyboard::Enter: + if(menu_index == 1) + { + resume_game = true; + menu_index = 1; + } + + else if(menu_index == 2) + { + menu = true; + menu_index = 1; + } + + else if(menu_index == 3) + { + exit_game = true; + menu_index = 1; + } + break; + + + case sf::Keyboard::Up: + if(menu_index == 1) + { + break; + } + + else + { + menu_index -= 1; + } + break; + + case sf::Keyboard::Down: + if(menu_index == 3) + { + break; + } + + else + { + menu_index += 1; + } + break; + + default: + break; + } + + break; + default: + break; + } +} + + +// Scoreboard menu -------------------------------------------- +Scoreboard_menu::Scoreboard_menu() : texture{}, texture2{}, sprite{}, menu{false}, exit_game{false}, + startmenutext{}, quittext{}, mouse_r{}, mouse_l{}, font{}, menu_index{1} +{ + texture.loadFromFile("assets/scoreboard.png"); + sprite.setTexture(texture);; + + sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); + sf::FloatRect gb {sprite.getGlobalBounds()}; + sprite.setPosition(0, 0); + + + // mouse + texture2.loadFromFile("assets/muspekare2.png"); + mouse_l.setTexture(texture2); + + mouse_l.setScale(0.1, 0.1); + mouse_l.setRotation(180); + sf::FloatRect gbm {mouse_l.getGlobalBounds()}; + + mouse_r.setTexture(texture2); + 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"); + + + // Scoreboard text + scoreboardtext = sf::Text{ "Scoreboard:", font, 39 }; + 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); + + // ----------------------------------------------------------------------- + // First place text + firstplacetext = sf::Text{ "1. NAME 100p\n 2. 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); + // ----------------------------------------------------------------------- + + // 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 - 15, ((S_HEIGHT / 2) + 80)); + + // Quit text + quittext = sf::Text{ "Quit", font, 24 }; + sf::FloatRect gbq {quittext.getGlobalBounds()}; + quittext.setOrigin(gbq.width / 2, gbq.height / 2); + quittext.setPosition (S_WIDTH / 2 - 15, ((S_HEIGHT / 2) + 130)); +} + + +void Scoreboard_menu::update(Context& context) +{ + if (menu) + { + context.next_state = std::make_unique(); + } + + // changes color on text depending on selection + if( menu_index == 1) + { + startmenutext.setFillColor(sf::Color::Black); + quittext.setFillColor(sf::Color::Red); + } + else if( menu_index == 2) + { + startmenutext.setFillColor(sf::Color::Red); + quittext.setFillColor(sf::Color::Black); + } + + // mouse placement + mouse_r.setPosition(((S_WIDTH / 2) + 45), ((S_HEIGHT / 2) + 65 + 50*(menu_index - 1))); + mouse_l.setPosition(((S_WIDTH / 2) - 73), ((S_HEIGHT / 2) + 112 + 50*(menu_index - 1))); +} + + +void Scoreboard_menu::render(sf::RenderWindow& window) const +{ + window.draw(sprite); + window.draw(mouse_l); + window.draw(mouse_r); + + window.draw(scoreboardtext); + window.draw(firstplacetext); + window.draw(startmenutext); + window.draw(quittext); + + if (exit_game) + { + window.close(); + } +} + + +void Scoreboard_menu::handle_input(sf::Event& event) +{ + switch (event.type) + { + case sf::Event::KeyPressed: + switch(event.key.code) + { + case sf::Keyboard::Enter: + + if(menu_index == 1) + { + menu = true; + menu_index = 1; + } + + else if(menu_index == 2) + { + exit_game = true; + menu_index = 1; + } + break; + + + case sf::Keyboard::Up: + if(menu_index == 1) + { + break; + } + + else + { + menu_index -= 1; + } + break; + + case sf::Keyboard::Down: + if(menu_index == 2) + { + break; + } + + else + { + menu_index += 1; + } + break; + + default: + break; + } + + break; + default: + break; + } +} + + +// GameOver menu -------------------------------------------- +GameOver_menu::GameOver_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, startmenutext{}, + quittext{}, font{}, exit_game{false}, menu{false}, menu_index{1} +{ + texture.loadFromFile("assets/game_over.png"); + sprite.setTexture(texture);; + + sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); + sf::FloatRect gb {sprite.getGlobalBounds()}; + sprite.setPosition(0, 0); + + + // mouse + texture2.loadFromFile("assets/muspekareGul.png"); + mouse_l.setTexture(texture2); + + mouse_l.setScale(0.1, 0.1); + mouse_l.setRotation(180); + sf::FloatRect gbm {mouse_l.getGlobalBounds()}; + + mouse_r.setTexture(texture2); + 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)); + + // 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)); +} + + +void GameOver_menu::update(Context& context) +{ + if (menu) + { + context.next_state = std::make_unique(); + } + + // changes color on text depending on selection + if( menu_index == 1) + { + startmenutext.setFillColor(sf::Color::Yellow); + quittext.setFillColor(sf::Color::Black); + } + else if( menu_index == 2) + { + startmenutext.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))); +} + + +void GameOver_menu::render(sf::RenderWindow& window) const +{ + window.draw(sprite); + window.draw(mouse_l); + window.draw(mouse_r); + + window.draw(startmenutext); + window.draw(quittext); + + if (exit_game) + { + window.close(); + } +} + + +void GameOver_menu::handle_input(sf::Event& event) +{ + switch (event.type) + { + case sf::Event::KeyPressed: + switch(event.key.code) + { + case sf::Keyboard::Enter: + + if(menu_index == 1) + { + menu = true; + menu_index = 1; + } + + else if(menu_index == 2) + { + exit_game = true; + menu_index = 1; + } + break; + + + case sf::Keyboard::Up: + if(menu_index == 1) + { + break; + } + + else + { + menu_index -= 1; + } + break; + + case sf::Keyboard::Down: + if(menu_index == 2) + { + break; + } + + else + { + menu_index += 1; + } + break; + + default: + break; + } + + break; + default: + break; + } +} 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/Menus.cc b/src/Menus.cc deleted file mode 100644 index 09f1d5c..0000000 --- a/src/Menus.cc +++ /dev/null @@ -1,765 +0,0 @@ -#include -#include - -#include "States.h" -#include "Context.h" -#include "constants.h" - - -// Start menu -------------------------------------------- -Start_menu::Start_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, starttext{}, scoreboardtext{}, - quittext{}, font{}, start_game{false}, exit_game{false}, scoreboard{false}, menu_index{1} -{ - //sprite - texture.loadFromFile("assets/meny_bild.png"); - sprite.setTexture(texture); - - sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); - sf::FloatRect gb {sprite.getGlobalBounds()}; - sprite.setPosition(0, 0); - - //mouse - texture2.loadFromFile("assets/muspekareGul.png"); - mouse_l.setTexture(texture2); - - mouse_l.setScale(0.1, 0.1); - mouse_l.setRotation(180); - sf::FloatRect gbm {mouse_l.getGlobalBounds()}; - - mouse_r.setTexture(texture2); - 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 - starttext = sf::Text{ "Start Game", font, 24 }; - sf::FloatRect gbts {starttext.getGlobalBounds()}; - starttext.setOrigin(gbts.width / 2, gbts.height / 2); - starttext.setPosition ((S_WIDTH) / 2, ((S_HEIGHT / 2) - 100)); - - //score - scoreboardtext = sf::Text{ "Scoreboard", font, 24 }; - sf::FloatRect gbtb {scoreboardtext.getGlobalBounds()}; - scoreboardtext.setOrigin(gbtb.width / 2, gbtb.height / 2); - scoreboardtext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) - 50)); - - //quit - quittext = sf::Text{ "Quit", font, 24 }; - sf::FloatRect gbtq {quittext.getGlobalBounds()}; - quittext.setOrigin(gbtq.width / 2, gbtq.height / 2); - quittext.setPosition (S_WIDTH / 2, S_HEIGHT / 2); -} - -void Start_menu::update(Context& context) -{ - if (start_game) - { - context.next_state = std::make_unique(); - } - - if (scoreboard) - { - context.next_state = std::make_unique(); - scoreboard = false; - return; - } - - // changes color on text depending on selection - if( menu_index == 1) - { - starttext.setFillColor(sf::Color::Yellow); - scoreboardtext.setFillColor(sf::Color::Black); - quittext.setFillColor(sf::Color::Black); - } - else if( menu_index == 2) - { - starttext.setFillColor(sf::Color::Black); - scoreboardtext.setFillColor(sf::Color::Yellow); - quittext.setFillColor(sf::Color::Black); - } - else if( menu_index == 3) - { - starttext.setFillColor(sf::Color::Black); - scoreboardtext.setFillColor(sf::Color::Black); - quittext.setFillColor(sf::Color::Yellow); - } - - // mouse placement - mouse_r.setPosition(((S_WIDTH / 2) + 60), ((S_HEIGHT / 2) - 115 + 50*(menu_index - 1))); - mouse_l.setPosition(((S_WIDTH / 2) - 58), ((S_HEIGHT / 2) - 68 + 50*(menu_index - 1))); - -} - -void Start_menu::render(sf::RenderWindow& window) const -{ - window.draw(sprite); - window.draw(mouse_l); - window.draw(mouse_r); - - window.draw(starttext); - window.draw(scoreboardtext); - window.draw(quittext); - - if (exit_game) - { - window.close(); - } -} - -void Start_menu::handle_input(sf::Event& event) -{ - switch (event.type) - { - case sf::Event::JoystickButtonPressed: - switch (event.joystickButton.button) - { - case 0: // A - if(menu_index == 1) - { - start_game = true; - menu_index = 1; - } - - else if(menu_index == 2) - { - scoreboard = true; - menu_index = 1; - } - - else if(menu_index == 3) - { - exit_game = true; - menu_index = 1; - } - break; - default: - - break; - } - - break; - case sf::Event::JoystickMoved: - switch(event.joystickMove.axis) - { - case sf::Joystick::Y: - case sf::Joystick::PovY: - if (event.joystickMove.position <= -50) - { - if(menu_index == 1) - { - break; - } - - else - { - menu_index -= 1; - } - break; - } - else if (event.joystickMove.position >= 50) - { - if(menu_index == 3) - { - break; - } - - else - { - menu_index += 1; - } - break; - } - break; - } - break; - - case sf::Event::KeyPressed: - - switch(event.key.code) - { - case sf::Keyboard::Enter: - if(menu_index == 1) - { - start_game = true; - menu_index = 1; - } - - else if(menu_index == 2) - { - scoreboard = true; - menu_index = 1; - } - - else if(menu_index == 3) - { - exit_game = true; - menu_index = 1; - } - break; - - - case sf::Keyboard::Up: - if(menu_index == 1) - { - break; - } - - else - { - menu_index -= 1; - } - break; - - case sf::Keyboard::Down: - if(menu_index == 3) - { - break; - } - - else - { - menu_index += 1; - } - break; - - default: - break; - } - - break; - default: - break; - } -} - - -// Pause menu -------------------------------------------- -Pause_menu::Pause_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, resumetext{}, startmenutext{}, - quittext{}, font{}, resume_game{false}, exit_game{false}, menu{false}, menu_index{1} -{ - texture.loadFromFile("assets/Pause_bild.png"); - sprite.setTexture(texture);; - - sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); - sf::FloatRect gb {sprite.getGlobalBounds()}; - sprite.setPosition(0, 0); - - // mouse - texture2.loadFromFile("assets/muspekareGul.png"); - mouse_l.setTexture(texture2); - - mouse_l.setScale(0.1, 0.1); - mouse_l.setRotation(180); - sf::FloatRect gbm {mouse_l.getGlobalBounds()}; - - mouse_r.setTexture(texture2); - 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"); - } - - - // Resume text - resumetext = sf::Text{ "Resume", font, 24 }; - sf::FloatRect gbr {resumetext.getGlobalBounds()}; - resumetext.setOrigin(gbr.width / 2, gbr.height / 2); - resumetext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) - 30)); - - // Start menu text - startmenutext = sf::Text{ "Start menu", font, 24 }; - sf::FloatRect gbs {startmenutext.getGlobalBounds()}; - startmenutext.setOrigin(gbs.width / 2, gbs.height / 2); - startmenutext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) + 20)); - - //quit text - quittext = sf::Text{ "Quit", font, 24 }; - sf::FloatRect gbq {quittext.getGlobalBounds()}; - quittext.setOrigin(gbq.width / 2, gbq.height / 2 ); - quittext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) + 70)); -} - -void Pause_menu::update(Context& context) -{ - if (resume_game) - { - context.next_state = std::move(context.saved_game); - } - else if (menu) - { - context.next_state = std::make_unique(); - context.saved_game.release(); - } - - // changes color on text depending on selection - if( menu_index == 1) - { - resumetext.setFillColor(sf::Color::Yellow); - startmenutext.setFillColor(sf::Color::Black); - quittext.setFillColor(sf::Color::Black); - } - else if( menu_index == 2) - { - resumetext.setFillColor(sf::Color::Black); - startmenutext.setFillColor(sf::Color::Yellow); - quittext.setFillColor(sf::Color::Black); - } - else if( menu_index == 3) - { - resumetext.setFillColor(sf::Color::Black); - startmenutext.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))); -} - -void Pause_menu::render(sf::RenderWindow& window) const -{ - window.draw(sprite); - window.draw(mouse_l); - window.draw(mouse_r); - - window.draw(resumetext); - window.draw(startmenutext); - window.draw(quittext); - - if (exit_game) - { - window.close(); - } -} - -void Pause_menu::handle_input(sf::Event& event) -{ - switch (event.type) - { - case sf::Event::JoystickButtonPressed: - switch (event.joystickButton.button) - { - case 0: // A - if(menu_index == 1) - { - resume_game = true; - menu_index = 1; - } - - else if(menu_index == 2) - { - menu = true; - menu_index = 1; - } - - else if(menu_index == 3) - { - exit_game = true; - menu_index = 1; - } - break; - default: - - break; - } - - break; - case sf::Event::JoystickMoved: - switch(event.joystickMove.axis) - { - case sf::Joystick::Y: - case sf::Joystick::PovY: - if (event.joystickMove.position <= -50) - { - if(menu_index == 1) - { - break; - } - - else - { - menu_index -= 1; - } - break; - } - else if (event.joystickMove.position >= 50) - { - if(menu_index == 3) - { - break; - } - - else - { - menu_index += 1; - } - break; - } - break; - } - break; - case sf::Event::KeyPressed: - switch(event.key.code) - { - case sf::Keyboard::Enter: - if(menu_index == 1) - { - resume_game = true; - menu_index = 1; - } - - else if(menu_index == 2) - { - menu = true; - menu_index = 1; - } - - else if(menu_index == 3) - { - exit_game = true; - menu_index = 1; - } - break; - - - case sf::Keyboard::Up: - if(menu_index == 1) - { - break; - } - - else - { - menu_index -= 1; - } - break; - - case sf::Keyboard::Down: - if(menu_index == 3) - { - break; - } - - else - { - menu_index += 1; - } - break; - - default: - break; - } - - break; - default: - break; - } -} - - -// Scoreboard menu -------------------------------------------- -Scoreboard_menu::Scoreboard_menu() : texture{}, texture2{}, sprite{}, menu{false}, exit_game{false}, - startmenutext{}, quittext{}, mouse_r{}, mouse_l{}, font{}, menu_index{1} -{ - texture.loadFromFile("assets/scoreboard.png"); - sprite.setTexture(texture);; - - sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); - sf::FloatRect gb {sprite.getGlobalBounds()}; - sprite.setPosition(0, 0); - - - // mouse - texture2.loadFromFile("assets/muspekare2.png"); - mouse_l.setTexture(texture2); - - mouse_l.setScale(0.1, 0.1); - mouse_l.setRotation(180); - sf::FloatRect gbm {mouse_l.getGlobalBounds()}; - - mouse_r.setTexture(texture2); - 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"); - - - // Scoreboard text - scoreboardtext = sf::Text{ "Scoreboard:", font, 39 }; - 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); - - // ----------------------------------------------------------------------- - // First place text - firstplacetext = sf::Text{ "1. NAME 100p\n 2. 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); - // ----------------------------------------------------------------------- - - // 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 - 15, ((S_HEIGHT / 2) + 80)); - - // Quit text - quittext = sf::Text{ "Quit", font, 24 }; - sf::FloatRect gbq {quittext.getGlobalBounds()}; - quittext.setOrigin(gbq.width / 2, gbq.height / 2); - quittext.setPosition (S_WIDTH / 2 - 15, ((S_HEIGHT / 2) + 130)); -} - - -void Scoreboard_menu::update(Context& context) -{ - if (menu) - { - context.next_state = std::make_unique(); - } - - // changes color on text depending on selection - if( menu_index == 1) - { - startmenutext.setFillColor(sf::Color::Black); - quittext.setFillColor(sf::Color::Red); - } - else if( menu_index == 2) - { - startmenutext.setFillColor(sf::Color::Red); - quittext.setFillColor(sf::Color::Black); - } - - // mouse placement - mouse_r.setPosition(((S_WIDTH / 2) + 45), ((S_HEIGHT / 2) + 65 + 50*(menu_index - 1))); - mouse_l.setPosition(((S_WIDTH / 2) - 73), ((S_HEIGHT / 2) + 112 + 50*(menu_index - 1))); -} - - -void Scoreboard_menu::render(sf::RenderWindow& window) const -{ - window.draw(sprite); - window.draw(mouse_l); - window.draw(mouse_r); - - window.draw(scoreboardtext); - window.draw(firstplacetext); - window.draw(startmenutext); - window.draw(quittext); - - if (exit_game) - { - window.close(); - } -} - - -void Scoreboard_menu::handle_input(sf::Event& event) -{ - switch (event.type) - { - case sf::Event::KeyPressed: - switch(event.key.code) - { - case sf::Keyboard::Enter: - - if(menu_index == 1) - { - menu = true; - menu_index = 1; - } - - else if(menu_index == 2) - { - exit_game = true; - menu_index = 1; - } - break; - - - case sf::Keyboard::Up: - if(menu_index == 1) - { - break; - } - - else - { - menu_index -= 1; - } - break; - - case sf::Keyboard::Down: - if(menu_index == 2) - { - break; - } - - else - { - menu_index += 1; - } - break; - - default: - break; - } - - break; - default: - break; - } -} - - -// GameOver menu -------------------------------------------- -GameOver_menu::GameOver_menu() : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, startmenutext{}, - quittext{}, font{}, exit_game{false}, menu{false}, menu_index{1} -{ - texture.loadFromFile("assets/game_over.png"); - sprite.setTexture(texture);; - - sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); - sf::FloatRect gb {sprite.getGlobalBounds()}; - sprite.setPosition(0, 0); - - - // mouse - texture2.loadFromFile("assets/muspekareGul.png"); - mouse_l.setTexture(texture2); - - mouse_l.setScale(0.1, 0.1); - mouse_l.setRotation(180); - sf::FloatRect gbm {mouse_l.getGlobalBounds()}; - - mouse_r.setTexture(texture2); - 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)); - - // 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)); -} - - -void GameOver_menu::update(Context& context) -{ - if (menu) - { - context.next_state = std::make_unique(); - } - - // changes color on text depending on selection - if( menu_index == 1) - { - startmenutext.setFillColor(sf::Color::Yellow); - quittext.setFillColor(sf::Color::Black); - } - else if( menu_index == 2) - { - startmenutext.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))); -} - - -void GameOver_menu::render(sf::RenderWindow& window) const -{ - window.draw(sprite); - window.draw(mouse_l); - window.draw(mouse_r); - - window.draw(startmenutext); - window.draw(quittext); - - if (exit_game) - { - window.close(); - } -} - - -void GameOver_menu::handle_input(sf::Event& event) -{ - switch (event.type) - { - case sf::Event::KeyPressed: - switch(event.key.code) - { - case sf::Keyboard::Enter: - - if(menu_index == 1) - { - menu = true; - menu_index = 1; - } - - else if(menu_index == 2) - { - exit_game = true; - menu_index = 1; - } - break; - - - case sf::Keyboard::Up: - if(menu_index == 1) - { - break; - } - - else - { - menu_index -= 1; - } - break; - - case sf::Keyboard::Down: - if(menu_index == 2) - { - break; - } - - else - { - menu_index += 1; - } - break; - - default: - break; - } - - break; - default: - break; - } -} 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()) {