Changed code to use direction vector instead of angle.

This commit is contained in:
Felix Ableitner 2013-03-30 02:30:11 +01:00
parent 8e65b94667
commit 47d9882e77
10 changed files with 37 additions and 38 deletions

View file

@ -57,7 +57,7 @@ Sprite::Sprite(const Data& data, const Yaml& config) :
mShape.shape->setTexture(&*mTexture, false); mShape.shape->setTexture(&*mTexture, false);
setPosition(data.position); setPosition(data.position);
setAngle(data.angle); setDirection(data.direction);
} }
/** /**
@ -69,10 +69,10 @@ Sprite::~Sprite() {
/** /**
* Initializes container. * Initializes container.
*/ */
Sprite::Data::Data(const sf::Vector2f& position, float angle, Sprite::Data::Data(const sf::Vector2f& position, const sf::Vector2f& direction,
Category category, unsigned short mask) : Category category, unsigned short mask) :
position(position), position(position),
angle(angle), direction(direction),
category(category), category(category),
mask(mask) { mask(mask) {
} }
@ -96,9 +96,9 @@ Sprite::getSpeed() const {
/** /**
* Returns the angle of the sprite. * Returns the angle of the sprite.
*/ */
float sf::Vector2f
Sprite::getAngle() const { Sprite::getDirection() const {
return mShape.shape->getRotation(); return thor::rotatedVector(sf::Vector2f(1, 0), mShape.shape->getRotation());
} }
/** /**
@ -172,12 +172,11 @@ Sprite::setSpeed(sf::Vector2f direction, float speed) {
mSpeed = direction; mSpeed = direction;
} }
/**
* Sets the angle of the Sprite.
*/
void void
Sprite::setAngle(float angle) { Sprite::setDirection(const sf::Vector2f& direction) {
mShape.shape->setRotation(angle); if (direction != sf::Vector2f()) {
mShape.shape->setRotation(thor::polarAngle(direction) + 90);
}
} }
/** /**

View file

@ -36,10 +36,10 @@ public:
*/ */
class Data { class Data {
public: public:
Data(const sf::Vector2f& position, float angle, Category category, Data(const sf::Vector2f& position, const sf::Vector2f& direction,
unsigned short mask); Category category, unsigned short mask);
const sf::Vector2f& position; const sf::Vector2f& position;
float angle; const sf::Vector2f& direction;
Category category; Category category;
unsigned short mask; unsigned short mask;
}; };
@ -59,7 +59,7 @@ public:
sf::Vector2f getPosition() const; sf::Vector2f getPosition() const;
sf::Vector2f getSpeed() const; sf::Vector2f getSpeed() const;
float getAngle() const; sf::Vector2f getDirection() const;
bool getDelete() const; bool getDelete() const;
Category getCategory() const; Category getCategory() const;
sf::Vector2f getSize() const; sf::Vector2f getSize() const;
@ -78,7 +78,7 @@ public:
protected: protected:
void setDelete(bool value); void setDelete(bool value);
void setSpeed(sf::Vector2f direction, float speed); void setSpeed(sf::Vector2f direction, float speed);
void setAngle(float angle); void setDirection(const sf::Vector2f& direction);
void setPosition(const sf::Vector2f& position); void setPosition(const sf::Vector2f& position);
float getRadius() const; float getRadius() const;

View file

@ -24,16 +24,16 @@ const float Bullet::DEFAULT_SPEED = 500;
* @param world Box2d world. * @param world Box2d world.
* @param texture Texture to display for bullet. * @param texture Texture to display for bullet.
*/ */
Bullet::Bullet(const sf::Vector2f& position, Sprite& shooter, float direction, Bullet::Bullet(const sf::Vector2f& position, Sprite& shooter,
const Yaml& config) : sf::Vector2f direction, const Yaml& config) :
Particle(config, Data(position, 0, CATEGORY_PARTICLE, ~CATEGORY_PARTICLE)), Particle(config, Data(position, sf::Vector2f(0, 0), CATEGORY_PARTICLE,
~CATEGORY_PARTICLE)),
mShooter(shooter), mShooter(shooter),
mDamage(config.get(KEY_DAMAGE, DEFAULT_DAMAGE)), mDamage(config.get(KEY_DAMAGE, DEFAULT_DAMAGE)),
mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) { mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) {
sf::Vector2f dir(1.0f, 0); thor::rotate(direction, - 90.0f);
thor::setPolarAngle(dir, direction - 90); setSpeed(direction, mSpeed);
setSpeed(dir, mSpeed); setDirection(direction);
setAngle(direction);
} }
/** /**

View file

@ -18,8 +18,8 @@ class Yaml;
class Bullet : public Particle { class Bullet : public Particle {
// Public functions. // Public functions.
public: public:
Bullet(const sf::Vector2f& position, Sprite& shooter, float direction, Bullet(const sf::Vector2f& position, Sprite& shooter,
const Yaml& config); sf::Vector2f direction, const Yaml& config);
void onCollide(std::shared_ptr<Sprite> other); void onCollide(std::shared_ptr<Sprite> other);

View file

@ -81,7 +81,7 @@ std::shared_ptr<Sprite>
Weapon::createParticle() { Weapon::createParticle() {
// Minus to account for positive y-axis going downwards in SFML. // Minus to account for positive y-axis going downwards in SFML.
sf::Vector2f offset(- mOffset); sf::Vector2f offset(- mOffset);
thor::rotate(offset, mHolder.getAngle()); thor::rotate(offset, thor::polarAngle(mHolder.getDirection()));
return std::shared_ptr<Sprite>(new Bullet(mHolder.getPosition() + offset, return std::shared_ptr<Sprite>(new Bullet(mHolder.getPosition() + offset,
mHolder, mHolder.getAngle(), Yaml(mBullet))); mHolder, mHolder.getDirection(), Yaml(mBullet)));
} }

View file

@ -8,6 +8,7 @@
#include "Corpse.h" #include "Corpse.h"
Corpse::Corpse(const sf::Vector2f& position, const Yaml& config) : Corpse::Corpse(const sf::Vector2f& position, const Yaml& config) :
Sprite(Data(position, 0, CATEGORY_NONSOLID, MASK_NONE), config) { Sprite(Data(position, sf::Vector2f(0, 0), CATEGORY_NONSOLID, MASK_NONE),
config) {
} }

View file

@ -7,7 +7,8 @@
#include "Enemy.h" #include "Enemy.h"
Enemy::Enemy(World& collection, const sf::Vector2f& position, const Yaml& config) : Enemy::Enemy(World& world, const sf::Vector2f& position,
Character(collection, Data(position, 0, CATEGORY_ACTOR, MASK_ALL), const Yaml& config) :
config) { Character(world, Data(position, sf::Vector2f(0, 0),
CATEGORY_ACTOR, MASK_ALL), config) {
} }

View file

@ -16,7 +16,7 @@ class Yaml;
class Enemy : public Character { class Enemy : public Character {
// Public functions. // Public functions.
public: public:
Enemy(World& collection, const sf::Vector2f& position, const Yaml& config); Enemy(World& world, const sf::Vector2f& position, const Yaml& config);
}; };
#endif /* DG_ENEMY_H_ */ #endif /* DG_ENEMY_H_ */

