diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index 08b164f..3707530 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -103,8 +103,7 @@ void CreatureLib::Battling::TurnHandler::HandleAttackForTarget(CreatureLib::Batt //HOOK: Change move type hit.SetEffectiveness(library->GetTypeLibrary()->GetEffectiveness(hit.GetType(), target->GetTypes())); - //TODO: Critical calculation - hit.SetCritical(false); + hit.SetCritical(library->GetCriticalLibrary()->IsCritical(&attack, target, hitIndex)); hit.SetBasePower(dmgLibrary->GetBasePower(&attack, target, hitIndex)); hit.SetDamage(dmgLibrary->GetDamage(&attack, target, hitIndex)); diff --git a/src/Battling/Library/BattleLibrary.cpp b/src/Battling/Library/BattleLibrary.cpp index 5470b61..02f8a31 100644 --- a/src/Battling/Library/BattleLibrary.cpp +++ b/src/Battling/Library/BattleLibrary.cpp @@ -1,14 +1,18 @@ #include "BattleLibrary.hpp" CreatureLib::Battling::BattleLibrary::BattleLibrary(CreatureLib::Library::DataLibrary *staticLib, - CreatureLib::Battling::BattleStatCalculator *statCalculator) - : _staticLib(staticLib), _statCalculator(statCalculator) + CreatureLib::Battling::BattleStatCalculator *statCalculator, + DamageLibrary* damageLibrary, + CriticalLibrary* criticalLibrary) + : _staticLib(staticLib), _statCalculator(statCalculator), + _damageLibrary(damageLibrary), _criticalLibrary(criticalLibrary) {} CreatureLib::Battling::BattleLibrary::~BattleLibrary() { delete _staticLib; delete _statCalculator; delete _damageLibrary; + delete _criticalLibrary; } const CreatureLib::Library::LibrarySettings& CreatureLib::Battling::BattleLibrary::GetSettings() const { @@ -39,3 +43,7 @@ const CreatureLib::Battling::DamageLibrary *CreatureLib::Battling::BattleLibrary return _damageLibrary; } +const CreatureLib::Battling::CriticalLibrary *CreatureLib::Battling::BattleLibrary::GetCriticalLibrary() const { + return _criticalLibrary; +} + diff --git a/src/Battling/Library/BattleLibrary.hpp b/src/Battling/Library/BattleLibrary.hpp index aa3aefa..67f2833 100644 --- a/src/Battling/Library/BattleLibrary.hpp +++ b/src/Battling/Library/BattleLibrary.hpp @@ -4,14 +4,17 @@ #include "BattleStatCalculator.hpp" #include "../../Library/DataLibrary.hpp" #include "DamageLibrary.hpp" +#include "CriticalLibrary.hpp" namespace CreatureLib::Battling { class BattleLibrary { const Library::DataLibrary* _staticLib; BattleStatCalculator* _statCalculator; DamageLibrary* _damageLibrary; + CriticalLibrary* _criticalLibrary; public: - BattleLibrary(Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator); + BattleLibrary(Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator, DamageLibrary* damageLibrary, + CriticalLibrary* criticalLibrary); ~BattleLibrary(); const Library::LibrarySettings& GetSettings() const; @@ -22,6 +25,7 @@ namespace CreatureLib::Battling { const BattleStatCalculator* GetStatCalculator() const; const DamageLibrary* GetDamageLibrary() const; + const CriticalLibrary* GetCriticalLibrary() const; }; } diff --git a/src/Battling/Library/CriticalLibrary.cpp b/src/Battling/Library/CriticalLibrary.cpp new file mode 100644 index 0000000..eb82f13 --- /dev/null +++ b/src/Battling/Library/CriticalLibrary.cpp @@ -0,0 +1,9 @@ +#include "CriticalLibrary.hpp" +#include "../Models/Battle.hpp" + +bool CreatureLib::Battling::CriticalLibrary::IsCritical(CreatureLib::Battling::ExecutingAttack *attack, + CreatureLib::Battling::Creature *target, uint8_t hit) const { + auto rand = target->GetBattle()->GetRandom(); + //HOOK: Increase chance for critical hits. + return rand.Get(10) <= 0; +} diff --git a/src/Battling/Library/CriticalLibrary.hpp b/src/Battling/Library/CriticalLibrary.hpp new file mode 100644 index 0000000..63406b1 --- /dev/null +++ b/src/Battling/Library/CriticalLibrary.hpp @@ -0,0 +1,14 @@ +#ifndef CREATURELIB_CRITICALLIBRARY_HPP +#define CREATURELIB_CRITICALLIBRARY_HPP + +#include "../Models/ExecutingAttack.hpp" + +namespace CreatureLib::Battling { + class CriticalLibrary { + public: + virtual bool IsCritical(ExecutingAttack* attack, Creature* target, uint8_t hit) const; + }; +} + + +#endif //CREATURELIB_CRITICALLIBRARY_HPP diff --git a/tests/TestLibrary/TestLibrary.cpp b/tests/TestLibrary/TestLibrary.cpp index 7443cf2..9e9c4f8 100644 --- a/tests/TestLibrary/TestLibrary.cpp +++ b/tests/TestLibrary/TestLibrary.cpp @@ -52,7 +52,8 @@ static TypeLibrary* BuildTypeLibrary(){ static BattleLibrary* BuildLibrary(){ auto l = new DataLibrary(LibrarySettings(100, 4), BuildSpeciesLibrary(), BuildAttackLibrary(), BuildItemLibrary(), BuildGrowthRateLibrary(), BuildTypeLibrary()); - auto battleLib = new BattleLibrary(l, new BattleStatCalculator()); + auto battleLib = new BattleLibrary(l, new BattleStatCalculator(), new DamageLibrary(), + new CriticalLibrary()); return battleLib; }