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

#include "gfCFlipsawMinigameComponent.h"

#include <fsCore/src/fsNRnd.h>
#include <fsCore/src/fsCAppSettings.h>
#include <fsCore/src/debug/fsAssert.h>

#include <fsAudio/fsIAudioManager.h>
#include <fsRenderer/src/components/fsCSpriteComponent.h>
#include <fsRenderer/src/fsCBrushManager.h>
#include <fsRenderer/src/fsCBrush.h>
#include <fsAppCore/src/fsCAppMessageDispatcher.h>

#include "gfCFlipsawMiniGameLogicComponent.h"
#include "../gfIStencil.h"


const fsCUniqueId gfCFlipsawMinigameComponent::mTypeId("gfCFlipsawMinigameComponent");
const fsCUniqueId gfCFlipsawMinigameComponent::mFlipsawPieceSelectedMsg("flipsawPieceSelected");
const fsCUniqueId gfCFlipsawMinigameComponent::mFlipsawAnotherPieceSelectedMsg("flipsawAnotherPieceSelected");
const fsCUniqueId gfCFlipsawMinigameComponent::mFlipsawClearHintTintMsg("flipsawClearTint");
const fsStr gfCFlipsawMinigameComponent::mFlipsawPrefix("FLP");

fsSColour4B gfCFlipsawMinigameComponent::mWrongColourTint(fsSColour4B::fsWHITE());
fsSColour4B gfCFlipsawMinigameComponent::mSelectedColourTint(200, 0, 200, 255);
fsSColour4B gfCFlipsawMinigameComponent::mHintColourTint(0, 200, 0, 255);
fsBool gfCFlipsawMinigameComponent::mLockHomePos(false);


void gfCFlipsawMinigameComponent::gameLogicSet(gfCFlipsawMiniGameLogicComponent* const pParentLogic)
{
	mGameLogic = pParentLogic;	
}

fsBool gfCFlipsawMinigameComponent::selected() const
{
	return mSelected;
}

void gfCFlipsawMinigameComponent::hintSwap()
{
	auto siblingEntities = fsCAppCoreMain::instanceGet()->sceneGet()->entitiesWithComponentsOfFamilyType<gfCFlipsawMinigameComponent>();
	auto home = std::find_if(std::begin(siblingEntities), std::end(siblingEntities),
		[this](fsCEntity const* ent)
		{
			return fsPoint{ent->componentGet<gfCFlipsawMinigameComponent>()->parentGet()->posGet()}
					== mHomePosition;
		});

	auto* sprComp = parentGet()->spriteComponentGet();
	if (!mSelected)
	{
		sprComp->colourSet(mHintColourTint);
		mHinted = true;
	}
	
	sprComp = (*home)->spriteComponentGet();
	sprComp->colourSet(mHintColourTint);
	(*home)->componentGet<gfCFlipsawMinigameComponent>()->mHinted = true;
}

void gfCFlipsawMinigameComponent::cut(std::shared_ptr<fsCBrush> pTexture, gfIStencil* const pStencil)
{
	std::shared_ptr<fsCBrush> brush = fsCBrushManager::instanceGet()->getNoCache(
		pStencil->textureGet()->dimensionsGet().x, pStencil->textureGet()->dimensionsGet().y);
	brush->textureCopyThroughStencil(pTexture, pStencil->textureGet(), pStencil->sourceTextureCutXGet(),
	                                 pStencil->sourceTextureCutYGet());

	fsISpriteComponent* const sc = parentGet()->spriteComponentGet();
	sc->brushSet(brush);
}

void gfCFlipsawMinigameComponent::listenersDeregisterDo()
{
	gfCRotateMinigameComponent::listenersDeregisterDo();
	fsCAppMessageDispatcher::instanceGet()->listenerDeregister(mFlipsawPieceSelectedMsg,
	                                                           static_cast<gfIMinigameComponent*>(this));
	fsCAppMessageDispatcher::instanceGet()->listenerDeregister(mFlipsawAnotherPieceSelectedMsg,
	                                                           static_cast<gfIMinigameComponent*>(this));
	fsCAppMessageDispatcher::instanceGet()->listenerDeregister(mFlipsawClearHintTintMsg,
															   static_cast<gfIMinigameComponent*>(this));
	//	fsIInputManager::instanceGet()->listenerDeregister( fsIInputManager::mPointerDownMsg, this );		// deregistered up in gesture component
	//	fsIInputManager::instanceGet()->listenerDeregister( fsIInputManager::mPointerUpMsg, this );		// deregistered up in gesture component
}

