"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":
{
"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",
{
"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":
{
-Gorilla,50
-Gorilla,36
-Gorilla,32
-Gorilla,30
-Gorilla,27
+Nisseboi, 5
+Lukas is KUNG, 5
--- /dev/null
+#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;
+ }
+}
+#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"]}
{
#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;
+#include <random>
+
#include "Bottle.h"
#include "constants.h"
#include "Context.h"
-#include "States.h"
-#include <random>
+
Bottle::Bottle(sf::Texture& txtr, json& params)
{
#include <SFML/Graphics.hpp>
#include "Static_object.h"
+
#include "json.hpp"
using json = nlohmann::json;
#ifndef CONTEXT_H
#define CONTEXT_H
-#include "States.h"
#include <SFML/Graphics.hpp>
+#include <memory>
+
+#include "States.h"
struct Context
{
#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;
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);
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");
// mouse
- texture2.loadFromFile("assets/muspekareRed.png");
+ texture2.loadFromFile(data["gameover_menu"]["texture_mouse"]);
mouse_l.setTexture(texture2);
mouse_l.setScale(-0.1, 0.1);
{
window.draw(sprite);
- window.draw(rectangle);uniq
+ window.draw(rectangle);
window.draw(mouse_l);
window.draw(mouse_r);
{
window.close();
}
-}uniq
-
+}
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));
}
- 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
-#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{}
{
#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);
#include <cmath>
-#include <memory>
-#include <iostream>
+
#include "Main_enemy.h"
#include "States.h"
#include "Context.h"
//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)
#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;
#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{}
#define MAP_H
#include <SFML/Graphics.hpp>
-#include "json.hpp"
#include "Object.h"
+#include "json.hpp"
+
using json = nlohmann::json;
class Map
+++ /dev/null
-#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;
- }
-}
#include <memory>
-#include <iostream>
-#include <string>
#include "States.h"
#include "Context.h"
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);
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);
// load textfont
- if ( !font.loadFromFile (params["pause_menu"]["font"]) )
+ if ( !font.loadFromFile (data["pause_menu"]["font"]) )
{
throw std::invalid_argument ("Unable to load font");
}
#include <cmath>
-#include <iostream> // TA BORT!
#include "Player.h"
-#include "States.h"
#include "Context.h"
#include "constants.h"
#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
#include <memory>
-#include <iostream>
-#include <string>
#include <fstream>
-#include <vector>
-#include <cmath>
#include "States.h"
#include "Context.h"
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);
// mouse
- texture2.loadFromFile("assets/muspekare2.png");
+ texture2.loadFromFile(data["scoreboard_menu"]["texture_mouse"]);
mouse_l.setTexture(texture2);
mouse_l.setScale(-0.1, 0.1);
// 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");
// 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};
#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);
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);
//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
{
if (start_game)
{
- context.next_state = std::make_unique<Game_state>();
+ context.next_state = std::make_unique<Game_state>(data);
}
if (scoreboard)
#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{},
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;
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)
#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"
class Game_state : public State
{
public:
- Game_state();
+ Game_state(json& params);
~Game_state() = default;
void update(Context& context) override;
sf::Text quittext;
sf::Font font;
- std::string highscore_file;
- int scoreboard_size;
-
bool exit_game;
bool menu;
json data;
#ifndef STATIC_OBJECT_H
#define STATIC_OBJECT_H
-#include "Object.h"
#include <SFML/Graphics.hpp>
+#include "Object.h"
+
class Static_object : public Object
{
+#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"]}
{
}
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);
}
#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;
*/
#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{
};
//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())
{