Moved creation of Body from Enemy to Character.

This commit is contained in:
Felix Ableitner 2012-10-04 19:24:46 +02:00
parent cdeac690f2
commit f013b3c000
5 changed files with 30 additions and 27 deletions

View file

@ -10,25 +10,33 @@
#include <algorithm> #include <algorithm>
#include <assert.h> #include <assert.h>
std::vector<Character*> Character::mInstances = std::vector<Character*>(); #include "../sprite/Body.h"
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(const sf::String& texturePath, const PhysicalData& data, int health) : Character::Character(const Instances& instances, const sf::String& texturePath,
const PhysicalData& data, int health) :
Sprite(texturePath, data), Sprite(texturePath, data),
mMaxHealth(health), mMaxHealth(health),
mCurrentHealth(health) { mCurrentHealth(health),
mInstances.push_back(this); mInstances(instances) {
mCharacterInstances.push_back(this);
} }
/** /**
* Deletes pointer from think() static var. * Deletes pointer from static variable mCharacterInstances, inserts body into world
* (done here to avoid altering Box2D data during timestep).
*/ */
Character::~Character() { Character::~Character() {
auto it = std::find(mInstances.begin(), mInstances.end(), this); auto it = std::find(mCharacterInstances.begin(), mCharacterInstances.end(), this);
assert(it != mInstances.end()); assert(it != mCharacterInstances.end());
mInstances.erase(it); mCharacterInstances.erase(it);
mInstances.collection.insert(std::shared_ptr<Sprite>(new Body(mInstances.world,
getPosition())));
} }
/** /**
@ -38,7 +46,7 @@ Character::~Character() {
*/ */
void void
Character::think(float elapsedTime) { Character::think(float elapsedTime) {
for (auto i : mInstances) { for (auto i : mCharacterInstances) {
i->onThink(elapsedTime); i->onThink(elapsedTime);
} }
} }
@ -58,8 +66,11 @@ Character::onDamage(int damage) {
} }
/** /**
* Called when health reaches zero. Does nothing by default. * Called when health reaches zero. Marks the object for deletion.
*
* @warning Implementations should call the default implementation.
*/ */
void void
Character::onDeath() { Character::onDeath() {
setDelete(true);
} }

View file

@ -11,16 +11,19 @@
#include <vector> #include <vector>
#include "Sprite.h" #include "Sprite.h"
#include "../Instances.h"
class Instances;
class Sprite; class Sprite;
/** /**
* Provides think function for AI. * Provides think function for AI, manages health, drops body on death.
*/ */
class Character : public Sprite { class Character : public Sprite {
// Public functions. // Public functions.
public: public:
Character(const sf::String& texturePath, const PhysicalData& data, int health); Character(const Instances& instances, const sf::String& texturePath,
const PhysicalData& data, int health);
virtual ~Character() = 0; virtual ~Character() = 0;
static void think(float elapsedTime); static void think(float elapsedTime);
@ -40,10 +43,11 @@ protected:
// Private variables. // Private variables.
private: private:
static std::vector<Character*> mInstances; static std::vector<Character*> mCharacterInstances;
const int mMaxHealth; const int mMaxHealth;
int mCurrentHealth; //< Current health. Between 0 and mMaxHealth. int mCurrentHealth; //< Current health. Between 0 and mMaxHealth.
Instances mInstances;
}; };
#endif /* DG_ACTOR_H_ */ #endif /* DG_ACTOR_H_ */

View file

@ -10,22 +10,12 @@
#include "Body.h" #include "Body.h"
Enemy::Enemy(const Instances& instances, const Vector2f& position) : Enemy::Enemy(const Instances& instances, const Vector2f& position) :
Character("enemy.png", PhysicalData(position, Vector2i(50, 50), instances.world, 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), 100),
mWorld(instances.world), mWorld(instances.world),
mCollection(instances.collection) { mCollection(instances.collection) {
} }
Enemy::~Enemy() {
// Insert here to avoid altering b2d data during timestep.
mCollection.insert(std::shared_ptr<Sprite>(new Body(mWorld, getPosition())));
}
void void
Enemy::onThink(float elapsedTime) { Enemy::onThink(float elapsedTime) {
} }
void
Enemy::onDeath() {
setDelete(true);
}

View file

@ -21,12 +21,10 @@ class Enemy : public Character {
// Public functions. // Public functions.
public: public:
Enemy(const Instances& instances, const Vector2f& position); Enemy(const Instances& instances, const Vector2f& position);
~Enemy();
// Private functions. // Private functions.
private: private:
void onThink(float elapsedTime); void onThink(float elapsedTime);
void onDeath();
// Private variablese. // Private variablese.
private: private:

View file

@ -20,7 +20,7 @@ const float Player::POINT_REACHED_DISTANCE = 1.0f;
* Initializes Sprite. * Initializes Sprite.
*/ */
Player::Player(const Instances& instances, const Vector2f& position) : Player::Player(const Instances& instances, const Vector2f& position) :
Character("player.png", PhysicalData(position, SIZE, instances.world, Character(instances, "player.png", PhysicalData(position, SIZE, instances.world,
CATEGORY_ACTOR, MASK_ALL, true, false, true), 100), CATEGORY_ACTOR, MASK_ALL, true, false, true), 100),
mWeapon(instances, *this, SIZE), mWeapon(instances, *this, SIZE),
mDirection(0), mDirection(0),