Added json, fixed time, fixed scales and positions, Nils Lukas
authorlukel495 <lukel495@su00-102.ad.liu.se>
Thu, 23 Nov 2023 11:02:32 +0000 (12:02 +0100)
committerlukel495 <lukel495@su00-102.ad.liu.se>
Thu, 23 Nov 2023 11:02:32 +0000 (12:02 +0100)
Makefile
assets/data.json
src/Main_enemy.cc
src/Map.cc
src/Map.h
src/Player.cc
src/States.cc
src/States.h

index bd5e4089bf2374fa9e5cb22f331376147031561c..844643121afc0a379f26d3290e9eaaf7cd7fbbdb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -34,7 +34,7 @@ test-target: $(OBJDIR)/_test.o $(OBJECTS)
        $(CC) $(LDFLAGS) $^ -o $(TEST) $(LDFLAGS)
 
 $(OBJDIR)/%.o: %.cc $(SRC_H)
-       $(CC) $(CCFLAGS) -c -o $@ $<
+       $(CC) $(CCFLAGS) $(LDFLAGS) -c -o $@ $<
 
 run:
        ./$(EXE)
index 5ed7ec8c0a5a01bcd063d2c3f4783213843a947d..7e8b395b76b59a6bdc5ac7528c13a64f06830407 100644 (file)
@@ -3,7 +3,14 @@
     {
         "start_pos": [200, 200],
         "scale": [0.5, 0.5],
-        "max_speed": 5
+        "max_speed": 5,
+        "sack" : 
+        {
+            "offset" : [30, 18],
+            "scale" : [0.3, 0.3],
+            "angle" : -25
+        }
+        
     },
     "helper": {
         "start_pos": [1000, 50],
     },
     "game_constants":
     {
-        "bottles_per_second" : 2
+        "bottles_per_second" : 2,
+        "game_time" : 20
+        
     },
     "map":
     {
-        "points_pos": [100, 100],
-        "font_size": 24
+        "points_pos": [5, 0],
+        "font_size": 40
     },
 
     "main_enemy":
     {
-        "scale" : [0.35, 0.35]
+        "scale" : [0.35, 0.35],
+        "start_pos" : [50, 600]
     }
-
 }
\ No newline at end of file
index 3b3ae8367cf7cbbd8d43373a68b12664319f9e40..68002b9cf9a11ab0f5acff90f1c8e25d033820f7 100644 (file)
@@ -10,7 +10,7 @@ Main_enemy::Main_enemy(sf::Texture& txtr, json& params)
     //texture.loadFromFile("assets/6Ling_figur2.png");
     texture = txtr;
     sprite.setTexture(texture);
-
+    position = {params["start_pos"][0], params["start_pos"][1]};
     sprite.setScale(params["scale"][0], params["scale"][1]);
     sf::FloatRect gb {sprite.getGlobalBounds()};
     sprite.setOrigin(gb.width / 2, gb.height / 2);
index 36728abb35b2cf624c242aa82add5afddad4f092..0251acae9e5a30db410cf7426af764400c75946a 100644 (file)
@@ -13,17 +13,17 @@ Map::Map(json& params) : texture{}, sprite{}, point_text{}, font{}
     if ( !font.loadFromFile ("assets/fonts/Philosopher-Regular.ttf") )
         throw std::invalid_argument ("Unable to load font");
 
-    point_text = sf::Text{"Points: 0", font, params["font_size"] };
+    point_text = sf::Text{"Points: 0", font, params["font_size"]};
     sf::FloatRect gbts {point_text.getGlobalBounds()};
-    point_text.setOrigin(gbts.width / 2, gbts.height / 2);
 
     point_text.setPosition (params["points_pos"][0], params["points_pos"][1]);
-    point_text.setFillColor(sf::Color::Red);
+    point_text.setFillColor(sf::Color::Black);
+
 }
 
-void Map::update(int points)
+void Map::update(int const points, int const game_time)
 {
-    point_text.setString("Points: " + std::to_string(points));
+    point_text.setString("Points: " + std::to_string(points) + "\n" + "Time left: " + std::to_string(game_time) + " s");
 }
 
 void Map::render(sf::RenderWindow& window) const