void gfCFlipsawMinigameComponent::listenersRegisterDo()
{
	gfCRotateMinigameComponent::listenersRegisterDo();
	fsCAppMessageDispatcher::instanceGet()->listenerRegister(mFlipsawPieceSelectedMsg,
	                                                         static_cast<gfIMinigameComponent*>(this));
	fsCAppMessageDispatcher::instanceGet()->listenerRegister(mFlipsawAnotherPieceSelectedMsg,
	                                                         static_cast<gfIMinigameComponent*>(this));
	fsCAppMessageDispatcher::instanceGet()->listenerRegister(mFlipsawClearHintTintMsg,
															 static_cast<gfIMinigameComponent*>(this));
	//	fsIInputManager::instanceGet()->listenerRegister( fsIInputManager::mPointerDownMsg, this );		// deregistered up in gesture component
	//	fsIInputManager::instanceGet()->listenerRegister( fsIInputManager::mPointerUpMsg, this );		// deregistered up in gesture component
}

fsBool gfCFlipsawMinigameComponent::messageProcessDo(const fsCUniqueId& pMessageType,
                                                     const fsIMessageDispatcher::sMessageParameters& pParam)
{
	if (mFlipsawPieceSelectedMsg == pMessageType && mSelected)
	{
		const gfCFlipsawMinigameComponent* const otherFlipComp = static_cast<const sFlipsawEventMsgParam&>(pParam).
			mFlipComp;
		positionsSwap(otherFlipComp);
		homeCheck(true);
		return true;
	}
	if (mFlipsawAnotherPieceSelectedMsg == pMessageType)
	{
		return mSelected;
	}
	if (mFlipsawClearHintTintMsg == pMessageType)
	{
		if (mHinted)
		{
			tintSet();
			mHinted = false;
		}
	}
	if (fsIInputManager::mPointerUpMsg == pMessageType)
	{
		if (!mLockHomePos || fsPoint{parentGet()->posGet()} != mHomePosition)
		{
			const auto& param = static_cast<const fsIInputManager::sPointerMsgParam&>(pParam);
			if (hitTest(param.mPos))
			{
				const fsBool anotherSelected = fsCAppMessageDispatcher::instanceGet()->messageDispatch(
					mFlipsawAnotherPieceSelectedMsg, fsIMessageDispatcher::mNullParam);

				if (mNumRotSteps && !anotherSelected && mRunningTime - mMouseDownTime < 300)
				{
					rotate();
				}
				else
				{
					flip();
				}
				return true;
			}
		}
	}
	return (gfCRotateMinigameComponent::messageProcessDo(pMessageType, pParam));
}

void gfCFlipsawMinigameComponent::flip()
{
	if (mSelected)
	{
		mSelected = false;
	}
	else
	{
		sFlipsawEventMsgParam msgParam(this);
		if (fsCAppMessageDispatcher::instanceGet()->messageDispatch(mFlipsawPieceSelectedMsg, msgParam))
		{
			homeCheck(true);
		}
		else
		{
			mSelected = true;
		}
		if (!mHinted)
		{
			fsCAppMessageDispatcher::instanceGet()->messageDispatch(mFlipsawClearHintTintMsg, msgParam);
		}

	}
	tintSet();
}

void gfCFlipsawMinigameComponent::serialiseDo()
{
	parentGet()->s32Serialise(mNumRotSteps);
	parentGet()->pointSerialise(parentGet()->posGet());

	if (mNumRotSteps)
	{
		gfCRotateMinigameComponent::serialiseDo();
	}
}

void gfCFlipsawMinigameComponent::deserialiseDo()
{
	mNumRotSteps = parentGet()->s32Deserialise();
	parentGet()->posSet(parentGet()->pointDeserialise());

	if (mNumRotSteps)
	{
		gfCRotateMinigameComponent::deserialiseDo();
	}
	else if (homeGet())
	{
		completionStateUpdate(true, false);
	}
	tintSet();
}

void gfCFlipsawMinigameComponent::resetDo()
{
	mInitialised = false;
	mSelected = false;
}

void gfCFlipsawMinigameComponent::homePositionStore()
{
	mHomePosition = parentGet()->posGet();
}

