Improve kart stats code readability

This commit is contained in:
Alayan 2019-10-21 18:46:43 +02:00
parent f55a206271
commit 17305a6cd8
3 changed files with 40 additions and 42 deletions

View File

@ -54,13 +54,9 @@ KartStatsWidget::KartStatsWidget(core::recti area, const int player_id,
// any kart names.
int id = kart_properties_manager->getKartByGroup(kart_group, 0);
if (id == -1)
{
props = kart_properties_manager->getKartById(0);
}
else
{
props = kart_properties_manager->getKartById(id);
}
if(!props)
Log::fatal("KartSelectionScreen", "Can't find default "
@ -104,47 +100,46 @@ void KartStatsWidget::setValues(const KartProperties* props,
// So values should be in the 0-99 range
// The base mass is of 350 ; 350/3.89 ~= 90
m_skills[SKILL_MASS]->setValue(
kp_computed.getCombinedCharacteristic()->getMass()/3.89f);
m_skills[SKILL_MASS]->setIcon(irr::core::stringc(
file_manager->getAsset(FileManager::GUI_ICON, "mass.png").c_str()));
m_skills[SKILL_MASS]->m_properties[PROP_ID] = StringUtils::insertValues("@p%i_mass", m_player_id);
m_skills[SKILL_MASS]->m_iconbutton->setTooltip( _("Mass") );
setSkillValues(SKILL_MASS,
kp_computed.getCombinedCharacteristic()->getMass()/3.89f,
"mass.png", "mass", _("Mass"));
// The base speed is of 25
// Here we are not fully proportional, because small differences matter more
m_skills[SKILL_SPEED]->setValue(
(kp_computed.getCombinedCharacteristic()->getEngineMaxSpeed() - 20.0f) * 15.0f);
m_skills[SKILL_SPEED]->setIcon(irr::core::stringc(
file_manager->getAsset(FileManager::GUI_ICON, "speed.png").c_str()));
m_skills[SKILL_SPEED]->m_properties[PROP_ID] = StringUtils::insertValues("@p%i_speed", m_player_id);
m_skills[SKILL_SPEED]->m_iconbutton->setTooltip( _("Maximum speed") );
setSkillValues(SKILL_SPEED,
(kp_computed.getCombinedCharacteristic()->getEngineMaxSpeed() - 20.0f) * 15.0f,
"speed.png", "speed", _("Maximum speed"));
// The acceleration depend on power and mass, and it changes depending on speed
// We call a function which gives us a single number to represent it
// power/mass gives numbers in the 1-10 range, so we multiply it by 10.
m_skills[SKILL_ACCELERATION]->setValue(kp_computed.getAccelerationEfficiency()*10.0f);
m_skills[SKILL_ACCELERATION]->setIcon(irr::core::stringc(
file_manager->getAsset(FileManager::GUI_ICON, "power.png").c_str()));
m_skills[SKILL_ACCELERATION]->m_properties[PROP_ID] =
StringUtils::insertValues("@p%i_acceleration", m_player_id);
m_skills[SKILL_ACCELERATION]->m_iconbutton->setTooltip( _("Acceleration") );
setSkillValues(SKILL_ACCELERATION,
kp_computed.getAccelerationEfficiency()*10.0f,
"power.png", "acceleration", _("Acceleration"));
// The base nitro consumption is 1, higher for heavier karts.
// Nitro efficiency is hence 90/nitro_consumption
m_skills[SKILL_NITRO_EFFICIENCY]->setValue(
90.0f/kp_computed.getCombinedCharacteristic()->getNitroConsumption());
m_skills[SKILL_NITRO_EFFICIENCY]->setIcon(irr::core::stringc(
file_manager->getAsset(FileManager::GUI_ICON, "nitro.png").c_str()));
m_skills[SKILL_NITRO_EFFICIENCY]->m_properties[PROP_ID] =
StringUtils::insertValues("@p%i_nitro_efficiency", m_player_id);
m_skills[SKILL_NITRO_EFFICIENCY]->m_iconbutton->setTooltip( _("Nitro efficiency") );
setSkillValues(SKILL_NITRO_EFFICIENCY,
90.0f/kp_computed.getCombinedCharacteristic()->getNitroConsumption(),
"nitro.png", "nitro", _("Nitro efficiency"));
race_manager->setDifficulty(previous_difficulty);
} // setValues
// -----------------------------------------------------------------------------
void KartStatsWidget::setSkillValues(Stats skill_type, float value, const std::string icon_name,
const std::string skillbar_propID, const irr::core::stringw icon_tooltip)
{
m_skills[skill_type]->setValue(value);
m_skills[skill_type]->setIcon(irr::core::stringc(
file_manager->getAsset(FileManager::GUI_ICON, icon_name).c_str()));
m_skills[skill_type]->m_properties[PROP_ID] = StringUtils::insertValues("@p%i_"+skillbar_propID, m_player_id);
m_skills[skill_type]->m_iconbutton->setTooltip(icon_tooltip);
} // setSkillValues
// -----------------------------------------------------------------------------
void KartStatsWidget::hideAll()
{

View File

@ -41,6 +41,18 @@ namespace GUIEngine
*/
class KartStatsWidget : public Widget
{
public:
enum Stats
{
SKILL_MASS,
SKILL_SPEED,
SKILL_ACCELERATION,
SKILL_NITRO_EFFICIENCY,
SKILL_COUNT
};
private:
/** When inferring widget size from its label length, this method will be called to
* if/how much space must be added to the raw label's size for the widget to be large enough */
virtual int getWidthNeededAroundLabel() const { return 35; }
@ -58,18 +70,12 @@ namespace GUIEngine
std::vector<SkillLevelWidget*> m_skills;
void setSkillValues(Stats skill_type, float value, const std::string icon_name,
const std::string skillbar_propID, const irr::core::stringw icon_tooltip);
public:
enum Stats
{
SKILL_MASS,
SKILL_SPEED,
SKILL_ACCELERATION,
SKILL_NITRO_EFFICIENCY,
SKILL_COUNT
};
LEAK_CHECK()
KartStatsWidget(core::recti area, const int player_id,

View File

@ -51,10 +51,7 @@ PlayerKartWidget::PlayerKartWidget(KartSelectionScreen* parent,
m_parent_screen = parent;
m_associated_player = associated_player;
x_speed = 1.0f;
y_speed = 1.0f;
w_speed = 1.0f;
h_speed = 1.0f;
x_speed = y_speed = w_speed = h_speed = 1.0f;
m_ready = false;
m_difficulty = PLAYER_DIFFICULTY_NORMAL;
m_not_updated_yet = true;