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

#include "gfCParkPlacementComponent.h"

#include <cmath>

#include <fsCore/fsPoint.h>
#include <fsCore/fsVec2d.h>
#include <fsCore/fsIFile.h>
#include <fsCore/fsSColour.h>
#include <fsCore/src/fsCResourceName.h>

#include <fsAppCore/fsIInputManager.h>
#include <fsAppCore/fsIScene.h>
#include <fsAppCore/src/fsCAppCoreMain.h>
#include <fsAppCore/src/fsCEntity.h>
#include <fsAppCore/src/fsCEntityFactory.h>

#include <fsRenderer/src/fsCBrushManager.h>
#include <fsRenderer/src/components/fsCSpriteComponent.h>

#include <fsTileMap/src/fsCView.h>
#include <fsTileMap/fsITileMap.h>

#include "gfCParkLevelComponent.h"
#include "gfCParkGuestViewComponent.h"
#include "sim/gfCParkDaySim.h"
#include "sim/gfCDayEconomySim.h"

const fsCUniqueId gfCParkPlacementComponent::mTypeId("gfCParkPlacementComponent");

namespace
{
fsS32 clampToRange(fsS32 pValue, fsS32 pMaximum)
{
	if (pValue < 0)
	{
		return 0;
	}
	return pValue > pMaximum ? pMaximum : pValue;
}

fsPoint buildingGridSnap(fsNTileMap::fsCView* const pView, const fsVec2d& pLocalPos,
                         fsS32 pFootprintColumns, fsS32 pFootprintRows)
{
	fsNTileMap::fsITileMap* const map = pView->mMap;
	const fsS32 mapColumns = map->widthGet();
	const fsS32 mapRows = map->heightGet();
	const fsS32 footprintColumns = pFootprintColumns > mapColumns ? mapColumns : pFootprintColumns;
	const fsS32 footprintRows = pFootprintRows > mapRows ? mapRows : pFootprintRows;

	const fsPoint mapTopLeft = pView->mapPixelToLocal(fsPoint(0, 0));
	const fsPoint firstTileBottomRight =
		pView->mapPixelToLocal(fsPoint(map->tileWidthGet(), map->tileHeightGet()));
	const fsF32 tileLocalWidth = static_cast<fsF32>(firstTileBottomRight.x - mapTopLeft.x);
	const fsF32 tileLocalHeight = static_cast<fsF32>(firstTileBottomRight.y - mapTopLeft.y);
	if (tileLocalWidth <= 0.0f || tileLocalHeight <= 0.0f)
	{
		return fsPoint(static_cast<fsS32>(pLocalPos.x), static_cast<fsS32>(pLocalPos.y));
	}

	// Runtime building positions are their bottom-centre contact point. Find the
	// nearest valid top-left grid cell for that anchor, then clamp the entire
	// footprint inside the map.
	const fsF32 desiredColumn =
		(pLocalPos.x - mapTopLeft.x) / tileLocalWidth - footprintColumns * 0.5f;
	const fsF32 desiredRow =
		(pLocalPos.y - mapTopLeft.y) / tileLocalHeight - footprintRows;
	const fsS32 column = clampToRange(
		static_cast<fsS32>(std::floor(desiredColumn + 0.5f)), mapColumns - footprintColumns);
	const fsS32 row = clampToRange(
		static_cast<fsS32>(std::floor(desiredRow + 0.5f)), mapRows - footprintRows);

	const fsS32 anchorMapX =
		column * map->tileWidthGet() + footprintColumns * map->tileWidthGet() / 2;
	const fsS32 anchorMapY = (row + footprintRows) * map->tileHeightGet();
	return pView->mapPixelToLocal(fsPoint(anchorMapX, anchorMapY));
}

fsBool pointerInsideMapGet(fsNTileMap::fsCView* const pView, const fsVec2d& pLocalPos)
{
	const fsPoint topLeft = pView->mapPixelToLocal(fsPoint(0, 0));
	const fsPoint bottomRight = pView->mapPixelToLocal(fsPoint(
		pView->mMap->widthGet() * pView->mMap->tileWidthGet(),
		pView->mMap->heightGet() * pView->mMap->tileHeightGet()));
	return pLocalPos.x >= topLeft.x && pLocalPos.x < bottomRight.x &&
	       pLocalPos.y >= topLeft.y && pLocalPos.y < bottomRight.y;
}

const fsS32 gPreviewDefaultWidth = 64;
const fsS32 gPreviewAlpha = 190;
const fsStr gPreviewEntityName("parkPlacementPreview");

fsBool pointerOverBuildUiGet(fsIScene* const pScene, const fsPoint& pScreenPos)
{
	const char* const buttonNames[] = {
		"shopButton",
		"buildingsCategoryButton",
		"staffCategoryButton",
		"decorationsCategoryButton",
		"backButton",
		"shopMenuScrollLeftButton",
		"shopMenuScrollRightButton",
		"itemButton0",
		"itemButton1",
		"itemButton2",
		"itemButton3",
		"itemButton4",
		"itemButton5",
		"itemButton6",
		"itemButton7"
	};

	for (const char* const buttonName : buttonNames)<--- Consider using std::any_of algorithm instead of a raw loop.
	{
		fsCEntity* const buttonEnt = pScene->childFind(buttonName);
		if (buttonEnt && buttonEnt->visibleGet())
		{
			if (fsISpriteComponent* const sprite = buttonEnt->spriteComponentGet())
			{
				if (sprite->hitTest(pScreenPos, false))
				{
					return true;
				}
			}
		}
	}
	return false;
}
}