void gfCFlipsawMinigameComponent::initialiseDo(const fsCVariableTable& pVars)
{
	mNumRotSteps = pVars.s32GetWithDefault("stepsFLP", 0);

	if (mNumRotSteps)
	{
		gfCRotateMinigameComponent::initialiseDo(pVars);
	}
	else
	{
		mLockHomeRot = true;
	}

	mLockHomePos = 1 == pVars.s32GetWithDefault("homePosLock", 0);

	if (!mInitialised)
	{
		mWrongColourTint = {pVars.strGetWithDefault("wrongColTint", "ffffffff")};
		mSelectedColourTint = {pVars.strGetWithDefault("selectedColTint", "cd00cdff")};
		mHintColourTint = {pVars.strGetWithDefault("selectedColTint", "00c800ff")};

		auto siblingEntities = mGameLogic->gamePiecesGet();

		for (auto ent : siblingEntities)
		{
			ent->homePositionStore();
		}

		auto s = mGameLogic->gamePiecesGet();
		positionsRandomise(s);

		for (auto ent : siblingEntities)
		{
			ent->initialisationFinalise();
		}
	}

	if (mNumRotSteps)
	{
		gfCRotateMinigameComponent::initialiseDo(pVars);
	}
}

void gfCFlipsawMinigameComponent::initialisationFinalise()
{
	tintSet();
	mInitialised = true;
}

void gfCFlipsawMinigameComponent::positionsRandomise(const std::vector<gfCFlipsawMinigameComponent*>& pFlipEnts)
{
	const fsS32 numFlipComps = static_cast<fsS32>(pFlipEnts.size());

	fsBool needToShuffle = true;
	while (needToShuffle)
	{
		for (fsS32 shuff = 0; shuff < numFlipComps; ++shuff)
		{
			const fsS32 firstInd = fsNRnd::uInt32GetWithLimit(numFlipComps);
			const fsS32 secondInd = fsNRnd::uInt32GetWithLimit(numFlipComps);
			pFlipEnts[firstInd]->positionsSwap(pFlipEnts[secondInd]);
		}

		needToShuffle = false;
		for (auto* const flipComp : pFlipEnts)
		{
			if (fsPoint{flipComp->parentGet()->posGet()} == flipComp->mHomePosition)
			{
				needToShuffle = true;
				break;
			}
		}
	}
}

void gfCFlipsawMinigameComponent::positionsSwap(const gfCFlipsawMinigameComponent* const pOther) const
{
	if(mGameLogic == pOther->mGameLogic)
	{
		const fsPoint firstPos = parentGet()->posGet();
		parentGet()->posSet(pOther->parentGet()->posGet());
		pOther->parentGet()->posSet(firstPos);
	}
}

void gfCFlipsawMinigameComponent::skipDo()
{
	gfCRotateMinigameComponent::skipDo();
	parentGet()->posSet(mHomePosition);
	mSelected = false;
	tintSet();
	//	completionStateUpdate( true, false );
}

void gfCFlipsawMinigameComponent::tintSet() const
{
	auto* const sprComp = parentGet()->spriteComponentGet();
	fsAssert(sprComp != nullptr, "Expected a sprite component on a Flipsaw entity.\n");
	if (mSelected)
	{
		sprComp->colourSet(mSelectedColourTint);
	}
	else if (fsPoint{parentGet()->posGet()} == mHomePosition)
	{
		sprComp->colourSet(fsSColour4B::fsWHITE());
	}
	else
	{
		sprComp->colourSet(mWrongColourTint);
	}
}

fsBool gfCFlipsawMinigameComponent::homeGet() const
{
	return fsPoint{parentGet()->posGet()} == mHomePosition && gfCRotateMinigameComponent::homeGet();
}

fsBool gfCFlipsawMinigameComponent::fullHomeLock() const
{
	return mLockHomePos || gfCRotateMinigameComponent::fullHomeLock();
}

void gfCFlipsawMinigameComponent::homeCheck(fsBool pPlayEffect)
{
	mSelected = false;
	if (fullHomeLock() && homeGet())
	{
		completionStateUpdate(true, pPlayEffect);
		fsStr effectName = fsCAppCoreMain::instanceGet()->settingsGet()->strGetWithDefault(
			"sfxFlipsawPlacePiece", fsCAppSettings::kSfxNotFoundDefault);
		fsIAudioManager::instanceGet()->effectPlay(fsCResourceName(effectName, fsCResourceName::eRES_LOCATION_ASSETS));
	}
	tintSet();
}

fsBool gfCFlipsawMinigameComponent::acceptingInputGet() const
{
	return (mHomePosition != parentGet()->posGet());
}

fsIComponent* gfCFlipsawMinigameComponent::create(fsCEntity* pParent)
{
	return new gfCFlipsawMinigameComponent(pParent);
}

gfCFlipsawMinigameComponent::gfCFlipsawMinigameComponent(fsCEntity* const pParent) :
	gfCRotateMinigameComponent(pParent),
	mInitialised(false),
	mSelected(false),
	mHinted(false),
	mGameLogic(nullptr)
{
}

