From a19de866279033c75d9cda9248615803d473b894 Mon Sep 17 00:00:00 2001 From: Philip <155102424+Brockengespenst@users.noreply.github.com> Date: Mon, 9 Sep 2024 21:06:19 +0200 Subject: [PATCH] Let bouncing snowball turn around on collision with other badguys (#2967) * Let bouncing snowball turn around on collision with other badguys If bouncing snowball collides with another badguy, it will similar to the walking badguy turn around and bounce in the other direction. * Use correct indentation Co-authored-by: Vankata453 <78196474+Vankata453@users.noreply.github.com> --------- Co-authored-by: Vankata453 <78196474+Vankata453@users.noreply.github.com> --- src/badguy/bouncing_snowball.cpp | 33 ++++++++++++++++++++++++++++---- src/badguy/bouncing_snowball.hpp | 4 ++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/badguy/bouncing_snowball.cpp b/src/badguy/bouncing_snowball.cpp index ff397a347a1..143468720d4 100644 --- a/src/badguy/bouncing_snowball.cpp +++ b/src/badguy/bouncing_snowball.cpp @@ -26,7 +26,9 @@ static const float BSNOWBALL_WALKSPEED = 80; BouncingSnowball::BouncingSnowball(const ReaderMapping& reader) : BadGuy(reader, "images/creatures/bouncing_snowball/bouncing_snowball.sprite"), - m_x_speed() + m_x_speed(), + m_turn_around_timer(), + m_turn_around_counter() { m_x_speed = BSNOWBALL_WALKSPEED; parse_type(reader); @@ -34,7 +36,9 @@ BouncingSnowball::BouncingSnowball(const ReaderMapping& reader) : BouncingSnowball::BouncingSnowball(const Vector& pos, Direction d, float x_vel) : BadGuy(pos, d, "images/creatures/bouncing_snowball/bouncing_snowball.sprite"), - m_x_speed() + m_x_speed(), + m_turn_around_timer(), + m_turn_around_counter() { m_countMe = false; m_x_speed = x_vel; @@ -140,7 +144,7 @@ BouncingSnowball::collision_solid(const CollisionHit& hit) if (get_state() == STATE_ACTIVE) { float bounce_speed = -m_physic.get_velocity_y()*0.8f; m_physic.set_velocity_y(std::min(JUMPSPEED, bounce_speed)); - set_action(m_dir, "up", /* loops = */ 1); + set_action(m_dir, "up", /* loops = */ 1); } else { m_physic.set_velocity_y(0); } @@ -155,10 +159,31 @@ BouncingSnowball::collision_solid(const CollisionHit& hit) HitResponse BouncingSnowball::collision_badguy(BadGuy& , const CollisionHit& hit) { - collision_solid(hit); + if (!m_frozen && ((hit.left && (m_dir == Direction::LEFT)) || (hit.right && (m_dir == Direction::RIGHT)))) + turn_around(); + else + collision_solid(hit); + return CONTINUE; } +void +BouncingSnowball::turn_around() +{ + m_dir = m_dir == Direction::LEFT ? Direction::RIGHT : Direction::LEFT; + set_action(m_dir); + m_physic.set_velocity_x(-m_physic.get_velocity_x()); + m_physic.set_acceleration_x(-m_physic.get_acceleration_x()); + + // if we get dizzy, we fall off the screen + if (m_turn_around_timer.started()) { + if (m_turn_around_counter++ > 10) kill_fall(); + } else { + m_turn_around_timer.start(1.0f); + m_turn_around_counter = 0; + } +} + void BouncingSnowball::unfreeze(bool melt) { diff --git a/src/badguy/bouncing_snowball.hpp b/src/badguy/bouncing_snowball.hpp index fa7d44eaeac..aec140bb9d2 100644 --- a/src/badguy/bouncing_snowball.hpp +++ b/src/badguy/bouncing_snowball.hpp @@ -50,6 +50,7 @@ class BouncingSnowball final : public BadGuy protected: virtual bool collision_squished(GameObject& object) override; + void turn_around(); private: enum Type { @@ -58,6 +59,9 @@ class BouncingSnowball final : public BadGuy }; float m_x_speed; + Timer m_turn_around_timer; + int m_turn_around_counter; /**< Counts number of turns since turn_around_timer was started */ + private: BouncingSnowball(const BouncingSnowball&) = delete;