AniLayoutObject.cpp 4.36 KB
//
//  AniLayoutObject.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 17.05.2017.
//
//

#include <stdio.h>

#include "AniLayoutObject.h"
#include "AniScalingUtils.h"
#include "AniJSONParseUtils.h"
#include "AniScenarioObject.h"
#include "AniLayoutParser.h"
//#include "SpriteAnimator.h"

AniLayoutObject::~AniLayoutObject()
{
    //bad bad bad design
//    SpriteAnimator::cleanUpAnimationData(dynamic_cast<cocos2d::Node*>(this));
}

void AniLayoutObject::loadCommonPropertiesFromJSON(const rapidjson::Value& jsonValue, AniLayoutViewInterface* scene, const std::string resFolder, const std::string altResFolder)
{
    if(jsonValue.HasMember("type")){
        const auto& classNameJSON = jsonValue["type"];
        this->className = classNameJSON.GetString();
    } else {
        this->className = "";
    }

   if(jsonValue.HasMember("name")){
        const auto& objectNameJSON = jsonValue["name"];
        this->objectName = objectNameJSON.GetString();
   } else {
        this->objectName = "";
   }
    
    cocos2d::Node* thisAsNode = dynamic_cast<cocos2d::Node*>(this);

    if(jsonValue.HasMember("scale")){
        const auto& scaleJSON = jsonValue["scale"];
        if(scaleJSON.IsFloat() || scaleJSON.IsInt()){
            thisAsNode->setScale(scaleJSON.GetFloat());
        }
    }
    
    if(jsonValue.HasMember("rotation")){
        const auto& rotationJSON = jsonValue["rotation"];
        if(rotationJSON.IsFloat() || rotationJSON.IsInt()){
            thisAsNode->setRotation(rotationJSON.GetFloat());
        }
    }
    
    if(jsonValue.HasMember("hidden")){
        const auto& hiddenJSON = jsonValue["hidden"];
        if(hiddenJSON.IsBool()){
            thisAsNode->setVisible(!hiddenJSON.GetBool());
        }
    }
    
    if(AniJSONParseUtils::checkMemberBool(jsonValue, "visible", false)){
        thisAsNode->setVisible(false);
    }

    if(jsonValue.HasMember("anchorPoint")){
        const auto& anchorPointJSON = jsonValue["anchorPoint"];
        float x = anchorPointJSON["x"].GetFloat();
        float y = anchorPointJSON["y"].GetFloat();
        thisAsNode->setAnchorPoint(cocos2d::Point(x, y));
    }

    if(jsonValue.HasMember("width") && jsonValue.HasMember("height")){
        float width = jsonValue["width"].GetFloat();
        float height = jsonValue["height"].GetFloat();
        width = AniScalingUtils::configureNodeDimension(width);
        height = AniScalingUtils::configureNodeDimension(height);
        thisAsNode->setContentSize(cocos2d::Size(width, height));
    }
    
    if(AniJSONParseUtils::hasMemberArray(jsonValue, "animations")){
        
        auto thisAsAniScenarioObject = dynamic_cast<AniScenarioObject*>(this);
        if(thisAsAniScenarioObject){
            for(const auto& animationData : jsonValue["animations"].GetArray()){
                std::string animationId = animationData["animationId"].GetString();
                thisAsAniScenarioObject->insertAnimationWithId(animationId, animationData);
            }
        }
    }
}

void AniLayoutObject::prepareSize(const rapidjson::Value& jsonValue, float& width, float& height)
{
    width = 0;
    height = 0;
}

//float AniLayoutObject::animate(rapidjson::Value* animationData){
//
//    if(!animationData){
//        return 0;
//    }
//
//    cocos2d::Action* animationAction = NULL;
//    float animationTime = 0;
//
//    std::string animationType = (*animationData)["animationType"].GetString();
//    if(animationType == "frameAnimation"){
//
//            animationTime = 0.2;
//            if(AniJSONParseUtils::hasMemberFloat(*animationData, "animationDuration")){
//                animationTime = (*animationData)["animationDuration"].GetFloat();
//            }
//
//            auto animation = cocos2d::Animation::create();
//            auto animationFramesPaths = AniJSONParseUtils::parseStringArray((*animationData)["animationImagePaths"]);
//
//            for(int i = 0; i < animationFramesPaths.size(); ++i){
//                animation->addSpriteFrameWithFile(animationFramesPaths[i]);
//            }
//
//            animation->setDelayPerUnit(animationTime/animationFramesPaths.size());
//            animationAction = cocos2d::Animate::create(animation);
//    }
//
////            if(animationAction){
////                animationAction->setTag(MapObjectAnimationTag);
////                this->runAction(animationAction);
////            }
//
//    return animationTime;
//}