Added Yaml config to all Sprite subclasses (except tiles).

This commit is contained in:
Felix Ableitner 2012-10-12 19:34:07 +02:00
parent f23f2225db
commit c93a6b0c9e
14 changed files with 38 additions and 22 deletions

View file

@ -61,11 +61,11 @@ Game::generate() {
Instances instances(mPathfinder, mTileManager, mCollection, mWorld);
mCollection.insert(std::shared_ptr<Sprite>(new Enemy(instances, Vector2f(400.0f, 200.0f))));
mCollection.insert(std::shared_ptr<Sprite>(new Enemy(instances, Vector2f(400.0f, 200.0f), Yaml("enemy.yaml"))));
mCollection.insert(std::shared_ptr<Sprite>(new Cover(Vector2f(300, 200), Vector2i(100, 150),
mWorld)));
mWorld, Yaml("cover.yaml"))));
mPlayer = std::unique_ptr<Player>(new Player(instances, Vector2f(200.0f, 100.0f)));
mPlayer = std::unique_ptr<Player>(new Player(instances, Vector2f(200.0f, 100.0f), Yaml("player.yaml")));
}
/**
* Closes window.

View file

@ -12,16 +12,17 @@
#include "../sprite/Body.h"
const String Character::KEY_HEALTH = "health";
std::vector<Character*> Character::mCharacterInstances = std::vector<Character*>();
/**
* Saves pointer to this instance in static var for think().
*/
Character::Character(const Instances& instances, const String& texturePath,
const PhysicalData& data, int health) :
const PhysicalData& data, const Yaml& config) :
Sprite(texturePath, data),
mMaxHealth(health),
mCurrentHealth(health),
mMaxHealth(config.get<int>(KEY_HEALTH)),
mCurrentHealth(mMaxHealth),
mInstances(instances) {
mCharacterInstances.push_back(this);
}
@ -36,7 +37,7 @@ Character::~Character() {
mCharacterInstances.erase(it);
mInstances.collection.insert(std::shared_ptr<Sprite>(new Body(mInstances.world,
getPosition())));
getPosition(), Yaml("body.yaml"))));
}
/**

View file

@ -13,9 +13,11 @@
#include "Sprite.h"
#include "../Instances.h"
#include "../util/String.h"
#include "../util/Yaml.h"
class Instances;
class Sprite;
class Yaml;
/**
* Provides think function for AI, manages health, drops body on death.
@ -24,7 +26,7 @@ class Character : public Sprite {
// Public functions.
public:
Character(const Instances& instances, const String& texturePath,
const PhysicalData& data, int health);
const PhysicalData& data, const Yaml& config);
virtual ~Character() = 0;
static void think(float elapsedTime);
@ -38,6 +40,8 @@ protected:
// Private variables.
private:
static const String KEY_HEALTH;
static std::vector<Character*> mCharacterInstances;
const int mMaxHealth;

View file

@ -26,7 +26,7 @@ Bullet::Bullet(const Vector2f& position, b2World& world, Physical& shooter, floa
const Yaml& config) :
Particle(ResourceManager::i().acquire(Loader::i().fromFile<sf::Texture>("bullet.png")),
PhysicalData(position, SIZE, world, CATEGORY_PARTICLE, CATEGORY_PARTICLE,
true, true, true)),
true, true, true), config),
mShooter(shooter),
mDamage(config.get<int>(KEY_DAMAGE)),
mSpeed(config.get<int>(KEY_SPEED)) {

View file

@ -7,7 +7,8 @@
#include "Particle.h"
Particle::Particle(const std::shared_ptr<sf::Texture>& texture, const PhysicalData& data) :
Particle::Particle(const std::shared_ptr<sf::Texture>& texture, const PhysicalData& data,
const Yaml& config) :
Sprite(texture, data) {
}

View file

@ -9,8 +9,10 @@
#define DG_PARTICLE_H_
#include "../abstract/Sprite.h"
#include "../util/Yaml.h"
class Sprite;
class Yaml;
/**
* Prototype for a particle.
@ -18,7 +20,8 @@ class Sprite;
class Particle : public Sprite {
// Public functions.
public:
Particle(const std::shared_ptr<sf::Texture>& texture, const PhysicalData& data);
Particle(const std::shared_ptr<sf::Texture>& texture, const PhysicalData& data,
const Yaml& config);
virtual ~Particle();
};

View file

@ -7,7 +7,7 @@
#include "Body.h"
Body::Body(b2World& world, const Vector2f& position) :
Body::Body(b2World& world, const Vector2f& position, const Yaml& config) :
Sprite("body.png", PhysicalData(position, Vector2i(50, 50), world,
CATEGORY_NONSOLID, MASK_NONE, false)) {
}

View file

@ -9,13 +9,15 @@
#define DG_BODY_H_
#include "../abstract/Sprite.h"
#include "../util/Yaml.h"
class Sprite;
class Yaml;
class Body : public Sprite {
// Public functions.
public:
Body(b2World& world, const Vector2f& position);
Body(b2World& world, const Vector2f& position, const Yaml& config);
};
#endif /* DG_BODY_H_ */

