CreatureLib/src/Battling/Models/CreateCreature.cpp

69 lines
2.3 KiB
C++

#include "CreateCreature.hpp"
using namespace CreatureLib::Battling;
CreateCreature CreateCreature::WithVariant(const ArbUt::StringView& variant) {
this->_variant = variant;
return *this;
}
CreateCreature CreateCreature::WithNickname(std::string nickname) {
this->_nickname = std::move(nickname);
return *this;
}
CreateCreature CreateCreature::WithGender(Library::Gender gender) {
this->_gender = gender;
return *this;
}
CreateCreature CreateCreature::WithAttack(const ArbUt::StringView& attackName, AttackLearnMethod learnMethod) {
if (_attacks.Count() >= _library->GetSettings()->GetMaximalAttacks()) {
THROW("You have already set the maximum amount of allowed moves.");
}
auto attackData = _library->GetAttackLibrary()->Get(attackName);
_attacks.Append(std::tuple(attackData, learnMethod));
return *this;
}
Creature* CreateCreature::Create() {
auto rand = ArbUt::Random();
auto species = this->_library->GetSpeciesLibrary()->Get(this->_species);
auto variant = species->GetVariant(this->_variant);
Library::TalentIndex talent;
if (this->_talent.IsEmpty()) {
talent = variant->GetRandomTalent(rand);
} else {
talent = variant->GetTalentIndex(this->_talent);
}
auto identifier = this->_identifier;
if (identifier == 0) {
identifier = rand.GetUnsigned();
}
auto gender = this->_gender;
if (gender == static_cast<Library::Gender>(-1)) {
gender = species->GetRandomGender(rand);
}
ArbUt::OptionalBorrowedPtr<const Library::Item> heldItem;
if (!this->_heldItem.IsEmpty()) {
auto val = _library->GetItemLibrary()->TryGet(this->_heldItem);
if (!val.has_value()) {
THROW("Invalid held item '", this->_heldItem.c_str(), "'.");
}
heldItem = val.value();
}
auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level);
auto attacks = std::vector<LearnedAttack*>(_attacks.Count());
for (size_t i = 0; i < _attacks.Count(); i++) {
auto kv = _attacks[i];
attacks[i] = new LearnedAttack(std::get<0>(kv), std::get<1>(kv));
}
auto c = new Creature(_library, species, variant, _level, experience, identifier, gender, _coloring, heldItem,
_nickname, talent, attacks);
c->Initialize();
return c;
}