ToyLayoutObject.cpp
4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// ToyLayoutObject.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 17.05.2017.
//
//
#include <stdio.h>
#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<cocos2d::Node*>(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<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(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<ToyScenarioObject*>(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;
}