collison between helper and player
authorlukel495 <lukel495@su10-103.ad.liu.se>
Fri, 17 Nov 2023 11:22:31 +0000 (12:22 +0100)
committerlukel495 <lukel495@su10-103.ad.liu.se>
Fri, 17 Nov 2023 11:22:31 +0000 (12:22 +0100)
src/Helper.cc
src/Helper.h
src/Object.h
src/Player.cc
src/Player.h
src/States.cc

index 454dd3baa13caefc1973cbba5acccf107c066ce6..56671c6037c2e76bd3def6a20b17f3a87b8c190f 100644 (file)
@@ -29,14 +29,17 @@ void Helper::collision(Object& other)
 
 }
 
+
 void Helper::update(Context& context)
 {
-    position += direction * max_speed;
+        position += direction * max_speed;
+
+        if (position.y >= stop_bot || position.y <= stop_top )
+        {
+            direction *= -1.0f;
+        }
+
 
-    if (position.y >= stop_bot || position.y <= stop_top )
-    {
-        direction *= -1.0f;
-    }
 
     //Game_state* game = static_cast<Game_state*>(context.current_state.get());
     sprite.setPosition(position);
index f420887e7372a79a44979e68b3d6076ab891e815..84beb13f568c1901c50bca836da88393ec116691 100644 (file)
@@ -8,7 +8,7 @@
 
 using json = nlohmann::json;
 
-class Helper : public Autonomous_object
+class Helper : public Object
 {
 public:
     Helper(json& params);
index a7b7170b71d091ce37352ef89bdc0ed2928a8979..2b7e6607ad3685289f45b7a5b7a087b5cc8768e1 100644 (file)
@@ -11,13 +11,14 @@ public:
     Object() : position{}, sprite{}, texture{}, direction{} {};
     virtual ~Object() = default;
 
-    bool collides(Object& other)
+    virtual bool collides(Object& other)
     {
         return get_hitbox().intersects(other.get_hitbox());
     }
     virtual void collision(Object& other) = 0;
     virtual void update(Context& context) = 0;
     virtual void render(sf::RenderWindow& window) const = 0;
+
     sf::FloatRect get_hitbox()
     {
         return sprite.getGlobalBounds();
index 2e1fa0f6ac7c417d9ae5c28712ebbebe51f1d70a..13b6fdb4170b843fcab97fa096c1a93af6fd4907 100644 (file)
@@ -83,7 +83,8 @@ void Player::handle_input(sf::Event& event)
     }    
 }
 
-void Player::add_collected()
+int Player::add_collected()
 {
     collected += 1;
+    return collected;
 }
\ No newline at end of file
index be0d03a95d74bb1e3ca18177e738b81ac70196da..0c3f0ee5151f428068d73e8f4cc2920c05b6109a 100644 (file)
@@ -20,7 +20,7 @@ public:
     void update(Context& context) override;
     void render(sf::RenderWindow& window) const override;
     void handle_input(sf::Event& event);
-    void add_collected();
+    int add_collected();
 protected:
 private:
     int collected;
index ee4a815fdf116a133eb4adb73c62dc22b0813bca..08459002c0b3008649f7ea5b07498ac17affe577 100644 (file)
@@ -15,6 +15,7 @@ Game_state::Game_state() :
     pause_game{false},
     player{},
     bottles{}, 
+    helper{},
     time_since_last_bottle{0.0f},
     bottle_texture {},
     data{},
@@ -69,6 +70,13 @@ void Game_state::update(Context &context)
             
         }
     }
+    if (player->collides(*helper))
+    {
+        //reset add collected 
+        // reset hitbox of sack
+
+    }
     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"])
@@ -78,8 +86,11 @@ void Game_state::update(Context &context)
         time_since_last_bottle = 0;
     }
     game_map.update(context);
+    if (!(player->collides(*helper)))
+    {
+        helper->update(context);
+    }
     player->update(context);
-    helper->update(context);
     enemy.update(context);
 }