2012-09-09 20:50:15 +00:00
|
|
|
/*
|
|
|
|
* Physical.h
|
|
|
|
*
|
|
|
|
* Created on: 11.08.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DG_PHYSICAL_H_
|
|
|
|
#define DG_PHYSICAL_H_
|
|
|
|
|
|
|
|
#include <Box2D/Box2D.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"
|
|
|
|
|
|
|
|
class Yaml;
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An object with physical properties.
|
|
|
|
*
|
|
|
|
* @warning May only handle bodies with one fixture.
|
|
|
|
*/
|
|
|
|
class Physical {
|
|
|
|
// Public types.
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* POD container that carries all data required to construct this class.
|
|
|
|
*/
|
|
|
|
class PhysicalData {
|
|
|
|
public:
|
|
|
|
PhysicalData() = default;
|
2012-10-13 10:22:18 +00:00
|
|
|
PhysicalData(const Vector2f& position, b2World& world, uint16 category,
|
|
|
|
uint16 maskExclude, bool moving, bool bullet = false, bool circle = false);
|
2012-09-13 12:34:36 +00:00
|
|
|
/// World position of the body in pixel coordinates.
|
|
|
|
const Vector2f& position;
|
|
|
|
/// Box2D world object.
|
|
|
|
b2World& world;
|
|
|
|
/// The category for collision filtering. Only one may be set. @link Physical::Category
|
|
|
|
uint16 category;
|
|
|
|
/// All categories set here will have collisions disabled with this object.
|
|
|
|
uint16 maskExclude;
|
|
|
|
/// True if the body may move on its own (player, monster).
|
|
|
|
bool moving;
|
|
|
|
/// True if the object is a bullet.
|
|
|
|
bool bullet;
|
2012-09-16 18:45:12 +00:00
|
|
|
/// True if the body collides as a circle. Radius is side length / 2,
|
|
|
|
/// both sides must be equal.
|
2012-09-13 12:34:36 +00:00
|
|
|
bool circle;
|
2012-09-09 20:50:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Categories of physical objects, for Box2D collision filtering.
|
2012-09-16 18:45:12 +00:00
|
|
|
* The order of categories is also used for render order in Collection::draw()
|
|
|
|
* (higher number on top).
|
2012-09-09 20:50:15 +00:00
|
|
|
*
|
|
|
|
* @warning An object may only have one category.
|
|
|
|
*/
|
|
|
|
enum Category {
|
|
|
|
CATEGORY_WORLD = 1 << 1,
|
2012-09-16 18:33:00 +00:00
|
|
|
CATEGORY_NONSOLID = 1 << 2,
|
|
|
|
CATEGORY_PARTICLE = 1 << 3,
|
|
|
|
CATEGORY_ACTOR = 1 << 4
|
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
|
|
|
*/
|
|
|
|
enum Mask {
|
|
|
|
MASK_NONE = 0xffff, //< Disables any collisions.
|
|
|
|
MASK_ALL = 0 //< Enables all collisions.
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public functions.
|
|
|
|
public:
|
2012-10-13 10:54:09 +00:00
|
|
|
Physical(const PhysicalData& data, const Yaml& config, const Vector2i& pSize = Vector2i());
|
2012-09-09 20:50:15 +00:00
|
|
|
virtual ~Physical() = 0;
|
|
|
|
|
|
|
|
Vector2f getPosition() const;
|
|
|
|
Vector2f getSpeed() const;
|
|
|
|
float getAngle() const;
|
|
|
|
bool getDelete() const;
|
2012-09-16 18:33:00 +00:00
|
|
|
Category getCategory() const;
|
2012-09-28 16:43:28 +00:00
|
|
|
Vector2f getSize() const;
|
|
|
|
|
|
|
|
bool isSolid() const;
|
|
|
|
bool isMovable() const;
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
virtual bool doesCollide(Physical& other);
|
|
|
|
virtual void onCollide(Physical& other, uint16 category);
|
|
|
|
|
2012-10-13 10:48:16 +00:00
|
|
|
// Public variables.
|
|
|
|
public:
|
|
|
|
static const String KEY_SIZE;
|
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
// Protected functions.
|
|
|
|
protected:
|
|
|
|
void setDelete(bool value);
|
|
|
|
void setSpeed(Vector2f direction, float speed);
|
|
|
|
void setAngle(float angle);
|
|
|
|
|
|
|
|
|
|
|
|
// Private variables.
|
|
|
|
private:
|
2012-09-16 18:45:12 +00:00
|
|
|
b2Body* mBody;
|
2012-09-09 20:50:15 +00:00
|
|
|
bool mDelete;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* DG_PHYSICAL_H_ */
|