Make variant keys case insensitive, added helper functions for variants.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-02-13 17:16:07 +01:00
parent 611198009b
commit 2ea5f9f9bd
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 35 additions and 8 deletions

View File

@ -7,7 +7,38 @@ CreatureSpecies::CreatureSpecies(uint16_t id, std::string name, const SpeciesVar
: _id(id), _genderRate(genderRatio), _growthRate(std::move(growthRate)), _captureRate(captureRate), : _id(id), _genderRate(genderRatio), _growthRate(std::move(growthRate)), _captureRate(captureRate),
_variants({{"default", defaultVariant}}), _name(std::move(name)) {} _variants({{"default", defaultVariant}}), _name(std::move(name)) {}
const SpeciesVariant* CreatureSpecies::GetVariant(const std::string& key) const { return _variants.at(key); } bool CreatureSpecies::HasVariant(const std::string& name) const {
auto key = name;
std::transform(key.begin(), key.end(), key.end(), ::tolower);
return _variants.find(key) != _variants.end();
}
bool CreatureSpecies::TryGetVariant(const std::string& name, const SpeciesVariant*& out) {
auto key = name;
std::transform(key.begin(), key.end(), key.end(), ::tolower);
auto find = _variants.find(key);
if (find != _variants.end()) {
out = find->second;
return true;
}
return false;
}
const SpeciesVariant* CreatureSpecies::GetVariant(const std::string& name) const {
auto key = name;
std::transform(key.begin(), key.end(), key.end(), ::tolower);
return _variants.at(key);
}
void CreatureSpecies::SetVariant(const std::string& name, const SpeciesVariant* variant) {
auto key = name;
std::transform(key.begin(), key.end(), key.end(), ::tolower);
auto find = _variants.find(key);
if (find != _variants.end()) {
delete find->second;
}
_variants[key] = variant;
}
Gender CreatureSpecies::GetRandomGender(CreatureLib::Core::Random& rand) const { Gender CreatureSpecies::GetRandomGender(CreatureLib::Core::Random& rand) const {
// TODO: Genderless creatures // TODO: Genderless creatures

View File

@ -36,17 +36,13 @@ namespace CreatureLib::Library {
inline const std::string& GetGrowthRate() const { return _growthRate; } inline const std::string& GetGrowthRate() const { return _growthRate; }
inline uint8_t GetCaptureRate() const { return _captureRate; } inline uint8_t GetCaptureRate() const { return _captureRate; }
[[nodiscard]] bool HasVariant(const std::string& key) const;
[[nodiscard]] bool TryGetVariant(const std::string& name, const SpeciesVariant*& out);
[[nodiscard]] const SpeciesVariant* GetVariant(const std::string& key) const; [[nodiscard]] const SpeciesVariant* GetVariant(const std::string& key) const;
[[nodiscard]] Gender GetRandomGender(Core::Random& rand) const; [[nodiscard]] Gender GetRandomGender(Core::Random& rand) const;
[[nodiscard]] const std::string& GetName() const; [[nodiscard]] const std::string& GetName() const;
void SetVariant(const std::string& name, const SpeciesVariant* variant) { void SetVariant(const std::string& name, const SpeciesVariant* variant);
auto find = _variants.find(name);
if (find != _variants.end()) {
delete find->second;
}
_variants[name] = variant;
}
}; };
} }