player can no longer go out of bound
authorstian853 <stian853@su10-106.ad.liu.se>
Wed, 29 Nov 2023 13:58:26 +0000 (14:58 +0100)
committerstian853 <stian853@su10-106.ad.liu.se>
Wed, 29 Nov 2023 13:58:26 +0000 (14:58 +0100)
src/Map.cc
src/Map.h
src/Player.cc
src/Player.h
src/States.cc

index de8879f0f4f42b2f62d2873ded2158f9f7b1c321..5f0e13ace4b328590ea30d9a91f9c1548cf696a5 100644 (file)
@@ -30,4 +30,12 @@ void Map::render(sf::RenderWindow& window) const
 {
     window.draw(sprite);
     window.draw(point_text);
+}
+
+bool Map::collides(Object& other)
+{
+    sf::FloatRect glb {sprite.getGlobalBounds()};
+    sf::FloatRect hb {other.get_hitbox()};
+    return !(glb.contains({hb.left, hb.top}) &&
+        glb.contains({hb.left + hb.width, hb.top + hb.height}));
 }
\ No newline at end of file
index f7a5b06c7a73431e055edcb7bb6f6b8746feabaa..7578e4705dd2b3dd49a512c3c93e70bce2e8f850 100644 (file)
--- a/src/Map.h
+++ b/src/Map.h
@@ -3,6 +3,7 @@
 
 #include <SFML/Graphics.hpp>
 #include "json.hpp"
+#include "Object.h"
 
 using json = nlohmann::json;
 
@@ -14,6 +15,7 @@ public:
 
     void update(int const collected, int const points, int const time_left);
     void render(sf::RenderWindow& window) const;
+    bool collides(Object& other);
 protected:
 private:
     sf::Texture texture;
index 1bf0ce550173f83cd926687914ea0d13e7770e4f..3920b945f6402a63f09a63b9237a159a0b3a708c 100644 (file)
@@ -34,6 +34,11 @@ void Player::collision(Object& other)
     move(false);
 }
 
+void Player::collision(Map& map)
+{
+    move(false);
+}
+
 void Player::collision(YF& yf)
 {
     move(false);
index 459b3ef6acaa446aac5d42365dbeaff1ecffd239..53036119eb36c7e73623ff11687ee134e6185ebc 100644 (file)
@@ -9,6 +9,7 @@
 #include "json.hpp"
 #include "YF.h"
 #include "Bike_enemy.h"
+#include "Map.h"
 
 using json = nlohmann::json;
 
@@ -19,6 +20,7 @@ public:
     ~Player() = default;
     
     void collision(Object& other) override;
+    void collision(Map& map);
     void collision(YF& yf);
     void collision(Bike_enemy& bike);
     void update(Context& context) override;
index 212937187934b227d5cec599ff385d8cf5b97bd9..2e8c1a90e672a4be6b8c448da050495611b65331 100644 (file)
@@ -99,7 +99,7 @@ void Game_state::update(Context &context)
         context.next_state = std::make_unique<GameOver_menu>();
         return;
     }
+    
 
     for (unsigned int i {0}; i < bottles.size(); ++i)
     {
@@ -151,7 +151,7 @@ void Game_state::update(Context &context)
             enemy->collision(*bike);
         }
     }
-
+    
 
     time_since_last_bottle += context.time.asSeconds();
     time_since_last_yf += context.time.asSeconds();
@@ -182,7 +182,10 @@ void Game_state::update(Context &context)
     {
         bike->update(context);
     }
-    
+    if(game_map->collides(*player))
+    {
+        player->collision(*game_map);
+    }
 }
 
 void Game_state::render(sf::RenderWindow &window) const