[Fix] Changed abs(Vec2::DistanceSquared(thisPos, goalPos)); to fabs(Vec2::DistanceSquared(thisPos, goalPos));
This commit is contained in:
parent
8b92224e72
commit
7ca0ad0d7d
@ -1,12 +1,12 @@
|
|||||||
#include "NPC.h"
|
#include "NPC.h"
|
||||||
#include "../Unuk/Player.h"
|
#include "../../Unuk/Player.h"
|
||||||
|
|
||||||
NPC::NPC(LevelGen* mapArg) : Character(mapArg) {
|
NPC::NPC(LevelGen* mapArg) : Character(mapArg) {
|
||||||
_moveTimer.Start();
|
_moveTimer.Start();
|
||||||
|
|
||||||
_moveChangeFrequency = 14000;
|
_moveChangeFrequency = 14000;
|
||||||
_moveDurationMax = 3000;
|
_moveDurationMax = 3000;
|
||||||
_moveDurationMin = 1000;
|
_moveDurationMin = 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
NPC::~NPC(void) {
|
NPC::~NPC(void) {
|
||||||
@ -14,80 +14,80 @@ NPC::~NPC(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void NPC::Update(void) {
|
void NPC::Update(void) {
|
||||||
// Store the NPC's health.
|
// Store the NPC's health.
|
||||||
// int health = GetHealth(); // not referenced
|
// int health = GetHealth(); // not referenced
|
||||||
|
|
||||||
Move();
|
Move();
|
||||||
|
|
||||||
if(xVel > 0) directionFacing = FACING_RIGHT;
|
if(xVel > 0) directionFacing = FACING_RIGHT;
|
||||||
else if(xVel < 0) directionFacing = FACING_LEFT;
|
else if(xVel < 0) directionFacing = FACING_LEFT;
|
||||||
else if(yVel > 0) directionFacing = FACING_DOWN;
|
else if(yVel > 0) directionFacing = FACING_DOWN;
|
||||||
else if(yVel < 0) directionFacing = FACING_UP;
|
else if(yVel < 0) directionFacing = FACING_UP;
|
||||||
|
|
||||||
_healthBar.SetProgress((float)GetHealth() / 100.0f);
|
_healthBar.SetProgress((float)GetHealth() / 100.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NPC::Move(void) {
|
void NPC::Move(void) {
|
||||||
if(_moving && _moveTimer.GetTicks() > _moveDurationMax) {
|
if(_moving && _moveTimer.GetTicks() > _moveDurationMax) {
|
||||||
xVel = 0.0f;
|
xVel = 0.0f;
|
||||||
yVel = 0.0f;
|
yVel = 0.0f;
|
||||||
_moving = false;
|
_moving = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_moving && _moveTimer.GetTicks() >= _moveDurationCurrent) {
|
if(_moving && _moveTimer.GetTicks() >= _moveDurationCurrent) {
|
||||||
xVel = 0.0f;
|
xVel = 0.0f;
|
||||||
yVel = 0.0f;
|
yVel = 0.0f;
|
||||||
_moving = false;
|
_moving = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_moveTimer.GetTicks() > _moveChangeFrequency) {
|
if(_moveTimer.GetTicks() > _moveChangeFrequency) {
|
||||||
_moveTimer.Start();
|
_moveTimer.Start();
|
||||||
_moveDurationCurrent = _moveDurationMin + (rand() % (_moveDurationMax - _moveDurationMin));
|
_moveDurationCurrent = _moveDurationMin + (rand() % (_moveDurationMax - _moveDurationMin));
|
||||||
if(rand() % 2) {
|
if(rand() % 2) {
|
||||||
yVel = 0.0f;
|
yVel = 0.0f;
|
||||||
if(rand() % 2)
|
if(rand() % 2)
|
||||||
xVel = CHARACTER_SPEED;
|
xVel = CHARACTER_SPEED;
|
||||||
else
|
else
|
||||||
xVel = -CHARACTER_SPEED;
|
xVel = -CHARACTER_SPEED;
|
||||||
} else {
|
} else {
|
||||||
xVel = 0.0f;
|
xVel = 0.0f;
|
||||||
if(rand() % 2)
|
if(rand() % 2)
|
||||||
yVel = CHARACTER_SPEED;
|
yVel = CHARACTER_SPEED;
|
||||||
else
|
else
|
||||||
yVel = -CHARACTER_SPEED;
|
yVel = -CHARACTER_SPEED;
|
||||||
}
|
}
|
||||||
_moving = true;
|
_moving = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
map->MoveIfPossible(this, xVel, yVel, false);
|
map->MoveIfPossible(this, xVel, yVel, false);
|
||||||
Character::HealthBarScroll();
|
Character::HealthBarScroll();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NPC::OnPlayerMove(Player* player) {
|
void NPC::OnPlayerMove(Player* player) {
|
||||||
MapTile& start = map->GetTile(x / TILE_WIDTH, y / TILE_HEIGHT);
|
MapTile& start = map->GetTile(x / TILE_WIDTH, y / TILE_HEIGHT);
|
||||||
MapTile& goal = map->GetTile(player->GetX() / TILE_WIDTH, player->GetY() / 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
|
// Dirty loop to calculate the path
|
||||||
while(true) {
|
while(true) {
|
||||||
_astar.SearchStep();
|
_astar.SearchStep();
|
||||||
|
|
||||||
int state = _astar.GetState();
|
int state = _astar.GetState();
|
||||||
if(state == AStarSearch<MapTile>::SEARCH_STATE_SUCCEEDED) {
|
if(state == AStarSearch<MapTile>::SEARCH_STATE_SUCCEEDED) {
|
||||||
solutionFound = true;
|
solutionFound = true;
|
||||||
break;
|
break;
|
||||||
} else if(state == AStarSearch<MapTile>::SEARCH_STATE_SEARCHING) {
|
} else if(state == AStarSearch<MapTile>::SEARCH_STATE_SEARCHING) {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(solutionFound) {
|
if(solutionFound) {
|
||||||
x = _astar.GetSolutionEnd()->GetTileX();
|
x = _astar.GetSolutionEnd()->GetTileX();
|
||||||
y = _astar.GetSolutionEnd()->GetTileY();
|
y = _astar.GetSolutionEnd()->GetTileY();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,67 +2,67 @@
|
|||||||
#include "LevelGen.h"
|
#include "LevelGen.h"
|
||||||
|
|
||||||
MapTile::MapTile(const MapTile& source) {
|
MapTile::MapTile(const MapTile& source) {
|
||||||
_level = source._level;
|
_level = source._level;
|
||||||
_tile = source._tile;
|
_tile = source._tile;
|
||||||
_entity = source._entity;
|
_entity = source._entity;
|
||||||
_zLevel = source._zLevel;
|
_zLevel = source._zLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MapTile::IsSameState(MapTile& tile) {
|
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) {
|
bool MapTile::IsGoal(MapTile& tile) {
|
||||||
return IsSameState(tile);
|
return IsSameState(tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
float MapTile::GoalDistanceEstimate(MapTile& goal) {
|
float MapTile::GoalDistanceEstimate(MapTile& goal) {
|
||||||
Vec2 thisPos(_tile.GetX(), _tile.GetY());
|
Vec2 thisPos(_tile.GetX(), _tile.GetY());
|
||||||
Vec2 goalPos(goal.GetTileX(), goal.GetTileY());
|
Vec2 goalPos(goal.GetTileX(), goal.GetTileY());
|
||||||
return abs(Vec2::DistanceSquared(thisPos, goalPos));
|
return fabs(Vec2::DistanceSquared(thisPos, goalPos));
|
||||||
}
|
}
|
||||||
|
|
||||||
float MapTile::GetCost(MapTile& goal) {
|
float MapTile::GetCost(MapTile& goal) {
|
||||||
return 64.0f*64.0f;
|
return 64.0f*64.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MapTile::GetSuccessors(AStarSearch<MapTile>* search, MapTile* parent) {
|
bool MapTile::GetSuccessors(AStarSearch<MapTile>* search, MapTile* parent) {
|
||||||
int tileX = _tile.GetX() / TILE_WIDTH;
|
int tileX = _tile.GetX() / TILE_WIDTH;
|
||||||
int tileY = _tile.GetY() / TILE_HEIGHT;
|
int tileY = _tile.GetY() / TILE_HEIGHT;
|
||||||
|
|
||||||
// Add tile to the left if possible.
|
// Add tile to the left if possible.
|
||||||
if(tileX > 0) {
|
if(tileX > 0) {
|
||||||
MapTile& successor = _level->GetTile(tileX - 1, tileY);
|
MapTile& successor = _level->GetTile(tileX - 1, tileY);
|
||||||
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
|
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
|
||||||
search->AddSuccessor(successor);
|
search->AddSuccessor(successor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add tile to the right if possible
|
// Add tile to the right if possible
|
||||||
// TODO: replace 64 with map width
|
// TODO: replace 64 with map width
|
||||||
if(tileX < 64) {
|
if(tileX < 64) {
|
||||||
MapTile& successor = _level->GetTile(tileX + 1, tileY);
|
MapTile& successor = _level->GetTile(tileX + 1, tileY);
|
||||||
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
|
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
|
||||||
search->AddSuccessor(successor);
|
search->AddSuccessor(successor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add tile to the bottom if possible
|
// Add tile to the bottom if possible
|
||||||
if(tileY > 0) {
|
if(tileY > 0) {
|
||||||
MapTile& successor = _level->GetTile(tileX, tileY - 1);
|
MapTile& successor = _level->GetTile(tileX, tileY - 1);
|
||||||
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
|
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
|
||||||
search->AddSuccessor(successor);
|
search->AddSuccessor(successor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add tile to the top if possible
|
// Add tile to the top if possible
|
||||||
// TODO: replace 64 with map height
|
// TODO: replace 64 with map height
|
||||||
if(tileY < 64) {
|
if(tileY < 64) {
|
||||||
MapTile& successor = _level->GetTile(tileX, tileY + 1);
|
MapTile& successor = _level->GetTile(tileX, tileY + 1);
|
||||||
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
|
if(successor.GetTileSolidity() || successor.GetEntitySolitity()) {
|
||||||
search->AddSuccessor(successor);
|
search->AddSuccessor(successor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -15,57 +15,57 @@ class LevelGen;
|
|||||||
|
|
||||||
class MapTile {
|
class MapTile {
|
||||||
public:
|
public:
|
||||||
MapTile(LevelGen* level = NULL) : _level(level) { }
|
MapTile(LevelGen* level = NULL) : _level(level) { }
|
||||||
~MapTile(void) { }
|
~MapTile(void) { }
|
||||||
|
|
||||||
void Render(void) { _tile.Render(), _entity.Render(); }
|
void Render(void) { _tile.Render(), _entity.Render(); }
|
||||||
|
|
||||||
// Tile Mutators.
|
// Tile Mutators.
|
||||||
SDL_Surface* SetTileTexture(SDL_Surface* arg) { _tile.SetTexture(arg); return NULL; }
|
SDL_Surface* SetTileTexture(SDL_Surface* arg) { _tile.SetTexture(arg); return NULL; }
|
||||||
void SetTileTextureName(string path) { _tile.SetTextureName(path); }
|
void SetTileTextureName(string path) { _tile.SetTextureName(path); }
|
||||||
string GetTileTextureName(void) { return _tile.GetTextureName(); }
|
string GetTileTextureName(void) { return _tile.GetTextureName(); }
|
||||||
void SetTileSolidity(bool arg) { _tile.SetSolidity(arg); }
|
void SetTileSolidity(bool arg) { _tile.SetSolidity(arg); }
|
||||||
bool GetTileSolidity(void) { return _tile.GetSolidity(); }
|
bool GetTileSolidity(void) { return _tile.GetSolidity(); }
|
||||||
// Well, it kinda helps if I lay the
|
// Well, it kinda helps if I lay the
|
||||||
// tiles rather than just get the
|
// tiles rather than just get the
|
||||||
// return value right??
|
// return value right??
|
||||||
void SetTileXY(int xArg, int yArg) { _tile.SetXY(xArg, yArg); }
|
void SetTileXY(int xArg, int yArg) { _tile.SetXY(xArg, yArg); }
|
||||||
int GetTileX(void) { return _tile.GetX(); }
|
int GetTileX(void) { return _tile.GetX(); }
|
||||||
int GetTileY(void) { return _tile.GetY(); }
|
int GetTileY(void) { return _tile.GetY(); }
|
||||||
|
|
||||||
|
|
||||||
// Entity Mutators.
|
// Entity Mutators.
|
||||||
void SetEntityTexture(SDL_Surface* arg) { _entity.SetTexture(arg); }
|
void SetEntityTexture(SDL_Surface* arg) { _entity.SetTexture(arg); }
|
||||||
void SetEntityTextureName(string path) { _entity.SetTextureName(path); }
|
void SetEntityTextureName(string path) { _entity.SetTextureName(path); }
|
||||||
void SetEntityXY(int xArg, int yArg) { _entity.SetXY(xArg, yArg); }
|
void SetEntityXY(int xArg, int yArg) { _entity.SetXY(xArg, yArg); }
|
||||||
void SetEntitySolidity(bool arg) { _entity.SetSolidity(arg); }
|
void SetEntitySolidity(bool arg) { _entity.SetSolidity(arg); }
|
||||||
bool GetEntitySolitity(void) { return _entity.GetSolidity(); }
|
bool GetEntitySolitity(void) { return _entity.GetSolidity(); }
|
||||||
|
|
||||||
// Entity Mutators.
|
// Entity Mutators.
|
||||||
int GetEntityX(void) { return _entity.GetX(); }
|
int GetEntityX(void) { return _entity.GetX(); }
|
||||||
int GetEntityY(void) { return _entity.GetY(); }
|
int GetEntityY(void) { return _entity.GetY(); }
|
||||||
int GetEntityWidth(void) { return _entity.GetWidth(); }
|
int GetEntityWidth(void) { return _entity.GetWidth(); }
|
||||||
int GetEntityHeight(void) { return _entity.GetHeight(); }
|
int GetEntityHeight(void) { return _entity.GetHeight(); }
|
||||||
string GetEntityTextureName(void) { return _entity.GetTextureName(); }
|
string GetEntityTextureName(void) { return _entity.GetTextureName(); }
|
||||||
|
|
||||||
// ZLevel Mutators.
|
// ZLevel Mutators.
|
||||||
void SetZLevel(int arg) { _zLevel = arg; }
|
void SetZLevel(int arg) { _zLevel = arg; }
|
||||||
int GetZLevel(void) { return _zLevel; }
|
int GetZLevel(void) { return _zLevel; }
|
||||||
|
|
||||||
// Pathfinding stuff.
|
// Pathfinding stuff.
|
||||||
MapTile(const MapTile& source);
|
MapTile(const MapTile& source);
|
||||||
bool IsSameState(MapTile& tile);
|
bool IsSameState(MapTile& tile);
|
||||||
bool IsGoal(MapTile& tile);
|
bool IsGoal(MapTile& tile);
|
||||||
float GoalDistanceEstimate(MapTile& goal);
|
float GoalDistanceEstimate(MapTile& goal);
|
||||||
float GetCost(MapTile& goal);
|
float GetCost(MapTile& goal);
|
||||||
bool GetSuccessors(AStarSearch<MapTile>* search, MapTile* parent);
|
bool GetSuccessors(AStarSearch<MapTile>* search, MapTile* parent);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LevelGen* _level;
|
LevelGen* _level;
|
||||||
MapElement _tile;
|
MapElement _tile;
|
||||||
MapEntityGeneric _entity;
|
MapEntityGeneric _entity;
|
||||||
|
|
||||||
// -1 is a 'special' tile, the next tile that the player walks
|
// -1 is a 'special' tile, the next tile that the player walks
|
||||||
// on is the players new zlevel.
|
// on is the players new zlevel.
|
||||||
int _zLevel;
|
int _zLevel;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user