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.
|
* @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) {
|
mDelete(false) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,15 +37,13 @@ Body::~Body() {
|
||||||
/**
|
/**
|
||||||
* Initializes container.
|
* Initializes container.
|
||||||
*/
|
*/
|
||||||
Body::PhysicalData::PhysicalData( const Vector2f& position, World& world, Category category,
|
Body::Data::Data(World& world, const Vector2f& position, float angle,
|
||||||
unsigned short maskExclude, bool moving, bool bullet, bool circle) :
|
Category category, unsigned short maskExclude) :
|
||||||
position(position),
|
|
||||||
world(world),
|
world(world),
|
||||||
|
position(position),
|
||||||
|
angle(angle),
|
||||||
category(category),
|
category(category),
|
||||||
maskExclude(maskExclude),
|
mask(maskExclude) {
|
||||||
moving(moving),
|
|
||||||
bullet(bullet),
|
|
||||||
circle(circle) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,7 +51,7 @@ Body::PhysicalData::PhysicalData( const Vector2f& position, World& world, Catego
|
||||||
*/
|
*/
|
||||||
Vector2f
|
Vector2f
|
||||||
Body::getPosition() const {
|
Body::getPosition() const {
|
||||||
return Vector2f();
|
return mPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,7 +59,7 @@ Body::getPosition() const {
|
||||||
*/
|
*/
|
||||||
Vector2f
|
Vector2f
|
||||||
Body::getSpeed() const {
|
Body::getSpeed() const {
|
||||||
return Vector2f();
|
return mSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -64,7 +67,7 @@ Body::getSpeed() const {
|
||||||
*/
|
*/
|
||||||
float
|
float
|
||||||
Body::getAngle() const {
|
Body::getAngle() const {
|
||||||
return 0;
|
return mAngle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -80,31 +83,15 @@ Body::getDelete() const {
|
||||||
*/
|
*/
|
||||||
Body::Category
|
Body::Category
|
||||||
Body::getCategory() const {
|
Body::getCategory() const {
|
||||||
return CATEGORY_WORLD;
|
return mCategory;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the size of the body as a vector.
|
* Returns the size of the body as a vector.
|
||||||
*/
|
*/
|
||||||
Vector2f
|
Vector2i
|
||||||
Body::getSize() const {
|
Body::getSize() const {
|
||||||
return Vector2f();
|
return mSize;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,6 +133,10 @@ Body::setDelete(bool value) {
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Body::setSpeed(Vector2f direction, float speed) {
|
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
|
void
|
||||||
Body::setAngle(float angle) {
|
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:
|
public:
|
||||||
PhysicalData() = default;
|
Data() = default;
|
||||||
PhysicalData(const Vector2f& position, World& world, Category category,
|
Data(World& world, const Vector2f& position, float angle,
|
||||||
unsigned short maskExclude, bool moving, bool bullet = false, bool circle = false);
|
Category category, unsigned short maskExclude);
|
||||||
/// World position of the body in pixel coordinates.
|
|
||||||
const Vector2f& position;
|
|
||||||
/// Box2D world object.
|
|
||||||
World& world;
|
World& world;
|
||||||
/// The category for collision filtering. Only one may be set. @link Physical::Category
|
const Vector2f& position;
|
||||||
|
float angle;
|
||||||
Category category;
|
Category category;
|
||||||
/// All categories set here will have collisions disabled with this object.
|
unsigned short mask;
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Common collision masking values.
|
* Common collision masking values.
|
||||||
*/
|
*/
|
||||||
enum Mask {
|
enum Mask : unsigned short {
|
||||||
MASK_NONE = 0xffff, //< Disables any collisions.
|
MASK_NONE = 0xffff, //< Disables any collisions.
|
||||||
MASK_ALL = 0 //< Enables all collisions.
|
MASK_ALL = 0 //< Enables all collisions.
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public functions.
|
// Public functions.
|
||||||
public:
|
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;
|
virtual ~Body() = 0;
|
||||||
|
|
||||||
Vector2f getPosition() const;
|
Vector2f getPosition() const;
|
||||||
|
@ -81,10 +71,7 @@ public:
|
||||||
float getAngle() const;
|
float getAngle() const;
|
||||||
bool getDelete() const;
|
bool getDelete() const;
|
||||||
Category getCategory() const;
|
Category getCategory() const;
|
||||||
Vector2f getSize() const;
|
Vector2i getSize() const;
|
||||||
|
|
||||||
bool isSolid() const;
|
|
||||||
bool isMovable() const;
|
|
||||||
|
|
||||||
virtual bool doesCollide(Body& other);
|
virtual bool doesCollide(Body& other);
|
||||||
virtual void onCollide(Body& other, Category category);
|
virtual void onCollide(Body& other, Category category);
|
||||||
|
@ -100,9 +87,14 @@ protected:
|
||||||
void setSpeed(Vector2f direction, float speed);
|
void setSpeed(Vector2f direction, float speed);
|
||||||
void setAngle(float angle);
|
void setAngle(float angle);
|
||||||
|
|
||||||
|
|
||||||
// Private variables.
|
// Private variables.
|
||||||
private:
|
private:
|
||||||
|
Vector2f mPosition;
|
||||||
|
Vector2i mSize;
|
||||||
|
Vector2f mSpeed;
|
||||||
|
float mAngle;
|
||||||
|
Category mCategory;
|
||||||
|
unsigned short mMask;
|
||||||
bool mDelete;
|
bool mDelete;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ std::vector<Character*> Character::mCharacterInstances = std::vector<Character*>
|
||||||
* Saves pointer to this instance in static var for think().
|
* Saves pointer to this instance in static var for think().
|
||||||
*/
|
*/
|
||||||
Character::Character(World& world, Collection& collection, Pathfinder& pathfinder,
|
Character::Character(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||||
const PhysicalData& data, const Yaml& config) :
|
const Data& data, const Yaml& config) :
|
||||||
Sprite(config, data),
|
Sprite(config, data),
|
||||||
mCollection(collection),
|
mCollection(collection),
|
||||||
mPathfinder(pathfinder),
|
mPathfinder(pathfinder),
|
||||||
|
|
|
@ -31,7 +31,7 @@ class Character : public Sprite {
|
||||||
// Public functions.
|
// Public functions.
|
||||||
public:
|
public:
|
||||||
Character(World& world, Collection& collection, Pathfinder& pathfinder,
|
Character(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||||
const PhysicalData& data, const Yaml& config);
|
const Data& data, const Yaml& config);
|
||||||
virtual ~Character() = 0;
|
virtual ~Character() = 0;
|
||||||
|
|
||||||
static void think(float elapsedTime);
|
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.
|
* @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),
|
Body(data, config, size),
|
||||||
mSize(Vector2i(getSize())) {
|
mSize(Vector2i(getSize())) {
|
||||||
String texture = config.get(KEY_TEXTURE, DEFAULT_TEXTURE);
|
String texture = config.get(KEY_TEXTURE, DEFAULT_TEXTURE);
|
||||||
|
|
|
@ -28,8 +28,8 @@ class Yaml;
|
||||||
class Sprite : public sf::Drawable, public Body {
|
class Sprite : public sf::Drawable, public Body {
|
||||||
// Public functions.
|
// Public functions.
|
||||||
public:
|
public:
|
||||||
Sprite(const Yaml& config, const PhysicalData& data, const Vector2i& size = Vector2i());
|
Sprite(const Yaml& config, const Data& data, const Vector2i& size = Vector2i());
|
||||||
Sprite(const std::shared_ptr<sf::Texture>& texture, const PhysicalData& data);
|
Sprite(const std::shared_ptr<sf::Texture>& texture, const Data& data);
|
||||||
virtual ~Sprite() = 0;
|
virtual ~Sprite() = 0;
|
||||||
|
|
||||||
// Protected functions.
|
// Protected functions.
|
||||||
|
|
|
@ -27,8 +27,7 @@ const float Bullet::DEFAULT_SPEED = 500;
|
||||||
*/
|
*/
|
||||||
Bullet::Bullet(const Vector2f& position, World& world, Body& shooter, float direction,
|
Bullet::Bullet(const Vector2f& position, World& world, Body& shooter, float direction,
|
||||||
const Yaml& config) :
|
const Yaml& config) :
|
||||||
Particle(config, PhysicalData(position, world, CATEGORY_PARTICLE, CATEGORY_PARTICLE,
|
Particle(config, Data(world, position, 0, CATEGORY_PARTICLE, CATEGORY_PARTICLE)),
|
||||||
true, true, true)),
|
|
||||||
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)) {
|
||||||
|
|
|
@ -25,7 +25,7 @@ Weapon::Weapon(World& world, Collection& collection, Body& holder, const Yaml& c
|
||||||
mWorld(world),
|
mWorld(world),
|
||||||
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
|
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
|
||||||
mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) {
|
mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) {
|
||||||
Vector2f holderSize = mHolder.getSize();
|
Vector2i holderSize = mHolder.getSize();
|
||||||
Yaml bullet(mBullet);
|
Yaml bullet(mBullet);
|
||||||
Vector2i bulletSize = bullet.get(Body::KEY_SIZE, Vector2i());
|
Vector2i bulletSize = bullet.get(Body::KEY_SIZE, Vector2i());
|
||||||
mOffset = Vector2f(0,
|
mOffset = Vector2f(0,
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
#include "Particle.h"
|
#include "Particle.h"
|
||||||
|
|
||||||
Particle::Particle(const Yaml& config, const PhysicalData& data) :
|
Particle::Particle(const Yaml& config, const Data& data) :
|
||||||
Sprite(config, data) {
|
Sprite(config, data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ class Yaml;
|
||||||
class Particle : public Sprite {
|
class Particle : public Sprite {
|
||||||
// Public functions.
|
// Public functions.
|
||||||
public:
|
public:
|
||||||
Particle(const Yaml& config, const PhysicalData& data);
|
Particle(const Yaml& config, const Data& data);
|
||||||
virtual ~Particle();
|
virtual ~Particle();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,6 @@
|
||||||
#include "Corpse.h"
|
#include "Corpse.h"
|
||||||
|
|
||||||
Corpse::Corpse(World& world, const Vector2f& position, const Yaml& config) :
|
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,
|
Enemy::Enemy(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||||
const Vector2f& position, const Yaml& config) :
|
const Vector2f& position, const Yaml& config) :
|
||||||
Character(world, collection, pathfinder,
|
Character(world, collection, pathfinder,
|
||||||
PhysicalData(position, world, CATEGORY_ACTOR, MASK_ALL,
|
Data(world, position, 0, CATEGORY_ACTOR, MASK_ALL),
|
||||||
true, false, true),
|
|
||||||
config),
|
config),
|
||||||
mWorld(world),
|
mWorld(world),
|
||||||
mCollection(collection) {
|
mCollection(collection) {
|
||||||
|
|
|
@ -19,8 +19,7 @@
|
||||||
Player::Player(World& world, Collection& collection, Pathfinder& pathfinder,
|
Player::Player(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||||
const Vector2f& position, const Yaml& config) :
|
const Vector2f& position, const Yaml& config) :
|
||||||
Character(world, collection, pathfinder,
|
Character(world, collection, pathfinder,
|
||||||
PhysicalData(position, world, CATEGORY_ACTOR, MASK_ALL, true,
|
Data(world, position, 0, CATEGORY_ACTOR, MASK_ALL),
|
||||||
false, true),
|
|
||||||
config),
|
config),
|
||||||
mDirection(0) {
|
mDirection(0) {
|
||||||
}
|
}
|
||||||
|
@ -95,7 +94,9 @@ Player::onThink(float elapsedTime) {
|
||||||
Character::move();
|
Character::move();
|
||||||
}
|
}
|
||||||
// Look towards crosshair.
|
// 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.
|
* @param world Box2D world object.
|
||||||
*/
|
*/
|
||||||
TileManager::Tile::Tile(Type type, const TilePosition& position, World& world) :
|
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),
|
Sprite(Yaml(getConfig(type)), Data(world,
|
||||||
world, CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL, false)),
|
Vector2f(position.x * TILE_SIZE.x, position.y * TILE_SIZE.y), 0,
|
||||||
|
CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL)),
|
||||||
mType(type) {
|
mType(type) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue