fixat json till bottle Nils
authorlukel495 <lukel495@su15-111.ad.liu.se>
Wed, 15 Nov 2023 13:33:15 +0000 (14:33 +0100)
committerlukel495 <lukel495@su15-111.ad.liu.se>
Wed, 15 Nov 2023 13:33:15 +0000 (14:33 +0100)
assets/data.json
src/Bottle.cc
src/Bottle.h
src/Helper.h
src/Player.h
src/States.cc
src/States.h

index 8b80c7c486235b770d351241f457843ef4c00778..f43f99e97a4d5912c8bde96ccf569422856e4108 100644 (file)
@@ -1,5 +1,6 @@
 {
-    "player" : {
+    "player" : 
+    {
         "start_pos": [200, 200],
         "scale": [0.5, 0.5],
         "max_speed": 5
         "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
index 7bc1e4d72bd4c8f7cf741f7f94677c8deaa0abc9..a284e173df1363053f0ae6af6ce3106e7f25cf2d 100644 (file)
@@ -1,14 +1,15 @@
 #include "Bottle.h"
 #include "constants.h"
 #include "Context.h"
+#include "States.h"
 #include <random>
 
-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);
index f7b2afa5b0aab63d4ff9184755f5ab037b6da75f..4eaa6e3252fe590f6c98c97720549abfb08819b1 100644 (file)
@@ -1,13 +1,18 @@
 #ifndef BOTTLE_H
 #define BOTTLE_H
 
-#include "Static_object.h"
 #include <SFML/Graphics.hpp>
 
+#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;
index aae5157e0d946a1e8dad65d6e72b3f8e604b0756..f420887e7372a79a44979e68b3d6076ab891e815 100644 (file)
@@ -2,6 +2,7 @@
 #define HELPER_H
 
 #include <SFML/Graphics.hpp> 
+
 #include "Autonomous_object.h"
 #include "json.hpp"
 
index f8184a6550043f95e6d6c5ec035776effba8cede..320e4b0d5d98dc39e2e60c99aa22e967643993ae 100644 (file)
@@ -4,6 +4,7 @@
 #define _USE_MATH_DEFINES
 
 #include <SFML/Graphics.hpp>
+
 #include "Moving_object.h"
 #include "json.hpp"
 
index fb371f079b7cab6bca8584a50524248ea59ca66f..14aa7e7fa9386a83f566a20878747f833a78b1b1 100644 (file)
@@ -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<Player>(data["player"]);
     helper = std::make_unique<Helper>(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>(bottle_texture));
+        bottles.push_back(std::make_unique<Bottle>(bottle_texture, data["bottle"]));
         std::cout << "placed bottle"<< std::endl;
         time_since_last_bottle = 0;
     }
index 7ac81f97b4bff3389c14b2674ce8860e5fe91a65..03f74a8fe5d17907095d5cb97ecdaa8ff18cffca 100644 (file)
@@ -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> helper;
     bool pause_game;
     sf::Texture bottle_texture;
+    json data;
 
 };