Skip to content

Commit

Permalink
Let bouncing snowball turn around on collision with other badguys (#2967
Browse files Browse the repository at this point in the history
)

* 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 <[email protected]>

---------

Co-authored-by: Vankata453 <[email protected]>
  • Loading branch information
Brockengespenst and Vankata453 committed Sep 9, 2024
1 parent 9f29715 commit a19de86
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/badguy/bouncing_snowball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ 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);
}

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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)
{
Expand Down
4 changes: 4 additions & 0 deletions src/badguy/bouncing_snowball.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class BouncingSnowball final : public BadGuy

protected:
virtual bool collision_squished(GameObject& object) override;
void turn_around();

private:
enum Type {
Expand All @@ -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;
Expand Down

0 comments on commit a19de86

Please sign in to comment.