Skip to content

Commit

Permalink
Code cleaning and enable wind for many badguys
Browse files Browse the repository at this point in the history
* All badguys for which it makes sense can interact and be pushed by
wind
* Wind interaction logic put in seperate functions
* Fixed many member variable names in `src/object/rock.hpp` to conform
to the style guide
  • Loading branch information
biggeryetbetter committed Jul 10, 2024
1 parent 1be22aa commit ba6cee5
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 107 deletions.
21 changes: 17 additions & 4 deletions src/badguy/badguy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,7 @@ BadGuy::get_allowed_directions() const
void
BadGuy::active_update(float dt_sec)
{
if (m_col.m_colliding_wind.empty()) {
m_wind_velocity = Vector(0.f, 0.f);
m_wind_acceleration = 0.0;
}
handle_wind();

if (!is_grabbed())
{
Expand All @@ -412,6 +409,22 @@ BadGuy::active_update(float dt_sec)
}
}

void
BadGuy::handle_wind()
{
if (!m_col.m_colliding_wind.empty())
{
if (on_ground() && m_wind_velocity.y > 0.f)
m_wind_velocity.y = 0.f;

m_physic.set_velocity(m_physic.get_velocity() + m_wind_velocity);
}
else {
m_wind_velocity = Vector(0.f, 0.f);
m_wind_acceleration = 0.0;
}
}

void
BadGuy::inactive_update(float )
{
Expand Down
4 changes: 4 additions & 0 deletions src/badguy/badguy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class BadGuy : public MovingSprite,
state and calls active_update and inactive_update */
virtual void update(float dt_sec) override;

/** Called each frame during active_update. Applies velocity from
* wind if the badguy is inside wind and resets it if not. */
virtual void handle_wind();

static std::string class_name() { return "badguy"; }
virtual std::string get_class_name() const override { return class_name(); }
virtual std::string get_exposed_class_name() const override { return "BadGuy"; }
Expand Down
7 changes: 6 additions & 1 deletion src/badguy/fish_harmless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ void
FishHarmless::initialize()
{
FishSwimming::initialize();
set_colgroup_active(COLGROUP_MOVING_ONLY_STATIC);
set_colgroup_active(COLGROUP_MOVING);
}

HitResponse
FishHarmless::collision_player(Player& player, const CollisionHit& hit) {
return HitResponse::ABORT_MOVE;
}

/* EOF */
1 change: 1 addition & 0 deletions src/badguy/fish_harmless.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class FishHarmless final : public FishSwimming

protected:
virtual void initialize() override;
virtual HitResponse collision_player(Player& player, const CollisionHit& hit) override;

private:
FishHarmless(const FishHarmless&) = delete;
Expand Down
19 changes: 10 additions & 9 deletions src/badguy/flyingsnowball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,24 @@ FlyingSnowBall::active_update(float dt_sec)

float delta = total_time_elapsed * GLOBAL_SPEED_MULT;

// Put that function in a graphing calculator :
// Derivative of the following function (put it in a graphing calculator):
// sin(x)^3 + sin(3(x - pi/3))/3
float targetHgt = (
std::pow(std::sin(delta), 3.f) +
std::sin(3.f *
((delta - math::PI) / 3.f)
) / 3.f
) * 100.f;
std::cos(3.f * (delta - math::PI/3.f))
+ std::pow(std::sin(delta), 2.f)
* std::cos(delta) * 3.f
);

m_physic.set_velocity_y(m_physic.get_velocity_y() + (targetHgt - prev_height) / dt_sec);
prev_height = targetHgt;
// Simple damping and then movement
m_physic.set_velocity_y(m_physic.get_velocity_y() * pow(0.5f, dt_sec));
m_physic.set_velocity_y(m_physic.get_velocity_y() + targetHgt);

m_physic.set_velocity_x(m_physic.get_velocity_x() * pow(0.5f, dt_sec));

BadGuy::handle_wind();

m_col.set_movement(m_physic.get_movement(dt_sec));

m_physic.set_velocity_y(0.f);

auto player = get_nearest_player();
if (player) {
Expand Down
7 changes: 0 additions & 7 deletions src/badguy/mriceblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,6 @@ MrIceBlock::active_update(float dt_sec)
return;
}

if (!m_col.m_colliding_wind.empty()) {
if (on_ground() && m_wind_velocity.y > 0.f)
m_wind_velocity.y = 0.f;

m_physic.set_velocity(m_physic.get_velocity() + m_wind_velocity);
}

BadGuy::active_update(dt_sec);
}

Expand Down
1 change: 1 addition & 0 deletions src/badguy/stalactite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Stalactite::active_update(float dt_sec)
set_colgroup_active(COLGROUP_MOVING);
}
} else if (state == STALACTITE_FALLING) {
BadGuy::handle_wind();
m_col.set_movement(m_physic.get_movement(dt_sec));
}

Expand Down
8 changes: 0 additions & 8 deletions src/badguy/walking_badguy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,6 @@ WalkingBadguy::active_update(float dt_sec, float dest_x_velocity, float modifier
assert(false);
}

if (!m_col.m_colliding_wind.empty()) {
if (on_ground() && m_wind_velocity.y > 0.f)
m_wind_velocity.y = 0.f;

m_physic.set_velocity(m_physic.get_velocity() + m_wind_velocity);
}


if (max_drop_height > -1 && on_ground() && might_fall(max_drop_height+1) && !m_stay_on_platform_overridden)
turn_around();
m_stay_on_platform_overridden = false;
Expand Down
Loading

0 comments on commit ba6cee5

Please sign in to comment.