2012-09-09 20:50:15 +00:00
|
|
|
/*
|
2012-12-20 14:07:47 +00:00
|
|
|
* Body.h
|
2012-09-09 20:50:15 +00:00
|
|
|
*
|
|
|
|
* Created on: 11.08.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
2012-12-20 14:07:47 +00:00
|
|
|
#ifndef DG_BODY_H_
|
|
|
|
#define DG_BODY_H_
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2012-12-22 00:14:30 +00:00
|
|
|
#include "../World.h"
|
2012-10-14 16:14:06 +00:00
|
|
|
#include "../types/String.h"
|
|
|
|
#include "../types/Vector.h"
|
2012-10-13 10:22:18 +00:00
|
|
|
#include "../util/Yaml.h"
|
|
|
|
|
2012-12-22 00:14:30 +00:00
|
|
|
class World;
|
2012-10-13 10:22:18 +00:00
|
|
|
class Yaml;
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An object with physical properties.
|
|
|
|
*
|
|
|
|
* @warning May only handle bodies with one fixture.
|
|
|
|
*/
|
2012-12-20 09:31:32 +00:00
|
|
|
class Body {
|
2012-09-09 20:50:15 +00:00
|
|
|
// Public types.
|
|
|
|
public:
|
2012-12-22 00:14:30 +00:00
|
|
|
/**
|
|
|
|
* Categories of physical objects, for Box2D collision filtering.
|
|
|
|
* The order of categories is also used for render order in Collection::draw()
|
|
|
|
* (higher number on top).
|
|
|
|
*
|
|
|
|
* @warning An object may only have one category.
|
|
|
|
*/
|
|
|
|
enum Category {
|
|
|
|
CATEGORY_WORLD = 1 << 1,
|
|
|
|
CATEGORY_NONSOLID = 1 << 2,
|
|
|
|
CATEGORY_PARTICLE = 1 << 3,
|
|
|
|
CATEGORY_ACTOR = 1 << 4
|
|
|
|
};
|
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
/**
|
2012-12-22 12:44:17 +00:00
|
|
|
* POD container that carries all data required to construct an object of this class.
|
2012-09-09 20:50:15 +00:00
|
|
|
*/
|
2012-12-22 12:44:17 +00:00
|
|
|
class Data {
|
2012-09-09 20:50:15 +00:00
|
|
|
public:
|
2012-12-22 12:44:17 +00:00
|
|
|
Data() = default;
|
|
|
|
Data(World& world, const Vector2f& position, float angle,
|
|
|
|
Category category, unsigned short maskExclude);
|
2012-12-22 00:14:30 +00:00
|
|
|
World& world;
|
2012-12-22 12:44:17 +00:00
|
|
|
const Vector2f& position;
|
|
|
|
float angle;
|
2012-12-22 00:14:30 +00:00
|
|
|
Category category;
|
2012-12-22 12:44:17 +00:00
|
|
|
unsigned short mask;
|
2012-09-09 20:50:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-09-16 18:45:12 +00:00
|
|
|
* Common collision masking values.
|
2012-09-09 20:50:15 +00:00
|
|
|
*/
|
2012-12-22 12:44:17 +00:00
|
|
|
enum Mask : unsigned short {
|
2012-09-09 20:50:15 +00:00
|
|
|
MASK_NONE = 0xffff, //< Disables any collisions.
|
|
|
|
MASK_ALL = 0 //< Enables all collisions.
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public functions.
|
|
|
|
public:
|
2012-12-22 12:44:17 +00:00
|
|
|
Body(const Data& data, const Yaml& config, const Vector2i& pSize = Vector2i());
|
2012-12-20 09:31:32 +00:00
|
|
|
virtual ~Body() = 0;
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
Vector2f getPosition() const;
|
|
|
|
Vector2f getSpeed() const;
|
|
|
|
float getAngle() const;
|
|
|
|
bool getDelete() const;
|
2012-09-16 18:33:00 +00:00
|
|
|
Category getCategory() const;
|
2012-12-22 12:44:17 +00:00
|
|
|
Vector2i getSize() const;
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2012-12-20 09:31:32 +00:00
|
|
|
virtual bool doesCollide(Body& other);
|
2012-12-22 00:14:30 +00:00
|
|
|
virtual void onCollide(Body& other, Category category);
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2012-10-13 10:48:16 +00:00
|
|
|
// Public variables.
|
|
|
|
public:
|
|
|
|
static const String KEY_SIZE;
|
2012-12-20 13:59:05 +00:00
|
|
|
static const Vector2i DEFAULT_SIZE;
|
2012-10-13 10:48:16 +00:00
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
// Protected functions.
|
|
|
|
protected:
|
|
|
|
void setDelete(bool value);
|
|
|
|
void setSpeed(Vector2f direction, float speed);
|
2012-12-22 12:44:17 +00:00
|
|
|
void setAngle(float angle);
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
// Private variables.
|
|
|
|
private:
|
2012-12-22 12:44:17 +00:00
|
|
|
Vector2f mPosition;
|
|
|
|
Vector2i mSize;
|
|
|
|
Vector2f mSpeed;
|
|
|
|
float mAngle;
|
|
|
|
Category mCategory;
|
|
|
|
unsigned short mMask;
|
2012-09-09 20:50:15 +00:00
|
|
|
bool mDelete;
|
|
|
|
};
|
|
|
|
|
2012-12-20 14:07:47 +00:00
|
|
|
#endif /* DG_BODY_H_ */
|