From fdf20428b0f2cea2d7dab4ed06526919b98c6c2f Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 1 Sep 2013 22:32:07 +0200 Subject: [PATCH] Moved class member initializations into class definitions. --- src/Game.cpp | 11 ++++------- src/Game.h | 6 +++--- src/Pathfinder.cpp | 4 +--- src/Pathfinder.h | 7 +++++-- src/generator/Generator.cpp | 18 +++++++----------- src/generator/Generator.h | 8 +++++--- src/sprites/Player.cpp | 3 +-- src/sprites/Player.h | 3 ++- src/sprites/abstract/Character.cpp | 4 ---- src/sprites/abstract/Character.h | 6 +++--- src/sprites/items/RingOfFire.h | 2 +- 11 files changed, 32 insertions(+), 40 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index 3a1d61b..8cd4498 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -14,9 +14,7 @@ #include "sprites/Player.h" #include "util/Loader.h" #include "util/ResourceManager.h" -#include "util/Yaml.h" - -const int Game::FPS_GOAL = 60; +#include "util/Yaml.h" /** * Initializes game, including window and objects (sprites). @@ -24,9 +22,7 @@ const int Game::FPS_GOAL = 60; Game::Game(tgui::Window& window) : mWindow(window), mWorldView(Vector2f(0, 0), mWindow.getView().getSize()), - mGenerator(mWorld, mPathfinder), - mQuit(false), - mPaused(false) { + mGenerator(mWorld, mPathfinder) { mWindow.setFramerateLimit(FPS_GOAL); mWindow.setKeyRepeatEnabled(false); srand(time(nullptr)); @@ -169,7 +165,8 @@ Game::input() { case sf::Event::MouseMoved: mPlayer->setCrosshairPosition(convertCoordinates(event.mouseMove.x, event.mouseMove.y)); - mCrosshair.setPosition(Vector2f(sf::Mouse::getPosition(mWindow) - Vector2i(mCrosshair.getTextureRect().width, mCrosshair.getTextureRect().height) / 2)); + mCrosshair.setPosition(Vector2f(sf::Mouse::getPosition(mWindow) - + Vector2i(mCrosshair.getTextureRect().width, mCrosshair.getTextureRect().height) / 2)); break; case sf::Event::MouseWheelMoved: mPlayer->toggleWeapon(); diff --git a/src/Game.h b/src/Game.h index 7d79914..22b310e 100644 --- a/src/Game.h +++ b/src/Game.h @@ -41,7 +41,7 @@ private: void initPlayer(); private: - static const int FPS_GOAL; + static const int FPS_GOAL = 60; tgui::Window& mWindow; sf::Clock mClock; @@ -60,8 +60,8 @@ private: Generator mGenerator; std::shared_ptr mPlayer; - bool mQuit; - bool mPaused; + bool mQuit = false; + bool mPaused = false; }; #endif /* DG_GAME_H_ */ diff --git a/src/Pathfinder.cpp b/src/Pathfinder.cpp index 2341d82..76a462a 100644 --- a/src/Pathfinder.cpp +++ b/src/Pathfinder.cpp @@ -16,8 +16,6 @@ #include "util/Interval.h" #include "sprites/Tile.h" -const float Pathfinder::WALL_DISTANCE_MULTIPLIER = 1.5f; - /** * Runs the A* path finding algorithm with areas as nodes and portals as edges. * @@ -242,4 +240,4 @@ Pathfinder::draw(sf::RenderTarget& target, sf::RenderStates states) const { target.draw(rect); } } -#endif +#endif /* RELEASE */ diff --git a/src/Pathfinder.h b/src/Pathfinder.h index 84f0076..10a9035 100644 --- a/src/Pathfinder.h +++ b/src/Pathfinder.h @@ -36,8 +36,11 @@ private: void draw(sf::RenderTarget& target, sf::RenderStates states) const; private: - static const float WALL_DISTANCE_MULTIPLIER; - std::vector mAreas; //< This has to be a vector as objects are compared by address. + /// character radius multiplied with this gives movement distance + // from the nearest wall. + static constexpr float WALL_DISTANCE_MULTIPLIER = 1.5f; + //< This has to be a vector as objects are compared by address. + std::vector mAreas; }; /** diff --git a/src/generator/Generator.cpp b/src/generator/Generator.cpp index 73152e8..9e271da 100644 --- a/src/generator/Generator.cpp +++ b/src/generator/Generator.cpp @@ -21,9 +21,6 @@ #include "../sprites/Enemy.h" #include "../util/Log.h" -const int Generator::GENERATE_AREA_SIZE = 4; -const float Generator::GENERATE_AREA_RANGE = 4.0f; - /** * Generates new random seed. */ @@ -125,10 +122,9 @@ Generator::createMinimalSpanningTree(const Vector2i& start, * Using basically Dijkstra on infinite graph/A* without destination node. * * @param start Tile to start path generation from (must be floor). - * @param limit Maximum weight of each path. */ void -Generator::connectRooms(const Vector2i& start, float limit) { +Generator::connectRooms(const Vector2i& start) { std::set open; std::set closed; std::map previous; @@ -163,7 +159,7 @@ Generator::connectRooms(const Vector2i& start, float limit) { destinations.insert(current); break; } - else if (distance.at(current) < limit) { + else if (distance.at(current) < ROOM_CONNECTION_VALUE) { process(Vector2i(current.x + 1, current.y), current); process(Vector2i(current.x, current.y + 1), current); process(Vector2i(current.x - 1, current.y), current); @@ -173,7 +169,7 @@ Generator::connectRooms(const Vector2i& start, float limit) { // take min length paths and set tiles float totalValue = 0.0f; - while (totalValue < limit && !destinations.empty()) { + while (totalValue < ROOM_CONNECTION_VALUE && !destinations.empty()) { std::vector path; float pathValue = 0; Vector2i current = *destinations.begin(); @@ -220,7 +216,7 @@ Generator::generateTiles(const sf::IntRect& area) { } } - std::vector selected = createMinimalSpanningTree(start, 12.0f); + std::vector selected = createMinimalSpanningTree(start, ROOM_SIZE_VALUE); // For rooms, take minimum bounding box of spanning tree. @@ -244,7 +240,7 @@ Generator::generateTiles(const sf::IntRect& area) { if (mTiles[x].count(y) == 0) mTiles[x][y] = Tile::Type::FLOOR; - connectRooms(start, 5.0f); + connectRooms(start); for (int x = area.left; x < area.left + area.width; x++) for (int y = area.top; y < area.top + area.height; y++) @@ -354,12 +350,12 @@ Generator::findClosestFloor(const Vector2i& start) const { return Vector2i(); } -#ifndef RELEASE /** * Debug only: Draws paths generated by connectRooms. * * mPaths is only required for this function. */ +#ifndef RELEASE void Generator::draw(sf::RenderTarget& target, sf::RenderStates states) const { for (auto& p : mPaths) { @@ -371,4 +367,4 @@ Generator::draw(sf::RenderTarget& target, sf::RenderStates states) const { } } } -#endif /* NDEBUG */ +#endif /* RELEASE */ diff --git a/src/generator/Generator.h b/src/generator/Generator.h index e1c7abd..e8bb1d4 100644 --- a/src/generator/Generator.h +++ b/src/generator/Generator.h @@ -38,12 +38,14 @@ private: Vector2i findClosestFloor(const Vector2i& start) const; std::vector createMinimalSpanningTree( const Vector2i& start, const float limit); - void connectRooms(const Vector2i& start, float limit); + void connectRooms(const Vector2i& start); void draw(sf::RenderTarget& target, sf::RenderStates states) const; private: - static const int GENERATE_AREA_SIZE; - static const float GENERATE_AREA_RANGE; + static constexpr int GENERATE_AREA_SIZE = 4; + static constexpr float GENERATE_AREA_RANGE = 4.0f; + static constexpr float ROOM_SIZE_VALUE = 12.0f; + static constexpr float ROOM_CONNECTION_VALUE = 5.0f; World& mWorld; Pathfinder& mPathfinder; diff --git a/src/sprites/Player.cpp b/src/sprites/Player.cpp index 443a702..f4446b4 100644 --- a/src/sprites/Player.cpp +++ b/src/sprites/Player.cpp @@ -18,8 +18,7 @@ Player::Player(World& world, Pathfinder& pathfinder, const Vector2f& position, const EquippedItems& items) : Character(position, CATEGORY_ACTOR, MASK_ALL, Yaml("player.yaml"), world, - pathfinder, items), - mDirection(0) { + pathfinder, items) { } Vector2f diff --git a/src/sprites/Player.h b/src/sprites/Player.h index ae93144..1470b5b 100644 --- a/src/sprites/Player.h +++ b/src/sprites/Player.h @@ -21,6 +21,7 @@ public: * Movement directions that can be set via Player::setDirection(). */ enum Direction : unsigned char { + NONE = 0, RIGHT = 1 << 0, LEFT = 1 << 1, UP = 1 << 2, @@ -58,7 +59,7 @@ private: private: Vector2f mCrosshairPosition; //< Relative position of the point to fire at (mouse cursor). - unsigned char mDirection; //< Current movement direction for direct control. + unsigned char mDirection = NONE; //< Current movement direction for direct control. }; #endif /* DG_PLAYER_H_ */ diff --git a/src/sprites/abstract/Character.cpp b/src/sprites/abstract/Character.cpp index 6514045..487b556 100644 --- a/src/sprites/abstract/Character.cpp +++ b/src/sprites/abstract/Character.cpp @@ -16,10 +16,6 @@ #include "../../World.h" #include "../../Pathfinder.h" -const float Character::VISION_DISTANCE = 500.0f; -const float Character::POINT_REACHED_DISTANCE = 1.0f; -const float Character::ITEM_PICKUP_MAX_DISTANCE = 50.0f; - /** * Saves pointer to this instance in static var for think(). */ diff --git a/src/sprites/abstract/Character.h b/src/sprites/abstract/Character.h index 553599e..33cf97f 100644 --- a/src/sprites/abstract/Character.h +++ b/src/sprites/abstract/Character.h @@ -36,9 +36,9 @@ public: }; /// Maximum distance where an enemy will be detected. - static const float VISION_DISTANCE; + static constexpr float VISION_DISTANCE = 500.0f; /// Maximum distance from character where an item can be picked up. - static const float ITEM_PICKUP_MAX_DISTANCE; + static constexpr float ITEM_PICKUP_MAX_DISTANCE = 50.0f; public: explicit Character(const Vector2f& position, Category category, @@ -87,7 +87,7 @@ private: /// Distance to a path point where it will be considered as reached (to /// avoid floating point equality check). - static const float POINT_REACHED_DISTANCE; + static constexpr float POINT_REACHED_DISTANCE = 1.0f; friend class World; World& mWorld; Pathfinder& mPathfinder; diff --git a/src/sprites/items/RingOfFire.h b/src/sprites/items/RingOfFire.h index 52849ba..30a5efc 100644 --- a/src/sprites/items/RingOfFire.h +++ b/src/sprites/items/RingOfFire.h @@ -26,7 +26,7 @@ protected: GadgetType getType() const override; private: - static const int WAVES_PER_USE = 3; + static constexpr int WAVES_PER_USE = 3; int mCurrentWave = WAVES_PER_USE + 1; Character* mCharacter;