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(); : mClock.restart().asMilliseconds();
mWorld.think(elapsed); mWorld.think(elapsed);
if (mPlayer->getHealth() == 0) { // Respawn player at start position on death.
if (mPlayer->getHealth() <= 0) {
Vector2f pos = mPlayer->getCrosshairPosition(); Vector2f pos = mPlayer->getCrosshairPosition();
mWorld.remove(mPlayer); mWorld.remove(mPlayer);
initPlayer(); initPlayer();

View file

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

View file

@ -49,18 +49,17 @@ Character::~Character() {
*/ */
void void
Character::onDamage(int damage) { Character::onDamage(int damage) {
mCurrentHealth -= damage; // Otherwise player might not respawn after death
if (mCurrentHealth <= 0)
return;
mCurrentHealth -= damage;
if (mCurrentHealth > mMaxHealth) if (mCurrentHealth > mMaxHealth)
mCurrentHealth = mMaxHealth; mCurrentHealth = mMaxHealth;
if (mCurrentHealth <= 0) { if (mCurrentHealth <= 0) {
// Seperated to avoid firing multiple times (when damaged multiple times).
if (!mIsDead)
onDeath(); onDeath();
mIsDead = true;
mCurrentHealth = 0;
setDelete(true); setDelete(true);
} }
} }
@ -73,7 +72,7 @@ Character::onDamage(int damage) {
*/ */
void void
Character::onThink(int elapsed) { Character::onThink(int elapsed) {
if (mCurrentHealth == 0) if (mCurrentHealth <= 0)
return; return;
mActiveWeapon->onThink(elapsed); 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 int
Character::getHealth() const { Character::getHealth() const {

View file

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