ActionSequenceHandler.cpp
5.4 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
//
// ActionSequenceHandler.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 29.06.2017.
//
//
#include <stdio.h>
#include "ActionSequenceHandler.h"
#include "ResourceUtilities.h"
#include "ValueStorage.h"
ActionSequenceHandler::ActionSequenceHandler(ScenarioObject* 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;
}
ActionSequenceHandler::~ActionSequenceHandler()
{
delete _JSONDataStorage;
delete _actions;
}
void ActionSequenceHandler::runNext()
{
auto i = _lastActionIndex+1;
auto actionsArray = _actions->GetArray();
if(i >= actionsArray.Size()){
return;
}
const auto& currentAction = actionsArray[i];
auto parsedAction = ActionParser::getInstance().parseJSONAction(currentAction, this);
_lastActionIndex = i;
if(parsedAction){
this->runAction(parsedAction);
} else {
this->actionFinished(currentAction);
}
_currentAction = parsedAction;
}
void ActionSequenceHandler::reset()
{
_lastActionIndex = -1;
_currentAction = nullptr;
}
void ActionSequenceHandler::stop()
{
if(_currentAction){
auto containerNode = dynamic_cast<cocos2d::Node*>(_containerNode);
if(containerNode){
containerNode->stopAction(_currentAction);
}
}
ActionParser::getInstance().cleanUp(this);
this->reset();
}
// methods for parseDelegate
ScenarioObject* ActionSequenceHandler::getObjectByName (std::string objectName)
{
return _containerNode->getScenarioObjectByName(objectName);
}
ScenarioObject* ActionSequenceHandler::getDefaultObjectForAction(std::string actionType)
{
return dynamic_cast<ScenarioObject*>(_containerNode);
}
ScenarioObject* ActionSequenceHandler::getDefaultObjectForConditionCheck()
{
return dynamic_cast<ScenarioObject*>(_containerNode);
}
void ActionSequenceHandler::addNewObject(std::string objectName, ScenarioObject* newObject)
{
// do nothing
}
std::string ActionSequenceHandler::getDefaultSoundsPath()
{
return ResourceUtilities::getInstance().getDownloadedResourcesPath(false);
}
std::string ActionSequenceHandler::getAlternativeSoundsPath()
{
return ResourceUtilities::getInstance().getDownloadedResourcesPath(false);
}
std::string ActionSequenceHandler::getValueStorageContainerName()
{
return _valueStorageContainerName;
}
void ActionSequenceHandler::runAction(cocos2d::Action* action)
{
auto containerNode = dynamic_cast<cocos2d::Node*>(_containerNode);
if(containerNode){
containerNode->runAction(action);
}
}
void ActionSequenceHandler::actionFinished(const rapidjson::Value& jsonActionObject)
{
if(_lastActionIndex < _actions->GetArray().Size() - 1){
this->runNext();
} else {
_onLastActionFinished();
ActionParser::getInstance().cleanUp(this);
}
}
void ActionSequenceHandler::runInstantly(std::function<void()> actionFunction)
{
actionFunction();
}
void ActionSequenceHandler::runCompletingAction(cocos2d::Action* action)
{
// do nothing
}
void ActionSequenceHandler::cancelAllCompletingActions()
{
// do nothing
}
void ActionSequenceHandler::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 ActionSequenceHandler::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 ActionSequenceHandler::unschedule(std::string key)
{
auto containerNode = dynamic_cast<cocos2d::Node*>(_containerNode);
if(containerNode){
containerNode->unschedule(key);
}
}
void ActionSequenceHandler::newTouchHandler(std::string key, TouchHandlerFunction touchHandler, TouchHandlerType touchType)
{
// do nothing
}
void ActionSequenceHandler::removeTouchHandler(std::string key, TouchHandlerType touchHandlerType)
{
// do nothing
}
long long ActionSequenceHandler::getLastScreenTouchTime()
{
return 0;
}
int ActionSequenceHandler::getLoopActionCounter(std::string loopId)
{
return 0;
}
int ActionSequenceHandler::addNewLoopActionCounter(std::string loopId, int timesRepeated)
{
return 0;
}
int ActionSequenceHandler::decrementLoopActionCounter(std::string loopId)
{
return 0;
}
void ActionSequenceHandler::deleteLoopActionCounter(std::string loopId)
{
// do nothing
}
void ActionSequenceHandler::setLastActionIndex(int index)
{
_lastActionIndex = index;
}
void ActionSequenceHandler::storeSoundId(std::string soundPath, unsigned int newSoundId)
{
// do nothing
}
unsigned int ActionSequenceHandler::removeStoredSoundId(std::string soundPath)
{
return -1;
}