Replaced all Box2D funcitonality with dummy behaviour.

This commit is contained in:
Felix Ableitner 2012-12-22 01:14:30 +01:00
parent fbcbcc6902
commit e20c99b89f
20 changed files with 88 additions and 214 deletions

View File

@ -26,18 +26,15 @@ const float Game::TICKS_GOAL = 1000 / Game::FPS_GOAL;
* Initializes game, including window and objects (sprites).
*/
Game::Game(sf::RenderWindow& window) :
mWorld(b2Vec2(0, 0)),
mWindow(window),
mView(Vector2f(0, 0), mWindow.getView().getSize()),
//mFps("test"),
mTileManager(mWorld),
mPathfinder(mTileManager),
mTileManager(mWorld),
mElapsed(0),
mQuit(false),
mPaused(false) {
mWindow.setFramerateLimit(FPS_GOAL);
mWindow.setKeyRepeatEnabled(true);
mWorld.SetContactListener(this);
mWindow.setKeyRepeatEnabled(true);
generate();
}
@ -83,7 +80,7 @@ Game::loop() {
for (; !mPaused && (left >= TICKS_GOAL); left -= TICKS_GOAL) {
Character::think(TICKS_GOAL);
mWorld.Step(1.0f / FPS_GOAL, 8, 3);
mWorld.step();
mCollection.checkDelete();
}
@ -245,19 +242,3 @@ String
Game::getFps() {
return str((mElapsed != 0) ? 1000.0f / mElapsed : 0.0f, 2);
}
/**
* Begin of collision, call callback function on both objects.
*/
void
Game::BeginContact(b2Contact* contact) {
Body& first = *static_cast<Body*>(contact->GetFixtureA()->GetBody()->GetUserData());
Body& second = *static_cast<Body*>(contact->GetFixtureB()->GetBody()->GetUserData());
if (!first.doesCollide(second) || !second.doesCollide(first)) {
contact->SetEnabled(false);
return;
}
first.onCollide(second, second.getCategory());
second.onCollide(first, first.getCategory());
}

View File

@ -13,28 +13,27 @@
#include <Thor/Resources.hpp>
#include <Box2D/Box2D.h>
#include "World.h"
#include "sprites/TileManager.h"
#include "sprites/Player.h"
#include "types/String.h"
#include "util/Collection.h"
#include "util/Pathfinder.h"
class World;
class Player;
class Collection;
/*
* Use vertex for tiles.
*/
class Game : private sf::NonCopyable, public b2ContactListener {
class Game : private sf::NonCopyable {
// Public functions.
public:
Game(sf::RenderWindow& window);
~Game();
void loop();
void BeginContact(b2Contact* contact);
// Private functions.
private:
@ -55,7 +54,7 @@ private:
static const int FPS_GOAL;
static const float TICKS_GOAL;
b2World mWorld;
World mWorld;
sf::RenderWindow& mWindow;
sf::Clock mClock;

View File

@ -21,63 +21,19 @@ const Vector2i Body::DEFAULT_SIZE = Vector2i(50, 50);
*/
Body::Body(const PhysicalData& data, const Yaml& config, const Vector2i& pSize) :
mDelete(false) {
Vector2i size = (pSize == Vector2i())
? config.get(KEY_SIZE, DEFAULT_SIZE)
: pSize;
assert(size != Vector2i());
b2BodyDef bodyDef;
bodyDef.type = (data.moving)
? b2_dynamicBody
: b2_staticBody;
bodyDef.position = vector(data.position);
bodyDef.allowSleep = true;
bodyDef.fixedRotation = true;
bodyDef.bullet = data.bullet;
bodyDef.userData = this;
mBody = data.world.CreateBody(&bodyDef);
b2Shape* shape;
if (data.circle) {
assert(size.x == size.y);
shape = new b2CircleShape;
shape->m_radius = pixelToMeter(size.x) / 2;
}
else {
b2PolygonShape* box = new b2PolygonShape;
box->SetAsBox(pixelToMeter(size.x) / 2,
pixelToMeter(size.y) / 2);
shape = dynamic_cast<b2Shape*>(box);
}
b2FixtureDef fixtureDef;
fixtureDef.shape = shape;
fixtureDef.density = 1.0f;
fixtureDef.filter.categoryBits = data.category;
fixtureDef.filter.maskBits = ~data.maskExclude;
fixtureDef.restitution = 0;
fixtureDef.density = (data.bullet) ? 0 : 10000;
mBody->CreateFixture(&fixtureDef);
delete shape;
}
/**
* Removes body from world.
* Used to make this class pure virtual without any pure virtual function.
*/
Body::~Body() {
mBody->GetWorld()->DestroyBody(mBody);
}
/**
* Initializes container.
*
* @link Physical::PhysicalData
*/
Body::PhysicalData::PhysicalData( const Vector2f& position, b2World& world, uint16 category,
uint16 maskExclude, bool moving, bool bullet, bool circle) :
Body::PhysicalData::PhysicalData( const Vector2f& position, World& world, Category category,
unsigned short maskExclude, bool moving, bool bullet, bool circle) :
position(position),
world(world),
category(category),
@ -92,7 +48,7 @@ Body::PhysicalData::PhysicalData( const Vector2f& position, b2World& world, uint
*/
Vector2f
Body::getPosition() const {
return vector(mBody->GetPosition());
return Vector2f();
}
/**
@ -100,7 +56,7 @@ Body::getPosition() const {
*/
Vector2f
Body::getSpeed() const {
return vector(mBody->GetLinearVelocity());
return Vector2f();
}
/**
@ -108,7 +64,7 @@ Body::getSpeed() const {
*/
float
Body::getAngle() const {
return thor::toDegree(mBody->GetAngle());
return 0;
}
/**
@ -124,7 +80,7 @@ Body::getDelete() const {
*/
Body::Category
Body::getCategory() const {
return (Category) mBody->GetFixtureList()->GetFilterData().categoryBits;
return CATEGORY_WORLD;
}
/**
@ -132,8 +88,7 @@ Body::getCategory() const {
*/
Vector2f
Body::getSize() const {
b2AABB aabb(mBody->GetFixtureList()->GetAABB(0));
return vector(aabb.upperBound - aabb.lowerBound);
return Vector2f();
}
/**
@ -141,7 +96,7 @@ Body::getSize() const {
*/
bool
Body::isSolid() const {
return mBody->GetFixtureList()->GetFilterData().maskBits != 0;
return false;
}
/**
@ -149,7 +104,7 @@ Body::isSolid() const {
*/
bool
Body::isMovable() const {
return mBody->GetType() != b2_staticBody;
return false;
}
/**
@ -172,7 +127,7 @@ Body::doesCollide(Body& other) {
* @param category The Category of the other object (as passed in constructor).
*/
void
Body::onCollide(Body& other, uint16 type) {
Body::onCollide(Body& other, Category type) {
}
/**
@ -191,11 +146,6 @@ Body::setDelete(bool value) {
*/
void
Body::setSpeed(Vector2f direction, float speed) {
if (direction != Vector2f()) {
direction = thor::unitVector<float>(direction);
}
direction *= speed;
mBody->SetLinearVelocity(vector(direction));
}
/**
@ -203,5 +153,4 @@ Body::setSpeed(Vector2f direction, float speed) {
*/
void
Body::setAngle(float angle) {
mBody->SetTransform(mBody->GetPosition(), thor::toRadian(angle));
}

View File

@ -8,12 +8,12 @@
#ifndef DG_BODY_H_
#define DG_BODY_H_
#include <Box2D/Box2D.h>
#include "../World.h"
#include "../types/String.h"
#include "../types/Vector.h"
#include "../util/Yaml.h"
class World;
class Yaml;
/**
@ -24,31 +24,6 @@ class Yaml;
class Body {
// Public types.
public:
/**
* POD container that carries all data required to construct this class.
*/
class PhysicalData {
public:
PhysicalData() = default;
PhysicalData(const Vector2f& position, b2World& world, uint16 category,
uint16 maskExclude, bool moving, bool bullet = false, bool circle = false);
/// 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;
/// True if the body collides as a circle. Radius is side length / 2,
/// both sides must be equal.
bool circle;
};
/**
* Categories of physical objects, for Box2D collision filtering.
* The order of categories is also used for render order in Collection::draw()
@ -63,6 +38,31 @@ public:
CATEGORY_ACTOR = 1 << 4
};
/**
* POD container that carries all data required to construct this class.
*/
class PhysicalData {
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.
World& world;
/// The category for collision filtering. Only one may be set. @link Physical::Category
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;
};
/**
* Common collision masking values.
*/
@ -87,7 +87,7 @@ public:
bool isMovable() const;
virtual bool doesCollide(Body& other);
virtual void onCollide(Body& other, uint16 category);
virtual void onCollide(Body& other, Category category);
// Public variables.
public:
@ -103,7 +103,6 @@ protected:
// Private variables.
private:
b2Body* mBody;
bool mDelete;
};

View File

@ -31,7 +31,7 @@ Sprite::Sprite(const Yaml& config, const PhysicalData& data, const Vector2i& siz
}
/**
* Does nothing.
* Used to make this class pure virtual without any pure virtual function.
*/
Sprite::~Sprite() {
}

View File

@ -6,6 +6,8 @@
*/
#include "Bullet.h"
#include <Thor/Vectors.hpp>
#include "../abstract/Character.h"
#include "../util/Loader.h"
@ -23,14 +25,16 @@ const float Bullet::DEFAULT_SPEED = 500;
* @param world Box2d world.
* @param texture Texture to display for bullet.
*/
Bullet::Bullet(const Vector2f& position, b2World& world, Body& shooter, float direction,
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)),
mShooter(shooter),
mDamage(config.get(KEY_DAMAGE, DEFAULT_DAMAGE)),
mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) {
setSpeed(angle(direction), mSpeed);
mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) {
Vector2f dir(1.0f, 0);
thor::setPolarAngle(dir, direction);
setSpeed(dir, mSpeed);
setAngle(direction);
}
@ -38,7 +42,7 @@ Bullet::Bullet(const Vector2f& position, b2World& world, Body& shooter, float di
* @copydoc Physical::onCollide
*/
void
Bullet::onCollide(Body& other, uint16 type) {
Bullet::onCollide(Body& other, Category type) {
// Make sure we do not damage twice.
if (!getDelete()) {
// Call onShot on other, with damage as param.

View File

@ -21,10 +21,10 @@ class Yaml;
class Bullet : public Particle {
// Public functions.
public:
Bullet(const Vector2f& position, b2World& world, Body& shooter, float direction,
Bullet(const Vector2f& position, World& world, Body& shooter, float direction,
const Yaml& config);
void onCollide(Body& other, uint16 category);
void onCollide(Body& other, Category category);
bool doesCollide(Body& other);
// Private variables.

View File

@ -25,10 +25,11 @@ Weapon::Weapon(const Instances& instances, Body& holder, const Yaml& config) :
mWorld(instances.world),
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) {
Vector2f holderSize = mHolder.getSize();
Yaml bullet(mBullet);
Vector2i bulletSize = bullet.get(Body::KEY_SIZE, Body::DEFAULT_SIZE);
mOffset = Vector2f(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 +
b2_linearSlop +
Vector2i bulletSize = bullet.get(Body::KEY_SIZE, Vector2i());
mOffset = Vector2f(0,
std::max(holderSize.x, holderSize.y) / 2 +
std::max(bulletSize.x, bulletSize.y) / 2);
}

View File

@ -46,7 +46,7 @@ private:
static const int DEFAULT_INTERVAL;
Body& mHolder;
b2World& mWorld;
World& mWorld;
Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).
const String mBullet;

View File

@ -7,7 +7,7 @@
#include "Corpse.h"
Corpse::Corpse(b2World& 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)) {
}

View File

@ -17,7 +17,7 @@ class Yaml;
class Corpse : public Sprite {
// Public functions.
public:
Corpse(b2World& world, const Vector2f& position, const Yaml& config);
Corpse(World& world, const Vector2f& position, const Yaml& config);
};
#endif /* DG_CORPSE_H_ */

View File

@ -30,7 +30,7 @@ private:
// Private variablese.
private:
b2World& mWorld;
World& mWorld;
Collection& mCollection;
};

View File

@ -61,22 +61,22 @@ Player::move(const Vector2f& destination) {
void
Player::setDirection(Direction direction, bool unset) {
if (unset) {
mDirection = mDirection & ~(uint8) direction;
mDirection = mDirection & ~direction;
} else {
mDirection = mDirection | (uint8) direction;
mDirection = mDirection | direction;
}
// Convert directions into a vector.
Vector2f dirVec(0, 0);
if (mDirection & (uint8) Direction::RIGHT) {
if (mDirection & Direction::RIGHT) {
dirVec.x += 1.0f;
}
if (mDirection & (uint8) Direction::LEFT) {
if (mDirection & Direction::LEFT) {
dirVec.x += - 1.0f;
}
if (mDirection & (uint8) Direction::DOWN) {
if (mDirection & Direction::DOWN) {
dirVec.y += 1.0f;
}
if (mDirection & (uint8) Direction::UP) {
if (mDirection & Direction::UP) {
dirVec.y += - 1.0f;
}
setSpeed(dirVec, getMovementSpeed());
@ -92,14 +92,14 @@ Player::onThink(float elapsedTime) {
Character::move();
}
// Look towards crosshair.
setAngle(angle(mCrosshairPosition));
setAngle(thor::polarAngle(mCrosshairPosition));
}
/**
* Stop movement if we collide with anything except bullets.
*/
void
Player::onCollide(Body& other, uint16 category) {
Player::onCollide(Body& other, Category category) {
if (category != CATEGORY_PARTICLE) {
setDestination(getPosition());
}

View File

@ -33,7 +33,7 @@ public:
/**
* Movement directions that can be set via Player::setDirection().
*/
enum class Direction : uint8 {
enum Direction : unsigned char {
RIGHT = 1 << 0,
LEFT = 1 << 1,
UP = 1 << 2,
@ -51,13 +51,13 @@ public:
// Private functions.
private:
void onCollide(Body& other, uint16 category);
void onCollide(Body& other, Category category);
void onThink(float elapsedTime);
// Private variables.
private:
Vector2f mCrosshairPosition; //< Relative position of the point to fire at (mouse cursor).
uint8 mDirection; //< Current movement direction for direct control.
unsigned char mDirection; //< Current movement direction for direct control.
};
#endif /* DG_PLAYER_H_ */

View File

@ -21,7 +21,7 @@ const Vector2i TileManager::TILE_SIZE = Vector2i(100, 100);
*
* @param world Box2D world to create (physical) tiles in.
*/
TileManager::TileManager(b2World& world) :
TileManager::TileManager(World& world) :
mWorld(world) {
}
@ -32,7 +32,7 @@ TileManager::TileManager(b2World& world) :
* @param pPosition Position of the tile in tile coordinates.
* @param world Box2D world object.
*/
TileManager::Tile::Tile(Type type, const TilePosition& position, b2World& 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),
world, CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL, false)),
mType(type) {

View File

@ -9,15 +9,14 @@
#define DG_TILEMANAGER_H_
#include <map>
#include <memory>
#include <vector>
#include <Box2D/Box2D.h>
#include "../World.h"
#include "../abstract/Sprite.h"
#include "../types/Vector.h"
#include "../types/String.h"
class World;
class Sprite;
class TileManager : public sf::Drawable {
@ -40,7 +39,7 @@ public:
// Public functions.
public:
TileManager(b2World& world);
TileManager(World& world);
void setTile(const TilePosition& position, Type type);
@ -54,7 +53,7 @@ private:
// Private variables.
private:
b2World& mWorld;
World& mWorld;
std::vector<std::unique_ptr<Tile> > mTiles;
};
@ -64,7 +63,7 @@ private:
class TileManager::Tile : public Sprite {
// Public functions.
public:
Tile(Type type, const TilePosition& position, b2World& world);
Tile(Type type, const TilePosition& position, World& world);
Type getType() const;
TilePosition getTilePosition() const;

View File

@ -8,12 +8,12 @@
#ifndef DG_INSTANCES_H_
#define DG_INSTANCES_H_
#include <Box2D/Box2D.h>
#include "../World.h"
#include "../sprites/TileManager.h"
#include "../util/Collection.h"
#include "../util/Pathfinder.h"
class World;
class Pathfinder;
class TileManager;
class Collection;
@ -23,13 +23,13 @@ class Collection;
*/
struct Instances {
Instances() = default;
Instances(Pathfinder& p, TileManager& t, Collection& c, b2World& w) :
Instances(Pathfinder& p, TileManager& t, Collection& c, World& w) :
pathfinder(p), tilemanager(t), collection(c), world(w) {};
Pathfinder& pathfinder;
TileManager& tilemanager;
Collection& collection;
b2World& world;
World& world;
};
#endif /* DG_INSTANCES_H_ */

View File

@ -12,8 +12,6 @@
#include <SFML/System.hpp>
#include <Box2D/Box2D.h>
#include <Thor/Vectors.hpp>
/**
@ -26,58 +24,4 @@ typedef sf::Vector2f Vector2f;
*/
typedef sf::Vector2i Vector2i;
/**
* Constant for conversion between Box2D vectors and SFML vectors.
*/
static const int PIXELS_PER_METER = 25;
/**
* Converts a distance from pixels to meters.
*/
inline float
pixelToMeter(float in) {
return in / PIXELS_PER_METER;
}
/**
* Converts a distance from meters to pixels.
*/
inline float
meterToPixel(float in) {
return in * PIXELS_PER_METER;
}
/**
* Converts Box2D metric vector to SFML pixel vector.
*/
inline Vector2f
vector(const b2Vec2& in) {
return Vector2f(meterToPixel(in.x), meterToPixel(in.y));
}
/**
* Converts SFML pixel vector to Box2D metric vector.
*/
inline b2Vec2
vector(const Vector2f& in) {
return b2Vec2(pixelToMeter(in.x), pixelToMeter(in.y));
}
/**
* Converts a vector to an SFML angle with the same direction.
*/
inline float
angle(Vector2f in) {
return 180 - thor::toDegree(atan2(in.x, in.y));
}
/**
* Converts an SFML angle to a unit vector with the same direction.
*/
inline Vector2f
angle(float in) {
in = thor::toRadian(180 - in);
return Vector2f(sin(in), cos(in));
}
#endif /* DG_VECTOR_H_ */

View File

@ -8,8 +8,6 @@
#ifndef PATHFINDER_H_
#define PATHFINDER_H_
#include <Box2D/Box2D.h>
#include "../abstract/Body.h"
#include "../types/Vector.h"

View File

@ -10,7 +10,7 @@
#include <assert.h>
/**
* INitializes time limit to zero.
* Initializes time limit to zero.
*/
Timer::Timer() {
}