Skip to content

Commit

Permalink
Add more convenience functions to Player class
Browse files Browse the repository at this point in the history
This reduces some code complexity in if statements
  • Loading branch information
tobbi committed Aug 7, 2024
1 parent 36837d0 commit f42d370
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/badguy/dispenser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Dispenser::active_update(float dt_sec)
auto player = get_nearest_player();
if (player)
{
if(player->is_dying() || player->is_dead())
if(!player->is_alive())
{
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/object/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ Camera::update_scroll_normal_multiplayer(float dt_sec)

for (const auto* p : Sector::get().get_players())
{
if (p->is_dead() || p->is_dying())
if (!p->is_alive())
continue;

float lft = p->get_bbox().get_left() - HORIZONTAL_MARGIN;
Expand Down Expand Up @@ -636,7 +636,7 @@ Camera::update_scroll_normal_multiplayer(float dt_sec)
void
Camera::update_scroll_autoscroll(float dt_sec)
{
if (!get_parent()->get_object_count<Player>([](const Player& p) { return !p.is_dead() && !p.is_dying(); }))
if (!get_parent()->get_object_count<Player>([](const Player& p) { return p.is_alive(); }))
return;

get_walker()->update(dt_sec);
Expand Down
4 changes: 2 additions & 2 deletions src/object/level_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ LevelTime::update(float dt_sec)
if (!running) return;

int players_alive = Sector::current() ? Sector::current()->get_object_count<Player>([](const Player& p) {
return !p.is_dead() && !p.is_dying() && !p.is_winning();
return p.is_active();
}) : 0;

if (!players_alive)
Expand All @@ -89,7 +89,7 @@ LevelTime::update(float dt_sec)
{
for (auto& p : Sector::get().get_players())
{
if (p->is_dead() || p->is_dying() || p->is_winning())
if (!p->is_active())
continue;

p->add_coins(-1);
Expand Down
16 changes: 8 additions & 8 deletions src/object/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,10 @@ Player::update(float dt_sec)
}

// Skip if in multiplayer respawn
if (is_dead() && m_target && Sector::get().get_object_count<Player>([this](const Player& p) { return !p.is_dead() && !p.is_dying() && !p.is_winning() && &p != this; }))
if (is_dead() && m_target && Sector::get().get_object_count<Player>([this](const Player& p) { return p.is_active() && &p != this; }))
{
auto* target = Sector::get().get_object_by_uid<Player>(*m_target);
if (!target || target->is_dying() || target->is_dead() || target->is_winning())
if (!target || !target->is_active())
{
next_target();
}
Expand Down Expand Up @@ -545,7 +545,7 @@ Player::update(float dt_sec)
set_bonus(NO_BONUS, true);
m_dead = true;

if (!Sector::get().get_object_count<Player>([](const Player& p) { return !p.is_dead() && !p.is_dying(); }))
if (!Sector::get().get_object_count<Player>([](const Player& p) { return p.is_alive(); }))
{
Sector::get().stop_looping_sounds();
}
Expand Down Expand Up @@ -1992,7 +1992,7 @@ Player::draw(DrawingContext& context)
if(Editor::is_active())
return;

if (is_dead() && m_target && Sector::get().get_object_count<Player>([this](const Player& p){ return !p.is_dead() && !p.is_dying() && !p.is_winning() && &p != this; }))
if (is_dead() && m_target && Sector::get().get_object_count<Player>([this](const Player& p){ return p.is_active() && &p != this; }))
{
auto* target = Sector::get().get_object_by_uid<Player>(*m_target);
if (target)
Expand Down Expand Up @@ -2463,7 +2463,7 @@ Player::kill(bool completely)
m_dying_timer.start(3.0);
set_group(COLGROUP_DISABLED);

auto alive_players = Sector::get().get_object_count<Player>([](const Player& p){ return !p.is_dead() && !p.is_dying(); });
auto alive_players = Sector::get().get_object_count<Player>([](const Player& p){ return p.is_alive(); });

if (!alive_players)
{
Expand Down Expand Up @@ -2539,7 +2539,7 @@ Player::check_bounds()
}

// If Tux is swimming, don't allow him to go below the sector
if (m_swimming && !m_ghost_mode && !is_dying() && !is_dead()
if (m_swimming && !m_ghost_mode && is_alive()
&& m_col.m_bbox.get_bottom() > Sector::get().get_height()) {
m_col.set_pos(Vector(m_col.m_bbox.get_left(),
Sector::get().get_height() - m_col.m_bbox.get_height()));
Expand Down Expand Up @@ -2910,7 +2910,7 @@ Player::next_target()
bool is_next = false;
for (auto* player : players)
{
if (!player->is_dead() && !player->is_dying() && !player->is_winning())
if (player->is_active())
{
if (!first)
{
Expand Down Expand Up @@ -2950,7 +2950,7 @@ Player::prev_target()
Player* last = nullptr;
for (auto* player : players)
{
if (!player->is_dead() && !player->is_dying() && !player->is_winning())
if (player->is_active())
{
if (m_target && player->get_uid() == *m_target && last)
{
Expand Down
12 changes: 12 additions & 0 deletions src/object/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ class Player final : public MovingObject
bool is_invincible() const { return m_invincible_timer.started(); }
bool is_dying() const { return m_dying; }

/**
* Returns true if the player is currently alive
* (not dying or dead)
*/
bool is_alive() const { return !is_dying() && !is_dead(); }

/**
* Returns true if the player can be controlled
* by the player (alive and not currently in a win sequence)
*/
bool is_active() const { return is_alive() && !is_winning(); }

Direction peeking_direction_x() const { return m_peekingX; }
Direction peeking_direction_y() const { return m_peekingY; }

Expand Down
8 changes: 4 additions & 4 deletions src/supertux/game_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ GameSession::on_escape_press(bool force_quick_respawn)
auto players = m_currentsector->get_players();

int alive = m_currentsector->get_object_count<Player>([](const Player& p) {
return !p.is_dead() && !p.is_dying();
return p.is_alive();
});

if ((!alive && (m_play_time > 2.0f || force_quick_respawn)) || m_end_sequence)
Expand Down Expand Up @@ -336,7 +336,7 @@ GameSession::get_fade_point(const Vector& position) const

for (const auto* player : m_currentsector->get_players())
{
if (!player->is_dead() && !player->is_dying())
if (player->is_alive())
{
average_position += player->get_bbox().get_middle();
alive_players++;
Expand Down Expand Up @@ -417,7 +417,7 @@ GameSession::check_end_conditions()

bool all_dead_or_winning = true;
for (const auto* p : m_currentsector->get_players())
if (!(all_dead_or_winning &= (p->is_dead() || p->is_dying() || p->is_winning())))
if (!(all_dead_or_winning &= (!p->is_active())))
break;

/* End of level? */
Expand Down Expand Up @@ -861,7 +861,7 @@ GameSession::start_sequence(Player* caller, Sequence seq, const SequenceData* da
caller->set_winning();

int remaining_players = get_current_sector().get_object_count<Player>([](const Player& p){
return !p.is_dead() && !p.is_dying() && !p.is_winning();
return p.is_active();
});

// Abort if a sequence is already playing.
Expand Down
2 changes: 1 addition & 1 deletion src/supertux/sector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ Sector::get_nearest_player (const Vector& pos) const
for (auto player_ptr : get_objects_by_type_index(typeid(Player)))
{
Player& player = *static_cast<Player*>(player_ptr);
if (player.is_dying() || player.is_dead())
if (!player.is_alive())
continue;

float dist = player.get_bbox ().distance(pos);
Expand Down

0 comments on commit f42d370

Please sign in to comment.