void gfCParkPlacementComponent::placementBegin(const fsStr& pDecorationImage, fsS32 pDrawWidth,
                                               fsS32 pFootprintColumns, fsS32 pFootprintRows,
	                                           fsS32 pItemCost, fsS32 pVisitorAppeal,
	                                           fsS32 pVisitorComfort)
{
	placementPreviewDestroy();
	mMode = eMode::eDecoration;
	mDecorationImage = pDecorationImage;
	mDecorationVisitorAppeal = pVisitorAppeal > 0 ? pVisitorAppeal : 0;
	mDecorationVisitorComfort = pVisitorComfort > 0 ? pVisitorComfort : 0;
	mDrawWidth = pDrawWidth;
	mFootprintColumns = pFootprintColumns > 0 ? pFootprintColumns : 1;
	mFootprintRows = pFootprintRows > 0 ? pFootprintRows : 1;
	mItemCost = pItemCost > 0 ? pItemCost : 0;
	placementPreviewCreate();
}

void gfCParkPlacementComponent::buildingPlacementBegin(const fsStr& pImage, const fsStr& pServiceType,
                                                       fsS32 pServiceTimeMs, fsS32 pMaxQueueLength,
                                                       fsS32 pDrawWidth, fsS32 pFootprintColumns,
                                                       fsS32 pFootprintRows, fsS32 pItemCost,
                                                       fsS32 pVisitorQuota, fsS32 pVisitorAppeal)
{
	placementPreviewDestroy();
	mMode = eMode::eServiceBuilding;
	mDecorationImage = pImage;
	mBuildingServiceType = pServiceType;
	mBuildingServiceTimeMs = pServiceTimeMs;
	mBuildingMaxQueueLength = pMaxQueueLength;
	mBuildingVisitorQuota = pVisitorQuota > 0 ? pVisitorQuota : 0;
	mBuildingVisitorAppeal = pVisitorAppeal > 0 ? pVisitorAppeal : 0;
	mDrawWidth = pDrawWidth;
	mFootprintColumns = pFootprintColumns > 0 ? pFootprintColumns : 1;
	mFootprintRows = pFootprintRows > 0 ? pFootprintRows : 1;
	mItemCost = pItemCost > 0 ? pItemCost : 0;
	placementPreviewCreate();
}

void gfCParkPlacementComponent::placementEnd()
{
	placementPreviewDestroy();
	parentGet()->destroy();
}

