Removed unnecessary brackets on if.

This commit is contained in:
Felix Ableitner 2013-04-04 23:13:08 +02:00
parent 273153709c
commit 48541059a0
11 changed files with 43 additions and 84 deletions

View file

@ -77,9 +77,8 @@ Game::loop() {
input(); input();
int elapsed = mClock.restart().asMilliseconds(); int elapsed = mClock.restart().asMilliseconds();
if (mPaused) { if (mPaused)
elapsed = 0; elapsed = 0;
}
mWorld.think(elapsed); mWorld.think(elapsed);

View file

@ -35,9 +35,8 @@ void
World::remove(std::shared_ptr<Sprite> drawable) { World::remove(std::shared_ptr<Sprite> drawable) {
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) { for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
auto item = std::find(v->second.begin(), v->second.end(), drawable); 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); v->second.erase(item);
}
} }
} }
@ -62,9 +61,8 @@ World::insertCharacter(std::shared_ptr<Character> character) {
void void
World::removeCharacter(std::shared_ptr<Character> character) { World::removeCharacter(std::shared_ptr<Character> character) {
auto item = std::find(mCharacters.begin(), mCharacters.end(), character); auto item = std::find(mCharacters.begin(), mCharacters.end(), character);
if (item != mCharacters.end()) { if (item != mCharacters.end())
mCharacters.erase(item); mCharacters.erase(item);
}
remove(character); remove(character);
} }
@ -126,9 +124,8 @@ World::generateAreas() {
std::vector<World::Portal*> std::vector<World::Portal*>
World::astarArea(Area* start, Area* end) const { World::astarArea(Area* start, Area* end) const {
assert(start); assert(start);
if (!end) { if (!end)
return std::vector<World::Portal*>(); return std::vector<World::Portal*>();
}
std::unordered_set<Area*> closed; std::unordered_set<Area*> closed;
std::unordered_map<Area*, float> openAreasEstimatedCost; std::unordered_map<Area*, float> openAreasEstimatedCost;
@ -159,9 +156,8 @@ World::astarArea(Area* start, Area* end) const {
float tentative_g_score = bestPathCost[current] + float tentative_g_score = bestPathCost[current] +
heuristic_cost_estimate(current,neighbor); heuristic_cost_estimate(current,neighbor);
if (closed.find(neighbor) != closed.end()) { if (closed.find(neighbor) != closed.end()) {
if (tentative_g_score >= bestPathCost[neighbor]) { if (tentative_g_score >= bestPathCost[neighbor])
continue; continue;
}
} }
if ((openAreasEstimatedCost.find(neighbor) == if ((openAreasEstimatedCost.find(neighbor) ==
@ -213,9 +209,8 @@ World::getPath(const sf::Vector2f& start, const sf::Vector2f& end,
point = p->end - startToEnd; point = p->end - startToEnd;
} }
} }
else { else
point = p->start + startToEnd * percentage; point = p->start + startToEnd * percentage;
}
// Take two points on a line orthogonal to the portal. // Take two points on a line orthogonal to the portal.
thor::setLength(startToEnd, radius); thor::setLength(startToEnd, radius);
@ -224,9 +219,8 @@ World::getPath(const sf::Vector2f& start, const sf::Vector2f& end,
path.push_back(point - startToEnd); path.push_back(point - startToEnd);
// Make sure the points are in the right order. // Make sure the points are in the right order.
if (thor::squaredLength(*(path.end() - 1) - *(path.end() - 3) ) < 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)); std::swap(*(path.end() - 1), *(path.end() - 2));
}
} }
return path; return path;
} }
@ -239,9 +233,8 @@ std::vector<std::shared_ptr<Character> >
std::vector<std::shared_ptr<Character> > visible; std::vector<std::shared_ptr<Character> > visible;
for (auto it : mCharacters) { for (auto it : mCharacters) {
if (thor::squaredLength(position - it->getPosition()) <= if (thor::squaredLength(position - it->getPosition()) <=
maxDistance * maxDistance) { maxDistance * maxDistance)
visible.push_back(it); visible.push_back(it);
}
} }
return visible; return visible;
} }
@ -266,32 +259,28 @@ World::step(int elapsed) {
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) { for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
for (auto it = v->second.begin(); it != v->second.end(); ) { for (auto it = v->second.begin(); it != v->second.end(); ) {
auto spriteA = *it; auto spriteA = *it;
if (spriteA->getDelete()) { if (spriteA->getDelete())
remove(spriteA); remove(spriteA);
}
else { else {
sf::Vector2f speed = spriteA->getSpeed(); sf::Vector2f speed = spriteA->getSpeed();
speed *= elapsed / 1000.0f; speed *= elapsed / 1000.0f;
bool overlap = false; bool overlap = false;
for (auto w = mDrawables.begin(); w != mDrawables.end(); w++) { for (auto w = mDrawables.begin(); w != mDrawables.end(); w++) {
for (auto spriteB : w->second) { for (auto spriteB : w->second) {
if (spriteA == spriteB) { if (spriteA == spriteB)
continue; continue;
}
// Ignore anything that is filtered by masks. // Ignore anything that is filtered by masks.
if (!spriteA->collisionEnabled(spriteB->getCategory()) || if (!spriteA->collisionEnabled(spriteB->getCategory()) ||
!spriteB->collisionEnabled(spriteA->getCategory())) { !spriteB->collisionEnabled(spriteA->getCategory()))
continue; continue;
}
if (testCollision(spriteA, spriteB, elapsed)) { if (testCollision(spriteA, spriteB, elapsed)) {
spriteA->onCollide(spriteB); spriteA->onCollide(spriteB);
overlap = true; overlap = true;
} }
} }
} }
if (!overlap) { if (!overlap)
spriteA->setPosition(spriteA->getPosition() + speed); spriteA->setPosition(spriteA->getPosition() + speed);
}
it++; it++;
} }
} }
@ -328,9 +317,8 @@ World::testCollision(std::shared_ptr<Sprite> spriteA,
sf::Vector2f axis = spriteA->getPosition() - spriteB->getPosition(); sf::Vector2f axis = spriteA->getPosition() - spriteB->getPosition();
// If both objects are at the exact same position, allow any movement for unstucking. // If both objects are at the exact same position, allow any movement for unstucking.
if (axis == sf::Vector2f()) { if (axis == sf::Vector2f())
return true; return true;
}
axis = thor::unitVector(axis); axis = thor::unitVector(axis);
float centerA = thor::dotProduct(axis, spriteA->getPosition()); float centerA = thor::dotProduct(axis, spriteA->getPosition());
float radiusA = spriteA->getRadius(); float radiusA = spriteA->getRadius();
@ -352,9 +340,8 @@ World::testCollision(std::shared_ptr<Sprite> spriteA,
(spriteB->mShape.type == Sprite::Shape::Type::CIRCLE))) { (spriteB->mShape.type == Sprite::Shape::Type::CIRCLE))) {
std::shared_ptr<Sprite> circle = spriteA; std::shared_ptr<Sprite> circle = spriteA;
std::shared_ptr<Sprite> rect = spriteB; std::shared_ptr<Sprite> rect = spriteB;
if (circle->mShape.type != Sprite::Shape::Type::CIRCLE) { if (circle->mShape.type != Sprite::Shape::Type::CIRCLE)
std::swap(circle, rect); std::swap(circle, rect);
}
float radius = circle->getRadius(); float radius = circle->getRadius();
sf::Vector2f halfsize = rect->getSize() / 2.0f; sf::Vector2f halfsize = rect->getSize() / 2.0f;
sf::Vector2f circlePos = circle->getPosition(); sf::Vector2f circlePos = circle->getPosition();
@ -377,18 +364,16 @@ World::testCollision(std::shared_ptr<Sprite> spriteA,
((overlapNoMovementY < overlapMovementY) && (overlapNoMovementX > 0))); ((overlapNoMovementY < overlapMovementY) && (overlapNoMovementX > 0)));
// If circle center is overlapping rectangle on x or y axis, we can take xyCollisionResult. // 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) || 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; return xyCollisionResult;
}
// Test if the circle is colliding with a corner of the rectangle. // Test if the circle is colliding with a corner of the rectangle.
else if (xyCollisionResult) { else if (xyCollisionResult) {
// This is the same as circle-circle collision. // This is the same as circle-circle collision.
sf::Vector2f axis = circle->getPosition() - rect->getPosition(); sf::Vector2f axis = circle->getPosition() - rect->getPosition();
// If both objects are at the exact same position, allow any // If both objects are at the exact same position, allow any
// movement for unstucking. // movement for unstucking.
if (axis == sf::Vector2f()) { if (axis == sf::Vector2f())
return true; return true;
}
axis = thor::unitVector(axis); axis = thor::unitVector(axis);
float circlePosProjected = thor::dotProduct(axis, circlePos); float circlePosProjected = thor::dotProduct(axis, circlePos);
@ -428,10 +413,9 @@ World::testCollision(std::shared_ptr<Sprite> spriteA,
World::Area* World::Area*
World::getArea(const sf::Vector2f& point) const { World::getArea(const sf::Vector2f& point) const {
for (auto area = mAreas.begin(); area != mAreas.end(); area++) { 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. // Make the return value non-const for convenience.
return &const_cast<Area&>(*area); return &const_cast<Area&>(*area);
}
} }
return nullptr; return nullptr;
} }

View file

@ -107,12 +107,10 @@ Character::releaseTrigger() {
bool bool
Character::setDestination(const sf::Vector2f& destination) { Character::setDestination(const sf::Vector2f& destination) {
mPath = mWorld.getPath(getPosition(), destination, getRadius()); mPath = mWorld.getPath(getPosition(), destination, getRadius());
if (!mPath.empty()) { if (!mPath.empty())
setSpeed(mPath.back() - getPosition(), mMovementSpeed); setSpeed(mPath.back() - getPosition(), mMovementSpeed);
} else
else {
LOG_W("No path found to destination."); LOG_W("No path found to destination.");
}
return !mPath.empty(); return !mPath.empty();
} }
@ -125,7 +123,6 @@ Character::move() {
if (!mPath.empty()) { if (!mPath.empty()) {
if (thor::length(mPath.back() - getPosition()) < POINT_REACHED_DISTANCE) { if (thor::length(mPath.back() - getPosition()) < POINT_REACHED_DISTANCE) {
mPath.pop_back(); mPath.pop_back();
(!mPath.empty()) (!mPath.empty())
? setSpeed(mPath.back() - getPosition(), mMovementSpeed) ? setSpeed(mPath.back() - getPosition(), mMovementSpeed)
: setSpeed(sf::Vector2f(), 0); : setSpeed(sf::Vector2f(), 0);

View file

@ -179,17 +179,15 @@ Sprite::setDelete(bool value) {
*/ */
void void
Sprite::setSpeed(sf::Vector2f direction, float speed) { Sprite::setSpeed(sf::Vector2f direction, float speed) {
if (direction != sf::Vector2f()) { if (direction != sf::Vector2f())
thor::setLength(direction, speed); thor::setLength(direction, speed);
}
mSpeed = direction; mSpeed = direction;
} }
void void
Sprite::setDirection(const sf::Vector2f& direction) { Sprite::setDirection(const sf::Vector2f& direction) {
if (direction != sf::Vector2f()) { if (direction != sf::Vector2f())
mShape.shape->setRotation(thor::polarAngle(direction) + 90); mShape.shape->setRotation(thor::polarAngle(direction) + 90);
}
} }
/** /**

View file

@ -60,20 +60,17 @@ Weapon::releaseTrigger() {
void void
Weapon::onThink(int elapsed) { Weapon::onThink(int elapsed) {
// Waiting for next shot, subtract time since last onThink. // Waiting for next shot, subtract time since last onThink.
if (mLastShotWaitInterval > 0) { if (mLastShotWaitInterval > 0)
mLastShotWaitInterval -= elapsed; mLastShotWaitInterval -= elapsed;
}
// Only reset to zero if we didn't recently fire (allow catching up for missed bullets). // Only reset to zero if we didn't recently fire (allow catching up for missed bullets).
else { else
mLastShotWaitInterval = 0; mLastShotWaitInterval = 0;
}
// Loop just in case we miss a bullet to fire. // Loop just in case we miss a bullet to fire.
while (mFire && mLastShotWaitInterval <= 0) { while (mFire && mLastShotWaitInterval <= 0) {
mLastShotWaitInterval += mFireInterval; mLastShotWaitInterval += mFireInterval;
emit(); emit();
if (!mAutomatic) { if (!mAutomatic)
mFire = false; mFire = false;
}
} }
} }

View file

@ -53,25 +53,21 @@ Player::releaseTrigger() {
*/ */
void void
Player::setDirection(Direction direction, bool unset) { Player::setDirection(Direction direction, bool unset) {
if (unset) { if (unset)
mDirection = mDirection & ~direction; mDirection = mDirection & ~direction;
} else { else
mDirection = mDirection | direction; mDirection = mDirection | direction;
}
// Convert directions into a vector. // Convert directions into a vector.
sf::Vector2f dirVec(0, 0); sf::Vector2f dirVec(0, 0);
if (mDirection & Direction::RIGHT) { if (mDirection & Direction::RIGHT) {
dirVec.x += 1.0f; dirVec.x += 1.0f;
} if (mDirection & Direction::LEFT)
if (mDirection & Direction::LEFT) {
dirVec.x += - 1.0f; dirVec.x += - 1.0f;
} if (mDirection & Direction::DOWN)
if (mDirection & Direction::DOWN) {
dirVec.y += 1.0f; dirVec.y += 1.0f;
} if (mDirection & Direction::UP)
if (mDirection & Direction::UP) {
dirVec.y += - 1.0f; dirVec.y += - 1.0f;
}
setSpeed(dirVec, getMovementSpeed()); setSpeed(dirVec, getMovementSpeed());
} }

View file

@ -87,10 +87,9 @@ void
TileManager::insertTile(const TilePosition& position, Type type) { TileManager::insertTile(const TilePosition& position, Type type) {
#ifndef NDEBUG #ifndef NDEBUG
for (auto it = mTiles.begin(); it != mTiles.end(); it++) { for (auto it = mTiles.begin(); it != mTiles.end(); it++) {
if ((*it)->getTilePosition() == position) { if ((*it)->getTilePosition() == position)
// Inserted multiple tiles at the same position. // Inserted multiple tiles at the same position.
assert(false); assert(false);
}
} }
#endif #endif
std::shared_ptr<Tile> tile = std::shared_ptr<Tile>(new Tile(type, position)); std::shared_ptr<Tile> tile = std::shared_ptr<Tile>(new Tile(type, position));
@ -123,13 +122,11 @@ TileManager::raycast(const sf::Vector2f& lineStart,
assert(lineStart != lineEnd); assert(lineStart != lineEnd);
sf::Vector2f lineCenter = lineStart + 0.5f * (lineEnd - lineStart); sf::Vector2f lineCenter = lineStart + 0.5f * (lineEnd - lineStart);
for (auto it : mTiles) { for (auto it : mTiles) {
if (it->getType() == Type::FLOOR) { if (it->getType() == Type::FLOOR)
continue; continue;
}
sf::Vector2f axis = it->getPosition() - lineCenter; sf::Vector2f axis = it->getPosition() - lineCenter;
if (axis == sf::Vector2f()) { if (axis == sf::Vector2f())
return false; return false;
}
axis = thor::unitVector(axis); axis = thor::unitVector(axis);
sf::Vector2f halfsize = it->getSize() / 2.0f; sf::Vector2f halfsize = it->getSize() / 2.0f;
@ -146,9 +143,8 @@ TileManager::raycast(const sf::Vector2f& lineStart,
Interval line = Interval::IntervalFromPoints(lineStartProjected, lineEndProjected); Interval line = Interval::IntervalFromPoints(lineStartProjected, lineEndProjected);
Interval rect = Interval::IntervalFromRadius(rectPosProjected, rectHalfWidthProjected); Interval rect = Interval::IntervalFromRadius(rectPosProjected, rectHalfWidthProjected);
// Allow movement if sprites are moving apart. // Allow movement if sprites are moving apart.
if (line.getOverlap(rect).getLength() > 0.0f) { if (line.getOverlap(rect).getLength() > 0.0f)
return false; return false;
}
} }
return true; return true;
} }

View file

@ -12,9 +12,8 @@
Interval::Interval(float start, float end) : Interval::Interval(float start, float end) :
start(start), start(start),
end(end) { end(end) {
if (this->start > this->end) { if (this->start > this->end)
std::swap(this->start, this->end); std::swap(this->start, this->end);
}
}; };
/** /**
* Creates an interval from a center point and a radius. The interval * Creates an interval from a center point and a radius. The interval
@ -39,22 +38,19 @@ Interval::IntervalFromPoints(float start, float end) {
*/ */
Interval Interval
Interval::getOverlap(Interval other) const { Interval::getOverlap(Interval other) const {
if ((start == other.start) && (end == other.end)) { if ((start == other.start) && (end == other.end))
return *this; return *this;
}
Interval smaller = *this; Interval smaller = *this;
Interval bigger = other; Interval bigger = other;
if (smaller.start > bigger.start) { if (smaller.start > bigger.start)
std::swap(smaller, bigger); std::swap(smaller, bigger);
}
Interval iv(0, 0); Interval iv(0, 0);
if (bigger.start < smaller.end) { if (bigger.start < smaller.end) {
iv.start = bigger.start; iv.start = bigger.start;
iv.end = smaller.end; iv.end = smaller.end;
} }
else { else
iv.start = iv.end = 0.0f; iv.start = iv.end = 0.0f;
}
return iv; return iv;
} }

View file

@ -110,13 +110,11 @@ private:
template <typename T> std::unique_ptr<LoaderBase>& template <typename T> std::unique_ptr<LoaderBase>&
getLoader() { getLoader() {
auto loader = mLoaders.find(typeid(T)); auto loader = mLoaders.find(typeid(T));
if (loader != mLoaders.end()) { if (loader != mLoaders.end())
return static_cast<std::unique_ptr<LoaderBase>&>(loader->second); return static_cast<std::unique_ptr<LoaderBase>&>(loader->second);
} else
else {
return (*mLoaders.insert(std::pair<std::type_index, std::unique_ptr<LoaderBase> > return (*mLoaders.insert(std::pair<std::type_index, std::unique_ptr<LoaderBase> >
(typeid(T), std::unique_ptr<LoaderBase>(new SpecificLoader<T>))).first).second; (typeid(T), std::unique_ptr<LoaderBase>(new SpecificLoader<T>))).first).second;
}
}; };
private: private:

View file

@ -18,9 +18,8 @@ std::string Yaml::mFolder = "";
Yaml::Yaml(const std::string& filename) : Yaml::Yaml(const std::string& filename) :
mFilename(mFolder+filename), mFilename(mFolder+filename),
mFile(mFilename) { mFile(mFilename) {
if (mFile.fail()) { if (mFile.fail())
LOG_W("Failed to open YAML file: " << mFolder << filename); LOG_W("Failed to open YAML file: " << mFolder << filename);
}
YAML::Parser parser(mFile); YAML::Parser parser(mFile);
parser.GetNextDocument(mNode); parser.GetNextDocument(mNode);
} }

View file

@ -71,9 +71,8 @@ T Yaml::get(const std::string& key, const T& defaultValue) const {
*node >> value; *node >> value;
return value; return value;
} }
else { else
return defaultValue; return defaultValue;
}
} }
catch(YAML::Exception&) { catch(YAML::Exception&) {
return defaultValue; return defaultValue;