[Fix] Changed abs(Vec2::DistanceSquared(thisPos, goalPos)); to fabs(Vec2::DistanceSquared(thisPos, goalPos));

This commit is contained in:
Rtch90 2012-02-18 23:49:13 +00:00
parent 8b92224e72
commit 7ca0ad0d7d
3 changed files with 156 additions and 156 deletions

View File

@ -1,12 +1,12 @@
#include "NPC.h"
#include "../Unuk/Player.h"
#include "../../Unuk/Player.h"
NPC::NPC(LevelGen* mapArg) : Character(mapArg) {
_moveTimer.Start();
_moveChangeFrequency = 14000;
_moveDurationMax = 3000;
_moveDurationMin = 1000;
_moveTimer.Start();
_moveChangeFrequency = 14000;
_moveDurationMax = 3000;
_moveDurationMin = 1000;
}
NPC::~NPC(void) {
@ -14,80 +14,80 @@ NPC::~NPC(void) {
}
void NPC::Update(void) {
// Store the NPC's health.
// int health = GetHealth(); // not referenced
// Store the NPC's health.
// int health = GetHealth(); // not referenced
Move();
Move();
if(xVel > 0) directionFacing = FACING_RIGHT;
else if(xVel < 0) directionFacing = FACING_LEFT;
else if(yVel > 0) directionFacing = FACING_DOWN;
else if(yVel < 0) directionFacing = FACING_UP;
if(xVel > 0) directionFacing = FACING_RIGHT;
else if(xVel < 0) directionFacing = FACING_LEFT;
else if(yVel > 0) directionFacing = FACING_DOWN;
else if(yVel < 0) directionFacing = FACING_UP;
_healthBar.SetProgress((float)GetHealth() / 100.0f);
_healthBar.SetProgress((float)GetHealth() / 100.0f);
}
void NPC::Move(void) {
if(_moving && _moveTimer.GetTicks() > _moveDurationMax) {
xVel = 0.0f;
yVel = 0.0f;
_moving = false;
}
if(_moving && _moveTimer.GetTicks() >= _moveDurationCurrent) {
xVel = 0.0f;
yVel = 0.0f;
_moving = false;
}
if(_moveTimer.GetTicks() > _moveChangeFrequency) {
_moveTimer.Start();
_moveDurationCurrent = _moveDurationMin + (rand() % (_moveDurationMax - _moveDurationMin));
if(rand() % 2) {
yVel = 0.0f;
if(rand() % 2)
xVel = CHARACTER_SPEED;
else
xVel = -CHARACTER_SPEED;
} else {
xVel = 0.0f;
if(rand() % 2)
yVel = CHARACTER_SPEED;
else
yVel = -CHARACTER_SPEED;
}
_moving = true;
}
map->MoveIfPossible(this, xVel, yVel, false);
Character::HealthBarScroll();
if(_moving && _moveTimer.GetTicks() > _moveDurationMax) {
xVel = 0.0f;
yVel = 0.0f;
_moving = false;
}
if(_moving && _moveTimer.GetTicks() >= _moveDurationCurrent) {
xVel = 0.0f;
yVel = 0.0f;
_moving = false;
}
if(_moveTimer.GetTicks() > _moveChangeFrequency) {
_moveTimer.Start();
_moveDurationCurrent = _moveDurationMin + (rand() % (_moveDurationMax - _moveDurationMin));
if(rand() % 2) {
yVel = 0.0f;
if(rand() % 2)
xVel = CHARACTER_SPEED;
else
xVel = -CHARACTER_SPEED;
} else {
xVel = 0.0f;
if(rand() % 2)
yVel = CHARACTER_SPEED;
else
yVel = -CHARACTER_SPEED;
}
_moving = true;
}
map->MoveIfPossible(this, xVel, yVel, false);
Character::HealthBarScroll();
}
void NPC::OnPlayerMove(Player* player) {
MapTile& start = map->GetTile(x / TILE_WIDTH, y / TILE_HEIGHT);
MapTile& goal = map->GetTile(player->GetX() / TILE_WIDTH, player->GetY() / TILE_HEIGHT);
MapTile& start = map->GetTile(x / TILE_WIDTH, y / TILE_HEIGHT);
MapTile& goal = map->GetTile(player->GetX() / TILE_WIDTH, player->GetY() / TILE_HEIGHT);
_astar.SetStartAndGoalStates(start, goal);
_astar.SetStartAndGoalStates(start, goal);
bool solutionFound = false;
bool solutionFound = false;
// Dirty loop to calculate the path
while(true) {
_astar.SearchStep();
// Dirty loop to calculate the path
while(true) {
_astar.SearchStep();
int state = _astar.GetState();
if(state == AStarSearch<MapTile>::SEARCH_STATE_SUCCEEDED) {
solutionFound = true;
break;
} else if(state == AStarSearch<MapTile>::SEARCH_STATE_SEARCHING) {
continue;
} else {
break;
}
}
int state = _astar.GetState();
if(state == AStarSearch<MapTile>::SEARCH_STATE_SUCCEEDED) {
solutionFound = true;
break;
} else if(state == AStarSearch<MapTile>::SEARCH_STATE_SEARCHING) {
continue;
} else {
break;
}
}
if(solutionFound) {
x = _astar.GetSolutionEnd()->GetTileX();
y = _astar.GetSolutionEnd()->GetTileY();
}
if(solutionFound) {
x = _astar.GetSolutionEnd()->GetTileX();
y = _astar.GetSolutionEnd()->GetTileY();
}
}

View File

@ -2,67 +2,67 @@
#include "LevelGen.h"
MapTile::MapTile(const MapTile& source) {
_level = source._level;
_tile = source._tile;
_entity = source._entity;
_zLevel = source._zLevel;
_level = source._level;
_tile = source._tile;
_entity = source._entity;
_zLevel = source._zLevel;
}
bool MapTile::IsSameState(MapTile& tile) {
return (tile.GetTileX() == _tile.GetX()) && (tile.GetTileY() == _tile.GetY());
return (tile.GetTileX() == _tile.GetX()) && (tile.GetTileY() == _tile.GetY());
}
bool MapTile::IsGoal(MapTile& tile) {
return IsSameState(tile);
return IsSameState(tile);
}
float MapTile::GoalDistanceEstimate(MapTile& goal) {
Vec2 thisPos(_tile.GetX(), _tile.GetY());
Vec2 goalPos(goal.GetTileX(), goal.GetTileY());
return abs(Vec2::DistanceSquared(thisPos, goalPos));
Vec2 thisPos(_tile.GetX(), _tile.GetY());
Vec2 goalPos(goal.GetTileX(), goal.GetTileY());
return fabs(Vec2::DistanceSquared(thisPos, goalPos));
}
float MapTile::GetCost(MapTile& goal) {
return 64.0f*64.0f;
return 64.0f*64.0f;
}
bool MapTile::GetSuccessors(AStarSearch<MapTile>* search, MapTile* parent) {
int tileX = _tile.GetX() / TILE_WIDTH;
int tileY = _tile.GetY() / TILE_HEIGHT;
// Add tile to the left if possible.
if(tileX > 0) {
MapTile& successor = _level->GetTile(tileX - 1, tileY);
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
search->AddSuccessor(successor);
}
}
int tileX = _tile.GetX() / TILE_WIDTH;
int tileY = _tile.GetY() / TILE_HEIGHT;
// Add tile to the right if possible
// TODO: replace 64 with map width
if(tileX < 64) {
MapTile& successor = _level->GetTile(tileX + 1, tileY);
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
search->AddSuccessor(successor);
}
}
// Add tile to the left if possible.
if(tileX > 0) {
MapTile& successor = _level->GetTile(tileX - 1, tileY);
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
search->AddSuccessor(successor);
}
}
// Add tile to the bottom if possible
if(tileY > 0) {
MapTile& successor = _level->GetTile(tileX, tileY - 1);
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
search->AddSuccessor(successor);
}
}
// Add tile to the right if possible
// TODO: replace 64 with map width
if(tileX < 64) {
MapTile& successor = _level->GetTile(tileX + 1, tileY);
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
search->AddSuccessor(successor);
}
}
// Add tile to the top if possible
// TODO: replace 64 with map height
if(tileY < 64) {
MapTile& successor = _level->GetTile(tileX, tileY + 1);
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
search->AddSuccessor(successor);
}
}
// Add tile to the bottom if possible
if(tileY > 0) {
MapTile& successor = _level->GetTile(tileX, tileY - 1);
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
search->AddSuccessor(successor);
}
}
return true;
// Add tile to the top if possible
// TODO: replace 64 with map height
if(tileY < 64) {
MapTile& successor = _level->GetTile(tileX, tileY + 1);
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
search->AddSuccessor(successor);
}
}
return true;
}

View File

@ -15,57 +15,57 @@ class LevelGen;
class MapTile {
public:
MapTile(LevelGen* level = NULL) : _level(level) { }
~MapTile(void) { }
MapTile(LevelGen* level = NULL) : _level(level) { }
~MapTile(void) { }
void Render(void) { _tile.Render(), _entity.Render(); }
void Render(void) { _tile.Render(), _entity.Render(); }
// Tile Mutators.
SDL_Surface* SetTileTexture(SDL_Surface* arg) { _tile.SetTexture(arg); return NULL; }
void SetTileTextureName(string path) { _tile.SetTextureName(path); }
string GetTileTextureName(void) { return _tile.GetTextureName(); }
void SetTileSolidity(bool arg) { _tile.SetSolidity(arg); }
bool GetTileSolidity(void) { return _tile.GetSolidity(); }
// Well, it kinda helps if I lay the
// tiles rather than just get the
// return value right??
void SetTileXY(int xArg, int yArg) { _tile.SetXY(xArg, yArg); }
int GetTileX(void) { return _tile.GetX(); }
int GetTileY(void) { return _tile.GetY(); }
// Tile Mutators.
SDL_Surface* SetTileTexture(SDL_Surface* arg) { _tile.SetTexture(arg); return NULL; }
void SetTileTextureName(string path) { _tile.SetTextureName(path); }
string GetTileTextureName(void) { return _tile.GetTextureName(); }
void SetTileSolidity(bool arg) { _tile.SetSolidity(arg); }
bool GetTileSolidity(void) { return _tile.GetSolidity(); }
// Well, it kinda helps if I lay the
// tiles rather than just get the
// return value right??
void SetTileXY(int xArg, int yArg) { _tile.SetXY(xArg, yArg); }
int GetTileX(void) { return _tile.GetX(); }
int GetTileY(void) { return _tile.GetY(); }
// Entity Mutators.
void SetEntityTexture(SDL_Surface* arg) { _entity.SetTexture(arg); }
void SetEntityTextureName(string path) { _entity.SetTextureName(path); }
void SetEntityXY(int xArg, int yArg) { _entity.SetXY(xArg, yArg); }
void SetEntitySolidity(bool arg) { _entity.SetSolidity(arg); }
bool GetEntitySolitity(void) { return _entity.GetSolidity(); }
// Entity Mutators.
int GetEntityX(void) { return _entity.GetX(); }
int GetEntityY(void) { return _entity.GetY(); }
int GetEntityWidth(void) { return _entity.GetWidth(); }
int GetEntityHeight(void) { return _entity.GetHeight(); }
string GetEntityTextureName(void) { return _entity.GetTextureName(); }
// Entity Mutators.
void SetEntityTexture(SDL_Surface* arg) { _entity.SetTexture(arg); }
void SetEntityTextureName(string path) { _entity.SetTextureName(path); }
void SetEntityXY(int xArg, int yArg) { _entity.SetXY(xArg, yArg); }
void SetEntitySolidity(bool arg) { _entity.SetSolidity(arg); }
bool GetEntitySolitity(void) { return _entity.GetSolidity(); }
// ZLevel Mutators.
void SetZLevel(int arg) { _zLevel = arg; }
int GetZLevel(void) { return _zLevel; }
// Entity Mutators.
int GetEntityX(void) { return _entity.GetX(); }
int GetEntityY(void) { return _entity.GetY(); }
int GetEntityWidth(void) { return _entity.GetWidth(); }
int GetEntityHeight(void) { return _entity.GetHeight(); }
string GetEntityTextureName(void) { return _entity.GetTextureName(); }
// Pathfinding stuff.
MapTile(const MapTile& source);
bool IsSameState(MapTile& tile);
bool IsGoal(MapTile& tile);
float GoalDistanceEstimate(MapTile& goal);
float GetCost(MapTile& goal);
bool GetSuccessors(AStarSearch<MapTile>* search, MapTile* parent);
// ZLevel Mutators.
void SetZLevel(int arg) { _zLevel = arg; }
int GetZLevel(void) { return _zLevel; }
// Pathfinding stuff.
MapTile(const MapTile& source);
bool IsSameState(MapTile& tile);
bool IsGoal(MapTile& tile);
float GoalDistanceEstimate(MapTile& goal);
float GetCost(MapTile& goal);
bool GetSuccessors(AStarSearch<MapTile>* search, MapTile* parent);
private:
LevelGen* _level;
MapElement _tile;
MapEntityGeneric _entity;
LevelGen* _level;
MapElement _tile;
MapEntityGeneric _entity;
// -1 is a 'special' tile, the next tile that the player walks
// on is the players new zlevel.
int _zLevel;
// -1 is a 'special' tile, the next tile that the player walks
// on is the players new zlevel.
int _zLevel;
};