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();
int elapsed = mClock.restart().asMilliseconds();
if (mPaused) {
if (mPaused)
elapsed = 0;
}
mWorld.think(elapsed);

View file

@ -35,11 +35,10 @@ void
World::remove(std::shared_ptr<Sprite> 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);
}
}
}
/**
* Inserts a character into the world. A character can only be inserted once.
@ -62,9 +61,8 @@ World::insertCharacter(std::shared_ptr<Character> character) {
void
World::removeCharacter(std::shared_ptr<Character> character) {
auto item = std::find(mCharacters.begin(), mCharacters.end(), character);
if (item != mCharacters.end()) {
if (item != mCharacters.end())
mCharacters.erase(item);
}
remove(character);
}
@ -126,9 +124,8 @@ World::generateAreas() {
std::vector<World::Portal*>
World::astarArea(Area* start, Area* end) const {
assert(start);
if (!end) {
if (!end)
return std::vector<World::Portal*>();
}
std::unordered_set<Area*> closed;
std::unordered_map<Area*, float> openAreasEstimatedCost;
@ -159,10 +156,9 @@ 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) ==
openAreasEstimatedCost.end()) ||
@ -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,10 +219,9 @@ 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,10 +233,9 @@ std::vector<std::shared_ptr<Character> >
std::vector<std::shared_ptr<Character> > 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<Sprite> 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<Sprite> spriteA,
(spriteB->mShape.type == Sprite::Shape::Type::CIRCLE))) {
std::shared_ptr<Sprite> circle = spriteA;
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);
}
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<Sprite> 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,11 +413,10 @@ World::testCollision(std::shared_ptr<Sprite> 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&>(*area);
}
}
return nullptr;
}

View file

@ -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);

View file

@ -179,18 +179,16 @@ 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);
}
}
/**
* Sets the position of thr Sprite.

View file

@ -60,22 +60,19 @@ 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;
}
}
}
std::shared_ptr<Sprite>
Weapon::createParticle() {

View file

@ -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());
}

View file

@ -87,11 +87,10 @@ 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> tile = std::shared_ptr<Tile>(new Tile(type, position));
mTiles.push_back(tile);
@ -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,10 +143,9 @@ 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;
}

View file

@ -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;
}

View file

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

View file

@ -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);
}

View file

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