From e6b6bc58fc58bc45e020b46d33b93bcec14921bf Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 22 Dec 2012 16:48:48 +0100 Subject: [PATCH] Re-implemented movement. --- source/World.cpp | 9 +++++++++ source/abstract/Body.cpp | 12 ++++++++++-- source/abstract/Body.h | 3 +++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/source/World.cpp b/source/World.cpp index a6d0c24..4e06be7 100755 --- a/source/World.cpp +++ b/source/World.cpp @@ -9,6 +9,8 @@ #include +#include + /** * Insert a drawable into the group. Drawables should only be handled with shared_ptr. * An object can't be inserted more than once at the same level. @@ -37,6 +39,13 @@ World::remove(std::shared_ptr drawable) { void World::step(int elapsed) { + for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) { + for (auto item = v->second.begin(); item != v->second.end(); item++) { + sf::Vector2f speed = (*item)->getSpeed(); + speed *= elapsed / 1000.0f; + (*item)->setPosition((*item)->getPosition() + speed); + } + } } /** diff --git a/source/abstract/Body.cpp b/source/abstract/Body.cpp index def7944..0572876 100755 --- a/source/abstract/Body.cpp +++ b/source/abstract/Body.cpp @@ -128,7 +128,7 @@ Body::setDelete(bool value) { * Sets movement speed and direction of the body. Set either value to zero to stop movement. * * @param direction The direction the body moves in, does not have to be normalized. - * @param speed The value of the movement speed to be used. + * @param speed Movement speed in pixels per second. */ void Body::setSpeed(sf::Vector2f direction, float speed) { @@ -139,9 +139,17 @@ Body::setSpeed(sf::Vector2f direction, float speed) { } /** - * Sets the angle of the body based on the direction of a vector. + * Sets the angle of the body. */ void Body::setAngle(float angle) { mAngle = angle; } + +/** + * Sets the position of thr body. + */ +void +Body::setPosition(const sf::Vector2f& position) { + mPosition = position; +} diff --git a/source/abstract/Body.h b/source/abstract/Body.h index f7fc375..d4f7e8c 100755 --- a/source/abstract/Body.h +++ b/source/abstract/Body.h @@ -77,9 +77,12 @@ public: // Protected functions. protected: + friend class World; + void setDelete(bool value); void setSpeed(sf::Vector2f direction, float speed); void setAngle(float angle); + void setPosition(const sf::Vector2f& position); // Private variables. private: