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

#include "gfCParkStaffViewComponent.h"

#include <cmath>

#include <fsCore/fsIFile.h>
#include <fsCore/src/fsCResourceName.h>
#include <fsCore/src/debug/fsLog.h>

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

#include <fsRenderer/src/components/fsCAnimatingSpriteComponent.h>

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

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

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

namespace
{
const fsF32 gStaffSpeedPxPerMs = 0.10f;
const fsF32 gStaffScale = 0.25f;

// The 4-directional characters name clips per facing (front set used here).
// Animation names are per character pack.
const fsStr gStaffIdleAnim("Front - Idle");
const fsStr gStaffWalkAnim("Front - Walking");

const fsStr gKioskCharacter("characters/kioskStaff/Animations.scml");
const fsStr gRepairCharacter("characters/repairStaff/Animations.scml");

const fsStr gStaffOfficeObjectName("staffOffice");

// Off-duty repairers drift around the office rather than standing on it.
const fsF32 gWanderRadiusPx = 70.0f;
const fsS32 gWanderIntervalMs = 2500;

// Stand beside the building rather than on top of it.
const fsF32 gPostOffsetPx = 40.0f;
}

fsNTileMap::fsCView* gfCParkStaffViewComponent::viewGet() const
{
	return dynamic_cast<fsNTileMap::fsCView*>(
		fsCAppCoreMain::instanceGet()->sceneGet()->childFind("tilemapView"));
}

fsBool gfCParkStaffViewComponent::mapPointGet(fsNTileMap::fsCView* const pView, const fsStr& pObjectName,
                                              fsVec2d& pOut) const
{
	fsNTileMap::fsITileMap* const map = pView->mMap;
	for (fsS32 layer = 0; layer < map->layerCountGet(); ++layer)
	{
		if (fsNTileMap::fsILayer::eType::OBJECT != map->layerTypeGet(layer))
		{
			continue;
		}

		for (fsNTileMap::fsIObjectRef const& obj : map->triggersGet(layer))
		{
			if (obj->nameGet() != pObjectName)
			{
				continue;
			}

			const fsPoint pos = obj->posGet();
			const fsPoint topLeft = pView->mapPixelToLocal(pos);
			const fsPoint botRight = pView->mapPixelToLocal(
				fsPoint(pos.x + obj->widthGet(), pos.y + obj->heightGet()));
			pOut = fsVec2d((topLeft.x + botRight.x) / 2.0f, (topLeft.y + botRight.y) / 2.0f);
			return true;
		}
	}
	return false;
}

gfCParkStaffViewComponent::SStaffView* gfCParkStaffViewComponent::viewFind(const fsStr& pKey)
{
	for (SStaffView& view : mViews)
	{
		if (view.mKey == pKey)
		{<--- Consider using std::find_if algorithm instead of a raw loop.
			return &view;
		}
	}
	return nullptr;
}

gfCParkStaffViewComponent::SStaffView& gfCParkStaffViewComponent::viewCreate(
	fsNTileMap::fsCView* const pView, const fsStr& pKey, const fsStr& pType, fsS32 pIndex,
	const fsStr& pCharacterFolder, const fsVec2d& pAt)
{
	SStaffView view;
	view.mKey = pKey;
	view.mType = pType;
	view.mIndex = pIndex;
	view.mPos = pAt;
	view.mTarget = pAt;
	view.mEntity = pView->entityFactoryGet()->emptyEntityCreate(pKey);
	view.mEntity->posSet(pAt);
	view.mEntity->scaleSet(fsVec2d(gStaffScale, gStaffScale));

	const fsCResourceName animRes(pCharacterFolder, fsCResourceName::eRES_LOCATION_ASSETS);
	if (fsIFile::fileExistsGet(animRes))
	{
		view.mEntity->componentAdd<fsCAnimatingSpriteComponent>()->animationDataSet(animRes, false);
	}
	else
	{
		fsLogError("Staff animation not found: %s", pCharacterFolder.utf8Get());
	}

	// Share the guest layer so staff and guests sort together against buildings.
	if (fsCEntity* const guestLayer = pView->childFind(gfCParkGuestViewComponent::mGuestLayerName))
	{
		guestLayer->childAdd(view.mEntity);
	}
	else
	{
		pView->childAdd(view.mEntity);
	}

	mViews.push_back(view);
	return mViews.back();
}