fsBool gfCFlipsawMinigameComponent::isActiveGet() const
{
	return selected();
}

fsBool gfCFlipsawMinigameComponent::atHomePositionGet() const
{
	return fsPoint{parentGet()->posGet()} == mHomePosition;
}

fsCEntity* gfCFlipsawMinigameComponent::homeOccupantGet() const
{
	for (fsCEntity* const ent : fsCAppCoreMain::instanceGet()->sceneGet()->
		entitiesWithComponentsOfFamilyType<gfCFlipsawMinigameComponent>())
	{
		if (ent != parentGet() && fsPoint{ent->posGet()} == mHomePosition)
		{<--- Consider using std::find_if algorithm instead of a raw loop.
			return ent;
		}
	}
	return nullptr;
}

fsCEntity* gfCFlipsawMinigameComponent::selectedEntGet() const
{
	for (fsCEntity* const ent : fsCAppCoreMain::instanceGet()->sceneGet()->
		entitiesWithComponentsOfFamilyType<gfCFlipsawMinigameComponent>())
	{
		if (ent->componentGet<gfCFlipsawMinigameComponent>()->mSelected)
		{<--- Consider using std::find_if algorithm instead of a raw loop.
			return ent;
		}
	}
	return nullptr;
}

fsBool gfCFlipsawMinigameComponent::pressGet(fsPoint& pAt) const
{
	if (mLevelCompleteWaitingForUserInput || mSelected)
	{
		return false;
	}

	// Input of any kind - blind taps included - can leave a piece selected. A press now
	// completes that swap, so the only one worth making is on whatever sits on its home.
	if (fsCEntity* const selectedEnt = selectedEntGet())
	{
		if (fsPoint{parentGet()->posGet()} != selectedEnt->componentGet<gfCFlipsawMinigameComponent>()->mHomePosition)
		{
			return false;
		}
	}
	else if (atHomePositionGet())
	{
		// A piece in place only needs turning, and a locked one ignores input.
		if (mLockHomePos || !mNumRotSteps || 0 == mCurrRotStep)
		{
			return false;
		}
	}
	// With rotation on, a quick tap turns and only a held press selects, so a
	// misplaced piece cannot be swapped by tapping.
	else if (mNumRotSteps || !homeOccupantGet())
	{
		return false;
	}

	pAt = aimPointGet();
	return true;
}

fsPoint gfCFlipsawMinigameComponent::aimPointGet() const
{
	const fsPoint origin(parentGet()->screenPositionGet());
	auto* const sprComp = parentGet()->spriteComponentGet();
	return sprComp ? hitCentreGet(origin, sprComp->widthGet(), sprComp->heightGet(),
	                              [this](const fsPoint& pPos) { return hitTest(pPos); })
	               : origin;
}

gfIMinigameNextMove::sMove gfCFlipsawMinigameComponent::moveAfterPress(const std::vector<fsCEntity*>&,
                                                                       const fsPoint&) const
{
	sMove move;
	move.mKind = sMove::eKIND_TAP;
	move.mTakenEnt = parentGet();
	move.mPosBefore = fsPoint{parentGet()->posGet()};
	move.mFinal = true;

	if (selectedEntGet())
	{
		// Something is already waiting for this one.
		move.mStateBefore = -1;
		move.mTapFailReason = "a tap on it did not swap it with the piece already selected";
	}
	else if (atHomePositionGet())
	{
		move.mStateBefore = mCurrRotStep;
		move.mTapFailReason = "a tap on it did not rotate it";
	}
	else
	{
		// The first tap selects it; a tap on whatever sits on its home swaps them.
		move.mStateBefore = -1;
		move.mThenTaps.push_back(homeOccupantGet()->componentGet<gfCFlipsawMinigameComponent>()->aimPointGet());
		move.mTapFailReason = "swapping it with the piece on its home did not send it home";
	}
	return move;
}

fsBool gfCFlipsawMinigameComponent::tapTookEffectGet(const sMove& pMove) const
{
	if (pMove.mStateBefore >= 0)
	{
		return mCurrRotStep != pMove.mStateBefore;
	}
	// Selected and swapped, or swapped with one already selected.
	return pMove.mThenTaps.empty() ? fsPoint{parentGet()->posGet()} != pMove.mPosBefore : atHomePositionGet();
}

fsBool gfCFlipsawMinigameComponent::hintDo()
{
	hintSwap();
	return true;
}

gfCFlipsawMinigameComponent::~gfCFlipsawMinigameComponent()
{
}