2019-11-09 11:15:45 +00:00
|
|
|
#ifndef CREATURELIB_SCRIPTAGGREGATOR_HPP
|
|
|
|
#define CREATURELIB_SCRIPTAGGREGATOR_HPP
|
|
|
|
|
|
|
|
#include <any>
|
|
|
|
#include <queue>
|
2019-11-28 11:55:22 +00:00
|
|
|
#include "../../Core/Exceptions/NotReachableException.hpp"
|
2019-11-09 11:15:45 +00:00
|
|
|
#include "Script.hpp"
|
|
|
|
#include "ScriptSet.hpp"
|
2019-11-10 16:08:42 +00:00
|
|
|
#include "ScriptWrapper.hpp"
|
2019-11-09 11:15:45 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
namespace CreatureLib::Battling {
|
|
|
|
class ScriptAggregator {
|
2019-11-11 19:04:59 +00:00
|
|
|
std::vector<ScriptWrapper> _scripts;
|
2019-12-07 21:15:50 +00:00
|
|
|
size_t _index = 0;
|
2019-11-09 11:15:45 +00:00
|
|
|
bool _isSetSet = false;
|
2019-11-12 17:38:09 +00:00
|
|
|
const std::vector<Script*>* _setScripts;
|
2019-12-07 21:15:50 +00:00
|
|
|
size_t _setIndex;
|
2019-11-28 11:55:22 +00:00
|
|
|
|
2019-11-09 11:15:45 +00:00
|
|
|
public:
|
2019-11-28 11:55:22 +00:00
|
|
|
ScriptAggregator(std::vector<ScriptWrapper> scripts) : _scripts(std::move(scripts)){};
|
2019-11-09 11:15:45 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
bool HasNext() { return _index < _scripts.size() || (_isSetSet && _setIndex < _setScripts->size()); }
|
2019-11-09 11:15:45 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
Script* GetNext() {
|
2019-11-09 11:15:45 +00:00
|
|
|
// We can probably do this in a cleaner version once C++ 20 drops with Coroutine support.
|
2019-11-28 11:55:22 +00:00
|
|
|
if (_isSetSet) {
|
|
|
|
if (_setIndex >= _setScripts->size()) {
|
2019-11-09 11:15:45 +00:00
|
|
|
_isSetSet = false;
|
|
|
|
return GetNext();
|
|
|
|
}
|
2019-11-12 17:38:09 +00:00
|
|
|
auto s = _setScripts->at(_setIndex);
|
|
|
|
_setIndex++;
|
2019-11-28 11:55:22 +00:00
|
|
|
if (_setIndex >= _setScripts->size()) {
|
2019-11-09 11:15:45 +00:00
|
|
|
_isSetSet = false;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2019-11-11 19:04:59 +00:00
|
|
|
if (_index >= _scripts.size())
|
2019-11-09 11:15:45 +00:00
|
|
|
return nullptr;
|
2019-11-11 19:04:59 +00:00
|
|
|
auto next = _scripts[_index];
|
|
|
|
_index++;
|
2019-11-28 11:55:22 +00:00
|
|
|
if (!next.IsSet()) {
|
2019-11-10 16:08:42 +00:00
|
|
|
auto scriptPtr = next.GetScript();
|
|
|
|
if (scriptPtr == nullptr)
|
|
|
|
return GetNext();
|
|
|
|
return *scriptPtr;
|
2019-11-28 11:55:22 +00:00
|
|
|
} else {
|
2019-11-10 16:08:42 +00:00
|
|
|
auto set = next.GetScriptSet();
|
2019-11-09 11:15:45 +00:00
|
|
|
if (set->Count() == 0)
|
|
|
|
return GetNext();
|
2019-11-12 17:38:09 +00:00
|
|
|
_setScripts = set->GetIterator();
|
2019-11-09 11:15:45 +00:00
|
|
|
_isSetSet = true;
|
2019-11-12 17:38:09 +00:00
|
|
|
_setIndex = 0;
|
2019-11-09 11:15:45 +00:00
|
|
|
return GetNext();
|
|
|
|
}
|
|
|
|
throw NotReachableException();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
#endif // CREATURELIB_SCRIPTAGGREGATOR_HPP
|