Added abstract Gadget and Heal gadget.

This commit is contained in:
Felix Ableitner 2013-07-06 21:21:25 +02:00
parent 19dcda6b8c
commit db3bf60b2c
8 changed files with 156 additions and 2 deletions

View file

@ -9,7 +9,8 @@
#include <Thor/Vectors.hpp>
#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<Player>(new Player(mWorld, mPathfinder,
mGenerator.getPlayerSpawn()));
mGenerator.getPlayerSpawn()));
mPlayer->setLeftGadget(std::shared_ptr<Gadget>(new Heal()));
mPlayer->setRightGadget(std::shared_ptr<Gadget>(new Heal()));
mWorld.insertCharacter(mPlayer);
mHealth = window.add<tgui::Label>();
@ -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;

View file

@ -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> gadget) {
mLeftGadget = gadget;
}
void
Character::setRightGadget(std::shared_ptr<Gadget> gadget) {
mRightGadget = gadget;
}
void
Character::useLeftGadget() {
mLeftGadget->use(*this);
}
void
Character::useRightGadget() {
mRightGadget->use(*this);
}
int
Character::getHealth() const {
return mCurrentHealth;

View file

@ -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> gadget);
void setRightGadget(std::shared_ptr<Gadget> gadget);
void useLeftGadget();
void useRightGadget();
protected:
private:
void move();
@ -73,6 +81,8 @@ private:
std::shared_ptr<Weapon> mFirstWeapon;
std::shared_ptr<Weapon> mSecondWeapon;
std::shared_ptr<Weapon> mActiveWeapon;
std::shared_ptr<Gadget> mLeftGadget;
std::shared_ptr<Gadget> mRightGadget;
std::vector<sf::Vector2f> mPath; //< Contains nodes to reach a set destination.
sf::Vector2f mLastPosition;
Faction mFaction;

18
source/items/Gadget.cpp Normal file
View file

@ -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());
}
}

28
source/items/Gadget.h Normal file
View file

@ -0,0 +1,28 @@
/*
* Gadget.h
*
* Created on: 06.07.2013
* Author: Felix
*/
#ifndef DG_GADGET_H_
#define DG_GADGET_H_
#include <Thor/Time.hpp>
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_ */

31
source/items/Heal.cpp Normal file
View file

@ -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);
}

25
source/items/Heal.h Normal file
View file

@ -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_ */

View file

@ -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: