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

#include "fsCSpriteComponent.h"

#include <fsAppCore/src/fsCEntity.h>

#include <fsCore/src/debug/fsAssert.h>
#include <fsCore/src/fsCResourceName.h>
#include <fsCore/src/fsCVariableTable.h>
#include <fsCore/src/fsNMaths.h>
#include <fsCore/fsRect.h>

#include "../fsCBrush.h"
#include "../fsCBrushManager.h"
#include "../fsCSprite.h"

#include <cfloat>

#ifdef COLLISION_OUTLINE_RENDERING
	#include <fsCore/fsSColour.h>
	#include <fsRenderer/src/fsCLine.h>
#endif


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

void fsCSpriteComponent::polyInfoSet(fsIRenderData::fsSPolyInfo const& pPolyInfo)
{
	mSprite->polyInfoSet(pPolyInfo);
}

fsBool fsCSpriteComponent::obscuredByBoundingBoxCheckDo(fsISpriteComponent* pOtherSprComp)
{
	fsAssert((parentGet()->scaleGet().x == 1.0f && pOtherSprComp->parentGet()->scaleGet().y == 1.0f),
	         "Sprite entities %s and %s must have a scale of 1.0f for this hit test.\n",
	         parentGet()->nameGet().utf8Get(), pOtherSprComp->parentGet()->nameGet().utf8Get());
	fsAssert((parentGet()->angleGet() == 0.0f && pOtherSprComp->parentGet()->angleGet() == 0.0f),
	         "Sprite entities %s and %s must have an angle of 0.0f for this hit test.\n",
	         parentGet()->nameGet().utf8Get(), pOtherSprComp->parentGet()->nameGet().utf8Get());

	const fsRect myRect(parentGet()->screenPositionGet(), mSprite->brushGet()->hitTestDimensionsGet().x,
	                    mSprite->brushGet()->hitTestDimensionsGet().y);
	const fsRect hisRect(pOtherSprComp->parentGet()->screenPositionGet(), pOtherSprComp->brushGet()->hitTestDimensionsGet().x,
	                     pOtherSprComp->brushGet()->hitTestDimensionsGet().y);

	return myRect.obscuredBy(hisRect);
}

fsRect getAABBFromTransformedCorners(fsVec2d const* pCorners, fsS32 pCount,
			      const fsAffineMtx2d& pMatrix)
{
	fsVec2d worldCorners[4];
	fsF32 minX = FLT_MAX, maxX = -FLT_MAX;
	fsF32 minY = FLT_MAX, maxY = -FLT_MAX;

	for (int i = 0; i < pCount; ++i)
	{
		pMatrix.fsVec2dTransform(pCorners[i], &worldCorners[i]);
		minX = fmin(minX, worldCorners[i].x);
		maxX = fmax(maxX, worldCorners[i].x);
		minY = fmin(minY, worldCorners[i].y);
		maxY = fmax(maxY, worldCorners[i].y);
	}

	return fsRect(
		static_cast<fsS32>(minX),
		static_cast<fsS32>(maxX),
		static_cast<fsS32>(minY),
		static_cast<fsS32>(maxY));
}

#ifdef COLLISION_OUTLINE_RENDERING

void fsCSpriteComponent::renderDebugCollisionOutline(const fsRect& intersect, const fsSColour4B& pColour) const
{
  mCollisionOutlineLine->colourSet(pColour);
  mCollisionOutlineLine->vertSet(
      std::vector<fsVec2d>{fsVec2d(intersect.lGet(), intersect.tGet()),
                           fsVec2d(intersect.rGet(), intersect.tGet()),
                           fsVec2d(intersect.rGet(), intersect.bGet()),
                           fsVec2d(intersect.lGet(), intersect.bGet()),
                           fsVec2d(intersect.lGet(), intersect.tGet())});
}

void fsCSpriteComponent::renderDebugMouseOverCollisionBox() const
{
	const fsF32 halfWidth = mSprite->widthGet() / 2.0f;
	const fsF32 halfHeight = mSprite->heightGet() / 2.0f;

	fsVec2d corners[4] = {<--- Variable 'corners' can be declared as const array
		fsVec2d(-halfWidth, -halfHeight),
		fsVec2d(halfWidth, -halfHeight),
		fsVec2d(halfWidth, halfHeight),
		fsVec2d(-halfWidth, halfHeight)
	};

	renderDebugCollisionOutline(
		getAABBFromTransformedCorners(corners, 4, parentGet()->renderMatrixGet()),
		fsSColour4B(255, 255, 0, 255));
}

void fsCSpriteComponent::clearDebugCollisionOutline() const
{
	renderDebugCollisionOutline(fsRect(), fsSColour4B(0, 0, 0, 0));
}

#endif

#ifdef OUTLINE_RENDERING

