[Fix] Change calculation of absolute value to support floating point variables.
This commit is contained in:
parent
2e50097502
commit
61f1f2b2a4
@ -3,8 +3,8 @@
|
|||||||
#include "../System/Vec2.h"
|
#include "../System/Vec2.h"
|
||||||
|
|
||||||
NPC::NPC(LevelGen* mapArg) : Character(mapArg) {
|
NPC::NPC(LevelGen* mapArg) : Character(mapArg) {
|
||||||
_walkInPath = false;
|
_walkInPath = false;
|
||||||
_moving = false;
|
_moving = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
NPC::~NPC(void) {
|
NPC::~NPC(void) {
|
||||||
@ -12,8 +12,8 @@ NPC::~NPC(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void NPC::ForceMove(void) {
|
void NPC::ForceMove(void) {
|
||||||
tileX = x / AStarTile::FAKE_SIZE;
|
tileX = x / AStarTile::FAKE_SIZE;
|
||||||
tileY = y / AStarTile::FAKE_SIZE;
|
tileY = y / AStarTile::FAKE_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NPC::Update(void) {
|
void NPC::Update(void) {
|
||||||
@ -31,83 +31,83 @@ void NPC::Update(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void NPC::Move(void) {
|
void NPC::Move(void) {
|
||||||
Vec2 realPos(x, y);
|
Vec2 realPos(x, y);
|
||||||
Vec2 tilePos(tileX, tileY);
|
Vec2 tilePos(tileX, tileY);
|
||||||
|
|
||||||
if(realPos != tilePos) {
|
if(realPos != tilePos) {
|
||||||
float targetX = (float)(tileX * AStarTile::FAKE_SIZE);
|
float targetX = (float)(tileX * AStarTile::FAKE_SIZE);
|
||||||
float targetY = (float)(tileY * AStarTile::FAKE_SIZE);
|
float targetY = (float)(tileY * AStarTile::FAKE_SIZE);
|
||||||
|
|
||||||
float dx = targetX - realPos.x;
|
float dx = targetX - realPos.x;
|
||||||
float dy = targetY - realPos.y;
|
float dy = targetY - realPos.y;
|
||||||
|
|
||||||
float distance = sqrtf(dx*dx + dy*dy);
|
float distance = sqrtf(dx*dx + dy*dy);
|
||||||
|
|
||||||
if(fabs(distance) > CHARACTER_SPEED) {
|
if(fabs(distance) > CHARACTER_SPEED) {
|
||||||
dx /= distance;
|
dx /= distance;
|
||||||
dy /= distance;
|
dy /= distance;
|
||||||
dx *= CHARACTER_SPEED;
|
dx *= CHARACTER_SPEED;
|
||||||
dy *= CHARACTER_SPEED;
|
dy *= CHARACTER_SPEED;
|
||||||
|
|
||||||
xVel = dx;
|
|
||||||
yVel = dy;
|
|
||||||
|
|
||||||
map->MoveIfPossible(this, xVel, yVel, false);
|
xVel = dx;
|
||||||
|
yVel = dy;
|
||||||
|
|
||||||
Character::HealthBarScroll();
|
map->MoveIfPossible(this, xVel, yVel, false);
|
||||||
|
|
||||||
_moving = true;
|
Character::HealthBarScroll();
|
||||||
} else {
|
|
||||||
xVel = 0.0f;
|
|
||||||
yVel = 0.0f;
|
|
||||||
_moving = false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
xVel = 0.0f;
|
|
||||||
yVel = 0.0f;
|
|
||||||
_moving = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(_walkInPath && !_moving) {
|
_moving = true;
|
||||||
Vec2 targetPos((float)_target->GetX(), (float)_target->GetY());
|
} else {
|
||||||
if(abs(Vec2::Distance(targetPos, realPos)) <= CHARACTER_SPEED) {
|
xVel = 0.0f;
|
||||||
_target = _astar.GetSolutionNext();
|
yVel = 0.0f;
|
||||||
if(_target == NULL || _target == _lastTarget) {
|
_moving = false;
|
||||||
_walkInPath = false;
|
}
|
||||||
} else {
|
} else {
|
||||||
tileX = _target->GetX() / AStarTile::FAKE_SIZE;
|
xVel = 0.0f;
|
||||||
tileY = _target->GetY() / AStarTile::FAKE_SIZE;
|
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) {
|
void NPC::OnPlayerMove(Player* player) {
|
||||||
AStarTile& start = map->GetAStarTile(x / AStarTile::FAKE_SIZE, y / 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);
|
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
|
// Dirty loop to calculate the path
|
||||||
while(true) {
|
while(true) {
|
||||||
unsigned int state = _astar.SearchStep();
|
unsigned int state = _astar.SearchStep();
|
||||||
if(state == AStarSearch<AStarTile>::SEARCH_STATE_SUCCEEDED) {
|
if(state == AStarSearch<AStarTile>::SEARCH_STATE_SUCCEEDED) {
|
||||||
_walkInPath = true;
|
_walkInPath = true;
|
||||||
break;
|
break;
|
||||||
} else if(state != AStarSearch<AStarTile>::SEARCH_STATE_SEARCHING) {
|
} else if(state != AStarSearch<AStarTile>::SEARCH_STATE_SEARCHING) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_lastTarget = _astar.GetSolutionEnd();
|
_lastTarget = _astar.GetSolutionEnd();
|
||||||
_target = _astar.GetSolutionStart();
|
_target = _astar.GetSolutionStart();
|
||||||
_target = _astar.GetSolutionNext();
|
_target = _astar.GetSolutionNext();
|
||||||
if(!_target) {
|
if(!_target) {
|
||||||
_walkInPath = false;
|
_walkInPath = false;
|
||||||
} else {
|
} else {
|
||||||
tileX = _target->GetX() / AStarTile::FAKE_SIZE;
|
tileX = _target->GetX() / AStarTile::FAKE_SIZE;
|
||||||
tileY = _target->GetY() / AStarTile::FAKE_SIZE;
|
tileY = _target->GetY() / AStarTile::FAKE_SIZE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user