void gfCParkPlacementComponent::placementPreviewCreate()
{
	fsIScene* const scene = fsCAppCoreMain::instanceGet()->sceneGet();
	auto* const view = dynamic_cast<fsNTileMap::fsCView*>(scene->childFind("tilemapView"));
	if (!view)
	{
		return;
	}

	mPlacementPreviewEntity = view->entityFactoryGet()->emptyEntityCreate(gPreviewEntityName);
	mPlacementPreviewEntity->componentAdd(fsCSpriteComponent::mTypeId);
	auto* const sprt = mPlacementPreviewEntity->spriteComponentGet();
	const fsCResourceName imageRes(mDecorationImage, fsCResourceName::eRES_LOCATION_ASSETS);
	if (!mDecorationImage.emptyGet() && fsIFile::fileExistsGet(imageRes))
	{
		sprt->brushSet(fsCBrushManager::instanceGet()->get(imageRes, false));
	}

	const fsS32 nativeW = sprt->widthGet();
	const fsS32 nativeH = sprt->heightGet();
	const fsS32 drawW = mDrawWidth > 0 ? mDrawWidth : gPreviewDefaultWidth;
	fsS32 drawH = drawW;
	if (nativeW > 0 && nativeH > 0)
	{
		drawH = static_cast<fsS32>(drawW *
			(static_cast<fsF32>(nativeH) / static_cast<fsF32>(nativeW)));
	}
	sprt->widthSet(drawW);
	sprt->heightSet(drawH);
	view->childAdd(mPlacementPreviewEntity);

	if (fsCEntity* const guestLayer = view->childFind(gfCParkGuestViewComponent::mGuestLayerName))
	{
		guestLayer->bringToFrontOfSiblings();
	}

	placementPreviewUpdate(fsIInputManager::instanceGet()->mousePosGet());
}

fsBool gfCParkPlacementComponent::placementPreviewUpdate(const fsPoint& pScreenPos)
{
	fsIScene* const scene = fsCAppCoreMain::instanceGet()->sceneGet();
	auto* const view = dynamic_cast<fsNTileMap::fsCView*>(scene->childFind("tilemapView"));
	if (!view)
	{
		mPlacementValid = false;
		return false;
	}

	fsVec2d localPos;
	if (!view->screenCoordConvertToLocal(fsVec2d(pScreenPos.x, pScreenPos.y), localPos))
	{
		mPlacementValid = false;
		return false;
	}

	const fsPoint snapped = buildingGridSnap(
		view, localPos, mFootprintColumns, mFootprintRows);
	mSnappedLocalX = snapped.x;
	mSnappedLocalY = snapped.y;
	mPlacementValid = false;
	if (pointerInsideMapGet(view, localPos))
	{
		if (fsCEntity* const levelEnt =
			scene->firstEntityWithComponentsOfFamilyType<gfCParkLevelComponent>())
		{
			if (gfCParkLevelComponent* const level =
				levelEnt->firstComponentOfFamilyType<gfCParkLevelComponent>())
			{
				mPlacementValid = level->parkPlacementValid(
					view, mSnappedLocalX, mSnappedLocalY,
					mFootprintColumns, mFootprintRows);
				if (mPlacementValid && mItemCost > 0)
				{
					const gfCParkDaySim* const sim = level->simGet();
					mPlacementValid = sim && sim->economyGet().coinsGet() >= mItemCost;
				}
			}
		}
	}

	if (mPlacementPreviewEntity)
	{
		if (auto* const sprt = mPlacementPreviewEntity->spriteComponentGet())
		{
			mPlacementPreviewEntity->posSet(fsPoint(
				mSnappedLocalX, mSnappedLocalY - sprt->heightGet() / 2));
			sprt->colourSet(mPlacementValid
				? fsSColour4B(255, 255, 255, gPreviewAlpha)
				: fsSColour4B(255, 70, 70, gPreviewAlpha));
		}
	}
	return mPlacementValid;
}

void gfCParkPlacementComponent::placementPreviewDestroy()
{
	if (mPlacementPreviewEntity)
	{
		mPlacementPreviewEntity->destroy();
		mPlacementPreviewEntity = nullptr;
	}
}

