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"
|
||||
|
||||
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:
|
||||
explicit impl(size_t levelAttackCapacity)
|
||||
: _learnedByLevel(
|
||||
ArbUt::Dictionary<uint8_t, ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>>(levelAttackCapacity)) {}
|
||||
|
||||
~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))
|
||||
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);
|
||||
}
|
||||
bool LearnableAttacks::HasAttacksForLevel(uint8_t level) const noexcept { return _impl->HasAttacksForLevel(level); }
|
||||
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
|
||||
|
||||
#include <Arbutils/Random.hpp>
|
||||
#include "../../Defines.hpp"
|
||||
#include "../Attacks/AttackData.hpp"
|
||||
|
||||
namespace CreatureLib::Library {
|
||||
class LearnableAttacks {
|
||||
protected:
|
||||
ArbUt::Dictionary<uint8_t, ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>> _learnedByLevel;
|
||||
ArbUt::List<ArbUt::BorrowedPtr<const AttackData>> _distinctLevelAttacks;
|
||||
private:
|
||||
struct impl;
|
||||
std::unique_ptr<impl> _impl;
|
||||
|
||||
public:
|
||||
explicit LearnableAttacks(size_t levelAttackCapacity)
|
||||
: _learnedByLevel(
|
||||
ArbUt::Dictionary<uint8_t, ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>>(levelAttackCapacity)) {}
|
||||
|
||||
virtual ~LearnableAttacks() = default;
|
||||
explicit LearnableAttacks(size_t levelAttackCapacity);
|
||||
virtual ~LearnableAttacks();
|
||||
|
||||
void AddLevelAttack(level_int_t level, ArbUt::BorrowedPtr<const AttackData> 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
bool HasAttacksForLevel(uint8_t level) const noexcept;
|
||||
const ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>& GetAttacksForLevel(uint8_t level) const;
|
||||
const ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>& GetDistinctLevelAttacks() const noexcept;
|
||||
virtual ArbUt::BorrowedPtr<const AttackData> GetRandomAttack(ArbUt::Random& rand) const;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace CreatureLib::Library {
|
|||
LookupGrowthRate(const ArbUt::List<uint32_t>& experience) : _experience(experience) {}
|
||||
|
||||
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) {
|
||||
return i;
|
||||
}
|
||||
|
|
|
@ -30,4 +30,7 @@
|
|||
#include <Arbutils/Memory/UniquePtrList.hpp>
|
||||
#include <Arbutils/StringView.hpp>
|
||||
|
||||
// CreatureLib
|
||||
#include "Defines.hpp"
|
||||
|
||||
#endif // CREATURELIB_PRECOMPILED_HXX
|
||||
|
|
Loading…
Reference in New Issue