index 48919014162cf8476cf357e604353e80c45f9ae4..5d2c9c1866b628a06a4be911d059ff32e3b53b54 100644 (file)
--- a/src/Map.h
+++ b/src/Map.h
@@ -12,7 +12,7 @@ public:
     Map(json& params);
     ~Map() = default;
 
-    void update(int points);
+    void update(int const points, int const game_state);
     void render(sf::RenderWindow& window) const;
 protected:
 private:
index 71eddcbb774e41be822f7b80f438bb178536c336..7f52064984422e139b4b795aefba91cfc54f883e 100644 (file)
@@ -20,11 +20,12 @@ Player::Player(json& params) : collected{0}, max_speed{params["max_speed"]}
     
     sack_texture.loadFromFile("assets/sack.png");
     sack.setTexture(sack_texture);
-    sack.setScale(0.5, 0.5);
+    sack.setScale(params["sack"]["scale"][0], params["sack"]["scale"][1]);
     
     sf::FloatRect gb_s {sack.getGlobalBounds()};
-    sack.setOrigin(gb_s.width / 2, gb_s.height / 2);
-    sack_offset = {gb.width/4, -gb.height/15};
+    sack.setOrigin(gb_s.width / 3, gb_s.height / 5);
+    sack_offset = {params["sack"]["offset"][0], params["sack"]["offset"][1]};
+    sack.rotate(params["sack"]["angle"]);
     sack.setPosition(position + sack_offset);
 
 }
index 7c57caceff91a1e316bb3abb6cfd27f01fa4663a..eff307cbe0e75354a7183f2df58114efc218a46c 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "json.hpp"
 #include <fstream>
+#include <cmath>
 
 using json = nlohmann::json;
 
@@ -18,6 +19,7 @@ Game_state::Game_state() :
     helper{},
     main_enemy_texture{},
     time_since_last_bottle{0.0f},
+    game_time{0},
     bottle_texture {},
     data{},
     enemy{},
@@ -47,17 +49,16 @@ void Game_state::update(Context &context)
         pause_game = false;
         return;
     }
-    // std::vector<Bottle> :: iterator it = bottles.begin();
-
-    /*for(std::unique_ptr<Bottle> &b: bottles)
+        
+    game_time += context.time.asSeconds();
+    int remaining_time {static_cast<int>(data["game_constants"]["game_time"]) - static_cast<int>(game_time)};
+    if (remaining_time < 0)
     {
-        if(player.collides(*b))
-        {
-            std::cout <<"crash" << std::endl;
+        context.next_state = std::make_unique<Start_menu>();
+        return;
+    }
 
-            player.add_collected();
-        }
-    }*/
     for (unsigned int i {0}; i < bottles.size(); ++i)
     {
         if (player->collides(*(bottles[i])))
@@ -76,8 +77,9 @@ void Game_state::update(Context &context)
             
         }
     }
-    helper->update(context);
     player->update(context);
+    helper->update(context);
+
     if (player->collides(*helper))
     {
         points += player->get_collected();
@@ -86,9 +88,15 @@ void Game_state::update(Context &context)
         player->collision(*helper);
         helper->collision(*player);
     }
+    /*
+    if (player->get_x() < 0) player->set_x(0);
+    if (player->get_x() > S_WIDTH) player->set_x(S_WIDTH);
+    if (player->get_y() < 0) player->set_y(0);
+    if (player->get_y() > S_HEIGHT) player->set_y(S_HEIGHT);
+    */
 
     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 >= data["game_constants"]["bottles_per_second"])
     {
@@ -96,8 +104,10 @@ void Game_state::update(Context &context)
         std::cout << "placed bottle"<< std::endl;
         time_since_last_bottle = 0;
     }
-    game_map->update(points);
+    //game time
     enemy->update(context);
+    game_map->update(points, remaining_time);
+
 }
 
 void Game_state::render(sf::RenderWindow &window) const
index 1c8c148505b39d481ce59006ce8c6afd20eaade1..9eedbf58890d6ad7f69020ac232c762bf3b54354 100644 (file)
@@ -41,6 +41,7 @@ public:
 protected:
 private:
     float time_since_last_bottle;
+    float game_time;
     
     std::unique_ptr<Map> game_map;
     std::unique_ptr<Player> player;