#ifndef CREATURELIB_SCRIPTAGGREGATOR_HPP #define CREATURELIB_SCRIPTAGGREGATOR_HPP #include #include "../../Library/Exceptions/NotReachableException.hpp" #include "Script.hpp" #include "ScriptSet.hpp" #include "ScriptWrapper.hpp" namespace CreatureLib::Battling { class ScriptAggregator { const ScriptWrapper* _scripts; size_t _size; size_t _index = 0; size_t _setIndex = 0; public: ScriptAggregator(){}; explicit ScriptAggregator(const ArbUt::List& scripts) : _scripts(scripts.RawData()), _size(scripts.Count()){}; inline void Reset() { _index = 0; _setIndex = 0; } inline bool HasNext() { return _index < _size; } Script* GetNextNotNull() { while (HasNext()) { auto s = GetNext(); if (s != nullptr) { return s; } } 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 { auto& set = current.GetScriptSet()->GetIterator(); auto count = set.Count(); if (_setIndex >= count) { _index++; _setIndex = 0; return nullptr; } auto v = set[_setIndex++]; if (_setIndex >= count) { _index++; _setIndex = 0; } return v.GetRaw(); } } }; } #endif // CREATURELIB_SCRIPTAGGREGATOR_HPP