fsBool gfCParkPlacementComponent::messageProcessDo(const fsCUniqueId& pMessageType,
                                                   const fsIMessageDispatcher::sMessageParameters& pParam)
{
	if (fsIInputManager::mPointerDownMsg != pMessageType &&
		fsIInputManager::mPointerMoveMsg != pMessageType)
	{
		return false;
	}

	const auto& params = static_cast<const fsIInputManager::sPointerMsgParam&>(pParam);
	if (0 != params.mIdx)
	{
		return false;
	}
	fsIScene* const scene = fsCAppCoreMain::instanceGet()->sceneGet();
	if (pointerOverBuildUiGet(scene, params.mPos))
	{
		// Placement is registered after the HUD buttons and therefore sees input
		// first. Yield GUI hits so the button gesture owns the click and nothing
		// is placed underneath the interface.
		return false;
	}
	auto* const view = dynamic_cast<fsNTileMap::fsCView*>(scene->childFind("tilemapView"));
	if (!view)
	{
		// No park to place into - abandon the mode rather than sticking around.
		placementEnd();
		return true;
	}

	const fsBool valid = placementPreviewUpdate(params.mPos);
	if (fsIInputManager::mPointerMoveMsg == pMessageType || !valid)
	{
		// An illegal click leaves placement mode armed so the player can move
		// the red preview to another cell and try again.
		return true;
	}

	const fsS32 localX = mSnappedLocalX;
	const fsS32 localY = mSnappedLocalY;

	if (fsCEntity* const levelEnt = scene->firstEntityWithComponentsOfFamilyType<gfCParkLevelComponent>())
	{
		if (gfCParkLevelComponent* const parkLevel = levelEnt->firstComponentOfFamilyType<gfCParkLevelComponent>())
		{
			gfCParkDaySim* const sim = parkLevel->simGet();
			if (mItemCost > 0 && (!sim || sim->economyGet().coinsGet() < mItemCost))
			{
				placementPreviewUpdate(params.mPos);
				return true;
			}

			if (eMode::eServiceBuilding == mMode)
			{
				if (!parkLevel->serviceBuildingPlace(view, mDecorationImage, mBuildingServiceType,
					                                     mBuildingServiceTimeMs, mBuildingMaxQueueLength,
					                                     mBuildingVisitorQuota, mBuildingVisitorAppeal,
				                                     mDrawWidth, mFootprintColumns,
				                                     mFootprintRows, localX, localY))
				{
					return true;
				}
			}
			else
			{
				if (!gfCParkLevelComponent::decorationPresentationPlace(
					view, mDecorationImage, mDrawWidth, localX, localY))
				{
					return true;
				}
				parkLevel->decorationPlacedRecord(
					mDecorationImage, mDrawWidth, mFootprintColumns, mFootprintRows,
					localX, localY, mDecorationVisitorAppeal, mDecorationVisitorComfort);
			}
			if (mItemCost > 0)
			{
				sim->economyGet().coinsSet(sim->economyGet().coinsGet() - mItemCost);
			}
			parkLevel->progressPersist();
			placementPreviewUpdate(params.mPos);
		}
	}

	// Keep the selected tool armed. The occupied cell becomes illegal/red on
	// the refresh above, and the next pointer move snaps the preview elsewhere.
	return true;
}

void gfCParkPlacementComponent::listenersRegisterDo()
{
	fsIMessageListener::listenersRegisterDo();
	fsIInputManager::instanceGet()->listenerRegister(fsIInputManager::mPointerDownMsg, this);
	fsIInputManager::instanceGet()->listenerRegister(fsIInputManager::mPointerMoveMsg, this);
}

void gfCParkPlacementComponent::listenersDeregisterDo()
{
	fsIMessageListener::listenersDeregisterDo();
	fsIInputManager::instanceGet()->listenerDeregister(fsIInputManager::mPointerDownMsg, this);
	fsIInputManager::instanceGet()->listenerDeregister(fsIInputManager::mPointerMoveMsg, this);
}

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

gfCParkPlacementComponent::gfCParkPlacementComponent(fsCEntity* const pParent):
fsIMessageListenerComponent(pParent),
mMode(eMode::eDecoration),
mBuildingServiceTimeMs(0),
mBuildingMaxQueueLength(0),
mBuildingVisitorQuota(0),
mBuildingVisitorAppeal(0),
mDecorationVisitorAppeal(0),
mDecorationVisitorComfort(0),
mDrawWidth(0),
mFootprintColumns(1),
mFootprintRows(1),
mItemCost(0),
mPlacementPreviewEntity(nullptr),
mSnappedLocalX(0),
mSnappedLocalY(0),
mPlacementValid(false)
{
}

gfCParkPlacementComponent::~gfCParkPlacementComponent()
{
}