From 15295ddd9875e7be57a7461b26d498a395e1e8ff Mon Sep 17 00:00:00 2001 From: Brockengespenst Date: Tue, 4 Jun 2024 20:13:33 +0200 Subject: [PATCH 1/2] 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. --- src/badguy/bouncing_snowball.cpp | 31 +++++++++++++++++++++++++++---- src/badguy/bouncing_snowball.hpp | 5 +++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/badguy/bouncing_snowball.cpp b/src/badguy/bouncing_snowball.cpp index 125a8d3d93e..4d160611fc1 100644 --- a/src/badguy/bouncing_snowball.cpp +++ b/src/badguy/bouncing_snowball.cpp @@ -24,8 +24,10 @@ static const float JUMPSPEED = -450; static const float BSNOWBALL_WALKSPEED = 80; -BouncingSnowball::BouncingSnowball(const ReaderMapping& reader) - : BadGuy(reader, "images/creatures/bouncing_snowball/bouncing_snowball.sprite") +BouncingSnowball::BouncingSnowball(const ReaderMapping& reader) : + BadGuy(reader, "images/creatures/bouncing_snowball/bouncing_snowball.sprite"), + m_turn_around_timer(), + m_turn_around_counter() { parse_type(reader); } @@ -128,7 +130,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); } @@ -143,10 +145,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 3b65c5319a2..cdf3f5f0465 100644 --- a/src/badguy/bouncing_snowball.hpp +++ b/src/badguy/bouncing_snowball.hpp @@ -48,6 +48,7 @@ class BouncingSnowball final : public BadGuy protected: virtual bool collision_squished(GameObject& object) override; + void turn_around(); private: enum Type { @@ -55,6 +56,10 @@ class BouncingSnowball final : public BadGuy FATBAT }; + 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; BouncingSnowball& operator=(const BouncingSnowball&) = delete; From 703f316c3f67cc1be3875f762243a36a8bde51a3 Mon Sep 17 00:00:00 2001 From: Philip <155102424+Brockengespenst@users.noreply.github.com> Date: Wed, 5 Jun 2024 06:52:33 +0200 Subject: [PATCH 2/2] Use correct indentation Co-authored-by: Vankata453 <78196474+Vankata453@users.noreply.github.com> --- src/badguy/bouncing_snowball.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/badguy/bouncing_snowball.cpp b/src/badguy/bouncing_snowball.cpp index 4d160611fc1..22d237cac37 100644 --- a/src/badguy/bouncing_snowball.cpp +++ b/src/badguy/bouncing_snowball.cpp @@ -25,9 +25,9 @@ static const float JUMPSPEED = -450; static const float BSNOWBALL_WALKSPEED = 80; BouncingSnowball::BouncingSnowball(const ReaderMapping& reader) : - BadGuy(reader, "images/creatures/bouncing_snowball/bouncing_snowball.sprite"), - m_turn_around_timer(), - m_turn_around_counter() + BadGuy(reader, "images/creatures/bouncing_snowball/bouncing_snowball.sprite"), + m_turn_around_timer(), + m_turn_around_counter() { parse_type(reader); }