"game_constants":
{
"bottles_per_second" : 2,
- "game_time" : 2,
+ "game_time" : 20,
"yf_per_second": 0.125,
"bikes_per_second": 0.17,
"scoreboard" : 5
+0. Arvid, 6
gorilla, 5
Lukas, 5
+Alma, 52.
gorilla, 4
-gorilla, 3
-, 3
--- /dev/null
+#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;
+
+
+GameOver_menu::GameOver_menu(int const points, json& params) : texture{}, texture2{}, sprite{}, rectangle{}, mouse_l{}, mouse_r{}, linetext{}, entertext{}, pointstext{},
+ playerInput{}, data{}, playerText{}, savetext{}, quittext{}, font{}, exit_game{false}, menu{false}, menu_index{1}, points{points}
+{
+ 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);
+
+ // load textfont
+ if ( !font.loadFromFile ("assets/fonts/Philosopher-Regular.ttf") )
+ throw std::invalid_argument ("Unable to load font");
+
+
+ // transparent box
+ rectangle = sf::CircleShape{200};
+ rectangle.setFillColor(sf::Color(255, 255, 0, 180));
+ rectangle.setOutlineThickness(7);
+ rectangle.setOutlineColor(sf::Color( 0, 0, 0, 128));
+ sf::FloatRect gbrs {rectangle.getGlobalBounds()};
+ rectangle.setOrigin(gbrs.width / 2, gbrs.height / 2);
+ rectangle.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2)));
+
+
+ // Points text
+ pointstext = sf::Text{ "Your points: " + std::to_string(points), font, 30 };
+ sf::FloatRect gbp {pointstext.getGlobalBounds()};
+ pointstext.setOrigin(gbp.width / 2, gbp.height / 2);
+ pointstext.setPosition (S_WIDTH / 2 , ((S_HEIGHT / 2) - 100));
+ pointstext.setFillColor(sf::Color::Black);
+
+
+ // Enter text
+ entertext = sf::Text{ "Enter your name:", font, 24 };
+ sf::FloatRect gbe {entertext.getGlobalBounds()};
+ entertext.setOrigin(gbe.width / 2, gbe.height / 2);
+ entertext.setPosition (S_WIDTH / 2 , ((S_HEIGHT / 2) - 30));
+ entertext.setFillColor(sf::Color::Black);
+
+
+ // Player text input
+ playerText.setFont( font );
+ playerText.setCharacterSize( 20 );
+ sf::FloatRect gbpt {playerText.getGlobalBounds()};
+ playerText.setOrigin(gbpt.width / 2, gbpt.height / 2);
+ playerText.setPosition (S_WIDTH / 2 - 130, ((S_HEIGHT / 2)));
+ playerText.setFillColor(sf::Color::Black);
+
+
+ // Line text
+ linetext = sf::Text{ "_________________________", font, 24 };
+ sf::FloatRect gblt {linetext.getGlobalBounds()};
+ linetext.setOrigin(gblt.width / 2, gblt.height / 2);
+ linetext.setPosition (S_WIDTH / 2 , ((S_HEIGHT / 2) + 5));
+ linetext.setFillColor(sf::Color::Black);
+
+
+ // mouse
+ texture2.loadFromFile("assets/muspekareRed.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);
+
+
+ // Start menu text
+ savetext = sf::Text{ "Save Game", font, 24 };
+ sf::FloatRect gbr {savetext.getGlobalBounds()};
+ savetext.setOrigin(gbr.width / 2, gbr.height / 2);
+ savetext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) + 100));
+
+ // 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) + 150));
+}
+
+
+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)
+ {
+ savetext.setFillColor(sf::Color(153, 0, 0));
+ quittext.setFillColor(sf::Color::Black);
+ }
+ else if( menu_index == 2)
+ {
+ savetext.setFillColor(sf::Color::Black);
+ quittext.setFillColor(sf::Color(153, 0, 0));
+ }
+
+ // mouse placement
+ mouse_r.setPosition(((S_WIDTH / 2) + 60 - 30*(menu_index - 1)), ((S_HEIGHT / 2) + 85 + 50*(menu_index - 1)));
+ mouse_l.setPosition(((S_WIDTH / 2) - 58 + 30*(menu_index - 1)), ((S_HEIGHT / 2) + 132 + 50*(menu_index - 1)));
+}
+
+
+void GameOver_menu::render(sf::RenderWindow& window) const
+{
+ window.draw(sprite);
+
+ window.draw(rectangle);
+ window.draw(mouse_l);
+ window.draw(mouse_r);
+
+ window.draw(entertext);
+ window.draw(playerText);
+ window.draw(linetext);
+ window.draw(pointstext);
+ window.draw(savetext);
+ 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)
+ {
+ load_to_csv(playerText);
+ 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;
+ }
+
+ // enter name
+ if (event.type == sf::Event::TextEntered)
+ {
+ // delete last charakter with backspace
+ if (sf::Keyboard::isKeyPressed( sf::Keyboard::Backspace ))
+ {
+ if(!playerInput.isEmpty())
+ {
+ playerInput.erase(playerInput.getSize() - 1, 1);
+ }
+
+ else
+ {
+ return;
+ }
+
+ }
+
+ // if not backspace, print letters
+ else if(playerInput.getSize() < 30)
+ {
+ playerInput += event.text.unicode;
+ }
+
+ playerText.setString(playerInput);
+
+ }
+
+
+}
+
+//spara namn till fil
+void GameOver_menu::load_to_csv(sf::Text playertext)
+{
+ std::string current_line{playerText.getString() + ", " + std::to_string(points)};
+
+ std::ifstream highscore_file_r{"assets/highscore.csv"};
+ std::vector<std::string> lines_read;
+ std::vector<std::string> lines_write;
+ for (std::string one_line; std::getline(highscore_file_r, one_line);lines_read.push_back(one_line));
+
+ bool added {false};
+ for (std::string::size_type line_count{0}; line_count < lines_read.size(); line_count++)
+ {
+ if (!added && points > std::stoi(lines_read[line_count].substr(lines_read[line_count].find_first_of(',') + 1)))
+ {
+ lines_write.push_back(current_line);
+ added = true;
+ }
+ lines_write.push_back(lines_read[line_count]);
+ }
+
+ if (!added)
+ {
+ lines_write.push_back(current_line);
+ }
+
+
+ if (lines_write.size() > data["game_constants"]["scoreboard"]) lines_write.pop_back();
+
+ highscore_file_r.close();
+
+ std::ofstream highscore_file_w{"assets/highscore.csv"};
+ for (std::string::size_type line_count{0}; line_count < lines_write.size(); highscore_file_w << lines_write[line_count++] << std::endl);
+ highscore_file_w.close();
+}
\ No newline at end of file
+++ /dev/null
-#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;
-
-
-// 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::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::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(153, 0, 0));
-
- // -----------------------------------------------------------------------
- // First place text
- firstplacetext = sf::Text{ "1. NAME 100p\n2. 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(153, 0, 0));
- // -----------------------------------------------------------------------
-
- // 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(153, 0, 0));
- }
- else if( menu_index == 2)
- {
- startmenutext.setFillColor(sf::Color(153, 0, 0));
- 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(int const points, json& params) : texture{}, texture2{}, sprite{}, rectangle{}, mouse_l{}, mouse_r{}, linetext{}, entertext{}, pointstext{},
- playerInput{}, data{}, playerText{}, savetext{}, quittext{}, font{}, exit_game{false}, menu{false}, menu_index{1}, points{points}
-{
- 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);
-
- // load textfont
- if ( !font.loadFromFile ("assets/fonts/Philosopher-Regular.ttf") )
- throw std::invalid_argument ("Unable to load font");
-
-
- // transparent box
- rectangle = sf::CircleShape{200};
- rectangle.setFillColor(sf::Color(255, 255, 0, 180));
- rectangle.setOutlineThickness(7);
- rectangle.setOutlineColor(sf::Color( 0, 0, 0, 128));
- sf::FloatRect gbrs {rectangle.getGlobalBounds()};
- rectangle.setOrigin(gbrs.width / 2, gbrs.height / 2);
- rectangle.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2)));
-
-
- // Points text
- pointstext = sf::Text{ "Your points: " + std::to_string(points), font, 30 };
- sf::FloatRect gbp {pointstext.getGlobalBounds()};
- pointstext.setOrigin(gbp.width / 2, gbp.height / 2);
- pointstext.setPosition (S_WIDTH / 2 , ((S_HEIGHT / 2) - 100));
- pointstext.setFillColor(sf::Color::Black);
-
-
- // Enter text
- entertext = sf::Text{ "Enter your name:", font, 24 };
- sf::FloatRect gbe {entertext.getGlobalBounds()};
- entertext.setOrigin(gbe.width / 2, gbe.height / 2);
- entertext.setPosition (S_WIDTH / 2 , ((S_HEIGHT / 2) - 30));
- entertext.setFillColor(sf::Color::Black);
-
-
- // Player text input
- playerText.setFont( font );
- playerText.setCharacterSize( 24 );
- sf::FloatRect gbpt {playerText.getGlobalBounds()};
- playerText.setOrigin(gbpt.width / 2, gbpt.height / 2);
- playerText.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2)));
- playerText.setFillColor(sf::Color::Black);
-
-
- // Line text
- linetext = sf::Text{ "_________________________", font, 24 };
- sf::FloatRect gblt {linetext.getGlobalBounds()};
- linetext.setOrigin(gblt.width / 2, gblt.height / 2);
- linetext.setPosition (S_WIDTH / 2 , ((S_HEIGHT / 2) + 10));
- linetext.setFillColor(sf::Color::Black);
-
-
- // mouse
- texture2.loadFromFile("assets/muspekareRed.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);
-
-
- // Start menu text
- savetext = sf::Text{ "Save Game", font, 24 };
- sf::FloatRect gbr {savetext.getGlobalBounds()};
- savetext.setOrigin(gbr.width / 2, gbr.height / 2);
- savetext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) + 100));
-
- // 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) + 150));
-}
-
-
-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)
- {
- savetext.setFillColor(sf::Color(153, 0, 0));
- quittext.setFillColor(sf::Color::Black);
- }
- else if( menu_index == 2)
- {
- savetext.setFillColor(sf::Color::Black);
- quittext.setFillColor(sf::Color(153, 0, 0));
- }
-
- // mouse placement
- mouse_r.setPosition(((S_WIDTH / 2) + 60 - 30*(menu_index - 1)), ((S_HEIGHT / 2) + 85 + 50*(menu_index - 1)));
- mouse_l.setPosition(((S_WIDTH / 2) - 58 + 30*(menu_index - 1)), ((S_HEIGHT / 2) + 132 + 50*(menu_index - 1)));
-}
-
-
-void GameOver_menu::render(sf::RenderWindow& window) const
-{
- window.draw(sprite);
-
- window.draw(rectangle);
- window.draw(mouse_l);
- window.draw(mouse_r);
-
- window.draw(entertext);
- window.draw(playerText);
- window.draw(linetext);
- window.draw(pointstext);
- window.draw(savetext);
- 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)
- {
- load_to_csv(playerText);
- 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;
- }
-
- // enter name
- if (event.type == sf::Event::TextEntered)
- {
- // delete last charakter with backspace
- if (sf::Keyboard::isKeyPressed( sf::Keyboard::Backspace ))
- {
- if(!playerInput.isEmpty())
- {
- std::cout << "yey" << std::endl;
- playerInput.erase(playerInput.getSize() - 1, 1);
- }
-
- else
- {
- return;
- }
-
- }
-
-
- else
- {
- playerInput += event.text.unicode;
- playerText.setString(playerInput);
- }
-
- }
-
-
-}
-
-//spara namn till fil
-void GameOver_menu::load_to_csv(sf::Text playertext)
-{
- std::string current_line{playerText.getString() + ", " + std::to_string(points)};
-
- std::ifstream highscore_file_r{"assets/highscore.csv"};
- std::vector<std::string> lines_read;
- std::vector<std::string> lines_write;
- for (std::string one_line; std::getline(highscore_file_r, one_line);lines_read.push_back(one_line));
-
- bool added {false};
- for (std::string::size_type line_count{0}; line_count < lines_read.size(); line_count++)
- {
- if (!added && points > std::stoi(lines_read[line_count].substr(lines_read[line_count].find_first_of(',') + 1)))
- {
- std::cout << "i loop" << std::endl;
- lines_write.push_back(current_line);
- added = true;
- }
- lines_write.push_back(lines_read[line_count]);
- }
-
- if (!added) lines_write.push_back(current_line);
-
-
-
- if (lines_write.size() > data["game_constants"]["scoreboard"]) lines_write.pop_back();
-
- highscore_file_r.close();
-
- std::ofstream highscore_file_w{"assets/highscore.csv"};
- for (std::string::size_type line_count{0}; line_count < lines_write.size(); highscore_file_w << lines_write[line_count++] << std::endl);
- highscore_file_w.close();
-}
\ No newline at end of file
--- /dev/null
+#include <memory>
+#include <iostream>
+#include <string>
+
+#include "States.h"
+#include "Context.h"
+#include "constants.h"
+
+
+
+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::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;
+ }
+}
--- /dev/null
+#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;
+
+
+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(153, 0, 0));
+
+ // -----------------------------------------------------------------------
+ // First place text
+
+ std::ifstream highscore_file_r{"assets/highscore.csv"};
+
+ std::string csv_content {};
+ for (std::string one_line; std::getline(highscore_file_r, one_line); csv_content += one_line + "p \n");
+
+
+ firstplacetext = sf::Text{ csv_content , font, 18 };
+ sf::FloatRect gbfp {firstplacetext.getGlobalBounds()};
+ firstplacetext.setOrigin(gbfp.width / 2, gbfp.height / 2);
+ firstplacetext.setPosition (S_WIDTH / 2 - 15, ((S_HEIGHT / 2) - 30));
+ firstplacetext.setFillColor(sf::Color(153, 0, 0));
+
+ // -----------------------------------------------------------------------
+
+ // 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(153, 0, 0));
+ }
+ else if( menu_index == 2)
+ {
+ startmenutext.setFillColor(sf::Color(153, 0, 0));
+ 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;
+ }
+}
--- /dev/null
+#include <memory>
+#include <iostream>
+#include <string>
+
+#include "States.h"
+#include "Context.h"
+#include "constants.h"
+
+
+
+
+
+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::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;
+ }
+}
+