View file

@ -13,8 +13,8 @@
* Initializes Sprite. * Initializes Sprite.
*/ */
Player::Player(World& world, const sf::Vector2f& position, const Yaml& config) : Player::Player(World& world, const sf::Vector2f& position, const Yaml& config) :
Character(world, Data(position, 0, CATEGORY_ACTOR, MASK_ALL), Character(world, Data(position, sf::Vector2f(0, 0), CATEGORY_ACTOR,
config), MASK_ALL), config),
mDirection(0) { mDirection(0) {
} }
@ -97,7 +97,5 @@ Player::onThink(int elapsed) {
Character::move(); Character::move();
} }
// Look towards crosshair. // Look towards crosshair.
if (mCrosshairPosition != sf::Vector2f()) { Sprite::setDirection(mCrosshairPosition);
setAngle(thor::polarAngle(mCrosshairPosition) + 90);
}
} }

View file

@ -31,7 +31,7 @@ TileManager::TileManager(World& world) :
*/ */
TileManager::Tile::Tile(Type type, const TilePosition& position) : TileManager::Tile::Tile(Type type, const TilePosition& position) :
Sprite(Data(sf::Vector2f(position.x * TILE_SIZE.x, position.y * TILE_SIZE.y), Sprite(Data(sf::Vector2f(position.x * TILE_SIZE.x, position.y * TILE_SIZE.y),
0, CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL), sf::Vector2f(0, 0), CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL),
Yaml(getConfig(type))), Yaml(getConfig(type))),
mType(type) { mType(type) {
} }