CreatureLib/src/Battling/ScriptHandling/ScriptAggregator.hpp

66 lines
1.8 KiB
C++
Raw Normal View History

2019-11-09 11:15:45 +00:00
#ifndef CREATURELIB_SCRIPTAGGREGATOR_HPP
#define CREATURELIB_SCRIPTAGGREGATOR_HPP
#include <Arbutils/Collections/List.hpp>
#include "../../Library/Exceptions/NotReachableException.hpp"
2019-11-09 11:15:45 +00:00
#include "Script.hpp"
#include "ScriptSet.hpp"
#include "ScriptWrapper.hpp"
2019-11-09 11:15:45 +00:00
namespace CreatureLib::Battling {
class ScriptAggregator {
2020-04-23 21:23:58 +00:00
const ScriptWrapper* _scripts;
size_t _size;
size_t _index = 0;
size_t _setIndex = 0;
2019-11-09 11:15:45 +00:00
public:
ScriptAggregator(){};
2020-05-26 16:31:06 +00:00
explicit ScriptAggregator(const ArbUt::List<ScriptWrapper>& scripts)
2020-04-23 21:23:58 +00:00
: _scripts(scripts.RawData()), _size(scripts.Count()){};
2019-11-09 11:15:45 +00:00
2020-04-23 21:23:58 +00:00
inline void Reset() {
_index = 0;
_setIndex = 0;
}
2020-04-23 21:23:58 +00:00
inline bool HasNext() { return _index < _size; }
2019-11-09 11:15:45 +00:00
2020-04-23 21:23:58 +00:00
Script* GetNextNotNull() {
while (HasNext()) {
auto s = GetNext();
if (s != nullptr) {
return s;
2019-11-09 11:15:45 +00:00
}
}
2020-04-23 21:23:58 +00:00
return nullptr;
}
Script* GetNext() {
auto& current = _scripts[_index];
if (!current.IsSet()) {
_index++;
auto s = current.GetScript();
if (s == nullptr)
return nullptr;
return (*s).get();
} else {
2020-06-02 13:03:31 +00:00
auto& set = current.GetScriptSet()->GetIterator();
2020-04-23 21:23:58 +00:00
auto count = set.Count();
if (_setIndex >= count) {
_index++;
_setIndex = 0;
return nullptr;
}
auto v = set[_setIndex++];
if (_setIndex >= count) {
_index++;
_setIndex = 0;
}
2020-06-02 13:03:31 +00:00
return v.GetRaw();
2019-11-09 11:15:45 +00:00
}
}
};
}
#endif // CREATURELIB_SCRIPTAGGREGATOR_HPP