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
#include "gfCTutorialProgressionComponent.h"

#include <fsCore/fsIFile.h>
#include <fsCore/src/fsCAppSettings.h>

#include <fsAppCore/fsIInputManager.h>
#include <fsAppCore/src/fsCEntityFactory.h>
#include <fsAppCore/src/fsCLocalisedStrings.h>
#include <fsAppCore/src/fsILevelBasedAppCoreMain.h>
#include <fsAppCore/src/fsCAppMessageDispatcher.h>
#include <fsAppCore/src/components/fsCNameComponent.h>
#include <fsRenderer/src/components/fsITextSpriteComponent.h>

#include <gfComponents/gfIPickListEntryGui.h>
#include <gfComponents/components/gfCHotComponent.h>

#include <gfGameCore/components/gfCTutorialParticleClusterComponent.h>

#include "gui/pickList/gfCPickListGui.h"
#include "../src/gfCHogUserSavedData.h"


const fsCUniqueId gfCTutorialProgressionComponent::mTypeId("fsCTutorialProgressionComponent");


void gfCTutorialProgressionComponent::serialiseDo()
{
	fsIMessageListenerComponent::serialiseDo();
	fsStr state;
	for (auto command : mCommands)
	{
		state += command.mRun ? "1 " : + "0 ";<--- Consider using std::accumulate algorithm instead of a raw loop.
	}
	fsCAppCoreMain::instanceGet()->settingsGet()->strSet(gfCHogUserSavedData::mTutorialState, state);
}

void gfCTutorialProgressionComponent::deserialiseDo()
{
	fsIMessageListenerComponent::deserialiseDo();
	fsStr state = fsCAppCoreMain::instanceGet()->settingsGet()->strGet(gfCHogUserSavedData::mTutorialState);
	for (auto& command : mCommands)
	{
		command.mRun = "1" == state.chomp(" ");
	}
}

fsBool gfCTutorialProgressionComponent::clickOnMe(fsCEntity* const pTarget) const
{
	return !mCommandActive || (mTargetEntity == pTarget);
}

void gfCTutorialProgressionComponent::listenersRegisterDo()
{
	fsIMessageListenerComponent::listenersRegisterDo();
	fsIInputManager::instanceGet()->listenerRegister(fsIInputManager::mPointerDownMsg, this);
	fsIInputManager::instanceGet()->listenerRegister(fsIInputManager::mPointerMoveMsg, this);
	fsIInputManager::instanceGet()->listenerRegister(fsIInputManager::mPointerUpMsg, this);
	fsCAppMessageDispatcher::instanceGet()->listenerRegister(gfICollectableComponent::mCollectionStartMsg, this);
}

void gfCTutorialProgressionComponent::listenersDeregisterDo()
{
	fsIMessageListenerComponent::listenersDeregisterDo();
	fsIInputManager::instanceGet()->listenerDeregister(fsIInputManager::mPointerDownMsg, this);
	fsIInputManager::instanceGet()->listenerDeregister(fsIInputManager::mPointerMoveMsg, this);
	fsIInputManager::instanceGet()->listenerDeregister(fsIInputManager::mPointerUpMsg, this);
	fsCAppMessageDispatcher::instanceGet()->listenerDeregister(gfICollectableComponent::mCollectionStartMsg, this);
}

void gfCTutorialProgressionComponent::cleanUp()
{
	if (parentGet()->componentGet<gfCTutorialPartcileClusterComponent>())
	{
		parentGet()->componentDestroy(gfCTutorialPartcileClusterComponent::mTypeId);
	}
	tutorialDialogueDestroy();
}

void gfCTutorialProgressionComponent::clearTutorial(const fsCEntity* const pEntity)
{
	fsStr prefix = fsCNameComponent::get(pEntity)->nameSansSuffixGet();
	prefix.erase(prefix.find("-"));
	for (auto& command : mCommands)
	{
		if (command.mTargetEntityName == prefix)
		{
			command.mRun = true;
		}
	}
}

