From 71033cffe28ec96b20625903b5f9768234ffb414 Mon Sep 17 00:00:00 2001 From: lukel495 Date: Fri, 17 Nov 2023 12:22:31 +0100 Subject: [PATCH] collison between helper and player --- src/Helper.cc | 13 ++++++++----- src/Helper.h | 2 +- src/Object.h | 3 ++- src/Player.cc | 3 ++- src/Player.h | 2 +- src/States.cc | 13 ++++++++++++- 6 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/Helper.cc b/src/Helper.cc index 454dd3b..56671c6 100644 --- a/src/Helper.cc +++ b/src/Helper.cc @@ -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(context.current_state.get()); sprite.setPosition(position); diff --git a/src/Helper.h b/src/Helper.h index f420887..84beb13 100644 --- a/src/Helper.h +++ b/src/Helper.h @@ -8,7 +8,7 @@ using json = nlohmann::json; -class Helper : public Autonomous_object +class Helper : public Object { public: Helper(json& params); diff --git a/src/Object.h b/src/Object.h index a7b7170..2b7e660 100644 --- a/src/Object.h +++ b/src/Object.h @@ -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(); diff --git a/src/Player.cc b/src/Player.cc index 2e1fa0f..13b6fdb 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -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 diff --git a/src/Player.h b/src/Player.h index be0d03a..0c3f0ee 100644 --- a/src/Player.h +++ b/src/Player.h @@ -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; diff --git a/src/States.cc b/src/States.cc index ee4a815..0845900 100644 --- a/src/States.cc +++ b/src/States.cc @@ -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); } -- 2.30.2