diff --git a/src/Game.cpp b/src/Game.cpp index 0d985e4..7f11007 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -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(); diff --git a/src/sprites/Player.h b/src/sprites/Player.h index 1470b5b..6ccf2c0 100644 --- a/src/sprites/Player.h +++ b/src/sprites/Player.h @@ -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: diff --git a/src/sprites/abstract/Character.cpp b/src/sprites/abstract/Character.cpp index d32f77d..1ed8fb0 100644 --- a/src/sprites/abstract/Character.cpp +++ b/src/sprites/abstract/Character.cpp @@ -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; + onDeath(); 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 { diff --git a/src/sprites/abstract/Character.h b/src/sprites/abstract/Character.h index 33cf97f..3ce29bd 100644 --- a/src/sprites/abstract/Character.h +++ b/src/sprites/abstract/Character.h @@ -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 > 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); void setSecondWeapon(std::shared_ptr weapon); void setLeftGadget(std::shared_ptr gadget); void setRightGadget(std::shared_ptr gadget); void useLeftGadget(); void useRightGadget(); - std::string getLeftGadgetName() const; - std::string getRightGadgetName() const; void pickUpItem(); private: @@ -103,7 +103,6 @@ private: std::vector mPath; //< Contains nodes to reach a set destination. Vector2f mLastPosition; Faction mFaction; - bool mIsDead = false; }; #endif /* DG_ACTOR_H_ */