void gfCParkStaffViewComponent::animationSet(SStaffView& pView, const fsStr& pAnimation)
{
	if (pView.mAnimation == pAnimation)
	{
		return;
	}
	pView.mAnimation = pAnimation;

	if (fsCAnimatingSpriteComponent* const anim =
		pView.mEntity->componentGet<fsCAnimatingSpriteComponent>())
	{
		anim->animationPlay(pAnimation);
	}
}

fsBool gfCParkStaffViewComponent::moveToward(SStaffView& pView, const fsVec2d& pTarget, fsS32 pDeltaMs) const
{
	const fsF32 dx = pTarget.x - pView.mPos.x;
	const fsF32 dy = pTarget.y - pView.mPos.y;
	const fsF32 distance = std::sqrt(dx * dx + dy * dy);
	const fsF32 step = gStaffSpeedPxPerMs * static_cast<fsF32>(pDeltaMs);

	if (distance <= step || distance < 1.0f)
	{
		pView.mPos = pTarget;
		pView.mEntity->posSet(pView.mPos);
		return false;
	}

	pView.mPos.x += (dx / distance) * step;
	pView.mPos.y += (dy / distance) * step;
	pView.mEntity->posSet(pView.mPos);
	return true;
}

fsBool gfCParkStaffViewComponent::moveTowardRouted(SStaffView& pStaff,
                                                   fsNTileMap::fsCView* const pTileView,
                                                   const gfCParkLevelComponent* const pLevel,
                                                   const fsVec2d& pTarget, fsS32 pDeltaMs) const
{
	// (Re)route only when the destination cell changes or the path ran out.
	const fsPoint targetCell = pLevel->cellFromLocal(pTileView, pTarget);
	if (targetCell.x != pStaff.mPathTargetCol || targetCell.y != pStaff.mPathTargetRow ||
		pStaff.mPath.empty())
	{
		pStaff.mPathTargetCol = targetCell.x;
		pStaff.mPathTargetRow = targetCell.y;
		pStaff.mPath.clear();
		// Snap a forest destination (idle wander near the office) to open ground
		// and finish on that cell's centre, so staff keep off uncleared forest.
		const fsPoint routeCell = pLevel->nearestWalkableCell(pTileView, targetCell);
		const fsBool targetWalkable = (routeCell.x == targetCell.x && routeCell.y == targetCell.y);
		const fsVec2d finalPoint = targetWalkable
			? pTarget
			: pLevel->cellCentreLocal(pTileView, routeCell.x, routeCell.y);
		const fsPoint fromCell = pLevel->cellFromLocal(pTileView, pStaff.mPos);
		const std::vector<fsPoint> cells = pLevel->pathFindCells(pTileView, fromCell, routeCell);
		if (cells.size() >= 2)
		{
			for (fsS32 i = 1; i < static_cast<fsS32>(cells.size()) - 1; ++i)
			{
				pStaff.mPath.push_back(pLevel->cellCentreLocal(pTileView, cells[i].x, cells[i].y));
			}
			pStaff.mPath.push_back(finalPoint);
		}
		else
		{
			pStaff.mPath.push_back(finalPoint);
		}
	}

	fsF32 step = gStaffSpeedPxPerMs * static_cast<fsF32>(pDeltaMs);
	while (step > 0.0f && !pStaff.mPath.empty())
	{
		const fsVec2d next = pStaff.mPath.front();
		const fsF32 dx = next.x - pStaff.mPos.x;
		const fsF32 dy = next.y - pStaff.mPos.y;
		const fsF32 distance = std::sqrt(dx * dx + dy * dy);
		if (distance <= step || distance < 1.0f)
		{
			pStaff.mPos = next;
			pStaff.mPath.erase(pStaff.mPath.begin());
			step -= distance;
		}
		else
		{
			pStaff.mPos.x += (dx / distance) * step;
			pStaff.mPos.y += (dy / distance) * step;
			step = 0.0f;
		}
	}
	pStaff.mEntity->posSet(pStaff.mPos);
	return !pStaff.mPath.empty();
}

