"direction": [0, 1],
"sin_amplitude": 50,
"sin_omega": 5
+ },
+ "game_state_assets":
+ {
+ "kir_file": "assets/kir.png",
+ "YF_file": "assets/YF.png",
+ "cyklist_file": "assets/cyklist.png",
+ "ling_file": "assets/6Ling_figur2.png",
+ "sack_file": "assets/sack.png",
+ "player_file": "assets/4V_figur.png",
+ "helper_file": "assets/ysex.png",
+ "map_file": "assets/bakgrund.png"
}
}
\ No newline at end of file
#include "States.h"
#include <random>
-Bottle::Bottle(sf::Texture& txtr, json& params)
+Bottle::Bottle(sf::Texture& txtr, json& params)
{
texture = txtr;
sprite.setTexture(texture);
#include <iostream>
-Helper::Helper(json& params) : stop_bot{params["stop_bot"]}, stop_top{params["stop_top"]}, max_speed{params["max_speed"]}, movement{}
+Helper::Helper(sf::Texture& txtr, json& params) : stop_bot{params["stop_bot"]}, stop_top{params["stop_top"]}, max_speed{params["max_speed"]}, movement{}
{
+ texture = txtr;
position = {params["start_pos"][0], params["start_pos"][1]};
direction = {params["direction"][0], params["direction"][1]};
movement = direction;
- texture.loadFromFile("assets/ysex.png");
sprite.setTexture(texture);
sprite.setScale(params["scale"][0], params["scale"][1]);
class Helper : public Object
{
public:
- Helper(json& params);
+ Helper(sf::Texture& txtr, json& params);
~Helper() = default;
void collision(Object& other) override;
#include "constants.h"
#include <string>
-Map::Map(json& params) : texture{}, sprite{}, point_text{}, collected_text{}, time_text{}, bottle_texture{}, bottle_sprite{}, font{}
+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{}
{
- texture.loadFromFile("assets/bakgrund.png");
sprite.setTexture(texture);
- bottle_texture.loadFromFile("assets/kir.png");
bottle_sprite.setTexture(bottle_texture);
sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF);
sprite.setPosition(0,0);
class Map
{
public:
- Map(json& params);
+ Map(sf::Texture& txtr, sf::Texture& bottle_txtr, json& params);
~Map() = default;
void update(int const collected, int const points, int const time_left);
#include "Context.h"
#include "constants.h"
-Player::Player(json& params) : collected{0}, max_speed{params["max_speed"]}, tumbling{false}, tumble_degrees{0}, rotation{params["degrees_per_second"]}
+Player::Player(sf::Texture& player_txtr, sf::Texture& sack_txtr, json& params) : sack_texture{sack_txtr}, collected{0}, max_speed{params["max_speed"]}, tumbling{false}, tumble_degrees{0}, rotation{params["degrees_per_second"]}
{
+ texture = player_txtr;
position = {params["start_pos"][0], params["start_pos"][1]};
texture.loadFromFile("assets/4V_figur.png");
class Player : public Moving_object
{
public:
- Player(json& params);
+ Player(sf::Texture& player_txtr, sf::Texture& sack_txtr, json& params);
~Player() = default;
void collision(Object& other) override;
bottles{},
helper{},
bike{},
- main_enemy_texture{},
time_since_last_bottle{0.0f},
game_time{0},
time_since_last_yf{0.0f},
{
std::ifstream f("assets/data.json");
data = json::parse(f);
+ sf::Texture main_enemy_texture;
+ sf::Texture sack_texture;
+ sf::Texture player_texture;
+ sf::Texture helper_texture;
+ sf::Texture map_texture;
- main_enemy_texture.loadFromFile("assets/6Ling_figur2.png");
+ main_enemy_texture.loadFromFile(data["game_state_assets"]["ling_file"]);
+ sack_texture.loadFromFile(data["game_state_assets"]["sack_file"]);
+ player_texture.loadFromFile(data["game_state_assets"]["player_file"]);
+ helper_texture.loadFromFile(data["game_state_assets"]["helper_file"]);
+ map_texture.loadFromFile(data["game_state_assets"]["map_file"]);
+
+ player = std::make_unique<Player>(player_texture, sack_texture, data["player"]);
+ helper = std::make_unique<Helper>(helper_texture, data["helper"]);
- player = std::make_unique<Player>(data["player"]);
- helper = std::make_unique<Helper>(data["helper"]);
- game_map = std::make_unique<Map>(data["map"]);
enemy = std::make_unique<Main_enemy>(main_enemy_texture, data["main_enemy"]);
- bottle_texture.loadFromFile("assets/kir.png");
- YF_texture.loadFromFile("assets/YF.png");
- bike_texture.loadFromFile("assets/cyklist.png");
+ bottle_texture.loadFromFile(data["game_state_assets"]["kir_file"]);
+ YF_texture.loadFromFile(data["game_state_assets"]["YF_file"]);
+ bike_texture.loadFromFile(data["game_state_assets"]["cyklist_file"]);
+
+ game_map = std::make_unique<Map>(map_texture, bottle_texture, data["map"]);
+
f.close();
}
int remaining_time {static_cast<int>(data["game_constants"]["game_time"]) - static_cast<int>(game_time)};
if (remaining_time < 0)
{
- // LÄGG ALLT NEDAN I GAME OVER MENYN.
- std::string current_line{"Gorilla," + 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();
-
context.next_state = std::make_unique<GameOver_menu>();
return;
}
using json = nlohmann::json;
-
-
struct Context;
class State
bool pause_game;
sf::Texture bottle_texture;
- sf::Texture main_enemy_texture;
sf::Texture YF_texture;
sf::Texture bike_texture;
//std::vector<std::unique_ptr<YF>> yf;