PkmnLib/src/Library/Natures/NatureLibrary.hpp

52 lines
1.8 KiB
C++
Raw Normal View History

#ifndef PKMNLIB_NATURELIBRARY_HPP
#define PKMNLIB_NATURELIBRARY_HPP
2022-05-16 16:16:15 +00:00
#include <Arbutils/Collections/StringViewDictionary.hpp>
#include <Arbutils/Memory/Memory.hpp>
#include <Arbutils/Random.hpp>
2020-04-17 16:20:48 +00:00
#include <CreatureLib/Library/Exceptions/CreatureException.hpp>
#include "Nature.hpp"
namespace PkmnLib::Library {
class NatureLibrary {
private:
2022-05-16 16:16:15 +00:00
ArbUt::StringViewDictionary<std::unique_ptr<const Nature>> _items;
public:
explicit NatureLibrary(size_t size = 32) noexcept : _items(size) {}
~NatureLibrary() = default;
2020-04-17 16:50:26 +00:00
2020-07-04 13:50:30 +00:00
inline void LoadNature(const ArbUt::StringView& name, const Nature* nature) {
2022-05-16 16:16:15 +00:00
_items.GetStdMap().insert({name, std::unique_ptr<const Nature>(nature)});
}
2020-07-04 13:50:30 +00:00
inline ArbUt::BorrowedPtr<const Nature> GetNatureByName(const ArbUt::StringView& name) const {
2022-05-16 16:16:15 +00:00
return _items.Get(name);
}
2022-06-07 21:23:40 +00:00
inline ArbUt::BorrowedPtr<const Nature> GetNatureByHash(u32 hash) const { return _items.GetFromHash(hash); }
2020-01-02 19:26:01 +00:00
2020-07-04 13:50:30 +00:00
inline const ArbUt::StringView& GetRandomNatureName(ArbUt::Random rand = ArbUt::Random()) const {
2022-05-16 16:16:15 +00:00
auto i = rand.Get(_items.Count());
return std::next(std::begin(_items), i)->first;
2020-01-02 19:26:01 +00:00
}
2022-05-16 16:16:15 +00:00
inline const ArbUt::StringView& GetNatureName(ArbUt::BorrowedPtr<const Nature> nature) {
2020-04-17 18:03:01 +00:00
for (const auto& v : _items) {
if (v.second.get() == nature.GetRaw()) {
return v.first;
}
}
2020-08-16 09:12:04 +00:00
throw ArbUt::Exception("Nature not found.");
}
2022-05-16 16:16:15 +00:00
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