Skip to content

Commit

Permalink
Reduce code duplication in PlayerStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
tobbi committed Aug 1, 2023
1 parent d08fbc6 commit 9b8a475
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 42 deletions.
67 changes: 25 additions & 42 deletions src/supertux/player_status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,30 +213,7 @@ PlayerStatus::read(const ReaderMapping& mapping)
}

auto map = iter.as_mapping();

std::string bonusname;
if (map.get("bonus", bonusname)) {
if (bonusname == "none") {
bonus[id] = NO_BONUS;
} else if (bonusname == "growup") {
bonus[id] = GROWUP_BONUS;
} else if (bonusname == "fireflower") {
bonus[id] = FIRE_BONUS;
} else if (bonusname == "iceflower") {
bonus[id] = ICE_BONUS;
} else if (bonusname == "airflower") {
bonus[id] = AIR_BONUS;
} else if (bonusname == "earthflower") {
bonus[id] = EARTH_BONUS;
} else {
log_warning << "Unknown bonus '" << bonusname << "' in savefile for player " << (id + 1) << std::endl;
bonus[id] = NO_BONUS;
}
}
map.get("fireflowers", max_fire_bullets[id]);
map.get("iceflowers", max_ice_bullets[id]);
map.get("airflowers", max_air_time[id]);
map.get("earthflowers", max_earth_time[id]);
parse_bonus_mapping(map, id);
}
}
catch (const std::exception& e)
Expand All @@ -245,34 +222,40 @@ PlayerStatus::read(const ReaderMapping& mapping)
}
}

parse_bonus_mapping(mapping, 0);

mapping.get("coins", coins);

mapping.get("worldmap-sprite", worldmap_sprite);
mapping.get("last-worldmap", last_worldmap);
}

void
PlayerStatus::parse_bonus_mapping(const ReaderMapping& map, int id)
{
std::string bonusname;
if (mapping.get("bonus", bonusname)) {
if (map.get("bonus", bonusname)) {
if (bonusname == "none") {
bonus[0] = NO_BONUS;
bonus[id] = NO_BONUS;
} else if (bonusname == "growup") {
bonus[0] = GROWUP_BONUS;
bonus[id] = GROWUP_BONUS;
} else if (bonusname == "fireflower") {
bonus[0] = FIRE_BONUS;
bonus[id] = FIRE_BONUS;
} else if (bonusname == "iceflower") {
bonus[0] = ICE_BONUS;
bonus[id] = ICE_BONUS;
} else if (bonusname == "airflower") {
bonus[0] = AIR_BONUS;
bonus[id] = AIR_BONUS;
} else if (bonusname == "earthflower") {
bonus[0] = EARTH_BONUS;
bonus[id] = EARTH_BONUS;
} else {
log_warning << "Unknown bonus '" << bonusname << "' in savefile" << std::endl;
bonus[0] = NO_BONUS;
log_warning << "Unknown bonus '" << bonusname << "' in savefile for player " << (id + 1) << std::endl;
bonus[id] = NO_BONUS;
}
}
mapping.get("fireflowers", max_fire_bullets[0]);
mapping.get("iceflowers", max_ice_bullets[0]);
mapping.get("airflowers", max_air_time[0]);
mapping.get("earthflowers", max_earth_time[0]);

mapping.get("coins", coins);

mapping.get("worldmap-sprite", worldmap_sprite);
mapping.get("last-worldmap", last_worldmap);
map.get("fireflowers", max_fire_bullets[id]);
map.get("iceflowers", max_ice_bullets[id]);
map.get("airflowers", max_air_time[id]);
map.get("earthflowers", max_earth_time[id]);
}

std::string
Expand Down
3 changes: 3 additions & 0 deletions src/supertux/player_status.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class PlayerStatus final
void add_player();
void remove_player(int player_id);

private:
void parse_bonus_mapping(const ReaderMapping& map, int id);

public:
int m_num_players;

Expand Down

0 comments on commit 9b8a475

Please sign in to comment.