diff --git a/source/Game.cpp b/source/Game.cpp index 6d3f9be..a646817 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -77,9 +77,8 @@ Game::loop() { input(); int elapsed = mClock.restart().asMilliseconds(); - if (mPaused) { + if (mPaused) elapsed = 0; - } mWorld.think(elapsed); diff --git a/source/World.cpp b/source/World.cpp index 404581d..f0eaf68 100755 --- a/source/World.cpp +++ b/source/World.cpp @@ -35,9 +35,8 @@ void World::remove(std::shared_ptr drawable) { for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) { auto item = std::find(v->second.begin(), v->second.end(), drawable); - if (item != v->second.end()) { + if (item != v->second.end()) v->second.erase(item); - } } } @@ -62,9 +61,8 @@ World::insertCharacter(std::shared_ptr character) { void World::removeCharacter(std::shared_ptr character) { auto item = std::find(mCharacters.begin(), mCharacters.end(), character); - if (item != mCharacters.end()) { + if (item != mCharacters.end()) mCharacters.erase(item); - } remove(character); } @@ -126,10 +124,9 @@ World::generateAreas() { std::vector World::astarArea(Area* start, Area* end) const { assert(start); - if (!end) { + if (!end) return std::vector(); - } - + std::unordered_set closed; std::unordered_map openAreasEstimatedCost; // Navigated areas with previous area/portal. @@ -159,9 +156,8 @@ World::astarArea(Area* start, Area* end) const { float tentative_g_score = bestPathCost[current] + heuristic_cost_estimate(current,neighbor); if (closed.find(neighbor) != closed.end()) { - if (tentative_g_score >= bestPathCost[neighbor]) { + if (tentative_g_score >= bestPathCost[neighbor]) continue; - } } if ((openAreasEstimatedCost.find(neighbor) == @@ -213,9 +209,8 @@ World::getPath(const sf::Vector2f& start, const sf::Vector2f& end, point = p->end - startToEnd; } } - else { + else point = p->start + startToEnd * percentage; - } // Take two points on a line orthogonal to the portal. thor::setLength(startToEnd, radius); @@ -224,9 +219,8 @@ World::getPath(const sf::Vector2f& start, const sf::Vector2f& end, path.push_back(point - startToEnd); // Make sure the points are in the right order. if (thor::squaredLength(*(path.end() - 1) - *(path.end() - 3) ) < - thor::squaredLength(*(path.end() - 2) - *(path.end() - 3) )) { + thor::squaredLength(*(path.end() - 2) - *(path.end() - 3) )) std::swap(*(path.end() - 1), *(path.end() - 2)); - } } return path; } @@ -239,9 +233,8 @@ std::vector > std::vector > visible; for (auto it : mCharacters) { if (thor::squaredLength(position - it->getPosition()) <= - maxDistance * maxDistance) { + maxDistance * maxDistance) visible.push_back(it); - } } return visible; } @@ -266,32 +259,28 @@ World::step(int elapsed) { for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) { for (auto it = v->second.begin(); it != v->second.end(); ) { auto spriteA = *it; - if (spriteA->getDelete()) { + if (spriteA->getDelete()) remove(spriteA); - } else { sf::Vector2f speed = spriteA->getSpeed(); speed *= elapsed / 1000.0f; bool overlap = false; for (auto w = mDrawables.begin(); w != mDrawables.end(); w++) { for (auto spriteB : w->second) { - if (spriteA == spriteB) { + if (spriteA == spriteB) continue; - } // Ignore anything that is filtered by masks. if (!spriteA->collisionEnabled(spriteB->getCategory()) || - !spriteB->collisionEnabled(spriteA->getCategory())) { + !spriteB->collisionEnabled(spriteA->getCategory())) continue; - } if (testCollision(spriteA, spriteB, elapsed)) { spriteA->onCollide(spriteB); overlap = true; } } } - if (!overlap) { + if (!overlap) spriteA->setPosition(spriteA->getPosition() + speed); - } it++; } } @@ -328,9 +317,8 @@ World::testCollision(std::shared_ptr spriteA, sf::Vector2f axis = spriteA->getPosition() - spriteB->getPosition(); // If both objects are at the exact same position, allow any movement for unstucking. - if (axis == sf::Vector2f()) { + if (axis == sf::Vector2f()) return true; - } axis = thor::unitVector(axis); float centerA = thor::dotProduct(axis, spriteA->getPosition()); float radiusA = spriteA->getRadius(); @@ -352,9 +340,8 @@ World::testCollision(std::shared_ptr spriteA, (spriteB->mShape.type == Sprite::Shape::Type::CIRCLE))) { std::shared_ptr circle = spriteA; std::shared_ptr rect = spriteB; - if (circle->mShape.type != Sprite::Shape::Type::CIRCLE) { + if (circle->mShape.type != Sprite::Shape::Type::CIRCLE) std::swap(circle, rect); - } float radius = circle->getRadius(); sf::Vector2f halfsize = rect->getSize() / 2.0f; sf::Vector2f circlePos = circle->getPosition(); @@ -377,18 +364,16 @@ World::testCollision(std::shared_ptr spriteA, ((overlapNoMovementY < overlapMovementY) && (overlapNoMovementX > 0))); // If circle center is overlapping rectangle on x or y axis, we can take xyCollisionResult. if (Interval::IntervalFromRadius(rectPos.x, halfsize.x).isInside(circlePos.x) || - Interval::IntervalFromRadius(rectPos.y, halfsize.y).isInside(circlePos.y)) { + Interval::IntervalFromRadius(rectPos.y, halfsize.y).isInside(circlePos.y)) return xyCollisionResult; - } // Test if the circle is colliding with a corner of the rectangle. else if (xyCollisionResult) { // This is the same as circle-circle collision. sf::Vector2f axis = circle->getPosition() - rect->getPosition(); // If both objects are at the exact same position, allow any // movement for unstucking. - if (axis == sf::Vector2f()) { + if (axis == sf::Vector2f()) return true; - } axis = thor::unitVector(axis); float circlePosProjected = thor::dotProduct(axis, circlePos); @@ -428,10 +413,9 @@ World::testCollision(std::shared_ptr spriteA, World::Area* World::getArea(const sf::Vector2f& point) const { for (auto area = mAreas.begin(); area != mAreas.end(); area++) { - if (area->area.contains(point)) { + if (area->area.contains(point)) // Make the return value non-const for convenience. return &const_cast(*area); - } } return nullptr; } diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index 16b02a7..5655583 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -107,12 +107,10 @@ Character::releaseTrigger() { bool Character::setDestination(const sf::Vector2f& destination) { mPath = mWorld.getPath(getPosition(), destination, getRadius()); - if (!mPath.empty()) { + if (!mPath.empty()) setSpeed(mPath.back() - getPosition(), mMovementSpeed); - } - else { + else LOG_W("No path found to destination."); - } return !mPath.empty(); } @@ -125,7 +123,6 @@ Character::move() { if (!mPath.empty()) { if (thor::length(mPath.back() - getPosition()) < POINT_REACHED_DISTANCE) { mPath.pop_back(); - (!mPath.empty()) ? setSpeed(mPath.back() - getPosition(), mMovementSpeed) : setSpeed(sf::Vector2f(), 0); diff --git a/source/abstract/Sprite.cpp b/source/abstract/Sprite.cpp index 6b5331e..4f0d819 100755 --- a/source/abstract/Sprite.cpp +++ b/source/abstract/Sprite.cpp @@ -179,17 +179,15 @@ Sprite::setDelete(bool value) { */ void Sprite::setSpeed(sf::Vector2f direction, float speed) { - if (direction != sf::Vector2f()) { + if (direction != sf::Vector2f()) thor::setLength(direction, speed); - } mSpeed = direction; } void Sprite::setDirection(const sf::Vector2f& direction) { - if (direction != sf::Vector2f()) { + if (direction != sf::Vector2f()) mShape.shape->setRotation(thor::polarAngle(direction) + 90); - } } /** diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index 8266a7d..4b3ea16 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -60,20 +60,17 @@ Weapon::releaseTrigger() { void Weapon::onThink(int elapsed) { // Waiting for next shot, subtract time since last onThink. - if (mLastShotWaitInterval > 0) { + if (mLastShotWaitInterval > 0) mLastShotWaitInterval -= elapsed; - } // Only reset to zero if we didn't recently fire (allow catching up for missed bullets). - else { + else mLastShotWaitInterval = 0; - } // Loop just in case we miss a bullet to fire. while (mFire && mLastShotWaitInterval <= 0) { mLastShotWaitInterval += mFireInterval; emit(); - if (!mAutomatic) { + if (!mAutomatic) mFire = false; - } } } diff --git a/source/sprites/Player.cpp b/source/sprites/Player.cpp index 24ff903..d95d9ec 100644 --- a/source/sprites/Player.cpp +++ b/source/sprites/Player.cpp @@ -53,25 +53,21 @@ Player::releaseTrigger() { */ void Player::setDirection(Direction direction, bool unset) { - if (unset) { + if (unset) mDirection = mDirection & ~direction; - } else { + else mDirection = mDirection | direction; - } + // Convert directions into a vector. sf::Vector2f dirVec(0, 0); if (mDirection & Direction::RIGHT) { dirVec.x += 1.0f; - } - if (mDirection & Direction::LEFT) { + if (mDirection & Direction::LEFT) dirVec.x += - 1.0f; - } - if (mDirection & Direction::DOWN) { + if (mDirection & Direction::DOWN) dirVec.y += 1.0f; - } - if (mDirection & Direction::UP) { + if (mDirection & Direction::UP) dirVec.y += - 1.0f; - } setSpeed(dirVec, getMovementSpeed()); } diff --git a/source/sprites/TileManager.cpp b/source/sprites/TileManager.cpp index 00db2a5..97d9410 100644 --- a/source/sprites/TileManager.cpp +++ b/source/sprites/TileManager.cpp @@ -87,10 +87,9 @@ void TileManager::insertTile(const TilePosition& position, Type type) { #ifndef NDEBUG for (auto it = mTiles.begin(); it != mTiles.end(); it++) { - if ((*it)->getTilePosition() == position) { + if ((*it)->getTilePosition() == position) // Inserted multiple tiles at the same position. assert(false); - } } #endif std::shared_ptr tile = std::shared_ptr(new Tile(type, position)); @@ -123,13 +122,11 @@ TileManager::raycast(const sf::Vector2f& lineStart, assert(lineStart != lineEnd); sf::Vector2f lineCenter = lineStart + 0.5f * (lineEnd - lineStart); for (auto it : mTiles) { - if (it->getType() == Type::FLOOR) { + if (it->getType() == Type::FLOOR) continue; - } sf::Vector2f axis = it->getPosition() - lineCenter; - if (axis == sf::Vector2f()) { + if (axis == sf::Vector2f()) return false; - } axis = thor::unitVector(axis); sf::Vector2f halfsize = it->getSize() / 2.0f; @@ -146,9 +143,8 @@ TileManager::raycast(const sf::Vector2f& lineStart, Interval line = Interval::IntervalFromPoints(lineStartProjected, lineEndProjected); Interval rect = Interval::IntervalFromRadius(rectPosProjected, rectHalfWidthProjected); // Allow movement if sprites are moving apart. - if (line.getOverlap(rect).getLength() > 0.0f) { + if (line.getOverlap(rect).getLength() > 0.0f) return false; - } } return true; } diff --git a/source/util/Interval.cpp b/source/util/Interval.cpp index bf1a3a5..78fb7e3 100644 --- a/source/util/Interval.cpp +++ b/source/util/Interval.cpp @@ -12,9 +12,8 @@ Interval::Interval(float start, float end) : start(start), end(end) { - if (this->start > this->end) { + if (this->start > this->end) std::swap(this->start, this->end); - } }; /** * Creates an interval from a center point and a radius. The interval @@ -39,22 +38,19 @@ Interval::IntervalFromPoints(float start, float end) { */ Interval Interval::getOverlap(Interval other) const { - if ((start == other.start) && (end == other.end)) { + if ((start == other.start) && (end == other.end)) return *this; - } Interval smaller = *this; Interval bigger = other; - if (smaller.start > bigger.start) { + if (smaller.start > bigger.start) std::swap(smaller, bigger); - } Interval iv(0, 0); if (bigger.start < smaller.end) { iv.start = bigger.start; iv.end = smaller.end; } - else { + else iv.start = iv.end = 0.0f; - } return iv; } diff --git a/source/util/Loader.h b/source/util/Loader.h index b1cf85e..7d65ab0 100755 --- a/source/util/Loader.h +++ b/source/util/Loader.h @@ -110,13 +110,11 @@ private: template std::unique_ptr& getLoader() { auto loader = mLoaders.find(typeid(T)); - if (loader != mLoaders.end()) { + if (loader != mLoaders.end()) return static_cast&>(loader->second); - } - else { + else return (*mLoaders.insert(std::pair > (typeid(T), std::unique_ptr(new SpecificLoader))).first).second; - } }; private: diff --git a/source/util/Yaml.cpp b/source/util/Yaml.cpp index 48af081..f04b4c4 100644 --- a/source/util/Yaml.cpp +++ b/source/util/Yaml.cpp @@ -18,9 +18,8 @@ std::string Yaml::mFolder = ""; Yaml::Yaml(const std::string& filename) : mFilename(mFolder+filename), mFile(mFilename) { - if (mFile.fail()) { + if (mFile.fail()) LOG_W("Failed to open YAML file: " << mFolder << filename); - } YAML::Parser parser(mFile); parser.GetNextDocument(mNode); } diff --git a/source/util/Yaml.h b/source/util/Yaml.h index 742b8ba..2691fa8 100644 --- a/source/util/Yaml.h +++ b/source/util/Yaml.h @@ -71,9 +71,8 @@ T Yaml::get(const std::string& key, const T& defaultValue) const { *node >> value; return value; } - else { + else return defaultValue; - } } catch(YAML::Exception&) { return defaultValue;