Massive changes, Nils, Lukas, Stina, Arvid, Alma, Malin
authorstian853 <stian853@su15-110.ad.liu.se>
Wed, 6 Dec 2023 14:57:13 +0000 (15:57 +0100)
committerstian853 <stian853@su15-110.ad.liu.se>
Wed, 6 Dec 2023 14:57:13 +0000 (15:57 +0100)
27 files changed:
assets/data.json
assets/highscore.csv
assets/oldsrc/Menus.cc [new file with mode: 0644]
src/Bike_enemy.cc
src/Bike_enemy.h
src/Bottle.cc
src/Bottle.h
src/Context.h
src/GameOver_menu.cc
src/Helper.cc
src/Helper.h
src/Main_enemy.cc
src/Main_enemy.h
src/Map.cc
src/Map.h
src/Menus.cc [deleted file]
src/Pause_menu.cc
src/Player.cc
src/Player.h
src/Scoreboard_menu.cc
src/Start_menu.cc
src/States.cc
src/States.h
src/Static_object.h
src/YF.cc
src/YF.h
src/_main.cc

index 94064f131bef58f925c1fecdebdba90f0a4b7b54..45662d1d51dc0cfe391af4e8207c1843ac90afdc 100644 (file)
     "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",
     },
     "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":
     {
index f5bc58172cc5e163a55f8a4c5e4c7dec2354146c..1def999e404f4cf6c08da32231a91ee9ff84b32e 100644 (file)
@@ -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 (file)
index 0000000..09f1d5c
--- /dev/null
@@ -0,0 +1,765 @@
+#include <memory>
+#include <iostream>
+
+#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<Game_state>();
+    }
+    
+    if (scoreboard)
+    {
+        context.next_state = std::make_unique<Scoreboard_menu>();
+        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<Start_menu>();
+        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<Start_menu>();
+    }
+    // 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<Start_menu>();
+    }
+    // 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;
+    }
+}
index ed20f0e05a5826e35a233dba5e85f5f71823557b..fcaed4772f7fb93c7de5fa144549c15819e281d8 100644 (file)
@@ -1,11 +1,9 @@
+#include <random>
+#include <cmath>
 
 #include "Bike_enemy.h"
 #include "constants.h"
 #include "Context.h"
