From: Arvid Sjöblom Date: Mon, 13 Nov 2023 13:43:30 +0000 (+0100) Subject: added collision between bottles and players, arvid, nils X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=a9f57e2de903ae79ecc936d97da4f7ef27839283;p=TDDC76_proj.git added collision between bottles and players, arvid, nils --- diff --git a/src/Bottle.cc b/src/Bottle.cc index 709ce16..568466f 100644 --- a/src/Bottle.cc +++ b/src/Bottle.cc @@ -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) { diff --git a/src/Bottle.h b/src/Bottle.h index 1dfabfa..ebba1ae 100644 --- a/src/Bottle.h +++ b/src/Bottle.h @@ -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; diff --git a/src/Object.h b/src/Object.h index bf5bbe4..1262cff 100644 --- a/src/Object.h +++ b/src/Object.h @@ -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; diff --git a/src/Player.cc b/src/Player.cc index dd0be27..42d54f7 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -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 diff --git a/src/Player.h b/src/Player.h index 17334a6..849c7b7 100644 --- a/src/Player.h +++ b/src/Player.h @@ -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; diff --git a/src/States.cc b/src/States.cc index 217c459..0d1422f 100644 --- a/src/States.cc +++ b/src/States.cc @@ -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 ()); } void Game_state::update(Context& context) @@ -21,17 +21,37 @@ void Game_state::update(Context& context) pause_game = false; return; } + //std::vector :: iterator it = bottles.begin(); + + /*for(std::unique_ptr &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()); + //std::cout << "placed bottle"<< std::endl; time_since_last_bottle = 0; } diff --git a/src/States.h b/src/States.h index 595aa31..f311aba 100644 --- a/src/States.h +++ b/src/States.h @@ -3,6 +3,7 @@ #include "SFML/Graphics.hpp" #include +#include #include "Map.h" #include "Player.h" @@ -31,7 +32,7 @@ public: protected: private: float time_since_last_bottle; - std::vector bottles; + std::vector> bottles; Map game_map; Player player; bool pause_game;