Make LearnableAttacks follow PIMPL idiom.
Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
parent
15deae1504
commit
4006d63872
|
@ -1,13 +1,68 @@
|
||||||
#include "LearnableAttacks.hpp"
|
#include "LearnableAttacks.hpp"
|
||||||
|
|
||||||
using namespace CreatureLib::Library;
|
namespace CreatureLib::Library {
|
||||||
|
struct LearnableAttacks::impl {
|
||||||
|
private:
|
||||||
|
ArbUt::Dictionary<uint8_t, ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>> _learnedByLevel;
|
||||||
|
ArbUt::List<ArbUt::BorrowedPtr<const AttackData>> _distinctLevelAttacks;
|
||||||
|
|
||||||
void LearnableAttacks::AddLevelAttack(level_int_t level, ArbUt::BorrowedPtr<const AttackData> attack) {
|
public:
|
||||||
if (_learnedByLevel.Has(level)) {
|
explicit impl(size_t levelAttackCapacity)
|
||||||
_learnedByLevel[level].Append(attack);
|
: _learnedByLevel(
|
||||||
} else {
|
ArbUt::Dictionary<uint8_t, ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>>(levelAttackCapacity)) {}
|
||||||
_learnedByLevel.Insert(level, {attack});
|
|
||||||
|
~impl() = default;
|
||||||
|
|
||||||
|
void AddLevelAttack(level_int_t level, const ArbUt::BorrowedPtr<const AttackData>& attack) {
|
||||||
|
if (_learnedByLevel.Has(level)) {
|
||||||
|
_learnedByLevel[level].Append(attack);
|
||||||
|
} else {
|
||||||
|
_learnedByLevel.Insert(level, {attack});
|
||||||
|
}
|
||||||
|
if (!_distinctLevelAttacks.Contains(attack)) {
|
||||||
|
_distinctLevelAttacks.Append(attack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool HasAttacksForLevel(uint8_t level) const noexcept { return _learnedByLevel.Has(level); }
|
||||||
|
inline const ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>& GetAttacksForLevel(uint8_t level) const {
|
||||||
|
if (!_learnedByLevel.Has(level)) {
|
||||||
|
THROW("No attacks found for level " << (uint32_t)level << ".");
|
||||||
|
}
|
||||||
|
return _learnedByLevel.Get(level);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>& GetDistinctLevelAttacks() const noexcept {
|
||||||
|
return _distinctLevelAttacks;
|
||||||
|
}
|
||||||
|
|
||||||
|
ArbUt::BorrowedPtr<const AttackData> GetRandomAttack(ArbUt::Random& rand) const {
|
||||||
|
if (_distinctLevelAttacks.Count() == 0) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
auto val = rand.Get(_distinctLevelAttacks.Count());
|
||||||
|
auto it = _distinctLevelAttacks.begin();
|
||||||
|
std::advance(it, val);
|
||||||
|
return *it;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
LearnableAttacks::LearnableAttacks(size_t levelAttackCapacity) : _impl(new impl(levelAttackCapacity)) {}
|
||||||
|
LearnableAttacks::~LearnableAttacks() = default;
|
||||||
|
|
||||||
|
void LearnableAttacks::AddLevelAttack(level_int_t level, ArbUt::BorrowedPtr<const AttackData> attack) {
|
||||||
|
_impl->AddLevelAttack(level, attack);
|
||||||
}
|
}
|
||||||
if (!_distinctLevelAttacks.Contains(attack))
|
bool LearnableAttacks::HasAttacksForLevel(uint8_t level) const noexcept { return _impl->HasAttacksForLevel(level); }
|
||||||
_distinctLevelAttacks.Append(attack);
|
const ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>& LearnableAttacks::GetAttacksForLevel(uint8_t level) const {
|
||||||
|
return _impl->GetAttacksForLevel(level);
|
||||||
|
}
|
||||||
|
const ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>&
|
||||||
|
LearnableAttacks::GetDistinctLevelAttacks() const noexcept {
|
||||||
|
return _impl->GetDistinctLevelAttacks();
|
||||||
|
}
|
||||||
|
ArbUt::BorrowedPtr<const AttackData> LearnableAttacks::GetRandomAttack(ArbUt::Random& rand) const {
|
||||||
|
return _impl->GetRandomAttack(rand);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -2,45 +2,23 @@
|
||||||
#define CREATURELIB_LEARNABLEATTACKS_HPP
|
#define CREATURELIB_LEARNABLEATTACKS_HPP
|
||||||
|
|
||||||
#include <Arbutils/Random.hpp>
|
#include <Arbutils/Random.hpp>
|
||||||
#include "../../Defines.hpp"
|
|
||||||
#include "../Attacks/AttackData.hpp"
|
#include "../Attacks/AttackData.hpp"
|
||||||
|
|
||||||
namespace CreatureLib::Library {
|
namespace CreatureLib::Library {
|
||||||
class LearnableAttacks {
|
class LearnableAttacks {
|
||||||
protected:
|
private:
|
||||||
ArbUt::Dictionary<uint8_t, ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>> _learnedByLevel;
|
struct impl;
|
||||||
ArbUt::List<ArbUt::BorrowedPtr<const AttackData>> _distinctLevelAttacks;
|
std::unique_ptr<impl> _impl;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit LearnableAttacks(size_t levelAttackCapacity)
|
explicit LearnableAttacks(size_t levelAttackCapacity);
|
||||||
: _learnedByLevel(
|
virtual ~LearnableAttacks();
|
||||||
ArbUt::Dictionary<uint8_t, ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>>(levelAttackCapacity)) {}
|
|
||||||
|
|
||||||
virtual ~LearnableAttacks() = default;
|
|
||||||
|
|
||||||
void AddLevelAttack(level_int_t level, ArbUt::BorrowedPtr<const AttackData> attack);
|
void AddLevelAttack(level_int_t level, ArbUt::BorrowedPtr<const AttackData> attack);
|
||||||
|
bool HasAttacksForLevel(uint8_t level) const noexcept;
|
||||||
inline bool HasAttacksForLevel(uint8_t level) const noexcept { return _learnedByLevel.Has(level); }
|
const ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>& GetAttacksForLevel(uint8_t level) const;
|
||||||
inline const ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>& GetAttacksForLevel(uint8_t level) const {
|
const ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>& GetDistinctLevelAttacks() const noexcept;
|
||||||
if (!_learnedByLevel.Has(level)) {
|
virtual ArbUt::BorrowedPtr<const AttackData> GetRandomAttack(ArbUt::Random& rand) const;
|
||||||
THROW("No attacks found for level " << (uint32_t)level << ".");
|
|
||||||
}
|
|
||||||
return _learnedByLevel.Get(level);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline const ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>& GetDistinctLevelAttacks() const noexcept {
|
|
||||||
return _distinctLevelAttacks;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ArbUt::BorrowedPtr<const AttackData> GetRandomAttack(ArbUt::Random& rand) const {
|
|
||||||
if (_distinctLevelAttacks.Count() == 0) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
auto val = rand.Get(_distinctLevelAttacks.Count());
|
|
||||||
auto it = _distinctLevelAttacks.begin();
|
|
||||||
std::advance(it, val);
|
|
||||||
return *it;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ namespace CreatureLib::Library {
|
||||||
LookupGrowthRate(const ArbUt::List<uint32_t>& experience) : _experience(experience) {}
|
LookupGrowthRate(const ArbUt::List<uint32_t>& experience) : _experience(experience) {}
|
||||||
|
|
||||||
level_int_t CalculateLevel(uint32_t experience) const override {
|
level_int_t CalculateLevel(uint32_t experience) const override {
|
||||||
for (level_int_t i = 0; i < _experience.Count(); i++) {
|
for (level_int_t i = 0; i < (level_int_t)_experience.Count(); i++) {
|
||||||
if (_experience[i] > experience) {
|
if (_experience[i] > experience) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,4 +30,7 @@
|
||||||
#include <Arbutils/Memory/UniquePtrList.hpp>
|
#include <Arbutils/Memory/UniquePtrList.hpp>
|
||||||
#include <Arbutils/StringView.hpp>
|
#include <Arbutils/StringView.hpp>
|
||||||
|
|
||||||
|
// CreatureLib
|
||||||
|
#include "Defines.hpp"
|
||||||
|
|
||||||
#endif // CREATURELIB_PRECOMPILED_HXX
|
#endif // CREATURELIB_PRECOMPILED_HXX
|
||||||
|
|
Loading…
Reference in New Issue