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 <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().
*/
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<Sprite>(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);
}

View file

@ -11,16 +11,19 @@
#include <vector>
#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<Character*> mInstances;
static std::vector<Character*> mCharacterInstances;
const int mMaxHealth;
int mCurrentHealth; //< Current health. Between 0 and mMaxHealth.
Instances mInstances;
};
#endif /* DG_ACTOR_H_ */

View file

@ -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<Sprite>(new Body(mWorld, getPosition())));
}
void
Enemy::onThink(float elapsedTime) {
}
void
Enemy::onDeath() {
setDelete(true);
}

View file

@ -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:

View file

@ -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),