void gfCParkStaffViewComponent::staffTypeUpdate(fsNTileMap::fsCView* const pView,
                                                const gfCParkDaySim* const pSim, const fsStr& pType,
                                                const fsStr& pCharacterFolder,
                                                const std::vector<SStaffPost>& pPosts,
                                                const fsVec2d& pOfficePos, fsBool pReWander)
{
	const fsS32 hired = pSim->staffTotalGet(pType);

	// Exactly one entity per hired head; all start at the office.
	for (fsS32 i = 0; i < hired; ++i)
	{
		const fsStr key = pType + ":" + fsStr(i);
		if (!viewFind(key))
		{
			viewCreate(pView, key, pType, i, pCharacterFolder, pOfficePos);
		}
	}

	// Drop anyone beyond the hired count.
	for (std::vector<SStaffView>::iterator it = mViews.begin(); it != mViews.end(); )
	{
		if (it->mType == pType && it->mIndex >= hired)
		{
			it->mEntity->destroy();
			it = mViews.erase(it);
		}
		else
		{
			++it;
		}
	}

	// A post that still exists stays with whoever is on it (no swapping).
	std::vector<fsBool> postTaken(pPosts.size(), false);
	for (SStaffView& view : mViews)
	{
		if (view.mType != pType || view.mAssignedSlot.emptyGet())
		{
			continue;
		}

		fsBool stillPosted = false;
		for (size_t p = 0; p < pPosts.size(); ++p)
		{
			if (!postTaken[p] && pPosts[p].mSlotName == view.mAssignedSlot)
			{
				postTaken[p] = true;
				view.mTarget = pPosts[p].mPos;
				stillPosted = true;
				break;
			}
		}
		if (!stillPosted)
		{
			// Job gone - head back to the office now.
			view.mAssignedSlot = "";
			view.mTarget = pOfficePos;
		}
	}

	// Spare posts go to whoever is off duty.
	for (size_t p = 0; p < pPosts.size(); ++p)
	{
		if (postTaken[p])
		{
			continue;
		}
		for (SStaffView& view : mViews)
		{
			if (view.mType == pType && view.mAssignedSlot.emptyGet())
			{
				view.mAssignedSlot = pPosts[p].mSlotName;
				view.mTarget = pPosts[p].mPos;
				postTaken[p] = true;
				break;
			}
		}
	}

	// Everyone still unassigned mills about near the office.
	fsS32 idleIndex = 0;
	for (SStaffView& view : mViews)
	{
		if (view.mType != pType || !view.mAssignedSlot.emptyGet())
		{
			continue;
		}

		if (pReWander)
		{
			// Deterministic scatter; mWanderSeed advances each re-roll so it moves.
			const fsF32 angle = static_cast<fsF32>((idleIndex * 67 + mWanderSeed * 41) % 360) * 0.01745f;
			view.mTarget = pOfficePos;
			view.mTarget.x += std::cos(angle) * gWanderRadiusPx;
			view.mTarget.y += std::sin(angle) * gWanderRadiusPx * 0.5f;
		}
		++idleIndex;
	}
}

void gfCParkStaffViewComponent::updateDo(fsS32 pDeltaMs)
{
	fsNTileMap::fsCView* const view = viewGet();
	if (!view)
	{
		return;
	}

	gfCParkLevelComponent* const level = parentGet()->firstComponentOfFamilyType<gfCParkLevelComponent>();
	const gfCParkDaySim* const sim = level ? level->simGet() : nullptr;
	if (!sim)
	{
		return;
	}

	fsVec2d officePos;
	if (!mapPointGet(view, gStaffOfficeObjectName, officePos))
	{
		// No staff office - nowhere for staff to come from.
		return;
	}

	mIdleWanderMs -= pDeltaMs;
	const fsBool rewander = mIdleWanderMs <= 0;
	if (rewander)
	{
		mIdleWanderMs = gWanderIntervalMs;
		++mWanderSeed;
	}

	// Derive the jobs (posts) from the slots; staffTypeUpdate then assigns real
	// people to them. (Deriving people from jobs directly drifted the counts.)
	std::vector<SStaffPost> kioskPosts;
	std::vector<SStaffPost> repairPosts;

	fsNTileMap::fsITileMap* const map = view->mMap;
	for (fsS32 layer = 0; layer < map->layerCountGet(); ++layer)
	{
		if (fsNTileMap::fsILayer::eType::OBJECT != map->layerTypeGet(layer))
		{
			continue;
		}

		for (fsNTileMap::fsIObjectRef const& obj : map->triggersGet(layer))
		{
			if (obj->typeGet().emptyGet())
			{
				continue;
			}

			const fsStr& slotName = obj->nameGet();
			fsVec2d slotPos;
			if (!mapPointGet(view, slotName, slotPos))
			{
				continue;
			}

			// Staffed building posts a kiosk hand - but not while broken (they
			// vacate to the office; the slot stays staffed so it auto-resumes).
			if (sim->slotStaffedGet(slotName) && !sim->slotBrokenGet(slotName))
			{
				SStaffPost post;
				post.mSlotName = slotName;
				// To one side, so the building art stays readable.
				post.mPos = slotPos;
				post.mPos.x += gPostOffsetPx;
				kioskPosts.push_back(post);
			}

			if (sim->slotRepairingGet(slotName))
			{
				SStaffPost post;
				post.mSlotName = slotName;
				post.mPos = slotPos;
				post.mPos.x -= gPostOffsetPx;
				repairPosts.push_back(post);
			}
		}
	}

	// Freely placed buildings have no TMX objects, but otherwise post staff and
	// repair jobs exactly like authored slots.
	for (fsS32 i = 0; ; ++i)
	{
		fsStr slotName;
		fsVec2d slotPos;
		if (!level->runtimeSlotGetByIndex(i, slotName, slotPos))
		{
			break;
		}

		if (sim->slotStaffedGet(slotName) && !sim->slotBrokenGet(slotName))
		{
			SStaffPost post;
			post.mSlotName = slotName;
			post.mPos = slotPos;
			post.mPos.x += gPostOffsetPx;
			kioskPosts.push_back(post);
		}

		if (sim->slotRepairingGet(slotName))
		{
			SStaffPost post;
			post.mSlotName = slotName;
			post.mPos = slotPos;
			post.mPos.x -= gPostOffsetPx;
			repairPosts.push_back(post);
		}
	}

	staffTypeUpdate(view, sim, gfCParkDaySim::mStaffTypeKiosk, gKioskCharacter,
	                kioskPosts, officePos, rewander);
	staffTypeUpdate(view, sim, gfCParkDaySim::mStaffTypeRepair, gRepairCharacter,
	                repairPosts, officePos, rewander);

	// Everyone walks to their target; off-duty staff head to the office, so no
	// retirement pass is needed.
	for (SStaffView& staffView : mViews)
	{
		animationSet(staffView,
			moveTowardRouted(staffView, view, level, staffView.mTarget, pDeltaMs)
			? gStaffWalkAnim : gStaffIdleAnim);
	}
}

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

gfCParkStaffViewComponent::gfCParkStaffViewComponent(fsCEntity* const pParent):
fsIComponent(pParent),
mIdleWanderMs(0),
mWanderSeed(0)
{
}

gfCParkStaffViewComponent::~gfCParkStaffViewComponent()
{
}