-#include "States.h"
-#include <random>
-#include <cmath>
-
 
 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"]}
 {
index 58cc348a808e9af57231c0fc570e8d7b7b686cc6..03b88d587f79ca54112396ec109909720c6aa200 100644 (file)
@@ -1,9 +1,9 @@
 #ifndef BIKE_ENEMY_H
 #define BIKE_ENEMY_H
 
-#include "Moving_object.h"
-#include "Object.h"
 #include <SFML/Graphics.hpp>
+
+#include "Moving_object.h"
 #include "json.hpp"
 
 using json = nlohmann::json;
index 3e642e8456a3af39bdb177d2f866b20170199036..2ead9464fef5e938682e7968a854cf1bfa10c8fb 100644 (file)
@@ -1,8 +1,9 @@
+#include <random>
+
 #include "Bottle.h"
 #include "constants.h"
 #include "Context.h"
-#include "States.h"
-#include <random>
+
 
 Bottle::Bottle(sf::Texture& txtr, json& params)
 {
index db00f036812d2d56ec383fa10af6ce18f8a62b45..15d605660dbb69a7777064b06031f1e3460c0cb5 100644 (file)
@@ -4,6 +4,7 @@
 #include <SFML/Graphics.hpp>
 
 #include "Static_object.h"
+
 #include "json.hpp"
 
 using json = nlohmann::json;
index 327387dc14462a64a272d07cb6d0f58223ee8cce..f439b24350ec5c686ead9e313283aa5d95d1d7fb 100644 (file)
@@ -1,8 +1,10 @@
 #ifndef CONTEXT_H
 #define CONTEXT_H
 
-#include "States.h"
 #include <SFML/Graphics.hpp>
+#include <memory>
+
+#include "States.h"
 
 struct Context 
 {
index 9718f194d1a1b257f1efa7f2557f6810fb7a2c75..181ce75b3b47e05125a7be9176466990f4d23b64 100644 (file)
@@ -1,13 +1,12 @@
 #include <memory>
-#include <iostream>
 #include <string>
 #include <fstream>
 #include <vector>
-#include <cmath>
 
 #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<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));
@@ -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
index 41e0d554997d5983252c3dc797402292fd7b4677..7bcd2a74451dca46f46a5c671253a3cc02d8054f 100644 (file)
@@ -1,11 +1,9 @@
-#include "Helper.h"
-#include "constants.h"
-#include "States.h"
-#include "Context.h"
 #include <cmath>
 
 
-#include <iostream>
+#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{}
 {
index 3b2cb6e4b2af1ad05251eecebc907516d4027d97..5db3cea37b455aa4786a742bc1f700ae8584fe3d 100644 (file)
@@ -4,11 +4,12 @@
 #include <SFML/Graphics.hpp> 
 
 #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);
index f62c459ae220a5d3ebf3aee9bcb00380121a949e..f9f9d9e686763d309618ca460f23c2ec6181c30c 100644 (file)
@@ -1,6 +1,5 @@
 #include <cmath>
-#include <memory>
-#include <iostream>
+
 #include "Main_enemy.h"
 #include "States.h"
 #include "Context.h"
@@ -78,9 +77,8 @@ sf::Vector2f Main_enemy::find_closest_bottle(std::vector<std::unique_ptr<Bottle>
     //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)
index 8959e392b31f01ea997b87ea57d928f48a1bda75..dad5e8a8d83bbefe7da67e63ab8987509e41b3e6 100644 (file)
@@ -1,14 +1,14 @@
 #ifndef MAIN_ENEMY_H
 #define MAIN_ENEMY_H
 
-#include <SFML/Graphics.hpp> 
+#include <SFML/Graphics.hpp>
+#include <vector>
+#include <memory>
 
 #include "Bottle.h"
 #include "Moving_object.h"
 #include "Player.h"
 
-#include <vector>
-
 #include "json.hpp"
 
 using json = nlohmann::json;
index 1271507f2c0b8c71917168ecf84b2eb34fed5c86..4be2c42a105c1da89035de6a0771c7733ce1f943 100644 (file)
@@ -1,5 +1,6 @@
 #include "Map.h"
 #include "constants.h"
+
 #include <string>
 
 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{}
index 12cdbca895d20be620dd59fb45f416b71d97dc13..a5e26437b6827604f68cfff12be836366123096f 100644 (file)
--- a/src/Map.h
+++ b/src/Map.h
@@ -2,9 +2,10 @@
 #define MAP_H
 
 #include <SFML/Graphics.hpp>
-#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 (file)
index 09f1d5c..0000000
+++ /dev/null
@@ -1,765 +0,0 @@
-#include <memory>
-#include <iostream>
-
-#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<Game_state>();
-    }
-    
-    if (scoreboard)
-    {
-        context.next_state = std::make_unique<Scoreboard_menu>();
-        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<Start_menu>();
-        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<Start_menu>();
-    }
-    // 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<Start_menu>();
-    }
-    // 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;
-    }
-}
index 42529727b1f2b466d407d2e291f17ab68584564d..3c1afc1b98624bf9da4295e67d5819d4902f0117 100644 (file)
@@ -1,6 +1,4 @@
 #include <memory>
-#include <iostream>
-#include <string>
 
 #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");
         }
index 2a9ee4b3a33c3b3d12c98b862470cd67f1146dfb..a4fdef070253b95577c34a7fc22075250a2e7e56 100644 (file)
@@ -1,8 +1,6 @@
 #include <cmath>
-#include <iostream> // TA BORT!
 
 #include "Player.h"
-#include "States.h"
 #include "Context.h"
 #include "constants.h"
 
