From: lukel495 Date: Wed, 15 Nov 2023 13:33:15 +0000 (+0100) Subject: fixat json till bottle Nils X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=772bdd27b88cc0be823d4fe013ff947fc38efc10;p=TDDC76_proj.git fixat json till bottle Nils --- diff --git a/assets/data.json b/assets/data.json index 8b80c7c..f43f99e 100644 --- a/assets/data.json +++ b/assets/data.json @@ -1,5 +1,6 @@ { - "player" : { + "player" : + { "start_pos": [200, 200], "scale": [0.5, 0.5], "max_speed": 5 @@ -13,5 +14,12 @@ "stop_top": 50, "stop_bot": 500 }, - "bottle": {} + "bottle": + { + "scale": [0.5, 0.5] + }, + "game_constants": + { + "bottles_per_second" : 2 + } } \ No newline at end of file diff --git a/src/Bottle.cc b/src/Bottle.cc index 7bc1e4d..a284e17 100644 --- a/src/Bottle.cc +++ b/src/Bottle.cc @@ -1,14 +1,15 @@ #include "Bottle.h" #include "constants.h" #include "Context.h" +#include "States.h" #include -Bottle::Bottle(sf::Texture& txtr) +Bottle::Bottle(sf::Texture& txtr, json& params) { texture = txtr; sprite.setTexture(texture); position ={rand()%(S_WIDTH*5/7 - S_WIDTH/5 +1) + S_WIDTH/5, rand()%S_HEIGHT+1}; //x-pixel WIDTH/5 - WIDTH*5/7 - sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); + sprite.setScale(params["scale"][0], params["scale"][1]); sf::FloatRect gb {sprite.getGlobalBounds()}; sprite.setOrigin(gb.width / 2, gb.height / 2); sprite.setPosition(position); diff --git a/src/Bottle.h b/src/Bottle.h index f7b2afa..4eaa6e3 100644 --- a/src/Bottle.h +++ b/src/Bottle.h @@ -1,13 +1,18 @@ #ifndef BOTTLE_H #define BOTTLE_H -#include "Static_object.h" #include +#include "Static_object.h" +#include "json.hpp" + +using json = nlohmann::json; + class Bottle : public Static_object { public: - Bottle(sf::Texture& txtr); + Bottle(sf::Texture& txtr, json& params); + void collision(Object& other) override; void update(Context& context) override; void render(sf::RenderWindow& window) const override; diff --git a/src/Helper.h b/src/Helper.h index aae5157..f420887 100644 --- a/src/Helper.h +++ b/src/Helper.h @@ -2,6 +2,7 @@ #define HELPER_H #include + #include "Autonomous_object.h" #include "json.hpp" diff --git a/src/Player.h b/src/Player.h index f8184a6..320e4b0 100644 --- a/src/Player.h +++ b/src/Player.h @@ -4,6 +4,7 @@ #define _USE_MATH_DEFINES #include + #include "Moving_object.h" #include "json.hpp" diff --git a/src/States.cc b/src/States.cc index fb371f0..14aa7e7 100644 --- a/src/States.cc +++ b/src/States.cc @@ -16,19 +16,17 @@ Game_state::Game_state() : player{}, bottles{}, time_since_last_bottle{0.0f}, - bottle_texture {} + bottle_texture {}, + data{} { std::ifstream f("assets/data.json"); - json data = json::parse(f); + data = json::parse(f); player = std::make_unique(data["player"]); helper = std::make_unique(data["helper"]); - bottle_texture.loadFromFile("assets/kir.png"); - - } void Game_state::update(Context &context) @@ -63,9 +61,9 @@ void Game_state::update(Context &context) } time_since_last_bottle += context.time.asSeconds(); std::cout << std::fixed << std::setprecision(3) << time_since_last_bottle << std::endl; - if (time_since_last_bottle >= 2) + if (time_since_last_bottle >= data["game_constants"]["bottles_per_second"]) { - bottles.push_back(std::make_unique(bottle_texture)); + bottles.push_back(std::make_unique(bottle_texture, data["bottle"])); std::cout << "placed bottle"<< std::endl; time_since_last_bottle = 0; } diff --git a/src/States.h b/src/States.h index 7ac81f9..03f74a8 100644 --- a/src/States.h +++ b/src/States.h @@ -9,6 +9,10 @@ #include "Player.h" #include "Helper.h" #include "Bottle.h" +#include "json.hpp" + +using json = nlohmann::json; + struct Context; //finns en strukt som säger att Context finns innan den är deklarerad @@ -39,6 +43,7 @@ private: std::unique_ptr helper; bool pause_game; sf::Texture bottle_texture; + json data; };