// // ToyLayoutObject.cpp // SteveMaggieCpp // // Created by Katarzyna Kalinowska-Górska on 17.05.2017. // // #include #include "ToyLayoutObject.h" #include "ToyScalingUtils.h" #include "ToyJSONParseUtils.h" #include "ToyScenarioObject.h" #include "ToyLayoutParser.h" #include "ToySpriteAnimator.h" ToyLayoutObject::~ToyLayoutObject() { //bad bad bad design ToySpriteAnimator::cleanUpAnimationData(dynamic_cast(this)); } void ToyLayoutObject::loadCommonPropertiesFromJSON(const rapidjson::Value& jsonValue, ToyLayoutViewInterface* 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(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(ToyJSONParseUtils::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 = ToyScalingUtils::configureNodeDimension(width); height = ToyScalingUtils::configureNodeDimension(height); thisAsNode->setContentSize(cocos2d::Size(width, height)); } if(ToyJSONParseUtils::hasMemberArray(jsonValue, "animations")){ auto thisAsToyScenarioObject = dynamic_cast(this); if(thisAsToyScenarioObject){ for(const auto& animationData : jsonValue["animations"].GetArray()){ std::string animationId = animationData["animationId"].GetString(); thisAsToyScenarioObject->insertAnimationWithId(animationId, animationData); } } } } void ToyLayoutObject::prepareSize(const rapidjson::Value& jsonValue, float& width, float& height) { width = 0; height = 0; } float ToyLayoutObject::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(ToyJSONParseUtils::hasMemberFloat(*animationData, "animationDuration")){ animationTime = (*animationData)["animationDuration"].GetFloat(); } auto animation = cocos2d::Animation::create(); auto animationFramesPaths = ToyJSONParseUtils::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; }