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 | #include "gfCLockComponent.h"
#include <fsAppCore/src/components/gui/fsCNotificationsGui.h>
#include <fsRenderer/src/components/fsCSpriteComponent.h>
#include <fsAppCore/src/components/fsCNameComponent.h>
#include <fsAppCore/src/fsCLocalisedStrings.h>
#include <fsAppCore/src/fsCAppMessageDispatcher.h>
#include <fsAppCore/src/fsILevelBasedAppCoreMain.h>
#include "gfCTaskComponent.h"
#include "gfCGhostSpriteComponent.h"
const fsCUniqueId gfCLockComponent::mTypeId("fsCLockComponent");
const fsStr gfCLockComponent::mEntityPrefix("PRE");
const fsStr gfCLockComponent::mInOrderPrefix("ORD");
void gfCLockComponent::listenersRegisterDo()
{
gfCTappableCollectableComponent::listenersRegisterDo();
fsCAppMessageDispatcher::instanceGet()->listenerRegister(fsILevelBasedAppCoreMain::mLevelEntitiesCreatedMsg, this);
fsCAppMessageDispatcher::instanceGet()->listenerRegister(fsILevelBasedAppCoreMain::mLevelPostResetMsg, this);
}
void gfCLockComponent::listenersDeregisterDo()
{
gfCTappableCollectableComponent::listenersDeregisterDo();
fsCAppMessageDispatcher::instanceGet()->listenerDeregister(fsILevelBasedAppCoreMain::mLevelEntitiesCreatedMsg, this);
fsCAppMessageDispatcher::instanceGet()->listenerDeregister(fsILevelBasedAppCoreMain::mLevelPostResetMsg, this);
}
fsBool gfCLockComponent::messageProcessDo(const fsCUniqueId& pMessageType,
const fsIMessageDispatcher::sMessageParameters& pParam)
{
if (pMessageType == fsILevelBasedAppCoreMain::mLevelEntitiesCreatedMsg ||
pMessageType == fsILevelBasedAppCoreMain::mLevelPostResetMsg)
{
activate();
return false;
}
return gfCTappableCollectableComponent::messageProcessDo(pMessageType, pParam);
}
void gfCLockComponent::midCollectionHandle()
{
if (eCOLLECTION_STATE::eCOLLECTED_EFFECT_PLAYING == stateGet())
{
stateSet(eCOLLECTION_STATE::eCOLLECTIBLE);
gfICollectableComponent::collectionProcessTriggerDo();
}
}
void gfCLockComponent::serialiseDo()
{
gfCTappableCollectableComponent::serialiseDo();
parentGet()->boolSerialise(mInOrderLock);
lockKeysStateSave();
}
void gfCLockComponent::lockKeysStateSave()
{
for (auto& lockKey : mKeys)
{
parentGet()->boolSerialise(lockKey.second);
}
}
void gfCLockComponent::deserialiseDo()
{
gfCTappableCollectableComponent::deserialiseDo();
mInOrderLock = parentGet()->boolDeserialise();
lockKeysStateLoad();
}
void gfCLockComponent::lockKeysStateLoad()
{
for (auto& lockKey : mKeys)
{
lockKey.second = parentGet()->boolDeserialise();
}
}
void gfCLockComponent::smash()
{
fsBool smashed = false;
for (auto& key : mKeys)
{
if (!key.second)
{
smashed = true;
key.second = true;
}
}
if (smashed)
{
gfICollectableComponent::collectionProcessTriggerDo();
}
}
void gfCLockComponent::deactivateDo()
{
gfCTappableCollectableComponent::deactivateDo();
for (auto& key : mKeys)
{
key.second = false;
}
}
void gfCLockComponent::collectionProcessTriggerDo()
{
fsStr hintTextKey = "HINT-";
for (auto key : mKeys)
{
if (key.second == false)
{
hintTextKey += key.first + "-";<--- Consider using std::accumulate algorithm instead of a raw loop.
}
}
if (!mTaskName.emptyGet())
{
hintTextKey += mTaskName + "+";
}
hintTextKey += fsCNameComponent::get(parentGet())->nameSansPrefixGet();
fsStr displayString;
if (fsCLocalisedStrings::localisedStringGetIfExists(hintTextKey, displayString))
{
fsCNotificationsGui::instanceGet()->notificationAddToQueue(displayString);
}
else
{
hintTextKey.replaceAll("HINT-", "hint-");
if (fsCLocalisedStrings::localisedStringGetIfExists(hintTextKey, displayString))
{
fsCNotificationsGui::instanceGet()->notificationAddToQueue(displayString);
}
}
}
void gfCLockComponent::onCollectionEffectCompleteDo()
{
if (!mTaskName.emptyGet())
{
fsCAppMessageDispatcher::instanceGet()->messageDispatch(gfCTaskComponent::mTaskCompleteMsg,
fsIMessageDispatcher::sStrMessageParam(mTaskName));
}
else
{
gfICollectableComponent::onCollectionEffectCompleteDo();
}
}
void gfCLockComponent::initialiseFromName()
{
fsStr entName = fsCNameComponent::get(parentGet())->nameSansSuffixGet();
entName.erase(0, mEntityPrefix.dataSizeGet() + 1);
if (0 == entName.find(mInOrderPrefix))
{
entName.erase(0, mEntityPrefix.dataSizeGet() + 1);
mInOrderLock = true;
}
while (fsStr::npos != entName.find("-"))
{
const fsStr keyName = entName.chomp("-");
mKeys.push_back(std::pair<fsStr, fsBool>(keyName, false));
}
if (fsStr::npos != entName.find(fsCNameComponent::mTaskChar))
{
mTaskName = entName.chomp("+");
hintStringExistsCheck();
}
fsAssert(mKeys.empty() == false, "Expected lock %s to require keys to unlock.\n", parentGet()->nameGet().utf8Get());
}
void gfCLockComponent::hintStringExistsCheck() const
{
#if defined fsDEBUG
fsStr hintTextKey = fsCNameComponent::get(parentGet())->nameSansSuffixGet();
hintTextKey.erase(0, hintTextKey.find('-') + 1);
hintTextKey = "HINT-" + hintTextKey;
fsStr displayString;
if (!fsCLocalisedStrings::localisedStringGetIfExists(hintTextKey, displayString))
{
hintTextKey.replaceAll("HINT-", "hint-");
if (!fsCLocalisedStrings::localisedStringGetIfExists(hintTextKey, displayString))
{
displayString = "Missing string entry for " + hintTextKey;
fsCNotificationsGui::instanceGet()->notificationAddToQueue(displayString);
}
}
#endif
}
fsBool gfCLockComponent::keyOver(fsCEntity* const pDragDropEnt)
{
return parentGet()->spriteComponentGet()->hitTest(pDragDropEnt->spriteComponentGet(), true);
}
fsBool gfCLockComponent::keyUsable(fsCEntity* const pKeyEnt)
{
auto keyIt = keyLocate(fsCNameComponent::get(pKeyEnt)->nameSansPrefixGet());
return mKeys.end() != keyIt && keyIt->second == false;
}
fsBool gfCLockComponent::keyConsumable(fsCEntity* const pKeyEnt, fsCEntity* const pDragDropEnt)
{
return keyUsable(pKeyEnt) && keyOver(pDragDropEnt);
}
fsBool gfCLockComponent::keyConsumed(fsCEntity* const pKeyEnt, fsCEntity* const pDragDropEnt)
{
if (keyConsumable(pKeyEnt, pDragDropEnt))
{
auto keyIt = keyLocate(fsCNameComponent::get(pKeyEnt)->nameSansPrefixGet());
keyIt->second = true;
if (!lockedGet())
{
gfICollectableComponent::collectionProcessTriggerDo();
}
return true;
}
return false;
}
std::vector<std::pair<fsStr, fsBool>>::iterator gfCLockComponent::keyLocate(const fsStr& pKeyName)
{
if (mInOrderLock)
{
auto firstUnusedKey = std::find_if(mKeys.begin(), mKeys.end(),
[](std::pair<fsStr, fsBool> keyIt)
{
return keyIt.second == false;
});
if (firstUnusedKey != mKeys.end() && firstUnusedKey->first == pKeyName)
{
return firstUnusedKey;
}
}
else
{
for (auto keyIt = mKeys.begin(); keyIt != mKeys.end(); ++keyIt)
{
if (keyIt->first == pKeyName)
{<--- Consider using std::find_if algorithm instead of a raw loop.
return keyIt;
}
}
}
return mKeys.end();
}
fsBool gfCLockComponent::lockedGet() const
{
for (auto key : mKeys)<--- Consider using std::any_of algorithm instead of a raw loop.
{
if (false == key.second)
{
return true;
}
}
return false;
}
fsIComponent* gfCLockComponent::create(fsCEntity* pParent)
{
gfCLockComponent* const lockComp = new gfCLockComponent(pParent);
gfCGhostSpriteComponent* ghostSprComp = pParent->componentAdd<gfCGhostSpriteComponent>();
ghostSprComp->parentEntityInteractiveSet(true);
lockComp->mGhostSpriteComp = ghostSprComp;
lockComp->initialiseFromName();
return lockComp;
}
gfCLockComponent::gfCLockComponent(fsCEntity* const pParent) :
gfCTappableCollectableComponent(pParent),
mInOrderLock(false)
{
}
gfCLockComponent::~gfCLockComponent()
{
}
|