ToySimpleActionParser.cpp
30.5 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//
// ToySimpleActionParser.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 31.05.2017.
//
//
#include <stdio.h>
#include "ToySimpleActionParser.h"
#include "ToyJSONParseUtils.h"
#include "ToySoundsRepo.h"
#include "ToySoundUtils.h"
//#include "ToyDragAndDropHandler.h"
#include "ToySpriteAnimator.h"
#include "ToySceneWithUtils.h"
#include "ToyValueStorage.h"
//#include "UserDefaultsKeys.h"
#include "ToyMathUtils.h"
class DynamicObjectMapper
{
public:
ToyScenarioObject* createObjectFromClassName(std::string className, const rapidjson::Value& objectData, ActionParseDelegate* parseDelegate)
{
ToyScenarioObject* object = NULL;
/*else if(className == "DragAndDropHandler"){
const auto& params = objectData["parameters"].GetArray();
// ToyDragAndDropHandler::ToyDragAndDropHandler(std::vector<cocos2d::Node*> objectsToDrag, cocos2d::Node* destinationObject, std::vector<cocos2d::Node*> wrongDraggedObjects, std::vector<cocos2d::Node*> wrongDestinationObjects)
object = new ToyDragAndDropHandler();
} */
return object;
}
};
// main parse function
cocos2d::Action* ToySimpleActionParser::parseJSONAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
cocos2d::Action* parsedAction = NULL;
// log("ToySimpleActionParser: parsing action of type "+jsonActionObject.actionType);
if(ToyJSONParseUtils::hasMemberString(jsonActionObject, "actionType")){
std::string actionType = jsonActionObject["actionType"].GetString();
if(actionType == "wait"){
parsedAction = this->parseWaitAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "createObject"){
parsedAction = this->parseCreateObjectAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "setProperty"){
parsedAction = this->parseSetPropertyAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "callFunction"){
parsedAction = this->parseCallFunctionAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "playSound"){
parsedAction = this->parsePlaySoundAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "playRandomSound"){
parsedAction = this->parsePlayRandomSoundAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "reorderInParent"){
parsedAction = this->parseReorderInParentAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "animateObjects"){
parsedAction = this->parseAnimateObjectsAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "animateObjectByAnimationId"){
parsedAction = this->parseAnimateObjectByAnimationIdAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "stopAnimations"){
parsedAction = this->parseStopAnimationsAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "dismissCurrentDialog"){
parsedAction = this->parseDismissCurrentDialogAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
} else if(actionType == "randomSwitchItemPositions"){
parsedAction = this->parseRandomObjectSwapAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
}
return parsedAction;
}
// functions for parsing differenct actions
/*
common properties:
"actionType"
"condition"
*/
/*
"actionType" = "wait"
"seconds" [float]
*/
cocos2d::Action* ToySimpleActionParser::parseWaitAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
if(ToyJSONParseUtils::hasMemberFloat(jsonActionObject, "seconds")){
float seconds = jsonActionObject["seconds"].GetFloat();
if(notifyDelegateWhenFinished){
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [](float pSeconds, std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){
static int keyModifier = 0;
static int modulus = 100;
static std::string keyBase = "waitAction";
std::string key = keyBase + std::to_string((++keyModifier)%modulus);
pParseDelegate->scheduleOnce(std::bind([](float, std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate){
p2ParseDelegate->actionFinished(*ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName()));
ToyValueStorage::getInstance().removeStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName()); // if the callback is unscheduled, this will not be called. However, when the scene changes, the ToyValueStorage is always cleared.
}, std::placeholders::_1, pStoredValueKey, pParseDelegate), pSeconds, key);
};
return cocos2d::CallFunc::create(std::bind(actionFunction, seconds, storedValueKey, parseDelegate));
}
return cocos2d::DelayTime::create(seconds);
}
return nullptr;
}
/*
"actionType" = "createObject"
"objectClass" [string]
"objectName" [string]
"parameters" [array of arbitrary things for the constructor or nothing; see parseParameters()]
*/
cocos2d::Action* ToySimpleActionParser::parseCreateObjectAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){
auto storedJsonActionObject = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
if(ToyActionParser::getInstance().checkLateCondition(*storedJsonActionObject, pParseDelegate)){
if(ToyJSONParseUtils::hasMemberString(*storedJsonActionObject, "objectClass") && ToyJSONParseUtils::hasMemberString(*storedJsonActionObject, "objectName")){
DynamicObjectMapper mapper;
auto newObject = mapper.createObjectFromClassName((*storedJsonActionObject)["objectClass"].GetString(), *storedJsonActionObject, pParseDelegate);
pParseDelegate->addNewObject((*storedJsonActionObject)["objectName"].GetString(), newObject);
}
}
ToyValueStorage::getInstance().removeStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
};
return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, std::bind(actionFunction, storedValueKey, parseDelegate));
}
/*
"actionType" = "setProperty"
"objectName" [string] | "objectNames" [array of strings] | nothing
"propertyName" [string]
"value" [anything]
*/
cocos2d::Action* ToySimpleActionParser::parseSetPropertyAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
std::function<void(std::string pStoredValueKey, ActionParseDelegate* pActionParseDelegate, bool pNotifyDelegate)> callback = [&](std::string pStoredValueKey, ActionParseDelegate* pActionParseDelegate, bool pNotifyDelegate){
auto storedJsonActionObject = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pActionParseDelegate->getToyValueStorageContainerName());
if(ToyActionParser::getInstance().checkLateCondition(*storedJsonActionObject, pActionParseDelegate)){
auto objects = ToyActionParser::getInstance().prepareObjectsForAction(*storedJsonActionObject, pActionParseDelegate);
for(auto object : objects){
object->setProperty((*storedJsonActionObject)["propertyName"].GetString(), (*storedJsonActionObject)["value"], pActionParseDelegate);
}
}
ToyValueStorage::getInstance().removeStoredValue(pStoredValueKey, pActionParseDelegate->getToyValueStorageContainerName());
};
auto actionFunction = std::bind(callback, storedValueKey, parseDelegate, notifyDelegateWhenFinished);
return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, actionFunction);
}
/*
"actionType" = "callFunction"
"objectName" [string] | "objectNames" [array of strings] | nothing
"functionName" [string]
"parameters" [array of arbitrary things or nothing; see parseParameters()]
*/
cocos2d::Action* ToySimpleActionParser::parseCallFunctionAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
auto key = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){
auto restoredValue = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
if(ToyActionParser::getInstance().checkLateCondition(*restoredValue, pParseDelegate)){
auto objects = ToyActionParser::getInstance().prepareObjectsForAction(*restoredValue, pParseDelegate);
const rapidjson::Value* params = NULL;
if(ToyJSONParseUtils::hasMemberArray(*restoredValue, "parameters")){
params = &((*restoredValue)["parameters"]);
}
for(auto object : objects){
object->callFunctionByName((*restoredValue)["functionName"].GetString(), params, pParseDelegate);
}
}
ToyValueStorage::getInstance().removeStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
};
return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, std::bind(actionFunction, key, parseDelegate));
}
/*
"actionType" = "playSound"
"sound" [string]
"useAlternativePath" [boolean]
"loop" [boolean]
"keepPreviousSounds" [boolean]
*/
cocos2d::Action* ToySimpleActionParser::parsePlaySoundAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
auto storageKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStorageKey, ActionParseDelegate* pParseDelegate){
auto storedJsonActionObject = ToyValueStorage::getInstance().getStoredValue(pStorageKey, pParseDelegate->getToyValueStorageContainerName());
if(ToyActionParser::getInstance().checkLateCondition(*storedJsonActionObject, pParseDelegate)){
if(!ToyJSONParseUtils::hasMemberString(*storedJsonActionObject, "sound")){
return;
}
std::string sound = (*storedJsonActionObject)["sound"].GetString();
auto soundPath = ToyJSONParseUtils::checkMemberBool(*storedJsonActionObject, "useAlternativePath", true) ? pParseDelegate->getAlternativeSoundsPath() +sound : pParseDelegate->getDefaultSoundsPath() + sound;
// auto loop = ToyJSONParseUtils::hasMemberBool(*storedJsonActionObject, "loop") && (*storedJsonActionObject)["loop"].GetBool() == true ? true : false;
if(!ToyJSONParseUtils::hasMemberBool(*storedJsonActionObject, "keepPreviousSounds") || ToyJSONParseUtils::checkMemberBool(*storedJsonActionObject, "keepPreviousSounds", false)){
// CocosDenshion::SimpleAudioEngine::getInstance()->stopAllEffects();
ToySoundsRepo::stopAllSounds();
}
bool cancelSameSound = ToyJSONParseUtils::checkMemberBool(*storedJsonActionObject, "cancelSameSound", true);
if(cancelSameSound){
auto soundId = pParseDelegate->removeStoredSoundId(soundPath);
if(soundId != -1){
ToySoundsRepo::stopSoundById(soundId);
// CocosDenshion::SimpleAudioEngine::getInstance()->stopEffect(soundId);
}
}
unsigned int newSoundId = ToySoundsRepo::playSound(soundPath.c_str()); //TODO loop //CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(soundPath.c_str(), loop);
if(cancelSameSound){
pParseDelegate->storeSoundId(soundPath, newSoundId);
}
}
ToyValueStorage::getInstance().removeStoredValue(pStorageKey, pParseDelegate->getToyValueStorageContainerName());
};
return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, std::bind(actionFunction, storageKey, parseDelegate));
}
/*
"actionType" = "playRandomSound"
"sounds" [array of strings]
"useAlternativePath" [boolean]
"keepPreviousSounds" [boolean]
*/
cocos2d::Action* ToySimpleActionParser::parsePlayRandomSoundAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){
auto storedActionObject = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
if(ToyActionParser::getInstance().checkLateCondition(*storedActionObject, pParseDelegate)){
auto soundFolder = ToyJSONParseUtils::checkMemberBool(*storedActionObject, "useAlternativePath", true) ? pParseDelegate->getAlternativeSoundsPath() : pParseDelegate->getDefaultSoundsPath();
auto stopEffects = !ToyJSONParseUtils::hasMemberBool(*storedActionObject, "keepPreviousSounds") || ToyJSONParseUtils::checkMemberBool(*storedActionObject, "keepPreviousSounds", false) ? true : false;
if(ToyJSONParseUtils::hasMemberArray(*storedActionObject, "sounds")){
std::vector<const std::string> sounds;
for(const auto& sound : (*storedActionObject)["sounds"].GetArray()){
sounds.push_back(sound.GetString());
}
ToySoundUtils::playRandomSound(soundFolder, sounds, stopEffects);
}
}
ToyValueStorage::getInstance().removeStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
};
return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, std::bind(actionFunction, storedValueKey, parseDelegate));
}
/*
"actionType" = "reorderInParent"
"objectName" [string]
"newDepth" [number]
*/
cocos2d::Action* ToySimpleActionParser::parseReorderInParentAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){
auto storedActionObject = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
if(ToyActionParser::getInstance().checkLateCondition(*storedActionObject, pParseDelegate)){
if(ToyJSONParseUtils::hasMemberString(*storedActionObject, "objectName") && ToyJSONParseUtils::hasMemberInt(*storedActionObject, "newDepth")){
auto object = pParseDelegate->getObjectByName((*storedActionObject)["objectName"].GetString());
auto objectNode = dynamic_cast<cocos2d::Node*>(object);
if(objectNode){
objectNode->getParent()->reorderChild(objectNode, (*storedActionObject)["newDepth"].GetInt());
}
}
}
ToyValueStorage::getInstance().removeStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
};
return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, std::bind(actionFunction, storedValueKey, parseDelegate));
}
/*
"actionType" = "animateObjects"
"objectName" [string] | "objectNames" [array of strings]
"animationName" [string]
"parameters" [array of arbitrary things or nothing; see parseParameters()]
"animationConditions" [array of conditions for different sprites in the array]
*/
cocos2d::Action* ToySimpleActionParser::parseAnimateObjectsAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished) //todo put all into the acton function for late parse
{
auto objects = ToyActionParser::getInstance().prepareObjectsForAction(jsonActionObject, parseDelegate);
if(ToyJSONParseUtils::hasMemberArray(jsonActionObject, "animationConditions")){
const auto& conditionsArray = jsonActionObject["animationConditions"].GetArray();
if(conditionsArray.Size() >= objects.size()){
for(int i = (int)objects.size()-1; i >= 0; --i){
if(conditionsArray[i].IsString() && ToyActionParser::getInstance().checkCondition(conditionsArray[i].GetString(), parseDelegate) == false){
objects.erase(objects.begin() + i, objects.begin() + i + 1);
}
}
}
}
std::vector<cocos2d::Node*> nodes;
for(auto object : objects){
auto objAsNode = dynamic_cast<cocos2d::Node*>(object);
nodes.push_back(objAsNode);
}
auto storageKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::vector<cocos2d::Node*> pNodes, std::string pStorageKey, ActionParseDelegate* pParseDelegate){
auto storedJsonObject = ToyValueStorage::getInstance().getStoredValue(pStorageKey, pParseDelegate->getToyValueStorageContainerName());
if(ToyActionParser::getInstance().checkLateCondition(*storedJsonObject, pParseDelegate)){
std::string animationName = (*storedJsonObject)["animationName"].GetString();
const rapidjson::Value* params = nullptr;
if(ToyJSONParseUtils::hasMemberArray(*storedJsonObject, "parameters")){
params = &((*storedJsonObject)["parameters"]);
}
ToySpriteAnimator::runAnimationForName(animationName, pNodes, params);
}
ToyValueStorage::getInstance().removeStoredValue(pStorageKey, pParseDelegate->getToyValueStorageContainerName());
};
return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, std::bind(actionFunction, nodes, storageKey, parseDelegate));
}
/*
"actionType" = "animateObjectByAnimationId"
"objectName" [string]
"animationId" [string]
The object should have the animation with given "animationId" specified in its "animations" field in the layout file.
*/
cocos2d::Action* ToySimpleActionParser::parseAnimateObjectByAnimationIdAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){
auto storedJsonActionObject = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
auto objects = ToyActionParser::getInstance().prepareObjectsForAction(*storedJsonActionObject, pParseDelegate);
auto animationObject = objects[0]->getAnimationById((*storedJsonActionObject)["animationId"].GetString()); //TODO sprobowac wywalic poza lambde
if(ToyActionParser::getInstance().checkLateCondition(*storedJsonActionObject, pParseDelegate)){
std::string animationName = (*animationObject)["animationName"].GetString();
const rapidjson::Value* params = NULL;
if(ToyJSONParseUtils::hasMemberArray((*animationObject), "parameters")){
params = &((*animationObject)["parameters"]);
}
ToySpriteAnimator::runAnimationForName(animationName, {dynamic_cast<cocos2d::Node*>(objects[0])}, params);
}
ToyValueStorage::getInstance().removeStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
};
auto boundActionFunction = std::bind(actionFunction, storedValueKey, parseDelegate);
return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, boundActionFunction);
}
/*
"actionType" = "stopAnimations"
"objectName" [string] | "objectNames" [array of strings]
*/
cocos2d::Action* ToySimpleActionParser::parseStopAnimationsAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){
auto storedJsonActionObject = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
if(ToyActionParser::getInstance().checkLateCondition(*storedJsonActionObject, pParseDelegate)){
auto objects = ToyActionParser::getInstance().prepareObjectsForAction(*storedJsonActionObject, pParseDelegate);
for(int i = 0; i < objects.size(); ++i){
dynamic_cast<cocos2d::Node*>(objects[i])->stopAllActions();
}
}
ToyValueStorage::getInstance().removeStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
};
return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, std::bind(actionFunction, storedValueKey, parseDelegate));
}
cocos2d::Action* ToySimpleActionParser::parseDismissCurrentDialogAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
return cocos2d::CallFunc::create([](){
auto currentScene = dynamic_cast<ToySceneWithUtils*>(cocos2d::Director::getInstance()->getRunningScene());
currentScene->dismissCurrentDialog();
});
}
cocos2d::Action* ToySimpleActionParser::finalizeParseActionWithActionFunction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished, std::function<void()> preparedActionFunction)
{
if(ToyJSONParseUtils::checkMemberBool(jsonActionObject, "instantAction", true)){
parseDelegate->runInstantly(preparedActionFunction);
return NULL;
} else {
return ToyActionParser::getInstance().embedFunctionInAction(preparedActionFunction, jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
}
cocos2d::Action* ToySimpleActionParser::parseRandomObjectSwapAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished){
// "objectGroups" :
// [
// {"t_doll1" : ["t_robot2", "t_robot4", "t_bear1", "t_bear3", "t_bear4", "blueBoat", "ballSpots", "ballStripy"]},
// {"t_doll2" : ["t_robot2", "t_robot4", "t_bear1", "t_bear3", "t_bear4", "blueBoat", "ballSpots", "ballStripy"]},
// {"t_doll3" : ["t_bear1", "t_bear3", "dinoRed", "dinoGreen"]},
// {"t_auto3" : ["t_bear2", "lego3"]},
// {"t_doll4" : ["t_robot2", "t_robot4", "t_bear1", "t_bear3", "t_bear4", "blueBoat", "ballSpots", "ballStripy", "t_auto1", "t_auto4", "t_train1", "t_train2", "t_train3", "t_train4", "t_bear2"]},
// {"t_auto1" : ["t_train1", "t_train2", "t_train3", "t_train4", "t_doll4", "t_bear2", "ballStripy"]},
// {"t_auto2" : ["dinoRed", "dinoGreen", "lego3", "blueBoat", "t_bear2", "t_bear4"]},
// {"t_auto4" : ["t_train1", "t_train2", "t_train3", "t_train4", "t_doll4", "t_bear2", "ballStripy"]},
// {"t_train1" : ["t_auto1", "t_auto4", "t_doll4", "t_bear2", "ballStripy"]},
// {"t_train2" : ["t_auto1", "t_auto4", "t_doll4", "t_bear2", "ballStripy"]},
// {"t_train3" : ["t_auto1", "t_auto4", "t_doll4", "t_bear2", "ballStripy"]},
// {"t_train4" : ["t_auto1", "t_auto4", "t_doll4", "t_bear2", "ballStripy"]},
// {"t_bear1" : ["t_robot2", "t_robot4", "t_doll1", "t_doll2", "t_doll4", "blueBoat", "ballSpots", "ballStripy", "t_doll3", "dinoRed", "dinoGreen"]},
// {"t_bear2" : ["dinoRed", "dinoGreen", "lego3", "blueBoat", "t_auto2", "t_auto3", "t_train1", "t_train2", "t_train3", "t_train4", "t_doll4", "ballStripy"]},
// {"t_bear3" : ["t_robot2", "t_robot4", "t_doll1", "t_doll2", "t_doll4", "blueBoat", "ballSpots", "ballStripy", "t_doll3", "dinoRed", "dinoGreen"]},
// {"t_bear4" : ["dinoRed", "dinoGreen", "lego3", "blueBoat", "t_auto2", "t_robot2", "t_robot4", "t_doll1", "t_doll2", "t_doll4", "ballSpots", "ballStripy"]}
// ]
//TODO add probability of swap, maybe different for different levels
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){
auto actionJson = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
if(ToyJSONParseUtils::hasMemberArray(*actionJson, "objectGroups")){
auto objectGroups = (*actionJson)["objectGroups"].GetArray();
auto swapProbability = (*actionJson)["swapProbability"].GetFloat();
// std::map<std::string, std::map<std::string, ToyScenarioObject*>> objectMap;
std::vector<std::string> usedObjects; //keep info which objects are already swapped, not to swap them again
for(int i = 0; i < objectGroups.Size(); ++i){
// first throw dice to see if we should swap at all
auto randomFloat = ToyMathUtils::getRandom(0, 1);
if(randomFloat > swapProbability){
continue;
}
std::string mainObjectName = objectGroups[i].MemberBegin()->name.GetString();
if(std::find(usedObjects.begin(), usedObjects.end(), mainObjectName) != usedObjects.end()) {
// printf("already swapped %s! skipping\n", mainObjectName.c_str());
continue; // object already swapped, move on
}
auto availableObjects = ToyJSONParseUtils::parseStringArray(objectGroups[i].MemberBegin()->value);
for(int i = (int)availableObjects.size()-1; i >= 0; --i){
if(std::find(usedObjects.begin(), usedObjects.end(), availableObjects[i]) != usedObjects.end()){
availableObjects.erase(availableObjects.begin()+i);
}
}
if(availableObjects.size() == 0){
// printf("nothing to swap %s with! skipping\n", mainObjectName.c_str());
continue; //nothing to swap with
}
auto objectToSwapWith = ToyMathUtils::getRandomElement(availableObjects);
auto object1 = dynamic_cast<cocos2d::Node*>(parseDelegate->getObjectByName(mainObjectName));
auto object2 = dynamic_cast<cocos2d::Node*>(parseDelegate->getObjectByName(objectToSwapWith));
auto tempPos = object1->getPosition();
auto tempZOrder = object1->getLocalZOrder();
object1->setPosition(object2->getPosition());
object1->setLocalZOrder(object2->getLocalZOrder());
object2->setPosition(tempPos);
object2->setLocalZOrder(tempZOrder);
usedObjects.push_back(mainObjectName);
usedObjects.push_back(objectToSwapWith);
// printf("swapping %s with %s\n", mainObjectName.c_str(), objectToSwapWith.c_str());
}
}
ToyValueStorage::getInstance().removeStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
};
return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, std::bind(actionFunction, storedValueKey, parseDelegate));
}