From 55fbf1be203282c5005db00e588da20c9addfc1a Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 30 Aug 2013 15:33:23 +0200 Subject: [PATCH] Added Ring of Fire gadget. --- src/Game.cpp | 6 ++-- src/sprites/Enemy.cpp | 12 ++++---- src/sprites/items/RingOfFire.cpp | 48 ++++++++++++++++++++++++++++++++ src/sprites/items/RingOfFire.h | 37 ++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 src/sprites/items/RingOfFire.cpp create mode 100644 src/sprites/items/RingOfFire.h diff --git a/src/Game.cpp b/src/Game.cpp index c6f7bdc..1616cfc 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -11,7 +11,7 @@ #include "generator/Generator.h" #include "sprites/items/Heal.h" -#include "sprites/items/Shield.h" +#include "sprites/items/RingOfFire.h" #include "sprites/Enemy.h" #include "sprites/Player.h" #include "util/Loader.h" @@ -58,8 +58,8 @@ Game::Game(tgui::Window& window) : void Game::initPlayer() { mPlayer = std::shared_ptr(new Player(mWorld, mPathfinder, mGenerator.getPlayerSpawn())); - mPlayer->setLeftGadget(std::shared_ptr < Gadget > (new Heal())); - mPlayer->setRightGadget(std::shared_ptr < Gadget > (new Shield())); + mPlayer->setLeftGadget(std::shared_ptr(new Heal())); + mPlayer->setRightGadget(std::shared_ptr(new RingOfFire(mWorld))); mWorld.insertCharacter(mPlayer); } diff --git a/src/sprites/Enemy.cpp b/src/sprites/Enemy.cpp index eac7ca0..2f893e4 100644 --- a/src/sprites/Enemy.cpp +++ b/src/sprites/Enemy.cpp @@ -10,6 +10,7 @@ #include #include "items/Heal.h" +#include "items/RingOfFire.h" #include "items/Shield.h" #include "items/Weapon.h" #include "../util/Yaml.h" @@ -36,14 +37,15 @@ Enemy::Enemy(World& world, Pathfinder& pathfinder, // Select secondary weapon. switch (std::uniform_int_distribution(0, 1)(generator)) { - case 0: setSecondWeapon(Weapon::getWeapon(world, *this, Weapon::WeaponType::PISTOL)); - case 1: setSecondWeapon(Weapon::getWeapon(world, *this, Weapon::WeaponType::KNIFE)); + case 0: setSecondWeapon(Weapon::getWeapon(world, *this, Weapon::WeaponType::PISTOL)); break; + case 1: setSecondWeapon(Weapon::getWeapon(world, *this, Weapon::WeaponType::KNIFE)); break; } // Select gadget. - switch (std::uniform_int_distribution(0, 1)(generator)) { - case 0: setLeftGadget(std::shared_ptr(new Heal())); - case 1: setLeftGadget(std::shared_ptr(new Shield())); + switch (std::uniform_int_distribution(0, 2)(generator)) { + case 0: setLeftGadget(std::shared_ptr(new Heal())); break; + case 1: setLeftGadget(std::shared_ptr(new Shield())); break; + case 2: setLeftGadget(std::shared_ptr(new RingOfFire(world))); break; } } diff --git a/src/sprites/items/RingOfFire.cpp b/src/sprites/items/RingOfFire.cpp new file mode 100644 index 0000000..d592c0c --- /dev/null +++ b/src/sprites/items/RingOfFire.cpp @@ -0,0 +1,48 @@ +/* + * RingOfFire.cpp + * + * Created on: 29.08.2013 + * Author: Felix + */ + +#include "RingOfFire.h" + +#include + +#include "../effects/Bullet.h" +#include "../../World.h" + +RingOfFire::RingOfFire(World& world) : + Gadget("Ring of Fire"), + mWorld(world), + mBullet("bullet.yaml") { +} + +void +RingOfFire::onUse(Character& character) { + mCharacter = &character; + mCurrentWave = 0; +} + +void +RingOfFire::onThink(int elapsed) { + if (mCurrentWave < WAVES_PER_USE && mTimer.isExpired()) { + for (int angle = mCurrentWave * 10; angle <= 360; angle += 30) { + Vector2f direction(thor::rotatedVector(mCharacter->getDirection(), (float) angle) * + mCharacter->getRadius()); + + std::shared_ptr projectile(new Bullet(mCharacter->getPosition() + direction, + *mCharacter, direction, mBullet, 200, 20, 0)); + mWorld.insert(projectile); + } + + mTimer.restart(sf::milliseconds(500)); + mCurrentWave++; + } +} + +sf::Time +RingOfFire::getCooldownTime() { + return sf::seconds(5); +} + diff --git a/src/sprites/items/RingOfFire.h b/src/sprites/items/RingOfFire.h new file mode 100644 index 0000000..35fd13c --- /dev/null +++ b/src/sprites/items/RingOfFire.h @@ -0,0 +1,37 @@ +/* + * RingOfFire.h + * + * Created on: 29.08.2013 + * Author: Felix + */ + +#ifndef DG_RINGOFFIRE_H_ +#define DG_RINGOFFIRE_H_ + +#include "Gadget.h" +#include "../abstract/Character.h" +#include "../../util/Yaml.h" + +/** + * Gadget that fires bullets in all directions for multiple waves. + */ +class RingOfFire : public Gadget { +public: + RingOfFire(World& world); + +protected: + void onUse(Character& character) override; + void onThink(int elapsed) override; + sf::Time getCooldownTime() override; + +private: + static const int WAVES_PER_USE = 3; + int mCurrentWave = WAVES_PER_USE + 1; + + Character* mCharacter; + thor::Timer mTimer; + World& mWorld; + const Yaml mBullet; +}; + +#endif /* DG_RINGOFFIRE_H_ */