Re-implemented Body funcionality (without library).
This commit is contained in:
parent
6c83d3827a
commit
f42bdf5a24
14 changed files with 58 additions and 74 deletions
|
@ -19,7 +19,12 @@ const Vector2i Body::DEFAULT_SIZE = Vector2i(50, 50);
|
|||
*
|
||||
* @param data Data needed for construction.
|
||||
*/
|
||||
Body::Body(const PhysicalData& data, const Yaml& config, const Vector2i& pSize) :
|
||||
Body::Body(const Data& data, const Yaml& config, const Vector2i& pSize) :
|
||||
mPosition(data.position),
|
||||
mSize(config.get(KEY_SIZE, DEFAULT_SIZE)),
|
||||
mAngle(0),
|
||||
mCategory(data.category),
|
||||
mMask(data.mask),
|
||||
mDelete(false) {
|
||||
}
|
||||
|
||||
|
@ -32,15 +37,13 @@ Body::~Body() {
|
|||
/**
|
||||
* Initializes container.
|
||||
*/
|
||||
Body::PhysicalData::PhysicalData( const Vector2f& position, World& world, Category category,
|
||||
unsigned short maskExclude, bool moving, bool bullet, bool circle) :
|
||||
position(position),
|
||||
Body::Data::Data(World& world, const Vector2f& position, float angle,
|
||||
Category category, unsigned short maskExclude) :
|
||||
world(world),
|
||||
position(position),
|
||||
angle(angle),
|
||||
category(category),
|
||||
maskExclude(maskExclude),
|
||||
moving(moving),
|
||||
bullet(bullet),
|
||||
circle(circle) {
|
||||
mask(maskExclude) {
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -48,7 +51,7 @@ Body::PhysicalData::PhysicalData( const Vector2f& position, World& world, Catego
|
|||
*/
|
||||
Vector2f
|
||||
Body::getPosition() const {
|
||||
return Vector2f();
|
||||
return mPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -56,7 +59,7 @@ Body::getPosition() const {
|
|||
*/
|
||||
Vector2f
|
||||
Body::getSpeed() const {
|
||||
return Vector2f();
|
||||
return mSpeed;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -64,7 +67,7 @@ Body::getSpeed() const {
|
|||
*/
|
||||
float
|
||||
Body::getAngle() const {
|
||||
return 0;
|
||||
return mAngle;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -80,31 +83,15 @@ Body::getDelete() const {
|
|||
*/
|
||||
Body::Category
|
||||
Body::getCategory() const {
|
||||
return CATEGORY_WORLD;
|
||||
return mCategory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the body as a vector.
|
||||
*/
|
||||
Vector2f
|
||||
Vector2i
|
||||
Body::getSize() const {
|
||||
return Vector2f();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if collisions are enabled for the body.
|
||||
*/
|
||||
bool
|
||||
Body::isSolid() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the body is able to move.
|
||||
*/
|
||||
bool
|
||||
Body::isMovable() const {
|
||||
return false;
|
||||
return mSize;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -146,6 +133,10 @@ Body::setDelete(bool value) {
|
|||
*/
|
||||
void
|
||||
Body::setSpeed(Vector2f direction, float speed) {
|
||||
if (direction != Vector2f()) {
|
||||
thor::setLength(direction, speed);
|
||||
}
|
||||
mSpeed = direction;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -153,4 +144,5 @@ Body::setSpeed(Vector2f direction, float speed) {
|
|||
*/
|
||||
void
|
||||
Body::setAngle(float angle) {
|
||||
mAngle = angle;
|
||||
}
|
||||
|
|
|
@ -39,41 +39,31 @@ public:
|
|||
};
|
||||
|
||||
/**
|
||||
* POD container that carries all data required to construct this class.
|
||||
* POD container that carries all data required to construct an object of this class.
|
||||
*/
|
||||
class PhysicalData {
|
||||
class Data {
|
||||
public:
|
||||
PhysicalData() = default;
|
||||
PhysicalData(const Vector2f& position, World& world, Category category,
|
||||
unsigned short maskExclude, bool moving, bool bullet = false, bool circle = false);
|
||||
/// World position of the body in pixel coordinates.
|
||||
const Vector2f& position;
|
||||
/// Box2D world object.
|
||||
Data() = default;
|
||||
Data(World& world, const Vector2f& position, float angle,
|
||||
Category category, unsigned short maskExclude);
|
||||
World& world;
|
||||
/// The category for collision filtering. Only one may be set. @link Physical::Category
|
||||
const Vector2f& position;
|
||||
float angle;
|
||||
Category category;
|
||||
/// All categories set here will have collisions disabled with this object.
|
||||
unsigned short maskExclude;
|
||||
/// True if the body may move on its own (player, monster).
|
||||
bool moving;
|
||||
/// True if the object is a bullet.
|
||||
bool bullet;
|
||||
/// True if the body collides as a circle. Radius is side length / 2,
|
||||
/// both sides must be equal.
|
||||
bool circle;
|
||||
unsigned short mask;
|
||||
};
|
||||
|
||||
/**
|
||||
* Common collision masking values.
|
||||
*/
|
||||
enum Mask {
|
||||
enum Mask : unsigned short {
|
||||
MASK_NONE = 0xffff, //< Disables any collisions.
|
||||
MASK_ALL = 0 //< Enables all collisions.
|
||||
};
|
||||
|
||||
// Public functions.
|
||||
public:
|
||||
Body(const PhysicalData& data, const Yaml& config, const Vector2i& pSize = Vector2i());
|
||||
Body(const Data& data, const Yaml& config, const Vector2i& pSize = Vector2i());
|
||||
virtual ~Body() = 0;
|
||||
|
||||
Vector2f getPosition() const;
|
||||
|
@ -81,10 +71,7 @@ public:
|
|||
float getAngle() const;
|
||||
bool getDelete() const;
|
||||
Category getCategory() const;
|
||||
Vector2f getSize() const;
|
||||
|
||||
bool isSolid() const;
|
||||
bool isMovable() const;
|
||||
Vector2i getSize() const;
|
||||
|
||||
virtual bool doesCollide(Body& other);
|
||||
virtual void onCollide(Body& other, Category category);
|
||||
|
@ -100,9 +87,14 @@ protected:
|
|||
void setSpeed(Vector2f direction, float speed);
|
||||
void setAngle(float angle);
|
||||
|
||||
|
||||
// Private variables.
|
||||
private:
|
||||
Vector2f mPosition;
|
||||
Vector2i mSize;
|
||||
Vector2f mSpeed;
|
||||
float mAngle;
|
||||
Category mCategory;
|
||||
unsigned short mMask;
|
||||
bool mDelete;
|
||||
};
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ std::vector<Character*> Character::mCharacterInstances = std::vector<Character*>
|
|||
* Saves pointer to this instance in static var for think().
|
||||
*/
|
||||
Character::Character(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
const PhysicalData& data, const Yaml& config) :
|
||||
const Data& data, const Yaml& config) :
|
||||
Sprite(config, data),
|
||||
mCollection(collection),
|
||||
mPathfinder(pathfinder),
|
||||
|
|
|
@ -31,7 +31,7 @@ class Character : public Sprite {
|
|||
// Public functions.
|
||||
public:
|
||||
Character(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
const PhysicalData& data, const Yaml& config);
|
||||
const Data& data, const Yaml& config);
|
||||
virtual ~Character() = 0;
|
||||
|
||||
static void think(float elapsedTime);
|
||||
|
|
|
@ -19,7 +19,7 @@ const String Sprite::DEFAULT_TEXTURE = "";
|
|||
*
|
||||
* @param texturePath Relative path to the texture file in the resource folder.
|
||||
*/
|
||||
Sprite::Sprite(const Yaml& config, const PhysicalData& data, const Vector2i& size) :
|
||||
Sprite::Sprite(const Yaml& config, const Data& data, const Vector2i& size) :
|
||||
Body(data, config, size),
|
||||
mSize(Vector2i(getSize())) {
|
||||
String texture = config.get(KEY_TEXTURE, DEFAULT_TEXTURE);
|
||||
|
|
|
@ -28,8 +28,8 @@ class Yaml;
|
|||
class Sprite : public sf::Drawable, public Body {
|
||||
// Public functions.
|
||||
public:
|
||||
Sprite(const Yaml& config, const PhysicalData& data, const Vector2i& size = Vector2i());
|
||||
Sprite(const std::shared_ptr<sf::Texture>& texture, const PhysicalData& data);
|
||||
Sprite(const Yaml& config, const Data& data, const Vector2i& size = Vector2i());
|
||||
Sprite(const std::shared_ptr<sf::Texture>& texture, const Data& data);
|
||||
virtual ~Sprite() = 0;
|
||||
|
||||
// Protected functions.
|
||||
|
|
|
@ -27,8 +27,7 @@ const float Bullet::DEFAULT_SPEED = 500;
|
|||
*/
|
||||
Bullet::Bullet(const Vector2f& position, World& world, Body& shooter, float direction,
|
||||
const Yaml& config) :
|
||||
Particle(config, PhysicalData(position, world, CATEGORY_PARTICLE, CATEGORY_PARTICLE,
|
||||
true, true, true)),
|
||||
Particle(config, Data(world, position, 0, CATEGORY_PARTICLE, CATEGORY_PARTICLE)),
|
||||
mShooter(shooter),
|
||||
mDamage(config.get(KEY_DAMAGE, DEFAULT_DAMAGE)),
|
||||
mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) {
|
||||
|
|
|
@ -25,7 +25,7 @@ Weapon::Weapon(World& world, Collection& collection, Body& holder, const Yaml& c
|
|||
mWorld(world),
|
||||
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
|
||||
mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) {
|
||||
Vector2f holderSize = mHolder.getSize();
|
||||
Vector2i holderSize = mHolder.getSize();
|
||||
Yaml bullet(mBullet);
|
||||
Vector2i bulletSize = bullet.get(Body::KEY_SIZE, Vector2i());
|
||||
mOffset = Vector2f(0,
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
#include "Particle.h"
|
||||
|
||||
Particle::Particle(const Yaml& config, const PhysicalData& data) :
|
||||
Particle::Particle(const Yaml& config, const Data& data) :
|
||||
Sprite(config, data) {
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ class Yaml;
|
|||
class Particle : public Sprite {
|
||||
// Public functions.
|
||||
public:
|
||||
Particle(const Yaml& config, const PhysicalData& data);
|
||||
Particle(const Yaml& config, const Data& data);
|
||||
virtual ~Particle();
|
||||
};
|
||||
|
||||
|
|
|
@ -8,6 +8,6 @@
|
|||
#include "Corpse.h"
|
||||
|
||||
Corpse::Corpse(World& world, const Vector2f& position, const Yaml& config) :
|
||||
Sprite(config, PhysicalData(position, world, CATEGORY_NONSOLID, MASK_NONE, false)) {
|
||||
Sprite(config, Data(world, position, 0, CATEGORY_NONSOLID, MASK_NONE)) {
|
||||
}
|
||||
|
||||
|
|
|
@ -12,8 +12,7 @@
|
|||
Enemy::Enemy(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
const Vector2f& position, const Yaml& config) :
|
||||
Character(world, collection, pathfinder,
|
||||
PhysicalData(position, world, CATEGORY_ACTOR, MASK_ALL,
|
||||
true, false, true),
|
||||
Data(world, position, 0, CATEGORY_ACTOR, MASK_ALL),
|
||||
config),
|
||||
mWorld(world),
|
||||
mCollection(collection) {
|
||||
|
|
|
@ -19,8 +19,7 @@
|
|||
Player::Player(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
const Vector2f& position, const Yaml& config) :
|
||||
Character(world, collection, pathfinder,
|
||||
PhysicalData(position, world, CATEGORY_ACTOR, MASK_ALL, true,
|
||||
false, true),
|
||||
Data(world, position, 0, CATEGORY_ACTOR, MASK_ALL),
|
||||
config),
|
||||
mDirection(0) {
|
||||
}
|
||||
|
@ -95,7 +94,9 @@ Player::onThink(float elapsedTime) {
|
|||
Character::move();
|
||||
}
|
||||
// Look towards crosshair.
|
||||
setAngle(thor::polarAngle(mCrosshairPosition));
|
||||
if (mCrosshairPosition != Vector2f()) {
|
||||
setAngle(thor::polarAngle(mCrosshairPosition) + 90);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -33,8 +33,9 @@ TileManager::TileManager(World& world) :
|
|||
* @param world Box2D world object.
|
||||
*/
|
||||
TileManager::Tile::Tile(Type type, const TilePosition& position, World& world) :
|
||||
Sprite(Yaml(getConfig(type)), PhysicalData(Vector2f(position.x * TILE_SIZE.x, position.y * TILE_SIZE.y),
|
||||
world, CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL, false)),
|
||||
Sprite(Yaml(getConfig(type)), Data(world,
|
||||
Vector2f(position.x * TILE_SIZE.x, position.y * TILE_SIZE.y), 0,
|
||||
CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL)),
|
||||
mType(type) {
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue