diff --git a/src/badguy/bouncing_snowball.cpp b/src/badguy/bouncing_snowball.cpp index ff397a347a..143468720d 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 fa7d44eaea..aec140bb9d 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;