From: stian853 Date: Wed, 29 Nov 2023 13:58:26 +0000 (+0100) Subject: player can no longer go out of bound X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=44b5b1d142811b445401841a216def8cd002d9bf;p=TDDC76_proj.git player can no longer go out of bound --- diff --git a/src/Map.cc b/src/Map.cc index de8879f..5f0e13a 100644 --- a/src/Map.cc +++ b/src/Map.cc @@ -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 diff --git a/src/Map.h b/src/Map.h index f7a5b06..7578e47 100644 --- a/src/Map.h +++ b/src/Map.h @@ -3,6 +3,7 @@ #include #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; diff --git a/src/Player.cc b/src/Player.cc index 1bf0ce5..3920b94 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -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); diff --git a/src/Player.h b/src/Player.h index 459b3ef..5303611 100644 --- a/src/Player.h +++ b/src/Player.h @@ -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; diff --git a/src/States.cc b/src/States.cc index 2129371..2e8c1a9 100644 --- a/src/States.cc +++ b/src/States.cc @@ -99,7 +99,7 @@ void Game_state::update(Context &context) context.next_state = std::make_unique(); 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