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

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

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


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

fsF32 fsCHookedLineTrailComponent::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 fsCHookedLineTrailComponent::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 fsCHookedLineTrailComponent::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));

	fsVec2d pos;

	const fsF32 radiusX = 50;
	const fsF32 radiusY = 50;

	const fsF32 direction = (pEndPos.x < 0.0f) ? -1.0f : 1.0f;

	fsF32 dist = distanceBetweenPoints(start, end);
	fsF32 dx = (pStartPos.x - pEndPos.x) / dist;
	fsF32 dy = (pStartPos.y - pEndPos.y) / dist;
	fsF32 theta = fsNMaths::atan2F32(dx, dy);

	for (fsS32 quadrants = 0; quadrants < 4 * 3; ++quadrants)
	{
		pos.x = pEndPos.x + radiusX * fsNMaths::cosF32(theta);
		pos.y = pEndPos.y + radiusY * fsNMaths::sinF32(theta);
		points.push_back(pos);
		theta = theta + direction * (fsNMaths::fsPI / 2.0f);
	}

	mPath = new fsCBezierPath(points, mDuration);
}

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

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

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

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