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

#include <fsCore/src/fsCUniqueId.h>

#include <fsCore/src/fsNMaths.h>

#include <fsAppCore/fsIInputManager.h>

#if defined fsTileMap
#include <fsTileMap/fsITileMap.h>
#include <fsTileMap/src/fsCWrapScroller.h>
#include <fsTileMap/src/fsCBoundryScroller.h>
#include <fsTileMap/src/fsCView.h>
#endif
#include "fsCPlayerMovementComponent.h"


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


void fsCPlayerMovementComponent::recentre(fsF32& pos, fsS32& pMove, fsS32 pCentre)<--- Either there is a missing 'override', or the member function 'fsCPlayerMovementComponent::recentre' can be static.<--- Parameter 'pMove' can be declared as reference to const
{
	if (pos != pCentre)
	{
		if (pos < pCentre && (pos - pMove > pCentre))
		{
			pos = pCentre;
		}
		else if (pos > pCentre && (pos - pMove < pCentre))
		{
			pos = pCentre;
		}
		else
		{
			pos -= pMove;
		}
	}
}

void fsCPlayerMovementComponent::tileMapUpdate()
{
#if defined fsTileMap
	fsS32 scrollX = mVelocity.x;
	fsS32 scrollY = mVelocity.y;

	fsVec2d pos = fsVec2d(parentGet()->posGet().x, parentGet()->posGet().y);

	if (pos.x != 512)
	{
		recentre(pos.x, scrollX, 512);
		parentGet()->posSet(pos);
		scrollX = 0;
	}

	if (pos.y != 256)
	{
		recentre(pos.y, scrollY, 256);
		parentGet()->posSet(pos);
		scrollY = 0;
	}

	if (scrollX || scrollY)
	{
		fsS32 currScrollX = scrollX;
		fsS32 currScrollY = scrollY;
		mMap->scroll(scrollX, scrollY);
		if (scrollX != currScrollX || scrollY != currScrollY)
		{
			if (scrollX != currScrollX)
			{
				pos.x -= scrollX;
			}
			if (scrollY != currScrollY)
			{
				pos.y -= scrollY;
			}
			parentGet()->posSet(pos);
		}
	}

	if (mMap->check(mMap->screenToMap(fsPoint(parentGet()->posGet().x, parentGet()->posGet().y))))
	{
//		if (!mVelocity.y)
		{
			mVelocity.y -= 5;
		}
	}
	else
	{
		if (mVelocity.y > 0)
		{
			mVelocity.y = 0;
		}
	}
#endif
}

void fsCPlayerMovementComponent::updateDo(fsS32 pDeltaMs)
{
	fsIActorMovementComponent::updateDo(pDeltaMs);
	if (mVelocity.x)
	{
		mVelocity.x -= mVelocity.x / 4;
		if (fsNMaths::fabsF32(mVelocity.x) < 0.1)
		{
			mVelocity.x = 0;
		}
	tileMapUpdate();
	}
	if (mVelocity.y < 0)
	{
		mVelocity.y -= mVelocity.y / 4;
		if (fsNMaths::fabsF32(mVelocity.y) < 0.1)
		{
			mVelocity.y = 0;
		}
	tileMapUpdate();
	}

}

fsBool fsCPlayerMovementComponent::typedCharacterHandle(fsS32 pTypedChar)
{
	if ('x' == pTypedChar)
	{
		mVelocity.x -= 5;
		parentGet()->scaleSet({fsNMaths::fabsF32(parentGet()->scaleGet().x), parentGet()->scaleGet().y});
		return true;
	}
	if ('z' == pTypedChar)
	{
		parentGet()->scaleSet({-fsNMaths::fabsF32(parentGet()->scaleGet().x), parentGet()->scaleGet().y});
		mVelocity.x += 5;
		return true;
	}
	if ('a' == pTypedChar)
	{
		mVelocity.y += 15;
		return true;
	}

	return false;
}

void fsCPlayerMovementComponent::listenersRegisterDo()
{
	fsIActorMovementComponent::listenersRegisterDo();
	fsIInputManager::instanceGet()->listenerRegister(fsIInputManager::mTypedCharMsg, this);
#if defined fsTileMap
	mMap = dynamic_cast<fsNTileMap::fsCView*>(fsCAppCoreMain::instanceGet()->sceneGet()->childFind("tilemapView"));
#endif
}

void fsCPlayerMovementComponent::listenersDeregisterDo()
{
	fsIActorMovementComponent::listenersDeregisterDo();
	fsIInputManager::instanceGet()->listenerDeregister(fsIInputManager::mTypedCharMsg, this);
}

fsBool fsCPlayerMovementComponent::messageProcessDo(const fsCUniqueId& pMessageType,
                                                    const fsIMessageDispatcher::sMessageParameters& pParam)
{
	fsIActorMovementComponent::messageProcessDo(pMessageType, pParam);
	if (pMessageType == fsIInputManager::mTypedCharMsg)
	{
		return typedCharacterHandle(static_cast<const fsIInputManager::sKeyboardMsgParam&>(pParam).mKey);
	}
	return false;
}

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

fsCPlayerMovementComponent::fsCPlayerMovementComponent(fsCEntity* const pParent):
	fsIActorMovementComponent(pParent)
#if defined fsTileMap
, mMap(nullptr)
#endif
{
}

fsCPlayerMovementComponent::~fsCPlayerMovementComponent()
{
}