// // ActionSequenceHandler.cpp // SteveMaggieCpp // // Created by Katarzyna Kalinowska-Górska on 29.06.2017. // // #include #include "ActionSequenceHandler.h" #include "ResourceUtilities.h" #include "ValueStorage.h" ActionSequenceHandler::ActionSequenceHandler(ScenarioObject* containerNode, const rapidjson::Value& actionsArray) { _containerNode = containerNode; _JSONDataStorage = new (std::nothrow) rapidjson::Document(); _actions = new (std::nothrow) rapidjson::Value(); _actions->CopyFrom(actionsArray, _JSONDataStorage->GetAllocator()); static int valueStorageKeyNumber = 0; valueStorageKeyNumber = (valueStorageKeyNumber + 1)%100; _valueStorageContainerName = "ActionSequenceHandlerValues" + std::to_string(valueStorageKeyNumber); _lastActionIndex = -1; _onLastActionFinished = [](){}; _currentAction = nullptr; } ActionSequenceHandler::~ActionSequenceHandler() { delete _JSONDataStorage; delete _actions; } void ActionSequenceHandler::runNext() { auto i = _lastActionIndex+1; auto actionsArray = _actions->GetArray(); if(i >= actionsArray.Size()){ return; } const auto& currentAction = actionsArray[i]; auto parsedAction = ActionParser::getInstance().parseJSONAction(currentAction, this); _lastActionIndex = i; if(parsedAction){ this->runAction(parsedAction); } else { this->actionFinished(currentAction); } _currentAction = parsedAction; } void ActionSequenceHandler::reset() { _lastActionIndex = -1; _currentAction = nullptr; } void ActionSequenceHandler::stop() { if(_currentAction){ auto containerNode = dynamic_cast(_containerNode); if(containerNode){ containerNode->stopAction(_currentAction); } } ActionParser::getInstance().cleanUp(this); this->reset(); } // methods for parseDelegate ScenarioObject* ActionSequenceHandler::getObjectByName (std::string objectName) { return _containerNode->getScenarioObjectByName(objectName); } ScenarioObject* ActionSequenceHandler::getDefaultObjectForAction(std::string actionType) { return dynamic_cast(_containerNode); } ScenarioObject* ActionSequenceHandler::getDefaultObjectForConditionCheck() { return dynamic_cast(_containerNode); } void ActionSequenceHandler::addNewObject(std::string objectName, ScenarioObject* newObject) { // do nothing } std::string ActionSequenceHandler::getDefaultSoundsPath() { return ResourceUtilities::getInstance().getDownloadedResourcesPath(false); } std::string ActionSequenceHandler::getAlternativeSoundsPath() { return ResourceUtilities::getInstance().getDownloadedResourcesPath(false); } std::string ActionSequenceHandler::getValueStorageContainerName() { return _valueStorageContainerName; } void ActionSequenceHandler::runAction(cocos2d::Action* action) { auto containerNode = dynamic_cast(_containerNode); if(containerNode){ containerNode->runAction(action); } } void ActionSequenceHandler::actionFinished(const rapidjson::Value& jsonActionObject) { if(_lastActionIndex < _actions->GetArray().Size() - 1){ this->runNext(); } else { _onLastActionFinished(); ActionParser::getInstance().cleanUp(this); } } void ActionSequenceHandler::runInstantly(std::function actionFunction) { actionFunction(); } void ActionSequenceHandler::runCompletingAction(cocos2d::Action* action) { // do nothing } void ActionSequenceHandler::cancelAllCompletingActions() { // do nothing } void ActionSequenceHandler::schedule(std::function callback, std::string key, float delay) { auto containerNode = dynamic_cast(_containerNode); if(containerNode){ containerNode->schedule(callback, delay, key); } } void ActionSequenceHandler::scheduleOnce(const std::function callback, float delay, std::string key) { auto containerNode = dynamic_cast(_containerNode); if(containerNode){ containerNode->scheduleOnce(callback, delay, key); } } void ActionSequenceHandler::unschedule(std::string key) { auto containerNode = dynamic_cast(_containerNode); if(containerNode){ containerNode->unschedule(key); } } void ActionSequenceHandler::newTouchHandler(std::string key, TouchHandlerFunction touchHandler, TouchHandlerType touchType) { // do nothing } void ActionSequenceHandler::removeTouchHandler(std::string key, TouchHandlerType touchHandlerType) { // do nothing } long long ActionSequenceHandler::getLastScreenTouchTime() { return 0; } int ActionSequenceHandler::getLoopActionCounter(std::string loopId) { return 0; } int ActionSequenceHandler::addNewLoopActionCounter(std::string loopId, int timesRepeated) { return 0; } int ActionSequenceHandler::decrementLoopActionCounter(std::string loopId) { return 0; } void ActionSequenceHandler::deleteLoopActionCounter(std::string loopId) { // do nothing } void ActionSequenceHandler::setLastActionIndex(int index) { _lastActionIndex = index; } void ActionSequenceHandler::storeSoundId(std::string soundPath, unsigned int newSoundId) { // do nothing } unsigned int ActionSequenceHandler::removeStoredSoundId(std::string soundPath) { return -1; }