Reworks nature library to be simpler.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,57 +1,36 @@
|
||||
#ifndef PKMNLIB_NATURELIBRARY_HPP
|
||||
#define PKMNLIB_NATURELIBRARY_HPP
|
||||
|
||||
#include <CreatureLib/Library/Exceptions/CreatureException.hpp>
|
||||
#include <Arbutils/Collections/Dictionary.hpp>
|
||||
#include <Arbutils/Collections/List.hpp>
|
||||
#include <Arbutils/ConstString.hpp>
|
||||
#include <Arbutils/Random.hpp>
|
||||
#include <CreatureLib/Library/Exceptions/CreatureException.hpp>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include "Nature.hpp"
|
||||
namespace PkmnLib::Library {
|
||||
class NatureLibrary {
|
||||
private:
|
||||
std::unordered_map<std::string, uint8_t> _keyLookup;
|
||||
std::vector<Nature> _natures;
|
||||
uint8_t _current = 0;
|
||||
Arbutils::Collections::Dictionary<Arbutils::CaseInsensitiveConstString, Nature> _items;
|
||||
|
||||
public:
|
||||
NatureLibrary(uint8_t size = 32)
|
||||
: _keyLookup(std::unordered_map<std::string, uint8_t>(size)), _natures(std::vector<Nature>(size)) {}
|
||||
explicit NatureLibrary(uint8_t size = 32)
|
||||
: _items(Arbutils::Collections::Dictionary<Arbutils::CaseInsensitiveConstString, Nature>(size)) {}
|
||||
|
||||
uint8_t LoadNature(const std::string& name, const Nature& nature) {
|
||||
auto find = _keyLookup.find(name);
|
||||
if (find != _keyLookup.end()) {
|
||||
auto key = _keyLookup[name];
|
||||
_natures[key] = nature;
|
||||
return key;
|
||||
} else {
|
||||
auto key = _current;
|
||||
_keyLookup[name] = key;
|
||||
_natures[_current++] = nature;
|
||||
return key;
|
||||
}
|
||||
inline void LoadNature(const Arbutils::CaseInsensitiveConstString& name, const Nature& nature) {
|
||||
_items.Insert(name, nature);
|
||||
}
|
||||
|
||||
const Nature& GetNatureByName(const std::string& name) const {
|
||||
auto find = _keyLookup.find(name);
|
||||
if (find == _keyLookup.end()) {
|
||||
throw CreatureException("Invalid nature name.");
|
||||
}
|
||||
return _natures[_keyLookup.at(name)];
|
||||
inline const Nature& GetNatureByName(const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
return _items[name];
|
||||
}
|
||||
|
||||
const Nature& GetNature(uint8_t id) const { return _natures[id]; }
|
||||
const Nature* GetNaturePtr(uint8_t id) const { return &_natures[id]; }
|
||||
|
||||
uint8_t GetNatureIdByName(const std::string& name) const {
|
||||
auto find = _keyLookup.find(name);
|
||||
if (find == _keyLookup.end()) {
|
||||
throw CreatureException("Invalid nature name.");
|
||||
}
|
||||
return _keyLookup.at(name);
|
||||
}
|
||||
|
||||
uint8_t GetRandomNature(Arbutils::Random rand = Arbutils::Random()) const{
|
||||
return rand.Get(_current);
|
||||
inline const Arbutils::CaseInsensitiveConstString&
|
||||
GetRandomNatureName(Arbutils::Random rand = Arbutils::Random()) const {
|
||||
auto i = rand.Get(_items.Count());
|
||||
auto& map = _items.GetStdMap();
|
||||
return std::next(std::begin(map), i)->first;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user