diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index 6d50e7e..4a3a3f0 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -10,25 +10,33 @@ #include #include -std::vector Character::mInstances = std::vector(); +#include "../sprite/Body.h" + +std::vector Character::mCharacterInstances = std::vector(); /** * 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), mMaxHealth(health), - mCurrentHealth(health) { - mInstances.push_back(this); + mCurrentHealth(health), + 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() { - auto it = std::find(mInstances.begin(), mInstances.end(), this); - assert(it != mInstances.end()); - mInstances.erase(it); + auto it = std::find(mCharacterInstances.begin(), mCharacterInstances.end(), this); + assert(it != mCharacterInstances.end()); + mCharacterInstances.erase(it); + + mInstances.collection.insert(std::shared_ptr(new Body(mInstances.world, + getPosition()))); } /** @@ -38,7 +46,7 @@ Character::~Character() { */ void Character::think(float elapsedTime) { - for (auto i : mInstances) { + for (auto i : mCharacterInstances) { 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 Character::onDeath() { + setDelete(true); } diff --git a/source/abstract/Character.h b/source/abstract/Character.h index dbdaab6..be6da00 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -11,16 +11,19 @@ #include #include "Sprite.h" +#include "../Instances.h" +class Instances; class Sprite; /** - * Provides think function for AI. + * Provides think function for AI, manages health, drops body on death. */ class Character : public Sprite { // Public functions. 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; static void think(float elapsedTime); @@ -40,10 +43,11 @@ protected: // Private variables. private: - static std::vector mInstances; + static std::vector mCharacterInstances; const int mMaxHealth; int mCurrentHealth; //< Current health. Between 0 and mMaxHealth. + Instances mInstances; }; #endif /* DG_ACTOR_H_ */ diff --git a/source/sprite/Enemy.cpp b/source/sprite/Enemy.cpp index a694426..1035a3b 100644 --- a/source/sprite/Enemy.cpp +++ b/source/sprite/Enemy.cpp @@ -10,22 +10,12 @@ #include "Body.h" 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), mWorld(instances.world), mCollection(instances.collection) { } -Enemy::~Enemy() { - // Insert here to avoid altering b2d data during timestep. - mCollection.insert(std::shared_ptr(new Body(mWorld, getPosition()))); -} - void Enemy::onThink(float elapsedTime) { } - -void -Enemy::onDeath() { - setDelete(true); -} diff --git a/source/sprite/Enemy.h b/source/sprite/Enemy.h index e262067..f888686 100644 --- a/source/sprite/Enemy.h +++ b/source/sprite/Enemy.h @@ -21,12 +21,10 @@ class Enemy : public Character { // Public functions. public: Enemy(const Instances& instances, const Vector2f& position); - ~Enemy(); // Private functions. private: void onThink(float elapsedTime); - void onDeath(); // Private variablese. private: diff --git a/source/sprite/Player.cpp b/source/sprite/Player.cpp index 0190e45..0b64f41 100644 --- a/source/sprite/Player.cpp +++ b/source/sprite/Player.cpp @@ -20,7 +20,7 @@ const float Player::POINT_REACHED_DISTANCE = 1.0f; * Initializes Sprite. */ 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), mWeapon(instances, *this, SIZE), mDirection(0),