void fsCSpriteComponent::debugOutlineUpdate() const
{
	const fsF32 halfWidth = mSprite->widthGet() / 2.0f;
	const fsF32 halfHeight = mSprite->heightGet() / 2.0f;

	std::vector<fsVec2d> verts =
	{
		fsVec2d(-halfWidth, -halfHeight),
		fsVec2d(halfWidth, -halfHeight),

		fsVec2d(halfWidth, -halfHeight),
		fsVec2d(halfWidth, halfHeight),

		fsVec2d(halfWidth, halfHeight),
		fsVec2d(-halfWidth, halfHeight),

		fsVec2d(-halfWidth, halfHeight),
		fsVec2d(-halfWidth, -halfHeight)
	};
	mLine->colourSet(fsSColour4B(0, 255, 0, 255));
	mLine->vertSet(verts);
}

#endif

fsBool fsCSpriteComponent::hitTestDo(fsISpriteComponent* pOtherSprComp, fsBool pRequireHitMap) const {
	// Instead of using brush dimensions directly, transform the corners
	// through the render matrix to get the actual world-space bounding box

	if (!parentGet()->visibleGet())
	{
		return false;
	}

	const fsAffineMtx2d myRenderMtx = parentGet()->renderMatrixGet();
	const fsAffineMtx2d hisRenderMtx = pOtherSprComp->parentGet()->renderMatrixGet();

	fsS32 myBrushW = mSprite->brushGet()->hitTestDimensionsGet().x;
	fsS32 myBrushH = mSprite->brushGet()->hitTestDimensionsGet().y;
	fsS32 hisBrushW = pOtherSprComp->brushGet()->hitTestDimensionsGet().x;
	fsS32 hisBrushH = pOtherSprComp->brushGet()->hitTestDimensionsGet().y;

	// Corners in centered local space (origin at sprite centre, matching fsCSprite::hitTest convention)
	const fsF32 myHalfW = myBrushW / 2.0f;
	const fsF32 myHalfH = myBrushH / 2.0f;
	const fsF32 hisHalfW = hisBrushW / 2.0f;
	const fsF32 hisHalfH = hisBrushH / 2.0f;

	fsVec2d myCorners[4] = {<--- Variable 'myCorners' can be declared as const array
	    fsVec2d(-myHalfW, -myHalfH),
	    fsVec2d( myHalfW, -myHalfH),
	    fsVec2d( myHalfW,  myHalfH),
	    fsVec2d(-myHalfW,  myHalfH)
	};

	fsVec2d hisCorners[4] = {<--- Variable 'hisCorners' can be declared as const array
	    fsVec2d(-hisHalfW, -hisHalfH),
	    fsVec2d( hisHalfW, -hisHalfH),
	    fsVec2d( hisHalfW,  hisHalfH),
	    fsVec2d(-hisHalfW,  hisHalfH)
	};

	// Transform corners to world space and compute AABB
	fsRect myWorldRect = getAABBFromTransformedCorners(myCorners, 4, myRenderMtx);
	fsRect hisWorldRect = getAABBFromTransformedCorners(hisCorners, 4, hisRenderMtx);

	// Now compute intersection in world space
	const fsRect intersect = myWorldRect.intersectGet(hisWorldRect);
	if (!intersect.isValidGet())
	    return false;

	if (!pRequireHitMap)
		return true;

	// Get inverse matrices for transforming world pixels back to local texture space
	const fsAffineMtx2d myInvMtx = myRenderMtx.inverseGet();
	const fsAffineMtx2d hisInvMtx = hisRenderMtx.inverseGet();

	std::shared_ptr<fsCBrush> myBrush = mSprite->brushGet();
	std::shared_ptr<fsCBrush> hisBrush = pOtherSprComp->brushGet();

	// Per-pixel collision detection
	for (fsS32 testY = intersect.tGet(); testY < intersect.bGet(); ++testY)
	{
	    for (fsS32 testX = intersect.lGet(); testX < intersect.rGet(); ++testX)
	    {
	        const fsVec2d worldPos(static_cast<fsF32>(testX), static_cast<fsF32>(testY));
	        fsVec2d localMy;
	        fsVec2d localHis;

	        // Transform world pixel to centered local space, then shift to texture space.
	        // Y is flipped because the render matrix uses Y-up convention but textures are Y-down.
	        myInvMtx.fsVec2dTransform(worldPos, &localMy);
	        hisInvMtx.fsVec2dTransform(worldPos, &localHis);

	    	localMy.x += myHalfW;
	    	localMy.y = myHalfH - localMy.y;

	    	localHis.x += hisHalfW;
	    	localHis.y = hisHalfH - localHis.y;
		        // Perform hit test on both brushes at transformed local coordinates
	        if (myBrush->hitTest(localMy) && hisBrush->hitTest(localHis))
	        {
#ifdef COLLISION_OUTLINE_RENDERING
		    renderDebugCollisionOutline(intersect, fsSColour4B(0, 255, 0, 255));
#endif
	        	fsLogMsg("Pixel collision detected between %s and %s.\n",
				parentGet()->nameGet().utf8Get(),
	        		pOtherSprComp->parentGet()->nameGet().utf8Get()
				);
	            return true;
	        }
	    }
	}

#ifdef COLLISION_OUTLINE_RENDERING
	renderDebugCollisionOutline(intersect, fsSColour4B(255, 0, 0, 255));
#endif

	return false;
}


