added collision between bottles and players, arvid, nils
authorArvid Sjöblom <arvsj277@student.liu.se>
Mon, 13 Nov 2023 13:43:30 +0000 (14:43 +0100)
committerArvid Sjöblom <arvsj277@student.liu.se>
Mon, 13 Nov 2023 13:43:30 +0000 (14:43 +0100)
src/Bottle.cc
src/Bottle.h
src/Object.h
src/Player.cc
src/Player.h
src/States.cc
src/States.h

index 709ce1602f0153a5d81b23ceb53b3c2ab0c02836..568466faaa8909cba2d7c22bae852f0314cd7c19 100644 (file)
@@ -13,10 +13,7 @@ Bottle::Bottle()
     sprite.setOrigin(gb.width / 2, gb.height / 2);
     sprite.setPosition(position);
 }
-bool Bottle::collides(Object& other)
-{
 
-}
 void Bottle::collision(Object& other) 
 {
 
index 1dfabfa4e1e13f7b6f7e315747f3e7686ec8dacc..ebba1aeec9350d9400058beed4b402803db2cff6 100644 (file)
@@ -8,9 +8,6 @@ class Bottle : public Static_object
 {
 public:
     Bottle();
-    
-    
-    bool collides(Object& other) override;
     void collision(Object& other) override;
     void update(Context& context) override;
     void render(sf::RenderWindow& window) const override;
index bf5bbe40ad626a5720167eb88b41f5513e90485c..1262cffa5858a4fe91b48ea20884091bdaf0653c 100644 (file)
@@ -11,11 +11,17 @@ public:
     Object() : position{}, sprite{}, texture{} {};
     virtual ~Object() = default;
 
-    virtual bool collides(Object& other) = 0;
+    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();
+    }
 protected:
     sf::Vector2f position;
     sf::Sprite sprite;
index dd0be279b4e5010111a816b12e3fedbb1f864a7b..42d54f77484b7b744f8bbbdb3a9dff8cf24aaefd 100644 (file)
@@ -18,10 +18,6 @@ Player::Player() : collected{0}, max_speed{10}
     sprite.setPosition(position);
 }
 
-bool Player::collides(Object& other)
-{
-    return false;
-}
 
 void Player::collision(Object& other)
 {
@@ -72,4 +68,9 @@ void Player::handle_input(sf::Event& event)
         direction.x *= M_SQRT1_2;
         direction.y *= M_SQRT1_2;
     }    
+}
+
+void Player::add_collected()
+{
+    collected += 1;
 }
\ No newline at end of file
index 17334a6512ac3928c02e5b821e287534d51f95da..849c7b70203347360bf9b87ceade3d3b7b20df37 100644 (file)
@@ -11,11 +11,12 @@ class Player : public Moving_object
 public:
     Player();
     ~Player() = default;
-    bool collides(Object& other) override;
+    
     void collision(Object& other) override;
     void update(Context& context) override;
     void render(sf::RenderWindow& window) const override;
     void handle_input(sf::Event& event);
+    void add_collected();
 protected:
 private:
     int collected;
index 217c45913793adaf25f13db14e82f1074e990f3e..0d1422f6e8a226321c61b3f4e0dc6a399abf361b 100644 (file)
@@ -7,8 +7,8 @@
 
 Game_state::Game_state() : game_map{}, player{}, pause_game{false}, bottles{}, time_since_last_bottle{0.0f}
 {
-    Bottle* bottle = new Bottle {};
-    bottles.push_back(bottle);
+    
+    bottles.push_back(std::make_unique<Bottle> ());
 }
 
 void Game_state::update(Context& context)
@@ -21,17 +21,37 @@ void Game_state::update(Context& context)
         pause_game = false;
         return;
     }
+    //std::vector<Bottle> :: iterator it = bottles.begin();
+
+    /*for(std::unique_ptr<Bottle> &b: bottles)
+    {
+        if(player.collides(*b))
+        {
+            std::cout <<"crash" << std::endl;
+            
+            player.add_collected();
+        }
+    }*/
+    for(unsigned int i = 0; i < bottles.size(); ++i)
+    {
+        if(player.collides(*(bottles[i])))
+        {
+            //std::cout <<"crash" << std::endl;
+            bottles.erase(bottles.begin() + i);
+            player.add_collected();
+        }
+    }
     time_since_last_bottle += context.time.asSeconds();
-    std::cout << std::fixed << std::setprecision(3) << time_since_last_bottle << std::endl;
+    //std::cout << std::fixed << std::setprecision(3) << time_since_last_bottle << std::endl;
     if (time_since_last_bottle >= 2)
     {
         if (bottles.size() > 10)
         {
             bottles.erase(bottles.begin());
         }
-        Bottle* bottle = new Bottle {};
-        bottles.push_back(bottle);
-        std::cout << "placed bottle"<< std::endl;
+        
+        bottles.push_back(std::make_unique<Bottle>());
+        //std::cout << "placed bottle"<< std::endl;
         time_since_last_bottle = 0;
 
     }
index 595aa317f36b3da98ea9957390c7ba8cf43d82eb..f311abaa2fcc94fafb8eee667e20a62105ea874c 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "SFML/Graphics.hpp"
 #include <vector>
+#include <memory>
 
 #include "Map.h"
 #include "Player.h"
@@ -31,7 +32,7 @@ public:
 protected:
 private:
     float time_since_last_bottle;
-    std::vector<Bottle*> bottles;
+    std::vector<std::unique_ptr<Bottle>> bottles;
     Map game_map;
     Player player;
     bool pause_game;