Skip to content

Commit

Permalink
Move ladder collision into its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmMoltony committed Feb 8, 2024
1 parent 5092894 commit 7889aec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
6 changes: 6 additions & 0 deletions include/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,12 @@ class Player
*/
UpdateResult updateGameplay(s16 oldX, s16 oldY, Block::List *blocks, EntityList *entities, BlockParticleList *blockParticles, Camera *camera);

/**
* @brief Update ladder collision
* @param collideLadder This will be set to true or false depending on if colliding with ladder or not
*/
void updateLadderCollision(bool &collideLadder);

/**
* @brief Update ladder climbing
*/
Expand Down
40 changes: 21 additions & 19 deletions source/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2593,26 +2593,9 @@ Player::UpdateResult Player::updateGameplay(s16 oldX, s16 oldY, Block::List *blo
statsSetEntry(STATS_KEY_BLOCKS_BROKEN, statsGetEntry(STATS_KEY_BLOCKS_BROKEN) + 1);
}

bool collideLadder = false;

// check if collide with ladder
for (const auto &block : *blocks)
{
// optimizaciones
if (block->getRect().x - camera->x < -16 ||
block->getRect().y - camera->y < -16 ||
block->id() != BID_LADDER)
continue;
if (block->getRect().x - camera->x > SCREEN_WIDTH + 48)
break;

if (Rect(x, y, 12, 32).intersects(block->getRect()))
{
collideLadder = true;
break;
}
}
bool collideLadder = false;

updateLadderCollision(collideLadder);
updateLadder(oldY, collideLadder);
updateFacing(camera);
updateControls(collideLadder);
Expand All @@ -2630,6 +2613,25 @@ Player::UpdateResult Player::updateGameplay(s16 oldX, s16 oldY, Block::List *blo
return ret;
}

void Player::updateLadderCollision(bool &collideLadder)
{
for (const auto &block : *blocks)
{
if (block->getRect().x - camera->x < -16 ||
block->getRect().y - camera->y < -16 ||
block->id() != BID_LADDER)
continue;
if (block->getRect().x - camera->x > SCREEN_WIDTH + 48)
break;

if (Rect(x, y, 12, 32).intersects(block->getRect()))
{
collideLadder = true;
break;
}
}
}

void Player::updateLadder(s16 oldY, bool collideLadder)
{
if (collideLadder)
Expand Down

0 comments on commit 7889aec

Please sign in to comment.