void fsCSpriteComponent::initialiseFromVariableTableDo(const fsCVariableTable& pVars)
{
	fsISpriteComponent::initialiseFromVariableTableDo(pVars);

	const fsStr& texFileName = pVars.strGet("image");
	if (!texFileName.emptyGet())
	{
		const fsBool hitMap = pVars.strGet("hitMap") == "true";
		brushSet(fsCBrushManager::instanceGet()->get(
			fsCResourceName(texFileName, fsCResourceName::eRES_LOCATION_ASSETS), hitMap));
	}

	const fsS32 rectW = pVars.s32Get("width");
	if (rectW)
	{
		widthSet(rectW);
	}

	const fsS32 rectH = pVars.s32Get("height");
	if (rectH)
	{
		heightSet(rectH);
	}

	const fsS32 rot = pVars.s32Get("rotation");
	if (rot)
	{
		parentGet()->angleSet(rot * fsNMaths::fsPI / 180.0f);
	}

	const fsF32 scale = pVars.f32Get("scale");
	if (scale)
	{
		parentGet()->scaleSet(fsVec2d(scale, scale));
	}
}

fsBool fsCSpriteComponent::hitTestDo(const fsPoint& pScreenPos, fsBool pRequireHitMap) const
{
	if (!parentGet()->visibleGet())
	{
		return false;
	}

	fsVec2d localPos;
	if (!parentGet()->screenCoordConvertToLocal(pScreenPos, localPos))
	{
		return false;
	}
	const fsBool hit = mSprite->hitTest(localPos, pRequireHitMap);
#ifdef COLLISION_OUTLINE_RENDERING
	if (hit)
	{
		renderDebugMouseOverCollisionBox();
	}
	else
	{
		clearDebugCollisionOutline();
	}
#endif
	return hit;
}

void fsCSpriteComponent::renderDo()
{
	mSprite->render();
#ifdef COLLISION_OUTLINE_RENDERING
	mCollisionOutlineLine->render();
#endif
#ifdef OUTLINE_RENDERING
	mLine->render();
#endif
}

void fsCSpriteComponent::matrixApplyDo()
{
	mSprite->matrixApply(parentGet()->renderMatrixGet());
#ifdef COLLISION_OUTLINE_RENDERING
	// Collision outlines are populated in world space by renderDebugCollisionOutline().
	// Applying the sprite matrix here would transform the intersection twice.
#endif
#ifdef OUTLINE_RENDERING
	mLine->matrixApply(parentGet()->renderMatrixGet());
#endif
}


void fsCSpriteComponent::brushSetDo(const std::shared_ptr<fsCBrush> pBrush)
{
	mSprite->brushSet(pBrush);
	matrixApplyDo();

#ifdef OUTLINE_RENDERING
	debugOutlineUpdate();
#endif
}

std::shared_ptr<fsCBrush> fsCSpriteComponent::brushGetDo() const
{
	return mSprite->brushGet();
}

void fsCSpriteComponent::alphaBlendSetDo(fsS32 pBlend)
{
	mSprite->alphaBlendModeSet(pBlend);
}

void fsCSpriteComponent::colourSetDo(const fsSColour4B& pColour)
{
	mSprite->colourSet(pColour);
}

fsS32 fsCSpriteComponent::widthGetDo() const
{
	return mSprite->widthGet();
}

fsS32 fsCSpriteComponent::heightGetDo() const
{
	return mSprite->heightGet();
}

void fsCSpriteComponent::widthSetDo(fsS32 pWidth, fsBool pModifyRenderData /*= true*/)
{
	mSprite->widthSet(pWidth, pModifyRenderData);
#ifdef OUTLINE_RENDERING
	debugOutlineUpdate();
#endif
}

void fsCSpriteComponent::heightSetDo(fsS32 pHeight, fsBool pModifyRenderData /*= true*/)
{
	mSprite->heightSet(pHeight, pModifyRenderData);
#ifdef OUTLINE_RENDERING
	debugOutlineUpdate();
#endif
}

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

fsCSpriteComponent::fsCSpriteComponent(fsCEntity* const pParent) :
	fsISpriteComponent(pParent),
	mSprite(new fsCSprite())
#ifdef OUTLINE_RENDERING
,mLine(new fsCLine(std::vector<fsVec2d>()))
#endif
{
#ifdef COLLISION_OUTLINE_RENDERING
	mCollisionOutlineLine = new fsCLine(std::vector<fsVec2d>());
#endif
}

fsCSpriteComponent::~fsCSpriteComponent()
{
	delete mSprite;
#ifdef OUTLINE_RENDERING
	delete mLine;
#endif
#ifdef COLLISION_OUTLINE_RENDERING
	delete mCollisionOutlineLine;
#endif
}