fsBool gfCTutorialProgressionComponent::messageProcessDo(const fsCUniqueId& pMessageType,
                                                         const fsIMessageDispatcher::sMessageParameters& pParam)
{
	if (pMessageType == gfICollectableComponent::mCollectionStartMsg)
	{
		clearTutorial(static_cast<const gfICollectableComponent::sEntityCollectedParam&>(pParam).mEnt);
	}
	else
	{
		if (!mCommandActive)
		{
			if (pMessageType == fsIInputManager::mPointerUpMsg)
			{
				mRunningTime = 0;
			}
		}
		else
		{
			if (mTargetEntity)
			{
				if (pMessageType == fsIInputManager::mPointerUpMsg)
				{
					const fsBool clickedOn = mTargetEntity->firstComponentOfFamilyType<fsIMessageListenerComponent>()->
					                                        messageProcess(pMessageType, pParam);
					if (clickedOn)
					{
						mCommandActive = false;
						mCommands[mCurrentCommand].mRun = true;
						++mCurrentCommand;
						fsIInputManager::instanceGet()->priorityListenerRegister(this);

						cleanUp();
					}
					return true;
				}
				if (pMessageType == fsIInputManager::mPointerDownMsg
					|| pMessageType == fsIInputManager::mPointerMoveMsg)
				{
					mTargetEntity->firstComponentOfFamilyType<fsIMessageListenerComponent>()->messageProcess(
						pMessageType, pParam);
					fsIInputManager::instanceGet()->priorityListenerRegister(this);
					return true;
				}
			}
		}
	}
	return false;
}

void gfCTutorialProgressionComponent::particleEffectCreate(fsCEntity* const pTutorialEntity,
                                                           fsCEntity* const pTutorialDialogueEntity)
{
	auto* parEmitComp = parentGet()->componentGet<gfCTutorialPartcileClusterComponent>();

	if (!parEmitComp)
	{
		parEmitComp = parentGet()->componentAdd<gfCTutorialPartcileClusterComponent>();
	}

	fsCEntity* const dialogueIcon = pTutorialDialogueEntity ? pTutorialDialogueEntity->childFind("tutorialDialogueIcon") : nullptr;
	parEmitComp->init(pTutorialEntity, dialogueIcon);
}

void gfCTutorialProgressionComponent::tutorialDialogueDestroy() const
{
	if (fsCEntity* const tutorialDialogueEnt = parentGet()->childFind("tutorialDialogueGui"))
	{
		tutorialDialogueEnt->destroy();
	}
}

fsCEntity* const gfCTutorialProgressionComponent::tutorialDialogueCreate() const
{
	fsStr dialogueLayout("gui/tutorialDialogue/tutorialDialogue.txt");

	tutorialDialogueDestroy();

	fsCEntity* const dialogueEnt = fsIGuiComponent::guiCreateFromFile(fsCResourceName(dialogueLayout,
		                                                                  fsCResourceName::eRES_LOCATION_ASSETS),
	                                                                  parentGet()->childFind("dialogueRoot"), false);

	return dialogueEnt;
}

fsCEntity* const gfCTutorialProgressionComponent::tutorialTextSet(const fsStr pTutorialHint) const
{
	fsCEntity* const dialogueEnt = tutorialDialogueCreate();

	fsCEntity* txtEnt = dialogueEnt->childFind("tutorialDialogueTextBox");
	fsITextSpriteComponent* const txt = txtEnt->textComponentGet();
	txt->textSet(fsCLocalisedStrings::localisedStringGet(pTutorialHint));

	return dialogueEnt;
}

fsCEntity* const gfCTutorialProgressionComponent::itemTypeAlwaysCollectable(const fsStr& pItemType) const
{
	if ("HOT" != pItemType)
	{
		return nullptr;
	}

	auto generalSceneEnts = fsILevelBasedAppCoreMain::instanceGet()->sceneGet()->entitiesWithComponentsOfFamilyType<gfCHotComponent>();
	for (fsCEntity* const ent : generalSceneEnts)
	{
		if (ent->visibleGet())
		{<--- Consider using std::find_if algorithm instead of a raw loop.
			return ent;
		}
	}
	return nullptr;
}

