52 lines
1.8 KiB
C++
52 lines
1.8 KiB
C++
#ifndef PKMNLIB_NATURELIBRARY_HPP
|
|
#define PKMNLIB_NATURELIBRARY_HPP
|
|
|
|
#include <Arbutils/Collections/StringViewDictionary.hpp>
|
|
#include <Arbutils/Memory/Memory.hpp>
|
|
#include <Arbutils/Random.hpp>
|
|
#include <CreatureLib/Library/Exceptions/CreatureException.hpp>
|
|
#include "Nature.hpp"
|
|
|
|
namespace PkmnLib::Library {
|
|
class NatureLibrary {
|
|
private:
|
|
ArbUt::StringViewDictionary<std::unique_ptr<const Nature>> _items;
|
|
|
|
public:
|
|
explicit NatureLibrary(size_t size = 32) noexcept : _items(size) {}
|
|
|
|
~NatureLibrary() = default;
|
|
|
|
inline void LoadNature(const ArbUt::StringView& name, const Nature* nature) {
|
|
_items.GetStdMap().insert({name, std::unique_ptr<const Nature>(nature)});
|
|
}
|
|
|
|
inline ArbUt::BorrowedPtr<const Nature> GetNatureByName(const ArbUt::StringView& name) const {
|
|
return _items.Get(name);
|
|
}
|
|
|
|
inline ArbUt::BorrowedPtr<const Nature> GetNatureByHash(u32 hash) const { return _items.GetFromHash(hash); }
|
|
|
|
inline const ArbUt::StringView& GetRandomNatureName(ArbUt::Random rand = ArbUt::Random()) const {
|
|
auto i = rand.Get(_items.Count());
|
|
return std::next(std::begin(_items), i)->first;
|
|
}
|
|
|
|
inline const ArbUt::StringView& GetNatureName(ArbUt::BorrowedPtr<const Nature> nature) {
|
|
for (const auto& v : _items) {
|
|
if (v.second.get() == nature.GetRaw()) {
|
|
return v.first;
|
|
}
|
|
}
|
|
throw ArbUt::Exception("Nature not found.");
|
|
}
|
|
|
|
size_t GetNatureCount() const noexcept { return _items.Count(); }
|
|
inline const ArbUt::StringView& GetNatureFromIndex(size_t index) const {
|
|
return std::next(std::begin(_items), index)->first;
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif // PKMNLIB_NATURELIBRARY_HPP
|