Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let bouncing snowball turn around on collision with other badguys #2967

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading