Skip to content

Commit

Permalink
- players are no longer passable
Browse files Browse the repository at this point in the history
- refactor items/players to use int32_t properties instead of int16_t
- update pathFinder code
- update player code
- update properties
- update walk code
- use stint16 everywhere
- update gamefiles (player now walks at normal speed, etc)
  • Loading branch information
demo committed Apr 26, 2017
1 parent 35ab0d7 commit 4df29d1
Show file tree
Hide file tree
Showing 14 changed files with 384 additions and 147 deletions.
2 changes: 1 addition & 1 deletion gamefiles/level/town/levelObjects.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{
"id": "l15",
"name": "Down to hell",
"mapPosition": [41, 80],
"mapPosition": [41, 79],
"texture": "empty",
"textureRect": [32, 32],
"action": { "name": "load", "file": ["level/l15/load.json", "positionPlayer", "[7, 7]"] }
Expand Down
2 changes: 1 addition & 1 deletion gamefiles/level/town/players2.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"id": "griswold",
"class": "blacksmith",
"name": "Griswold the blacksmith",
"mapPosition": [62, 64],
"mapPosition": [62, 63],
"action": { "name": "load", "file": "towners/griswold/panel.json" },
"inventory": [
{
Expand Down
27 changes: 27 additions & 0 deletions src/Actions/ActPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class ActPlayerMove : public Action
auto player = level->getPlayerOrCurrent(idPlayer);
if (player != nullptr)
{
player->clearWalkPath();
player->MapPosition(*level, position);
if (resetDirection == true)
{
Expand Down Expand Up @@ -221,3 +222,29 @@ class ActPlayerSetProperty : public Action
return true;
}
};

class ActPlayerSetSpeed : public Action
{
private:
std::string idPlayer;
std::string idLevel;
int speed;

public:
ActPlayerSetSpeed(const std::string& idPlayer_, const std::string& idLevel_,
int speed_) : idPlayer(idPlayer_), idLevel(idLevel_), speed(speed_) {}

virtual bool execute(Game& game)
{
auto level = game.Resources().getLevel(idLevel);
if (level != nullptr)
{
auto player = level->getPlayerOrCurrent(idPlayer);
if (player != nullptr)
{
player->setWalkSpeed(speed);
}
}
return true;
}
};
6 changes: 3 additions & 3 deletions src/Game/Item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ void Item::executeAction(Game& game) const

void Item::MapPosition(Level& level, const MapCoord& pos)
{
auto oldObj = level.Map()[mapPosition.x][mapPosition.y].getObject(this);
level.Map()[mapPosition.x][mapPosition.y].deleteObject(this);
auto oldObj = level.Map()[mapPosition].getObject(this);
level.Map()[mapPosition].deleteObject(this);
mapPosition = pos;
level.Map()[mapPosition.x][mapPosition.y].addFront(oldObj);
level.Map()[mapPosition].addFront(oldObj);
}

void Item::update(Game& game, Level& level)
Expand Down
2 changes: 2 additions & 0 deletions src/Game/LevelCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class LevelCell
std::shared_ptr<LevelObject> back() const;
std::shared_ptr<LevelObject> front() const;

bool hasObjects() const { return objects.empty() == false; }

std::shared_ptr<LevelObject> getObject(LevelObject* obj) const;

template <class T>
Expand Down
49 changes: 38 additions & 11 deletions src/Game/LevelMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,46 +154,73 @@ MapCoord LevelMap::getTile(const sf::Vector2f& coords) const
return MapCoord((Coord)isoPosX, (Coord)isoPosY);
}

std::queue<MapCoord> LevelMap::getPath(const MapCoord& a, const MapCoord& b) const
std::vector<MapCoord> LevelMap::getPath(const MapCoord& a, const MapCoord& b) const
{
std::queue<MapCoord> path;
std::vector<MapCoord> path;

if (a == b)
{
path.push_back(a);
return path;
}

MapSearchNode start(this, a.x, a.y, PlayerDirection::All);
MapSearchNode end(this, b.x, b.y, PlayerDirection::All);
MapSearchNode endOrig(end);

if (end.IsPassableIgnoreObject() == false)
if (end.IsValid() == false)
{
return path;
}
if (end.IsPassable() == false)
{
if (((*this)[b]).hasObjects() == true)
{
if (start.GoalDistanceEstimateC(end) == 1.f ||
getNearestPassableEndNode(start, end) == false)
{
path.push_back(b);
return path;
}
if (end.IsPassableIgnoreObject() == false)
{
return path;
}
}
else
{
return path;
}
}

PathFinder pathFinder(PATH_FINDER_MAX);
pathFinder.SetStartAndGoalStates(start, end);

unsigned int SearchState;

do
{
SearchState = pathFinder.SearchStep();

auto StepCount = pathFinder.GetStepCount();
if (StepCount == PATH_FINDER_MAX)
if (pathFinder.GetStepCount() == PATH_FINDER_MAX)
{
pathFinder.CancelSearch();
}
} while (SearchState == PathFinder::SEARCH_STATE_SEARCHING);

if (SearchState == PathFinder::SEARCH_STATE_SUCCEEDED)
{
auto node = pathFinder.GetSolutionStart();

if (endOrig.IsPassable() == false)
{
path.push_back(MapCoord(endOrig.x, endOrig.y));
}
auto node = pathFinder.GetSolutionEnd();
while (true)
{
if (node == nullptr)
{
break;
}
path.push(MapCoord(node->x, node->y));
node = pathFinder.GetSolutionNext();
path.push_back(MapCoord(node->x, node->y));
node = pathFinder.GetSolutionPrev();
};
pathFinder.FreeSolutionNodes();
}
Expand Down
6 changes: 4 additions & 2 deletions src/Game/LevelMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "Helper2D.h"
#include "LevelCell.h"
#include "MapCoord.h"
#include <queue>
#include "TileSet.h"
#include "Sol.h"
#include <vector>
Expand Down Expand Up @@ -44,6 +43,9 @@ class LevelMap
return Misc::Helper2D<const LevelMap, const LevelCell&, Coord>(*this, x, get);
}

LevelCell& operator[] (MapCoord coord) { return get(coord.x, coord.y, *this); }
const LevelCell& operator[] (MapCoord coord) const { return get(coord.x, coord.y, *this); }

Coord Width() const { return mapSize.x; }
Coord Height() const { return mapSize.y; }

Expand All @@ -54,5 +56,5 @@ class LevelMap
sf::Vector2f getCoord(const MapCoord& tile) const;
MapCoord getTile(const sf::Vector2f& coords) const;

std::queue<MapCoord> getPath(const MapCoord& a, const MapCoord& b) const;
std::vector<MapCoord> getPath(const MapCoord& a, const MapCoord& b) const;
};
93 changes: 87 additions & 6 deletions src/Game/PathFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
#include <cmath>
#include "LevelMap.h"

bool MapSearchNode::IsPassableIgnoreObject()
bool MapSearchNode::IsValid() const
{
if (x >= 0 &&
return (x >= 0 &&
x < map->Width() &&
y >= 0 &&
y < map->Height())
y < map->Height());
}

bool MapSearchNode::IsPassableIgnoreObject() const
{
if (IsValid() == true)
{
return (*map)[(Coord)x][(Coord)y].PassableIgnoreObject();
}
return false;
}

bool MapSearchNode::IsPassable(int16_t x_, int16_t y_)
bool MapSearchNode::IsPassable(int16_t x_, int16_t y_) const
{
if (x_ >= 0 &&
x_ < map->Width() &&
Expand All @@ -31,11 +36,16 @@ bool MapSearchNode::IsSameState(MapSearchNode& rhs)
return ((x == rhs.x) && (y == rhs.y));
}

float MapSearchNode::GoalDistanceEstimate(MapSearchNode& nodeGoal)
float MapSearchNode::GoalDistanceEstimateC(const MapSearchNode& nodeGoal) const
{
return (float)(std::abs(x - nodeGoal.x) + std::abs(y - nodeGoal.y));
}

float MapSearchNode::GoalDistanceEstimate(MapSearchNode& nodeGoal)
{
return GoalDistanceEstimateC(nodeGoal);
}

bool MapSearchNode::IsGoal(MapSearchNode& nodeGoal)
{
return ((x == nodeGoal.x) && (y == nodeGoal.y));
Expand Down Expand Up @@ -97,11 +107,82 @@ bool MapSearchNode::GetSuccessors(AStarSearch<MapSearchNode>* astarsearch, MapSe
return true;
}

float MapSearchNode::GetCost(MapSearchNode& successor)
float MapSearchNode::GetCost() const
{
if (IsPassable(x, y) == true)
{
return 1.f;
}
return 9.f;
}

float MapSearchNode::GetCost(MapSearchNode& successor)
{
return GetCost();
}

const bool addNearestSuccessor(std::vector<MapSearchNode>& neighbours,
int16_t x_, int16_t y_, MapSearchNode parent)
{
MapSearchNode neighbour(parent);
neighbour.x = x_;
neighbour.y = y_;
if ((neighbour.IsPassable() == true) && !((parent.x == x_) && (parent.y == y_)))
{
neighbours.push_back(neighbour);
return true;
}
return false;
}

bool getNearestPassableEndNode(const MapSearchNode& start, MapSearchNode& end)
{
std::vector<MapSearchNode> neighbours;

bool canWalkLeft = addNearestSuccessor(neighbours, end.x - 1, end.y, end);
bool canWalkRight = addNearestSuccessor(neighbours, end.x + 1, end.y, end);
bool canWalkUp = addNearestSuccessor(neighbours, end.x, end.y - 1, end);
bool canWalkDown = addNearestSuccessor(neighbours, end.x, end.y + 1, end);

if (canWalkLeft == true)
{
if (canWalkUp == true)
{
addNearestSuccessor(neighbours, end.x - 1, end.y - 1, end);
}
if (canWalkDown == true)
{
addNearestSuccessor(neighbours, end.x - 1, end.y + 1, end);
}
}
if (canWalkRight == true)
{
if (canWalkUp == true)
{
addNearestSuccessor(neighbours, end.x + 1, end.y - 1, end);
}
if (canWalkDown == true)
{
addNearestSuccessor(neighbours, end.x + 1, end.y + 1, end);
}
}

std::sort(neighbours.begin(), neighbours.end(),
[&start](const MapSearchNode& lhs, const MapSearchNode& rhs)
{
float costA = lhs.GetCost() + lhs.GoalDistanceEstimateC(start);
float costB = rhs.GetCost() + rhs.GoalDistanceEstimateC(start);
return costA < costB;
});

if (neighbours.empty() == true)
{
return false;
}
if (neighbours.front().IsPassable() == false)
{
return false;
}
end = neighbours.front();
return true;
}
13 changes: 10 additions & 3 deletions src/Game/PathFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ class MapSearchNode
MapSearchNode(const LevelMap* map_, int16_t x_, int16_t y_,
const PlayerDirection& direction_) : map(map_), x(x_), y(y_), direction(direction_) {}

bool IsPassableIgnoreObject();
bool IsPassable() { return IsPassable(x, y); }
bool IsPassable(int16_t x_, int16_t y_);
bool IsValid() const;

bool IsPassableIgnoreObject() const;
bool IsPassable() const { return IsPassable(x, y); }
bool IsPassable(int16_t x_, int16_t y_) const;

float GoalDistanceEstimateC(const MapSearchNode& nodeGoal) const;
float GetCost() const;

float GoalDistanceEstimate(MapSearchNode& nodeGoal);
bool IsGoal(MapSearchNode& nodeGoal);
Expand All @@ -34,4 +39,6 @@ class MapSearchNode
bool IsSameState(MapSearchNode& rhs);
};

bool getNearestPassableEndNode(const MapSearchNode& start, MapSearchNode& end);

typedef AStarSearch<MapSearchNode> PathFinder;

0 comments on commit 4df29d1

Please sign in to comment.