fsCEntity* const gfCTutorialProgressionComponent::targetEntityGet()
{
	fsS32 lastCommand = mCurrentCommand;
	while (mCurrentCommand < static_cast<fsS32>(mCommands.size()))
	{
		const fsSTutorial& currCommand = mCommands[mCurrentCommand];
		if (currCommand.mRun)
		{
			++mCurrentCommand;
			continue;
		}
		if (currCommand.mType == fsSTutorial::eOBJECT)
		{
			fsStr target = currCommand.mTargetEntityName;
			//			target.erase(0, target.find(' ') + 1);

			auto entityOfType = [&target](gfIPickListEntryGui* const listEntry)
			{
				if (listEntry->isActiveGet())
				{
					auto ent = listEntry->firstEntityGet();
					fsStr prefix = fsCNameComponent::get(ent)->nameSansSuffixGet();
					prefix.erase(prefix.find("-"));
					return (prefix == target);
				}
				return false;
			};

			if (auto* const nonListEnt = itemTypeAlwaysCollectable(target))
			{
				return nonListEnt;
			}

			std::vector<fsStr> pickLists = {"hogPickList", "bonusPickList", "recyclePickList"};
			for (auto pickList : pickLists)
			{
				auto shoppingList = fsILevelBasedAppCoreMain::instanceGet()->sceneGet()->entityNamed(pickList)
				                                                           ->componentGet<gfCPickListGui>()->
				                                                           entriesGet();

				auto targetEnt = (std::find_if(shoppingList.begin(), shoppingList.end(), entityOfType));
				if (shoppingList.end() != targetEnt)
				{
					return (*targetEnt)->firstEntityGet();
				}
			}
		}
		else if (currCommand.mType == fsSTutorial::eBUTTON)
		{
			fsStr target = currCommand.mTargetEntityName;
			target.erase(0, target.find(' ') + 1);
			return static_cast<fsCEntity*>(fsILevelBasedAppCoreMain::instanceGet()->sceneGet()->childFind(target));
		}
		++mCurrentCommand;
	}
	mCurrentCommand = lastCommand;
	return nullptr;
}

void gfCTutorialProgressionComponent::nextTutorialActivate()
{
	if ((mTargetEntity = targetEntityGet()))
	{
		fsCEntity* const dialogueEnt = tutorialTextSet(mCommands[mCurrentCommand].mHint);
		particleEffectCreate(mTargetEntity, dialogueEnt);
	}
	else
	{
		tutorialDialogueDestroy();
	}
}

fsIComponent* gfCTutorialProgressionComponent::create(fsCEntity* pParent)
{
	pParent->childAdd(pParent->entityFactoryGet()->emptyEntityCreate("dialogueRoot"));

	gfCTutorialProgressionComponent* const tutComp = new gfCTutorialProgressionComponent(pParent);

	auto commands = fsIFile::strGetFromFile(fsCResourceName("tutorial.txt", fsCResourceName::eRES_LOCATION_ASSETS));
	while (!commands.emptyGet())
	{
		auto command = commands.chomp("\r\n");
		auto hint = commands.chomp("\r\n");
		tutComp->mCommands.push_back(fsSTutorial(command, hint));
	}

	return tutComp;
}

void gfCTutorialProgressionComponent::updateDo(fsS32 pDeltaMs)
{
	if (!mCommandActive)
	{
		auto blockingEntity = fsILevelBasedAppCoreMain::instanceGet()->sceneGet()->entityNamed("conversationPanel");
		if (!blockingEntity)
		{
			mRunningTime += pDeltaMs;
			if (mRunningTime > 5000)
			{
				nextTutorialActivate();
				mRunningTime = 0;
				mCommandActive = mTargetEntity != nullptr;
			}
		}
	}
}

gfCTutorialProgressionComponent::gfCTutorialProgressionComponent(fsCEntity* const pParent):
	fsIMessageListenerComponent(pParent, ePRIORITY_TEXT_ENTRY),
	mCurrentCommand(0),
	mCommandActive(false),
	mRunningTime(0),
	mTargetEntity(nullptr)
{
}

gfCTutorialProgressionComponent::~gfCTutorialProgressionComponent()
{
}

gfCTutorialProgressionComponent::fsSTutorial::fsSTutorial(const fsStr& pCommand, const fsStr& pHint) :
	mHint(pHint),
	mTargetEntityName(pCommand),
	mType(fsStr::npos != pCommand.find("object ") ? mType = eOBJECT : mType = eBUTTON),
	mRun(false)
{
	mTargetEntityName.erase(0, mTargetEntityName.find(' ') + 1);
}