From c8314d60186413db723d6405f3abc7d1ff37a8b4 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Wed, 10 Jun 2020 14:39:20 +0200 Subject: [PATCH] Support for changing variants. --- CInterface/Battling/Creature.cpp | 5 +++++ src/Battling/Models/Creature.cpp | 29 ++++++++++++++++++++++++++++- src/Battling/Models/Creature.hpp | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CInterface/Battling/Creature.cpp b/CInterface/Battling/Creature.cpp index da97070..180272b 100644 --- a/CInterface/Battling/Creature.cpp +++ b/CInterface/Battling/Creature.cpp @@ -25,6 +25,11 @@ export void CreatureLib_Creature_Destruct(const Creature* p) { delete p; } SIMPLE_GET_FUNC_SMART_PTR(Creature, GetSpecies, const CreatureLib::Library::CreatureSpecies*); SIMPLE_GET_FUNC_SMART_PTR(Creature, GetVariant, const CreatureLib::Library::SpeciesVariant*); + +export uint8_t CreatureLib_Creature_ChangeVariant(Creature* p, const CreatureLib::Library::SpeciesVariant* variant) { + Try(p->ChangeVariant(variant);) +} + SIMPLE_GET_FUNC(Creature, GetLevel, uint8_t); SIMPLE_GET_FUNC(Creature, GetExperience, uint32_t); SIMPLE_GET_FUNC(Creature, GetGender, CreatureLib::Library::Gender); diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index f7e219a..9764d3e 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -30,6 +30,33 @@ Battling::Creature::Creature(ArbUt::BorrowedPtr library, } } +void Battling::Creature::ChangeVariant(ArbUt::BorrowedPtr variant) { + AssertNotNull(variant) + _variant = variant; + + // Set the types to the new variant. + _types.clear(); + for (auto t : variant->GetTypes()) { + _types.insert(t); + } + + // Grab the new active talent. + _activeTalent = + std::unique_ptr(_library->LoadScript(ScriptCategory::Talent, GetActiveTalent())); + + // We modify the health of the creature by the change in its max health. + auto prevHealth = GetBoostedStat(CreatureLib::Library::Statistic::Health); + RecalculateFlatStats(); + int32_t diffHealth = GetBoostedStat(CreatureLib::Library::Statistic::Health) - prevHealth; + if (_currentHealth < -diffHealth) { + _currentHealth = 0; + } else { + _currentHealth += diffHealth; + } + + // TODO: consider variant specific attacks? +} + void Battling::Creature::ChangeLevelBy(int8_t amount) { auto level = _level + amount; if (level > _library->GetSettings()->GetMaximalLevel()) @@ -230,4 +257,4 @@ void Battling::Creature::AddVolatileScript(const ConstString& name) { void Battling::Creature::AddVolatileScript(Script* script) { _volatile.Add(script); } void Battling::Creature::RemoveVolatileScript(const ConstString& name) { _volatile.Remove(name); } void Battling::Creature::RemoveVolatileScript(Battling::Script* script) { _volatile.Remove(script->GetName()); } -bool Battling::Creature::HasVolatileScript(const ConstString& name) const { return _volatile.Has(name); } +bool Battling::Creature::HasVolatileScript(const ConstString& name) const { return _volatile.Has(name); } \ No newline at end of file diff --git a/src/Battling/Models/Creature.hpp b/src/Battling/Models/Creature.hpp index 3f106d4..1e64b19 100644 --- a/src/Battling/Models/Creature.hpp +++ b/src/Battling/Models/Creature.hpp @@ -84,6 +84,7 @@ namespace CreatureLib::Battling { return _species; } inline const ArbUt::BorrowedPtr& GetVariant() const noexcept { return _variant; } + virtual void ChangeVariant(ArbUt::BorrowedPtr variant); inline uint8_t GetLevel() const noexcept { return _level; } inline uint32_t GetExperience() const noexcept { return _experience; } inline Library::Gender GetGender() const noexcept { return _gender; }