ToyActionSequenceHandler.cpp 5.55 KB
//
//  ToyActionSequenceHandler.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 29.06.2017.
//
//

#include <stdio.h>
#include "ToyActionSequenceHandler.h"
#include "ToyResourceUtilities.h"
#include "ToyValueStorage.h"

ToyActionSequenceHandler::ToyActionSequenceHandler(ToyScenarioObject* 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;
}

ToyActionSequenceHandler::~ToyActionSequenceHandler()
{
    delete _JSONDataStorage;
    delete _actions;
}

void ToyActionSequenceHandler::runNext()
{
    auto i = _lastActionIndex+1;
    auto actionsArray = _actions->GetArray();
    
    if(i >= actionsArray.Size()){
        return;
    }
    
    const auto& currentAction = actionsArray[i];
    
    auto parsedAction = ToyActionParser::getInstance().parseJSONAction(currentAction, this);
    
    _lastActionIndex = i;
    
    if(parsedAction){
        this->runAction(parsedAction);
    } else {
        this->actionFinished(currentAction);
    }
    
    _currentAction = parsedAction;
}
    
void ToyActionSequenceHandler::reset()
{
    _lastActionIndex = -1;
    _currentAction = nullptr;
}

void ToyActionSequenceHandler::stop()
{
    if(_currentAction){
        auto containerNode = dynamic_cast<cocos2d::Node*>(_containerNode);
        if(containerNode){
            containerNode->stopAction(_currentAction);
        }
    }
    ToyActionParser::getInstance().cleanUp(this);
    this->reset();
}

// methods for parseDelegate

ToyScenarioObject* ToyActionSequenceHandler::getObjectByName (std::string objectName)
{
    return _containerNode->getToyScenarioObjectByName(objectName);
}
    
ToyScenarioObject* ToyActionSequenceHandler::getDefaultObjectForAction(std::string actionType)
{
    return dynamic_cast<ToyScenarioObject*>(_containerNode);
}
    
ToyScenarioObject* ToyActionSequenceHandler::getDefaultObjectForConditionCheck()
{
    return dynamic_cast<ToyScenarioObject*>(_containerNode);
}
    
void ToyActionSequenceHandler::addNewObject(std::string objectName, ToyScenarioObject* newObject)
{
    // do nothing
}
        
std::string ToyActionSequenceHandler::getDefaultSoundsPath()
{
    return ToyResourceUtilities::getInstance().getDownloadedResourcesPath(false);
}
    
std::string ToyActionSequenceHandler::getAlternativeSoundsPath()
{
    return ToyResourceUtilities::getInstance().getDownloadedResourcesPath(false);
}

std::string ToyActionSequenceHandler::getToyValueStorageContainerName()
{
    return _valueStorageContainerName;
}
 
void ToyActionSequenceHandler::runAction(cocos2d::Action* action)
{
    auto containerNode = dynamic_cast<cocos2d::Node*>(_containerNode);
    if(containerNode){
        containerNode->runAction(action);
    }
}

void ToyActionSequenceHandler::actionFinished(const rapidjson::Value& jsonActionObject)
{
    if(_lastActionIndex < _actions->GetArray().Size() - 1){
        this->runNext();
    } else {
        _onLastActionFinished();
        ToyActionParser::getInstance().cleanUp(this);
    }
}
    
void ToyActionSequenceHandler::runInstantly(std::function<void()> actionFunction)
{
    actionFunction();
}

void ToyActionSequenceHandler::runCompletingAction(cocos2d::Action* action)
{
    // do nothing
}
    
void ToyActionSequenceHandler::cancelAllCompletingActions()
{
    // do nothing
}
    
void ToyActionSequenceHandler::schedule(std::function<void(float)> callback, std::string key, float delay)
{
    auto containerNode = dynamic_cast<cocos2d::Node*>(_containerNode);
    if(containerNode){
        containerNode->schedule(callback, delay, key);
    }
}
    
void ToyActionSequenceHandler::scheduleOnce(const std::function<void(float)> callback, float delay, std::string key)
{
    auto containerNode = dynamic_cast<cocos2d::Node*>(_containerNode);
    if(containerNode){
        containerNode->scheduleOnce(callback, delay, key);
    }
}
    
void ToyActionSequenceHandler::unschedule(std::string key)
{
    auto containerNode = dynamic_cast<cocos2d::Node*>(_containerNode);
    if(containerNode){
        containerNode->unschedule(key);
    }
}

void ToyActionSequenceHandler::newTouchHandler(std::string key, TouchHandlerFunction touchHandler, TouchHandlerType touchType)
{
    // do nothing
}
    
void ToyActionSequenceHandler::removeTouchHandler(std::string key, TouchHandlerType touchHandlerType)
{
    // do nothing
}
    
long long ToyActionSequenceHandler::getLastScreenTouchTime()
{
    return 0;
}
    
int ToyActionSequenceHandler::getLoopActionCounter(std::string loopId)
{
    return 0;
}
    
int ToyActionSequenceHandler::addNewLoopActionCounter(std::string loopId, int timesRepeated)
{
    return 0;
}
    
int ToyActionSequenceHandler::decrementLoopActionCounter(std::string loopId)
{
     return 0;
}
    
void ToyActionSequenceHandler::deleteLoopActionCounter(std::string loopId)
{
     // do nothing
}
    
void ToyActionSequenceHandler::setLastActionIndex(int index)
{
    _lastActionIndex = index;
}
 
void ToyActionSequenceHandler::storeSoundId(std::string soundPath, unsigned int newSoundId)
{
    // do nothing
}
    
unsigned int ToyActionSequenceHandler::removeStoredSoundId(std::string soundPath)
{
    return -1;
}