[Fix] Change calculation of absolute value to support floating point variables.

This commit is contained in:
Rtch90 2012-02-20 21:34:50 +00:00
parent 2e50097502
commit 61f1f2b2a4

View File

@ -3,8 +3,8 @@
#include "../System/Vec2.h"
NPC::NPC(LevelGen* mapArg) : Character(mapArg) {
_walkInPath = false;
_moving = false;
_walkInPath = false;
_moving = false;
}
NPC::~NPC(void) {
@ -12,8 +12,8 @@ NPC::~NPC(void) {
}
void NPC::ForceMove(void) {
tileX = x / AStarTile::FAKE_SIZE;
tileY = y / AStarTile::FAKE_SIZE;
tileX = x / AStarTile::FAKE_SIZE;
tileY = y / AStarTile::FAKE_SIZE;
}
void NPC::Update(void) {
@ -31,83 +31,83 @@ void NPC::Update(void) {
}
void NPC::Move(void) {
Vec2 realPos(x, y);
Vec2 tilePos(tileX, tileY);
Vec2 realPos(x, y);
Vec2 tilePos(tileX, tileY);
if(realPos != tilePos) {
float targetX = (float)(tileX * AStarTile::FAKE_SIZE);
float targetY = (float)(tileY * AStarTile::FAKE_SIZE);
if(realPos != tilePos) {
float targetX = (float)(tileX * AStarTile::FAKE_SIZE);
float targetY = (float)(tileY * AStarTile::FAKE_SIZE);
float dx = targetX - realPos.x;
float dy = targetY - realPos.y;
float dx = targetX - realPos.x;
float dy = targetY - realPos.y;
float distance = sqrtf(dx*dx + dy*dy);
float distance = sqrtf(dx*dx + dy*dy);
if(fabs(distance) > CHARACTER_SPEED) {
dx /= distance;
dy /= distance;
dx *= CHARACTER_SPEED;
dy *= CHARACTER_SPEED;
xVel = dx;
yVel = dy;
if(fabs(distance) > CHARACTER_SPEED) {
dx /= distance;
dy /= distance;
dx *= CHARACTER_SPEED;
dy *= CHARACTER_SPEED;
map->MoveIfPossible(this, xVel, yVel, false);
xVel = dx;
yVel = dy;
Character::HealthBarScroll();
map->MoveIfPossible(this, xVel, yVel, false);
_moving = true;
} else {
xVel = 0.0f;
yVel = 0.0f;
_moving = false;
}
} else {
xVel = 0.0f;
yVel = 0.0f;
_moving = false;
}
Character::HealthBarScroll();
if(_walkInPath && !_moving) {
Vec2 targetPos((float)_target->GetX(), (float)_target->GetY());
if(abs(Vec2::Distance(targetPos, realPos)) <= CHARACTER_SPEED) {
_target = _astar.GetSolutionNext();
if(_target == NULL || _target == _lastTarget) {
_walkInPath = false;
} else {
tileX = _target->GetX() / AStarTile::FAKE_SIZE;
tileY = _target->GetY() / AStarTile::FAKE_SIZE;
}
}
}
_moving = true;
} else {
xVel = 0.0f;
yVel = 0.0f;
_moving = false;
}
} else {
xVel = 0.0f;
yVel = 0.0f;
_moving = false;
}
if(_walkInPath && !_moving) {
Vec2 targetPos((float)_target->GetX(), (float)_target->GetY());
if(fabs(Vec2::Distance(targetPos, realPos)) <= CHARACTER_SPEED) {
_target = _astar.GetSolutionNext();
if(_target == NULL || _target == _lastTarget) {
_walkInPath = false;
} else {
tileX = _target->GetX() / AStarTile::FAKE_SIZE;
tileY = _target->GetY() / AStarTile::FAKE_SIZE;
}
}
}
}
void NPC::OnPlayerMove(Player* player) {
AStarTile& start = map->GetAStarTile(x / AStarTile::FAKE_SIZE, y / AStarTile::FAKE_SIZE);
AStarTile& goal = map->GetAStarTile(player->GetX() / AStarTile::FAKE_SIZE, player->GetY() / AStarTile::FAKE_SIZE);
AStarTile& start = map->GetAStarTile(x / AStarTile::FAKE_SIZE, y / AStarTile::FAKE_SIZE);
AStarTile& goal = map->GetAStarTile(player->GetX() / AStarTile::FAKE_SIZE, player->GetY() / AStarTile::FAKE_SIZE);
_astar.SetStartAndGoalStates(start, goal);
_astar.SetStartAndGoalStates(start, goal);
_walkInPath = false;
_walkInPath = false;
// Dirty loop to calculate the path
while(true) {
unsigned int state = _astar.SearchStep();
if(state == AStarSearch<AStarTile>::SEARCH_STATE_SUCCEEDED) {
_walkInPath = true;
break;
} else if(state != AStarSearch<AStarTile>::SEARCH_STATE_SEARCHING) {
break;
}
}
// Dirty loop to calculate the path
while(true) {
unsigned int state = _astar.SearchStep();
if(state == AStarSearch<AStarTile>::SEARCH_STATE_SUCCEEDED) {
_walkInPath = true;
break;
} else if(state != AStarSearch<AStarTile>::SEARCH_STATE_SEARCHING) {
break;
}
}
_lastTarget = _astar.GetSolutionEnd();
_target = _astar.GetSolutionStart();
_target = _astar.GetSolutionNext();
if(!_target) {
_walkInPath = false;
} else {
tileX = _target->GetX() / AStarTile::FAKE_SIZE;
tileY = _target->GetY() / AStarTile::FAKE_SIZE;
}
_lastTarget = _astar.GetSolutionEnd();
_target = _astar.GetSolutionStart();
_target = _astar.GetSolutionNext();
if(!_target) {
_walkInPath = false;
} else {
tileX = _target->GetX() / AStarTile::FAKE_SIZE;
tileY = _target->GetY() / AStarTile::FAKE_SIZE;
}
}