Removed unneeded comments, improved documentation.
This commit is contained in:
parent
a094c01d55
commit
273153709c
18 changed files with 30 additions and 75 deletions
|
@ -11,7 +11,6 @@
|
||||||
#include "sprites/Player.h"
|
#include "sprites/Player.h"
|
||||||
#include "util/Yaml.h"
|
#include "util/Yaml.h"
|
||||||
|
|
||||||
/// Goal amount of frames per second.
|
|
||||||
const int Game::FPS_GOAL = 60;
|
const int Game::FPS_GOAL = 60;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -19,14 +19,12 @@ class World;
|
||||||
* Use vertex for tiles.
|
* Use vertex for tiles.
|
||||||
*/
|
*/
|
||||||
class Game : private sf::NonCopyable {
|
class Game : private sf::NonCopyable {
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
explicit Game(sf::RenderWindow& window);
|
explicit Game(sf::RenderWindow& window);
|
||||||
~Game();
|
~Game();
|
||||||
|
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
// Private functions.
|
|
||||||
private:
|
private:
|
||||||
void input();
|
void input();
|
||||||
void render();
|
void render();
|
||||||
|
@ -39,7 +37,6 @@ private:
|
||||||
void generate();
|
void generate();
|
||||||
sf::Vector2<float> convertCoordinates(int x, int y);
|
sf::Vector2<float> convertCoordinates(int x, int y);
|
||||||
|
|
||||||
// Private variables.
|
|
||||||
private:
|
private:
|
||||||
static const int FPS_GOAL;
|
static const int FPS_GOAL;
|
||||||
|
|
||||||
|
@ -55,5 +52,4 @@ private:
|
||||||
bool mPaused;
|
bool mPaused;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif /* DG_GAME_H_ */
|
#endif /* DG_GAME_H_ */
|
||||||
|
|
|
@ -130,45 +130,48 @@ World::astarArea(Area* start, Area* end) const {
|
||||||
return std::vector<World::Portal*>();
|
return std::vector<World::Portal*>();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unordered_set<Area*> closedset; // The set of nodes already evaluated.
|
std::unordered_set<Area*> closed;
|
||||||
// Set of nodes to be evaluated, with corresponding estimated cost start -> area -> goal
|
std::unordered_map<Area*, float> openAreasEstimatedCost;
|
||||||
std::unordered_map<Area*, float> openset;
|
// Navigated areas with previous area/portal.
|
||||||
// The map of navigated nodes, with previous, lowest cost Area/Portal.
|
std::unordered_map<Area*, std::pair<Area*, Portal*>> previousAreaAndPortal;
|
||||||
std::unordered_map<Area*, std::pair<Area*, Portal*>> came_from;
|
std::unordered_map<Area*, float> bestPathCost;
|
||||||
std::unordered_map<Area*, float> g_score; // Cost from start along best known path.
|
|
||||||
|
|
||||||
openset[start] = heuristic_cost_estimate(start, end);
|
openAreasEstimatedCost[start] = heuristic_cost_estimate(start, end);
|
||||||
g_score[start] = 0;
|
bestPathCost[start] = 0;
|
||||||
|
|
||||||
while (!openset.empty()) {
|
while (!openAreasEstimatedCost.empty()) {
|
||||||
// the node in openset having the lowest f_score value.
|
Area* current = std::min_element(openAreasEstimatedCost.begin(),
|
||||||
Area* current = std::min_element(openset.begin(), openset.end())->first;
|
openAreasEstimatedCost.end())->first;
|
||||||
if (current == end) {
|
if (current == end) {
|
||||||
std::vector<Portal*> path;
|
std::vector<Portal*> path;
|
||||||
auto previous = current;
|
auto previous = current;
|
||||||
while (previous != start) {
|
while (previous != start) {
|
||||||
path.push_back(came_from[previous].second);
|
path.push_back(previousAreaAndPortal[previous].second);
|
||||||
previous = came_from[previous].first;
|
previous = previousAreaAndPortal[previous].first;
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
openset.erase(current);
|
openAreasEstimatedCost.erase(current);
|
||||||
closedset.insert(current);
|
closed.insert(current);
|
||||||
for (Portal& portal : current->portals) {
|
for (Portal& portal : current->portals) {
|
||||||
Area* neighbor = portal.area;
|
Area* neighbor = portal.area;
|
||||||
// Use edge weight instead of heuristic cost estimate?
|
float tentative_g_score = bestPathCost[current] +
|
||||||
float tentative_g_score = g_score[current] + heuristic_cost_estimate(current,neighbor);
|
heuristic_cost_estimate(current,neighbor);
|
||||||
if (closedset.find(neighbor) != closedset.end()) {
|
if (closed.find(neighbor) != closed.end()) {
|
||||||
if (tentative_g_score >= g_score[neighbor]) {
|
if (tentative_g_score >= bestPathCost[neighbor]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((openset.find(neighbor) == openset.end()) || (tentative_g_score < g_score[neighbor])) {
|
if ((openAreasEstimatedCost.find(neighbor) ==
|
||||||
came_from[neighbor] = std::make_pair(current, &portal);
|
openAreasEstimatedCost.end()) ||
|
||||||
g_score[neighbor] = tentative_g_score;
|
(tentative_g_score < bestPathCost[neighbor])) {
|
||||||
openset[neighbor] = g_score[neighbor] + heuristic_cost_estimate(neighbor, end);
|
previousAreaAndPortal[neighbor] = std::make_pair(current,
|
||||||
|
&portal);
|
||||||
|
bestPathCost[neighbor] = tentative_g_score;
|
||||||
|
openAreasEstimatedCost[neighbor] = bestPathCost[neighbor] +
|
||||||
|
heuristic_cost_estimate(neighbor, end);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,6 @@ class Sprite;
|
||||||
* Render order is determined by Physical::Category (higher number on top).
|
* Render order is determined by Physical::Category (higher number on top).
|
||||||
*/
|
*/
|
||||||
class World : public sf::Drawable {
|
class World : public sf::Drawable {
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
void insert(std::shared_ptr<Sprite> drawable);
|
void insert(std::shared_ptr<Sprite> drawable);
|
||||||
void remove(std::shared_ptr<Sprite> drawable);
|
void remove(std::shared_ptr<Sprite> drawable);
|
||||||
|
@ -35,7 +34,6 @@ public:
|
||||||
std::vector<std::shared_ptr<Character> >
|
std::vector<std::shared_ptr<Character> >
|
||||||
getCharacters(const sf::Vector2f& position, float maxDistance) const;
|
getCharacters(const sf::Vector2f& position, float maxDistance) const;
|
||||||
|
|
||||||
// Private types.
|
|
||||||
private:
|
private:
|
||||||
struct Area;
|
struct Area;
|
||||||
/**
|
/**
|
||||||
|
@ -58,7 +56,6 @@ private:
|
||||||
std::vector<Portal> portals;
|
std::vector<Portal> portals;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Private functions.
|
|
||||||
private:
|
private:
|
||||||
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
||||||
bool testCollision(std::shared_ptr<Sprite> spriteA, std::shared_ptr<Sprite> spriteB,
|
bool testCollision(std::shared_ptr<Sprite> spriteA, std::shared_ptr<Sprite> spriteB,
|
||||||
|
@ -67,7 +64,6 @@ private:
|
||||||
float heuristic_cost_estimate(Area* start, Area* end) const;
|
float heuristic_cost_estimate(Area* start, Area* end) const;
|
||||||
std::vector<Portal*> astarArea(Area* start, Area* end) const;
|
std::vector<Portal*> astarArea(Area* start, Area* end) const;
|
||||||
|
|
||||||
// Private variables.
|
|
||||||
private:
|
private:
|
||||||
std::map<Sprite::Category, std::vector<std::shared_ptr<Sprite> > > mDrawables;
|
std::map<Sprite::Category, std::vector<std::shared_ptr<Sprite> > > mDrawables;
|
||||||
std::vector<Area> mAreas; //< This has to be a vector as objects are compared by address.
|
std::vector<Area> mAreas; //< This has to be a vector as objects are compared by address.
|
||||||
|
|
|
@ -118,7 +118,7 @@ Character::setDestination(const sf::Vector2f& destination) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move towards a destination. Call setDestination() for setting the destination.
|
* Move towards a destination. Call setDestination() for setting the destination.
|
||||||
* This should be called from think().
|
* This should be called from think() if path finding is to be used.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Character::move() {
|
Character::move() {
|
||||||
|
|
|
@ -18,14 +18,12 @@ class Yaml;
|
||||||
* Provides think function for AI, manages health, drops body on death.
|
* Provides think function for AI, manages health, drops body on death.
|
||||||
*/
|
*/
|
||||||
class Character : public Sprite {
|
class Character : public Sprite {
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
explicit Character(World& world, const Data& data, const Yaml& config);
|
explicit Character(World& world, const Data& data, const Yaml& config);
|
||||||
virtual ~Character() = 0;
|
virtual ~Character() = 0;
|
||||||
|
|
||||||
void onDamage(int damage);
|
void onDamage(int damage);
|
||||||
|
|
||||||
// Protected functions.
|
|
||||||
protected:
|
protected:
|
||||||
virtual void onThink(int elapsed);
|
virtual void onThink(int elapsed);
|
||||||
virtual void onDeath();
|
virtual void onDeath();
|
||||||
|
@ -36,7 +34,6 @@ protected:
|
||||||
void move();
|
void move();
|
||||||
std::vector<std::shared_ptr<Character> > getCharacters(float maxDistance) const;
|
std::vector<std::shared_ptr<Character> > getCharacters(float maxDistance) const;
|
||||||
|
|
||||||
// Private variables.
|
|
||||||
private:
|
private:
|
||||||
static const std::string KEY_HEALTH;
|
static const std::string KEY_HEALTH;
|
||||||
static const int DEFAULT_HEALTH;
|
static const int DEFAULT_HEALTH;
|
||||||
|
|
|
@ -16,14 +16,12 @@ class Yaml;
|
||||||
* Bullet particle fired by a weapon, may damage actors.
|
* Bullet particle fired by a weapon, may damage actors.
|
||||||
*/
|
*/
|
||||||
class Bullet : public Particle {
|
class Bullet : public Particle {
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
explicit Bullet(const sf::Vector2f& position, Sprite& shooter,
|
explicit Bullet(const sf::Vector2f& position, Sprite& shooter,
|
||||||
sf::Vector2f direction, const Yaml& config);
|
sf::Vector2f direction, const Yaml& config);
|
||||||
|
|
||||||
void onCollide(std::shared_ptr<Sprite> other);
|
void onCollide(std::shared_ptr<Sprite> other);
|
||||||
|
|
||||||
// Private variables.
|
|
||||||
private:
|
private:
|
||||||
static const std::string KEY_DAMAGE;
|
static const std::string KEY_DAMAGE;
|
||||||
static const int DEFAULT_DAMAGE;
|
static const int DEFAULT_DAMAGE;
|
||||||
|
|
|
@ -25,7 +25,6 @@ class Particle;
|
||||||
* - pass xml filename
|
* - pass xml filename
|
||||||
*/
|
*/
|
||||||
class Weapon : public Emitter {
|
class Weapon : public Emitter {
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
explicit Weapon(World& world, Sprite& holder, const Yaml& config);
|
explicit Weapon(World& world, Sprite& holder, const Yaml& config);
|
||||||
|
|
||||||
|
@ -33,11 +32,9 @@ public:
|
||||||
void releaseTrigger();
|
void releaseTrigger();
|
||||||
void onThink(int elapsed);
|
void onThink(int elapsed);
|
||||||
|
|
||||||
// Protected functions.
|
|
||||||
protected:
|
protected:
|
||||||
std::shared_ptr<Sprite> createParticle();
|
std::shared_ptr<Sprite> createParticle();
|
||||||
|
|
||||||
// Private variables.
|
|
||||||
private:
|
private:
|
||||||
static const std::string KEY_BULLET;
|
static const std::string KEY_BULLET;
|
||||||
static const std::string DEFAULT_BULLET;
|
static const std::string DEFAULT_BULLET;
|
||||||
|
|
|
@ -15,18 +15,15 @@ class Particle;
|
||||||
class Sprite;
|
class Sprite;
|
||||||
|
|
||||||
class Emitter {
|
class Emitter {
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
explicit Emitter(World& world);
|
explicit Emitter(World& world);
|
||||||
virtual ~Emitter();
|
virtual ~Emitter();
|
||||||
|
|
||||||
// Protected functions.
|
|
||||||
protected:
|
protected:
|
||||||
void emit();
|
void emit();
|
||||||
/// Creates a particle. Allows to use a user-defined particle class and custom settings.
|
/// Creates a particle. Allows to use a user-defined particle class and custom settings.
|
||||||
virtual std::shared_ptr<Sprite> createParticle() = 0;
|
virtual std::shared_ptr<Sprite> createParticle() = 0;
|
||||||
|
|
||||||
// Private variables.
|
|
||||||
private:
|
private:
|
||||||
World& mWorld;
|
World& mWorld;
|
||||||
};
|
};
|
||||||
|
|
|
@ -16,7 +16,6 @@ class Yaml;
|
||||||
* Prototype for a particle.
|
* Prototype for a particle.
|
||||||
*/
|
*/
|
||||||
class Particle : public Sprite {
|
class Particle : public Sprite {
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
explicit Particle(const Yaml& config, const Data& data);
|
explicit Particle(const Yaml& config, const Data& data);
|
||||||
virtual ~Particle();
|
virtual ~Particle();
|
||||||
|
|
|
@ -13,7 +13,6 @@
|
||||||
class Yaml;
|
class Yaml;
|
||||||
|
|
||||||
class Corpse : public Sprite {
|
class Corpse : public Sprite {
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
explicit Corpse(const sf::Vector2f& position, const Yaml& config);
|
explicit Corpse(const sf::Vector2f& position, const Yaml& config);
|
||||||
};
|
};
|
||||||
|
|
|
@ -14,7 +14,6 @@ class World;
|
||||||
class Yaml;
|
class Yaml;
|
||||||
|
|
||||||
class Enemy : public Character {
|
class Enemy : public Character {
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
explicit Enemy(World& world, const sf::Vector2f& position, const Yaml& config);
|
explicit Enemy(World& world, const sf::Vector2f& position, const Yaml& config);
|
||||||
};
|
};
|
||||||
|
|
|
@ -81,10 +81,5 @@ Player::setDirection(Direction direction, bool unset) {
|
||||||
void
|
void
|
||||||
Player::onThink(int elapsed) {
|
Player::onThink(int elapsed) {
|
||||||
Character::onThink(elapsed);
|
Character::onThink(elapsed);
|
||||||
if (!mDirection) {
|
|
||||||
// Only use path finding movement if no direct input movement active.
|
|
||||||
Character::move();
|
|
||||||
}
|
|
||||||
// Look towards crosshair.
|
|
||||||
Sprite::setDirection(mCrosshairPosition);
|
Sprite::setDirection(mCrosshairPosition);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,6 @@ class World;
|
||||||
* Player object.
|
* Player object.
|
||||||
*/
|
*/
|
||||||
class Player : public Character {
|
class Player : public Character {
|
||||||
// Public types.
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Movement directions that can be set via Player::setDirection().
|
* Movement directions that can be set via Player::setDirection().
|
||||||
|
@ -29,7 +28,6 @@ public:
|
||||||
DOWN = 1 << 3
|
DOWN = 1 << 3
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
explicit Player(World& world, const sf::Vector2f& position, const Yaml& config);
|
explicit Player(World& world, const sf::Vector2f& position, const Yaml& config);
|
||||||
|
|
||||||
|
@ -38,11 +36,9 @@ public:
|
||||||
void releaseTrigger();
|
void releaseTrigger();
|
||||||
void setDirection(Direction direction, bool unset);
|
void setDirection(Direction direction, bool unset);
|
||||||
|
|
||||||
// Private functions.
|
|
||||||
private:
|
private:
|
||||||
void onThink(int elapsed);
|
void onThink(int elapsed);
|
||||||
|
|
||||||
// Private variables.
|
|
||||||
private:
|
private:
|
||||||
sf::Vector2f mCrosshairPosition; //< Relative position of the point to fire at (mouse cursor).
|
sf::Vector2f mCrosshairPosition; //< Relative position of the point to fire at (mouse cursor).
|
||||||
unsigned char mDirection; //< Current movement direction for direct control.
|
unsigned char mDirection; //< Current movement direction for direct control.
|
||||||
|
|
|
@ -13,7 +13,6 @@
|
||||||
class World;
|
class World;
|
||||||
|
|
||||||
class TileManager : public sf::Drawable {
|
class TileManager : public sf::Drawable {
|
||||||
// Public types.
|
|
||||||
public:
|
public:
|
||||||
enum class Type {
|
enum class Type {
|
||||||
FLOOR,
|
FLOOR,
|
||||||
|
@ -25,12 +24,9 @@ public:
|
||||||
*/
|
*/
|
||||||
typedef sf::Vector2i TilePosition;
|
typedef sf::Vector2i TilePosition;
|
||||||
|
|
||||||
// Public variables.
|
|
||||||
public:
|
public:
|
||||||
/// The size of a single tile (pixels).
|
static const sf::Vector2i TILE_SIZE; //< Tile size in pixels.
|
||||||
static const sf::Vector2i TILE_SIZE;
|
|
||||||
|
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
explicit TileManager(World& world);
|
explicit TileManager(World& world);
|
||||||
|
|
||||||
|
@ -39,15 +35,12 @@ public:
|
||||||
bool raycast(const sf::Vector2f& lineStart,
|
bool raycast(const sf::Vector2f& lineStart,
|
||||||
const sf::Vector2f& lineEnd) const;
|
const sf::Vector2f& lineEnd) const;
|
||||||
|
|
||||||
// Private types.
|
|
||||||
private:
|
private:
|
||||||
class Tile;
|
class Tile;
|
||||||
|
|
||||||
// Private functions.
|
|
||||||
private:
|
private:
|
||||||
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
||||||
|
|
||||||
// Private variables.
|
|
||||||
private:
|
private:
|
||||||
World& mWorld;
|
World& mWorld;
|
||||||
std::vector<std::shared_ptr<Tile> > mTiles;
|
std::vector<std::shared_ptr<Tile> > mTiles;
|
||||||
|
@ -57,7 +50,6 @@ private:
|
||||||
* Holds information about a single tile.
|
* Holds information about a single tile.
|
||||||
*/
|
*/
|
||||||
class TileManager::Tile : public Sprite {
|
class TileManager::Tile : public Sprite {
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
explicit Tile(Type type, const TilePosition& position);
|
explicit Tile(Type type, const TilePosition& position);
|
||||||
|
|
||||||
|
@ -66,7 +58,6 @@ public:
|
||||||
|
|
||||||
static std::string getConfig(Type type);
|
static std::string getConfig(Type type);
|
||||||
|
|
||||||
// Private variables.
|
|
||||||
private:
|
private:
|
||||||
Type mType;
|
Type mType;
|
||||||
};
|
};
|
||||||
|
|
|
@ -37,7 +37,6 @@
|
||||||
* @endcode
|
* @endcode
|
||||||
*/
|
*/
|
||||||
class Loader : public Singleton<Loader> {
|
class Loader : public Singleton<Loader> {
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Sets the general resource folder path.
|
* Sets the general resource folder path.
|
||||||
|
@ -60,7 +59,6 @@ public:
|
||||||
return static_cast<SpecificLoader<T>* >(getLoader<T>().get())->fromFile(mFolder, file);
|
return static_cast<SpecificLoader<T>* >(getLoader<T>().get())->fromFile(mFolder, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private types.
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* We need this to save templates of different types in the same container.
|
* We need this to save templates of different types in the same container.
|
||||||
|
@ -99,7 +97,6 @@ private:
|
||||||
std::string mSubfolder;
|
std::string mSubfolder;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Private functions.
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* For Singleton behaviour.
|
* For Singleton behaviour.
|
||||||
|
@ -122,7 +119,6 @@ private:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Private variables.
|
|
||||||
private:
|
private:
|
||||||
std::string mFolder;
|
std::string mFolder;
|
||||||
std::map<std::type_index, std::unique_ptr<LoaderBase> > mLoaders;
|
std::map<std::type_index, std::unique_ptr<LoaderBase> > mLoaders;
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
*/
|
*/
|
||||||
class ResourceManager : public thor::MultiResourceCache,
|
class ResourceManager : public thor::MultiResourceCache,
|
||||||
public Singleton<ResourceManager> {
|
public Singleton<ResourceManager> {
|
||||||
// Private functions.
|
|
||||||
private:
|
private:
|
||||||
friend class Singleton<ResourceManager>;
|
friend class Singleton<ResourceManager>;
|
||||||
ResourceManager() {
|
ResourceManager() {
|
||||||
|
|
|
@ -21,7 +21,6 @@
|
||||||
* Interface to a YAML file.
|
* Interface to a YAML file.
|
||||||
*/
|
*/
|
||||||
class Yaml {
|
class Yaml {
|
||||||
// Public functions.
|
|
||||||
public:
|
public:
|
||||||
explicit Yaml(const std::string& filename);
|
explicit Yaml(const std::string& filename);
|
||||||
~Yaml();
|
~Yaml();
|
||||||
|
@ -33,7 +32,6 @@ public:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T get(const std::string& key, const T& defaultValue) const;
|
T get(const std::string& key, const T& defaultValue) const;
|
||||||
|
|
||||||
// Private variables.
|
|
||||||
private:
|
private:
|
||||||
static std::string mFolder;
|
static std::string mFolder;
|
||||||
|
|
||||||
|
|
Reference in a new issue