81 lines
2.3 KiB
C++
81 lines
2.3 KiB
C++
#ifndef CREATURELIB_SCRIPTSET_HPP
|
|
#define CREATURELIB_SCRIPTSET_HPP
|
|
|
|
#include <any>
|
|
#include "BattleScript.hpp"
|
|
|
|
namespace CreatureLib::Battling {
|
|
class ScriptSet {
|
|
ArbUt::UniquePtrList<BattleScript> _scripts;
|
|
ArbUt::Dictionary<uint32_t, size_t> _lookup;
|
|
|
|
public:
|
|
~ScriptSet() = default;
|
|
|
|
static constexpr size_t defaultCapacity = 8;
|
|
ScriptSet() : _scripts(defaultCapacity), _lookup(defaultCapacity){};
|
|
|
|
void Clone(ScriptSet& s) {
|
|
for (auto* script : _scripts) {
|
|
s.Add(script->Clone());
|
|
}
|
|
}
|
|
|
|
BattleScript* Add(BattleScript* script) {
|
|
auto v = _lookup.TryGet(script->GetName());
|
|
if (v.has_value()) {
|
|
_scripts[v.value()]->Stack();
|
|
delete script;
|
|
return _scripts[v.value()];
|
|
}
|
|
|
|
_scripts.Append(script);
|
|
_lookup.Insert(script->GetName(), _scripts.Count() - 1);
|
|
return script;
|
|
}
|
|
|
|
ArbUt::OptionalBorrowedPtr<BattleScript> Get(const ArbUt::BasicStringView& key) const {
|
|
return Get(key.GetHash());
|
|
}
|
|
|
|
ArbUt::OptionalBorrowedPtr<BattleScript> Get(uint32_t keyHash) const noexcept {
|
|
auto v = _lookup.TryGet(keyHash);
|
|
if (v.has_value()) {
|
|
return _scripts[v.value()].GetRaw();
|
|
}
|
|
return {};
|
|
}
|
|
|
|
void Remove(const ArbUt::BasicStringView& key) { Remove(key.GetHash()); }
|
|
|
|
void Remove(uint32_t keyHash) {
|
|
auto v = _lookup.TryGet(keyHash);
|
|
if (v.has_value()) {
|
|
auto script = _scripts[v.value()];
|
|
script->OnRemove();
|
|
_scripts.Remove(v.value());
|
|
_lookup.Remove(keyHash);
|
|
}
|
|
}
|
|
|
|
void Clear() {
|
|
for (auto s : _scripts) {
|
|
s->OnRemove();
|
|
delete s;
|
|
}
|
|
_scripts.Clear();
|
|
_lookup.Clear();
|
|
}
|
|
|
|
bool Has(const ArbUt::BasicStringView& key) const { return _lookup.Has(key); }
|
|
|
|
bool Has(uint32_t keyHash) const { return _lookup.Has(keyHash); }
|
|
|
|
inline size_t Count() const { return _scripts.Count(); }
|
|
|
|
inline const ArbUt::UniquePtrList<BattleScript>& GetIterator() const { return _scripts; }
|
|
};
|
|
}
|
|
|
|
#endif // CREATURELIB_SCRIPTSET_HPP
|