ToyActionSequenceHandler.cpp
5.55 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
//
// 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;
}