CreatureLib/src/Battling/Models/CreateCreature.cpp

69 lines
2.4 KiB
C++
Raw Normal View History

2019-10-06 11:50:52 +00:00
#include "CreateCreature.hpp"
using namespace CreatureLib::Battling;
2019-10-06 11:50:52 +00:00
2020-06-26 15:08:23 +00:00
CreateCreature CreateCreature::WithVariant(const ArbUt::StringView& variant) {
this->_variant = variant;
2020-04-25 08:41:15 +00:00
return *this;
2019-10-06 11:50:52 +00:00
}
2020-04-25 08:41:15 +00:00
CreateCreature CreateCreature::WithNickname(std::string nickname) {
2019-10-06 11:50:52 +00:00
this->_nickname = std::move(nickname);
2020-04-25 08:41:15 +00:00
return *this;
2019-10-06 11:50:52 +00:00
}
2020-04-25 08:41:15 +00:00
CreateCreature CreateCreature::WithGender(Library::Gender gender) {
2019-10-06 11:50:52 +00:00
this->_gender = gender;
2020-04-25 08:41:15 +00:00
return *this;
2019-10-06 11:50:52 +00:00
}
2020-06-26 15:08:23 +00:00
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.");
2020-07-28 17:37:03 +00:00
}
auto attackData = _library->GetAttackLibrary()->Get(attackName.GetHash());
_attacks.Append(std::tuple(attackData, learnMethod));
2020-04-25 08:41:15 +00:00
return *this;
}
Creature* CreateCreature::Create() {
2020-05-26 16:31:06 +00:00
auto rand = ArbUt::Random();
auto species = this->_library->GetSpeciesLibrary()->Get(this->_species.GetHash());
2019-10-06 11:50:52 +00:00
auto variant = species->GetVariant(this->_variant);
Library::TalentIndex talent;
if (this->_talent.IsEmpty()) {
2020-07-12 13:26:00 +00:00
talent = variant->GetRandomTalent(rand);
} else {
2019-10-06 11:50:52 +00:00
talent = variant->GetTalentIndex(this->_talent);
}
auto identifier = this->_identifier;
if (identifier == 0) {
2022-03-25 18:21:02 +00:00
identifier = rand.GetUnsigned();
2019-10-06 11:50:52 +00:00
}
auto gender = this->_gender;
if (gender == static_cast<Library::Gender>(-1)) {
2019-10-06 11:50:52 +00:00
gender = species->GetRandomGender(rand);
}
ArbUt::OptionalBorrowedPtr<const Library::Item> heldItem;
if (!this->_heldItem.IsEmpty()) {
auto val = _library->GetItemLibrary()->TryGet(this->_heldItem.GetHash());
if (!val.has_value()) {
2021-11-21 11:39:07 +00:00
THROW("Invalid held item '", this->_heldItem.c_str(), "'.");
}
heldItem = val.value();
2019-10-06 11:50:52 +00:00
}
auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level);
auto attacks = std::vector<LearnedAttack*>(_attacks.Count());
2020-04-25 08:41:15 +00:00
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;
2019-10-06 11:50:52 +00:00
}