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

#include <fsCore/src/fsCBezierPath.h>
#include <fsCore/src/fsNMaths.h>

#include "../../src/fsCEntity.h"


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

fsF32 fsCArcLineTrailComponent::distanceBetweenPoints(const fsVec2d& pKnotA, const fsVec2d& pKnotB)
{
	fsF32 deltaX = pKnotA.x - pKnotB.x;
	fsF32 deltaY = pKnotA.y - pKnotB.y;
	return fsNMaths::sqrtF32(deltaX * deltaX + deltaY * deltaY);
}

fsVec2d fsCArcLineTrailComponent::midPoint(const fsVec2d& pStart, const fsVec2d& pEnd)
{
	fsF32 dist = distanceBetweenPoints(pStart, pEnd);
	fsF32 dx = (pStart.x - pEnd.x) / dist;
	fsF32 dy = (pStart.y - pEnd.y) / dist;

	fsVec2d mid(pStart.x + (pEnd.x - pStart.x) / 2.0f,
	            pStart.y + (pEnd.y - pStart.y) / 2.0f);

	dist = dist / 5.0f;
	if (pStart.x > pEnd.x)
	{
		return fsVec2d(mid.x + dist * dy, mid.y - dist * dx);
	}

	return fsVec2d(mid.x - dist * dy, mid.y + dist * dx);
}

void fsCArcLineTrailComponent::initialiseDo(const fsPoint& pStartPos, const fsPoint& pEndPos)
{
	std::vector<fsVec2d> points;

	fsVec2d start(static_cast<fsF32>(pStartPos.x), static_cast<fsF32>(pStartPos.y));
	fsVec2d end(static_cast<fsF32>(pEndPos.x), static_cast<fsF32>(pEndPos.y));

	points.push_back(start);
	points.push_back(midPoint(start, end));
	points.push_back(end);

	mPath = new fsCBezierPath(points, mDuration);
}

void fsCArcLineTrailComponent::updateDo()
{
	if (mElapsedTime < mDuration)
	{
		fsVec2d pos = mPath->update(mElapsedTime);
		parentGet()->posSet(fsPoint(static_cast<fsS32>(pos.x), static_cast<fsS32>(pos.y)));
	}
}

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

fsCArcLineTrailComponent::fsCArcLineTrailComponent(fsCEntity* const pParent) :
	fsITrailComponent(pParent),
	mPath(nullptr)
{
}

fsCArcLineTrailComponent::~fsCArcLineTrailComponent()
{
	delete mPath;<--- Class 'fsCArcLineTrailComponent' does not have a copy constructor which is recommended since it has dynamic memory/resource management.<--- Class 'fsCArcLineTrailComponent' does not have a operator= which is recommended since it has dynamic memory/resource management.
}