Skip to content

Commit 1a1c8c9

Browse files
committed
Refactor: Extract collision logic to handle_collision_logic()
Avoids code duplication between Player::collision_solid and Player::collision by introducing a new helper function, handle_collision_logic(). Closes #3209
1 parent 83c8ce0 commit 1a1c8c9

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

src/object/player.cpp

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,7 +2307,7 @@ Player::collision_tile(uint32_t tile_attributes)
23072307
}
23082308

23092309
void
2310-
Player::collision_solid(const CollisionHit& hit)
2310+
Player::handle_collision_logic(const CollisionHit& hit)
23112311
{
23122312
if (hit.bottom) {
23132313
if (m_physic.get_velocity_y() > 0)
@@ -2360,6 +2360,12 @@ Player::collision_solid(const CollisionHit& hit)
23602360
m_boost = 0.f;
23612361
}
23622362

2363+
void
2364+
Player::collision_solid(const CollisionHit& hit)
2365+
{
2366+
handle_collision_logic(hit);
2367+
}
2368+
23632369
HitResponse
23642370
Player::collision(MovingObject& other, const CollisionHit& hit)
23652371
{
@@ -2392,31 +2398,8 @@ Player::collision(MovingObject& other, const CollisionHit& hit)
23922398
return FORCE_MOVE;
23932399
if (m_stone)
23942400
return ABORT_MOVE;
2395-
if (badguy->is_frozen() && hit.bottom && badguy->get_physic().get_velocity_y() != 0) {
2396-
if (m_physic.get_velocity_y() > 0)
2397-
m_physic.set_velocity_y(0);
2398-
2399-
m_on_ground_flag = true;
2400-
m_floor_normal = hit.slope_normal;
2401-
m_is_slidejump_falling = false;
2402-
m_slidejumping = false;
2403-
2404-
// Butt Jump landed
2405-
if (m_does_buttjump) {
2406-
m_does_buttjump = false;
2407-
m_buttjump_stomp = true;
2408-
m_physic.set_velocity_y(-300);
2409-
m_on_ground_flag = false;
2410-
Sector::get().add<Particles>(
2411-
m_col.m_bbox.p2(),
2412-
50, 70, 260, 280, Vector(0, 300), 3,
2413-
Color(.4f, .4f, .4f), 3, .8f, LAYER_OBJECTS+1);
2414-
Sector::get().add<Particles>(
2415-
Vector(m_col.m_bbox.get_left(), m_col.m_bbox.get_bottom()),
2416-
-70, -50, 260, 280, Vector(0, 300), 3,
2417-
Color(.4f, .4f, .4f), 3, .8f, LAYER_OBJECTS+1);
2418-
Sector::get().get_camera().shake(.1f, 0.f, 10.f);
2419-
}
2401+
if (badguy->is_frozen() && badguy->get_physic().get_velocity_y() != 0) {
2402+
handle_collision_logic(hit);
24202403
}
24212404
}
24222405

src/object/player.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ class Player final : public MovingObject
493493
*/
494494
void ungrab_object(GameObject* gameobject = nullptr);
495495

496+
/** Method for shared collision logic */
497+
void handle_collision_logic(const CollisionHit& hit);
498+
496499
void next_target();
497500
void prev_target();
498501

0 commit comments

Comments
 (0)