Fixed player not respawning after death.

This commit is contained in:
Felix Ableitner 2013-09-16 17:52:49 +02:00
parent 14d5347e6f
commit 4d83ff8c52
4 changed files with 14 additions and 20 deletions

View file

@ -116,7 +116,8 @@ Game::loop() {
: mClock.restart().asMilliseconds();
mWorld.think(elapsed);
if (mPlayer->getHealth() == 0) {
// Respawn player at start position on death.
if (mPlayer->getHealth() <= 0) {
Vector2f pos = mPlayer->getCrosshairPosition();
mWorld.remove(mPlayer);
initPlayer();

View file

@ -38,8 +38,6 @@ public:
using Character::releaseTrigger;
void setDirection(Direction direction, bool unset);
void setDestination(const Vector2f& destination);
using Character::getMagazineAmmo;
using Character::getTotalAmmo;
using Character::getWeaponName;
using Character::reload;
using Character::toggleWeapon;
@ -49,9 +47,6 @@ public:
using Character::setRightGadget;
using Character::useLeftGadget;
using Character::useRightGadget;
using Character::getHealth;
using Character::getLeftGadgetName;
using Character::getRightGadgetName;
using Character::pickUpItem;
private:

View file

@ -49,18 +49,17 @@ Character::~Character() {
*/
void
Character::onDamage(int damage) {
mCurrentHealth -= damage;
// Otherwise player might not respawn after death
if (mCurrentHealth <= 0)
return;
mCurrentHealth -= damage;
if (mCurrentHealth > mMaxHealth)
mCurrentHealth = mMaxHealth;
if (mCurrentHealth <= 0) {
// Seperated to avoid firing multiple times (when damaged multiple times).
if (!mIsDead)
onDeath();
mIsDead = true;
mCurrentHealth = 0;
setDelete(true);
}
}
@ -73,7 +72,7 @@ Character::onDamage(int damage) {
*/
void
Character::onThink(int elapsed) {
if (mCurrentHealth == 0)
if (mCurrentHealth <= 0)
return;
mActiveWeapon->onThink(elapsed);
@ -253,7 +252,7 @@ Character::selectSecondWeapon() {
}
/**
* Returns current player health.
* Returns current player health. A value <= 0 means this character is dead.
*/
int
Character::getHealth() const {

View file

@ -49,6 +49,11 @@ public:
void onDamage(int damage);
Faction getFaction() const;
EquippedItems getEquippedItems() const;
int getHealth() const;
int getMagazineAmmo() const;
int getTotalAmmo() const;
std::string getLeftGadgetName() const;
std::string getRightGadgetName() const;
protected:
virtual void onThink(int elapsed);
@ -60,22 +65,17 @@ protected:
bool isPathEmpty() const;
bool isVisible(const Vector2f& target) const;
std::vector<std::shared_ptr<Character> > getCharacters() const;
int getMagazineAmmo() const;
int getTotalAmmo() const;
std::string getWeaponName() const;
void reload();
void toggleWeapon();
void selectFirstWeapon();
void selectSecondWeapon();
int getHealth() const;
void setFirstWeapon(std::shared_ptr<Weapon> weapon);
void setSecondWeapon(std::shared_ptr<Weapon> weapon);
void setLeftGadget(std::shared_ptr<Gadget> gadget);
void setRightGadget(std::shared_ptr<Gadget> gadget);
void useLeftGadget();
void useRightGadget();
std::string getLeftGadgetName() const;
std::string getRightGadgetName() const;
void pickUpItem();
private:
@ -103,7 +103,6 @@ private:
std::vector<Vector2f> mPath; //< Contains nodes to reach a set destination.
Vector2f mLastPosition;
Faction mFaction;
bool mIsDead = false;
};
#endif /* DG_ACTOR_H_ */