Code: Select all
#include "Movement.h"
#include "GameObject.h"
#include <Irrlicht.h>
#include <Windows.h>
using namespace irr;
using namespace scene;
Movement::Movement(GameObject* gameObj)
{
this->gameObjToMove = gameObj;
this->targetX = 0;
this->targetY = 0;
this->movementTimer = 0;
this->positionUpdateRequired = false;
}
Movement::~Movement()
{
}
void Movement::process()
{
/* Move the game object if a position update is required */
if (gameObjToMove->positionUpdateRequired)
{
IAnimatedMeshSceneNode* sceneNode = (IAnimatedMeshSceneNode*)gameObjToMove->getObjSceneNode();
core::vector3df nodeCoords = sceneNode->getPosition();
time_t tCount = GetTickCount();
time_t timeSince_Millis = tCount - this->movementTimer;
/* Some processors are slower than others because of this it is important
* to calculate how many + 1 movements they have missed out on and move the player
* appropriately. */
s16 posToAdd;
if ((timeSince_Millis > 100))
{
/* This code needs revising, as it is not working completely right */
posToAdd = (timeSince_Millis) / 100;
if (posToAdd == 0)
posToAdd = 1;
// Prevents jumping loop when close to coordinates
else if ((nodeCoords.X > this->targetX && nodeCoords.X < this->targetX + posToAdd)
|| (nodeCoords.Y > this->targetY && nodeCoords.Y < this->targetY + posToAdd))
{
posToAdd = 1;
}
posToAdd = 1;
if(nodeCoords.X > this->targetX)
nodeCoords.X -= posToAdd;
else if (nodeCoords.X < this->targetX)
nodeCoords.X += posToAdd;
if(nodeCoords.Z > this->targetY)
nodeCoords.Z -= posToAdd;
else if (nodeCoords.Z < this->targetY)
nodeCoords.Z += posToAdd;
sceneNode->setPosition(core::vector3df(nodeCoords.X, 0, nodeCoords.Z));
sceneNode->updateAbsolutePosition();
if (nodeCoords.X == this->targetX && nodeCoords.Y == this->targetY)
{
this->positionUpdateRequired = false;
this->targetX = 0;
this->targetY = 0;
}
this->movementTimer = GetTickCount();
}
}
}
void Movement::walkTo(unsigned short targetX, unsigned short targetY)
{
this->targetX = targetX;
this->targetY = targetY;
this->positionUpdateRequired = true;
this->movementTimer = GetTickCount();
}
Anyone know a way to get smoother movement please?