View file

@ -7,7 +7,7 @@
#include "Cover.h"
Cover::Cover(const Vector2f& position, const Vector2i& size, b2World& world) :
Cover::Cover(const Vector2f& position, const Vector2i& size, b2World& world, const Yaml& config) :
Sprite("cover.png", PhysicalData(position, size, world, CATEGORY_WORLD, MASK_ALL, false)) {
}

View file

@ -9,8 +9,10 @@
#define DG_COVER_H_
#include "../abstract/Sprite.h"
#include "../util/Yaml.h"
class Sprite;
class Yaml;
/**
* A wall that can be placed anywhere (not limited by tiles) and have any (rectangular) size.
@ -18,7 +20,7 @@ class Sprite;
class Cover : public Sprite {
// Public functions.
public:
Cover(const Vector2f& position, const Vector2i& size, b2World& world);
Cover(const Vector2f& position, const Vector2i& size, b2World& world, const Yaml& config);
};
#endif /* DG_COVER_H_ */

View file

@ -9,9 +9,9 @@
#include "Body.h"
Enemy::Enemy(const Instances& instances, const Vector2f& position) :
Enemy::Enemy(const Instances& instances, const Vector2f& position, const Yaml& config) :
Character(instances, "enemy.png", PhysicalData(position, Vector2i(50, 50), instances.world,
CATEGORY_ACTOR, MASK_ALL, true, false, true), 100),
CATEGORY_ACTOR, MASK_ALL, true, false, true), config),
mWorld(instances.world),
mCollection(instances.collection) {
}

View file

@ -12,15 +12,17 @@
#include "../abstract/Character.h"
#include "../util/Collection.h"
#include "../util/Vector.h"
#include "../util/Yaml.h"
class Character;
class Collection;
class Instances;
class Yaml;
class Enemy : public Character {
// Public functions.
public:
Enemy(const Instances& instances, const Vector2f& position);
Enemy(const Instances& instances, const Vector2f& position, const Yaml& config);
// Private functions.
private:

View file

@ -12,7 +12,6 @@
#include "../util/Vector.h"
#include "../items/Weapon.h"
#include "../util/String.h"
#include "../util/Yaml.h"
const float Player::SPEED = 100.0f;
const Vector2i Player::SIZE = Vector2i(50, 50);
@ -21,9 +20,9 @@ const float Player::POINT_REACHED_DISTANCE = 1.0f;
/**
* Initializes Sprite.
*/
Player::Player(const Instances& instances, const Vector2f& position) :
Player::Player(const Instances& instances, const Vector2f& position, const Yaml& config) :
Character(instances, "player.png", PhysicalData(position, SIZE, instances.world,
CATEGORY_ACTOR, MASK_ALL, true, false, true), 100),
CATEGORY_ACTOR, MASK_ALL, true, false, true), config),
mWeapon(instances, *this, Yaml("weapon.yaml")),
mDirection(0),
mPathfinder(instances.pathfinder) {

View file

@ -16,11 +16,13 @@
#include "../abstract/Character.h"
#include "../items/Weapon.h"
#include "../util/Vector.h"
#include "../util/Yaml.h"
class Character;
class Instances;
class Pathfinder;
class Weapon;
class Yaml;
/**
* Player object.
@ -40,7 +42,7 @@ public:
// Public functions.
public:
Player(const Instances& instances, const Vector2f& position);
Player(const Instances& instances, const Vector2f& position, const Yaml& config);
void setCrosshairPosition(const Vector2f& position);
void fire();