index 0d2d698c5e58737843474d6d93a2ca06dd420fdd..6bfef298fb6ee0a8fe2fc282bb40143eacc95fb2 100644 (file)
@@ -6,11 +6,12 @@
 #include <SFML/Graphics.hpp>
 
 #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
index 41160c8230a2fb2490750e9a32c542cdcbf4b4b5..d7cfac95bac84cdafb54caf8b09107f7bbe5f294 100644 (file)
@@ -1,9 +1,5 @@
 #include <memory>
-#include <iostream>
-#include <string>
 #include <fstream>
-#include <vector>
-#include <cmath>
 
 #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};
index 263baebca6e8fbd4fb6d330c239a11a4d7654abf..0bda96a800d8f6c6b087314688b162c49cd05699 100644 (file)
@@ -1,22 +1,20 @@
 #include <memory>
-#include <iostream>
-#include <string>
 
 #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<Game_state>();
+        context.next_state = std::make_unique<Game_state>(data);
     }
     
     if (scoreboard)
index 10e7e8a14e3b5148aaeb3e044add0f82ca38c9f2..6063c652647858cf01fff133339a36c24402ec37 100644 (file)
@@ -1,19 +1,15 @@
 #include <memory>
-#include <iostream>
-#include <iomanip>
+#include <cmath>
+
 #include "States.h"
 #include "Context.h"
 #include "constants.h"
 
 #include "json.hpp"
-#include <fstream>
-#include <string>
-#include <vector>
-#include <cmath>
 
 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>(map_texture, bottle_texture, data["map"]);
-    
-    f.close();
 }
 
 void Game_state::update(Context &context)
index 4cf7aae06f9af8c6ef7b9bf5c6855a7fb33e67cd..8e97c34315666263ba4e4f1f4720f09673ddc724 100644 (file)
@@ -2,10 +2,8 @@
 #define STATES_H
 
 #include <memory>
-#include "SFML/Graphics.hpp"
+#include <SFML/Graphics.hpp>
 #include <vector>
-#include <memory>
-#include <fstream>
 
 #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;
index 2b63c0a0edc8505d7daf0d6b6f51af051ab85099..2110e6d352e08745a7f89617747581c2c68af96b 100644 (file)
@@ -1,8 +1,9 @@
 #ifndef STATIC_OBJECT_H
 #define STATIC_OBJECT_H
 
-#include "Object.h"
 #include <SFML/Graphics.hpp>
+#include "Object.h"
+
 
 class Static_object : public Object
 {
index fcf3ed65460bc853e5af88bacddbb9f76597dc1f..803ed255423d62a1b1ec136f6aaa9ec22fb052a6 100644 (file)
--- a/src/YF.cc
+++ b/src/YF.cc
@@ -1,9 +1,8 @@
+#include <random>
+
 #include "YF.h"
 #include "constants.h"
 #include "Context.h"
-#include "States.h"
-#include <random>
-
 
 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<Game_state*>(context.current_state.get());
+    //Game_state* game = static_cast<Game_state*>(context.current_state.get());
     position += direction*max_speed;
     sprite.setPosition(position);
 }
index 6054da1fd0a24109020f373602394e22f0bb37b8..97f4e71676fd6b0678bd5b77fd44b6e5eaa8244d 100644 (file)
--- 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 <SFML/Graphics.hpp>
+
+#include "Moving_object.h"
+
 #include "json.hpp"
 
 using json = nlohmann::json;
index 29b2e2dd3561f1139a8919db6dc7e46009dc34dd..0204bc028b0470a9a6266d56fc9ae6cdfaea35d9 100644 (file)
@@ -5,14 +5,18 @@
 */
 
 #include <SFML/Graphics.hpp>
-#include <iostream>
 #include <memory>
 #include <random>
+#include <fstream>
 
 #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<Start_menu>();
     
+    std::ifstream f("assets/data.json");
+    json data = json::parse(f);
+    game_context.current_state = std::make_unique<Start_menu>(data);
+    f.close();
+
     sf::Clock game_clock;
     while (window.isOpen())
     {