diff --git a/source/Game.cpp b/source/Game.cpp index 7ed549f..3963ec7 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -9,7 +9,8 @@ #include -#include "generator/Generator.h" +#include "generator/Generator.h" +#include "items/Heal.h" #include "sprites/Enemy.h" #include "sprites/Player.h" #include "util/Yaml.h" @@ -30,7 +31,9 @@ Game::Game(tgui::Window& window) : mGenerator.generateCurrentAreaIfNeeded(sf::Vector2f()); mPlayer = std::shared_ptr(new Player(mWorld, mPathfinder, - mGenerator.getPlayerSpawn())); + mGenerator.getPlayerSpawn())); + mPlayer->setLeftGadget(std::shared_ptr(new Heal())); + mPlayer->setRightGadget(std::shared_ptr(new Heal())); mWorld.insertCharacter(mPlayer); mHealth = window.add(); @@ -181,6 +184,12 @@ Game::keyDown(const sf::Event& event) { case sf::Keyboard::D: mPlayer->setDirection(Player::Direction::RIGHT, false); break; + case sf::Keyboard::Q: + mPlayer->useLeftGadget(); + break; + case sf::Keyboard::E: + mPlayer->useRightGadget(); + break; case sf::Keyboard::R: mPlayer->reload(); break; diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index 418c319..8fc1894 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -52,6 +52,10 @@ Character::~Character() { void Character::onDamage(int damage) { mCurrentHealth -= damage; + + if (mCurrentHealth > mMaxHealth) + mCurrentHealth = mMaxHealth; + if (mCurrentHealth <= 0) { mCurrentHealth = 0; onDeath(); @@ -68,6 +72,10 @@ Character::onDamage(int damage) { void Character::onThink(int elapsed) { mActiveWeapon->onThink(elapsed); + if (mLeftGadget) + mLeftGadget->onThink(elapsed); + if (mRightGadget) + mRightGadget->onThink(elapsed); move(); } @@ -211,6 +219,27 @@ Character::selectSecondWeapon() { mActiveWeapon->cancelReload(); mActiveWeapon = mSecondWeapon; } + +void +Character::setLeftGadget(std::shared_ptr gadget) { + mLeftGadget = gadget; +} + +void +Character::setRightGadget(std::shared_ptr gadget) { + mRightGadget = gadget; +} + +void +Character::useLeftGadget() { + mLeftGadget->use(*this); +} + +void +Character::useRightGadget() { + mRightGadget->use(*this); +} + int Character::getHealth() const { return mCurrentHealth; diff --git a/source/abstract/Character.h b/source/abstract/Character.h index bebba2a..318583e 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -10,6 +10,8 @@ #include "Circle.h" +#include "../items/Gadget.h" + class Pathfinder; class World; class Weapon; @@ -55,6 +57,12 @@ protected: void selectFirstWeapon(); void selectSecondWeapon(); int getHealth() const; + void setLeftGadget(std::shared_ptr gadget); + void setRightGadget(std::shared_ptr gadget); + void useLeftGadget(); + void useRightGadget(); + +protected: private: void move(); @@ -73,6 +81,8 @@ private: std::shared_ptr mFirstWeapon; std::shared_ptr mSecondWeapon; std::shared_ptr mActiveWeapon; + std::shared_ptr mLeftGadget; + std::shared_ptr mRightGadget; std::vector mPath; //< Contains nodes to reach a set destination. sf::Vector2f mLastPosition; Faction mFaction; diff --git a/source/items/Gadget.cpp b/source/items/Gadget.cpp new file mode 100644 index 0000000..ee47e20 --- /dev/null +++ b/source/items/Gadget.cpp @@ -0,0 +1,18 @@ +/* + * Gadget.cpp + * + * Created on: 06.07.2013 + * Author: Felix + */ + +#include "Gadget.h" + +#include "../abstract/Character.h" + +void +Gadget::use(Character& character) { + if (mCooldownTimer.isExpired()) { + onUse(character); + mCooldownTimer.restart(getCooldownTime()); + } +} diff --git a/source/items/Gadget.h b/source/items/Gadget.h new file mode 100644 index 0000000..9d10acc --- /dev/null +++ b/source/items/Gadget.h @@ -0,0 +1,28 @@ +/* + * Gadget.h + * + * Created on: 06.07.2013 + * Author: Felix + */ + +#ifndef DG_GADGET_H_ +#define DG_GADGET_H_ + +#include + +class Character; + +class Gadget { +public: + virtual void use(Character& character); + virtual void onThink(int elapsed) = 0; + +protected: + virtual void onUse(Character& character) = 0; + virtual sf::Time getCooldownTime() = 0; + +protected: + thor::Timer mCooldownTimer; +}; + +#endif /* DG_GADGET_H_ */ diff --git a/source/items/Heal.cpp b/source/items/Heal.cpp new file mode 100644 index 0000000..c407ee3 --- /dev/null +++ b/source/items/Heal.cpp @@ -0,0 +1,31 @@ +/* + * SlowHeal.cpp + * + * Created on: 06.07.2013 + * Author: Felix + */ + +#include "Heal.h" + +#include "../abstract/Character.h" + +void +Heal::onUse(Character& character) { + mCharacter = &character; + mHealedTotal = 0; +} + +void +Heal::onThink(int elapsed) { + if (mHealedTotal < 50 && mTimer.isExpired()) { + mCharacter->onDamage(-1); + mHealedTotal += 1; + mTimer.restart(sf::milliseconds(75)); + } +} + +sf::Time +Heal::getCooldownTime() { + return sf::seconds(5); +} + diff --git a/source/items/Heal.h b/source/items/Heal.h new file mode 100644 index 0000000..7550172 --- /dev/null +++ b/source/items/Heal.h @@ -0,0 +1,25 @@ +/* + * SlowHeal.h + * + * Created on: 06.07.2013 + * Author: Felix + */ + +#ifndef DG_SLOWHEAL_H_ +#define DG_SLOWHEAL_H_ + +#include "Gadget.h" + +class Heal : public Gadget { +protected: + void onUse(Character& character) override; + void onThink(int elapsed) override; + sf::Time getCooldownTime() override; + +private: + Character* mCharacter; + thor::Timer mTimer; + int mHealedTotal = 50; +}; + +#endif /* DG_SLOWHEAL_H_ */ diff --git a/source/sprites/Player.h b/source/sprites/Player.h index 14cf0eb..ab41d4f 100644 --- a/source/sprites/Player.h +++ b/source/sprites/Player.h @@ -43,6 +43,10 @@ public: using Character::toggleWeapon; using Character::selectFirstWeapon; using Character::selectSecondWeapon; + using Character::setLeftGadget; + using Character::setRightGadget; + using Character::useLeftGadget; + using Character::useRightGadget; using Character::getHealth; private: