This repository has been archived on 2019-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
dungeon-gunner/source/abstract/Character.cpp

77 lines
1.6 KiB
C++
Raw Normal View History

2012-10-01 09:02:44 +00:00
/*
* Actor.cpp
*
* Created on: 02.09.2012
* Author: Felix
2012-09-12 12:21:57 +00:00
*/
2012-10-01 09:02:44 +00:00
#include "Character.h"
#include <algorithm>
#include <assert.h>
#include "../sprite/Body.h"
std::vector<Character*> Character::mCharacterInstances = std::vector<Character*>();
2012-10-01 09:02:44 +00:00
/**
* Saves pointer to this instance in static var for think().
*/
Character::Character(const Instances& instances, const sf::String& texturePath,
const PhysicalData& data, int health) :
2012-10-01 09:13:26 +00:00
Sprite(texturePath, data),
mMaxHealth(health),
mCurrentHealth(health),
mInstances(instances) {
mCharacterInstances.push_back(this);
2012-10-01 09:02:44 +00:00
}
/**
* Deletes pointer from static variable mCharacterInstances, inserts body into world
* (done here to avoid altering Box2D data during timestep).
2012-10-01 09:02:44 +00:00
*/
Character::~Character() {
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())));
2012-10-01 09:02:44 +00:00
}
/**
* Calls onThink on all Actor instances.
*
* @param elapsedTime Amount of time to simulate.
*/
void
Character::think(float elapsedTime) {
for (auto i : mCharacterInstances) {
2012-10-01 09:02:44 +00:00
i->onThink(elapsedTime);
}
}
/**
* Subtracts health from Actor.
*
* @param damage Amount of health to subtract.
*/
void
Character::onDamage(int damage) {
mCurrentHealth -= damage;
if (mCurrentHealth <= 0) {
mCurrentHealth = 0;
onDeath();
}
2012-09-12 12:21:57 +00:00
}
2012-10-01 09:02:44 +00:00
/**
* Called when health reaches zero. Marks the object for deletion.
*
* @warning Implementations should call the default implementation.
2012-10-01 09:02:44 +00:00
*/
void
Character::onDeath() {
setDelete(true);
2012-09-12 12:21:57 +00:00
}