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);
setPosition(data.position);
setAngle(data.angle);
setDirection(data.direction);
}
/**
@ -69,10 +69,10 @@ Sprite::~Sprite() {
/**
* 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) :
position(position),
angle(angle),
direction(direction),
category(category),
mask(mask) {
}
@ -96,9 +96,9 @@ Sprite::getSpeed() const {
/**
* Returns the angle of the sprite.
*/
float
Sprite::getAngle() const {
return mShape.shape->getRotation();
sf::Vector2f
Sprite::getDirection() const {
return thor::rotatedVector(sf::Vector2f(1, 0), mShape.shape->getRotation());
}
/**
@ -172,12 +172,11 @@ Sprite::setSpeed(sf::Vector2f direction, float speed) {
mSpeed = direction;
}
/**
* Sets the angle of the Sprite.
*/
void
Sprite::setAngle(float angle) {
mShape.shape->setRotation(angle);
Sprite::setDirection(const sf::Vector2f& direction) {
if (direction != sf::Vector2f()) {
mShape.shape->setRotation(thor::polarAngle(direction) + 90);
}
}
/**

View file

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

View file

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

View file

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

View file

@ -81,7 +81,7 @@ std::shared_ptr<Sprite>
Weapon::createParticle() {
// Minus to account for positive y-axis going downwards in SFML.
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,
mHolder, mHolder.getAngle(), Yaml(mBullet)));
mHolder, mHolder.getDirection(), Yaml(mBullet)));
}

View file

@ -8,6 +8,7 @@
#include "Corpse.h"
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"
Enemy::Enemy(World& collection, const sf::Vector2f& position, const Yaml& config) :
Character(collection, Data(position, 0, CATEGORY_ACTOR, MASK_ALL),
config) {
Enemy::Enemy(World& world, const sf::Vector2f& position,
const Yaml& 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 {
// Public functions.
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_ */

View file

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

View file

@ -31,7 +31,7 @@ TileManager::TileManager(World& world) :
*/
TileManager::Tile::Tile(Type type, const TilePosition& position) :
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))),
mType(type) {
}