diff --git a/resources/textures/shield.png b/resources/textures/shield.png new file mode 100644 index 0000000..1fbbabc Binary files /dev/null and b/resources/textures/shield.png differ diff --git a/resources/yaml/rotating_shield.yaml b/resources/yaml/rotating_shield.yaml new file mode 100644 index 0000000..f4d30c7 --- /dev/null +++ b/resources/yaml/rotating_shield.yaml @@ -0,0 +1,3 @@ +name: Shield +texture: shield.png +size: [75, 5] \ No newline at end of file diff --git a/source/Game.cpp b/source/Game.cpp index 3963ec7..1a598c4 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -10,7 +10,8 @@ #include #include "generator/Generator.h" -#include "items/Heal.h" +#include "items/Heal.h" +#include "items/Shield.h" #include "sprites/Enemy.h" #include "sprites/Player.h" #include "util/Yaml.h" @@ -33,7 +34,7 @@ Game::Game(tgui::Window& window) : mPlayer = std::shared_ptr(new Player(mWorld, mPathfinder, mGenerator.getPlayerSpawn())); mPlayer->setLeftGadget(std::shared_ptr(new Heal())); - mPlayer->setRightGadget(std::shared_ptr(new Heal())); + mPlayer->setRightGadget(std::shared_ptr(new Shield())); mWorld.insertCharacter(mPlayer); mHealth = window.add(); diff --git a/source/World.cpp b/source/World.cpp index 5029a99..631ef0e 100755 --- a/source/World.cpp +++ b/source/World.cpp @@ -156,7 +156,7 @@ World::raycast(const sf::Vector2f& lineStart, assert(lineStart != lineEnd); sf::Vector2f lineCenter = lineStart + 0.5f * (lineEnd - lineStart); for (const auto& it : mDrawables.at(Sprite::Category::CATEGORY_WORLD)) { - if (dynamic_cast(it.get())->getType() != Tile::Type::WALL) + if (!it->collisionEnabled(Sprite::CATEGORY_ACTOR)) continue; sf::Vector2f axis = it->getPosition() - lineCenter; if (axis == sf::Vector2f()) diff --git a/source/abstract/Character.h b/source/abstract/Character.h index 318583e..cafbaf1 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -68,6 +68,8 @@ private: void move(); private: + friend class Shield; + /// Distance to a path point where it will be considered as reached (to /// avoid floating point equality check). static const float POINT_REACHED_DISTANCE; diff --git a/source/abstract/Sprite.cpp b/source/abstract/Sprite.cpp index 7dc89b3..9f8f234 100755 --- a/source/abstract/Sprite.cpp +++ b/source/abstract/Sprite.cpp @@ -56,7 +56,7 @@ Sprite::getSpeed() const { */ sf::Vector2f Sprite::getDirection() const { - return thor::rotatedVector(sf::Vector2f(1, 0), mShape.getRotation()); + return thor::rotatedVector(sf::Vector2f(0, - 1), mShape.getRotation()); } /** diff --git a/source/items/Shield.cpp b/source/items/Shield.cpp new file mode 100644 index 0000000..c5db5d1 --- /dev/null +++ b/source/items/Shield.cpp @@ -0,0 +1,36 @@ +/* + * Shield.cpp + * + * Created on: 06.07.2013 + * Author: Felix + */ + +#include "Shield.h" + +#include "../abstract/Character.h" +#include "../sprites/RotatingShield.h" +#include "../World.h" + +void +Shield::onUse(Character& character) { + mCharacter = &character; + if (mRotatingShield) + mRotatingShield->setDelete(true); + sf::Vector2f offset = mCharacter->getDirection() * mCharacter->getRadius(); + mRotatingShield = std::shared_ptr( + new RotatingShield(mCharacter->getPosition() + offset)); + mCharacter->mWorld.insert(mRotatingShield); +} + +void +Shield::onThink(int elapsed) { + if (mRotatingShield) { + mRotatingShield->setDirection(mCharacter->getPosition() - + mRotatingShield->getPosition()); + } +} + +sf::Time +Shield::getCooldownTime() { + return sf::seconds(10); +} diff --git a/source/items/Shield.h b/source/items/Shield.h new file mode 100644 index 0000000..bae6791 --- /dev/null +++ b/source/items/Shield.h @@ -0,0 +1,27 @@ +/* + * Shield.h + * + * Created on: 06.07.2013 + * Author: Felix + */ + +#ifndef DG_SHIELD_H_ +#define DG_SHIELD_H_ + +#include "Gadget.h" + +class RotatingShield; +class Sprite; + +class Shield : public Gadget { +protected: + void onUse(Character& character) override; + void onThink(int elapsed) override; + sf::Time getCooldownTime() override; + +private: + Character* mCharacter; + std::shared_ptr mRotatingShield; +}; + +#endif /* DG_SHIELD_H_ */ diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index 3263890..6b9a221 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -148,18 +148,14 @@ Weapon::cancelReload() { */ void Weapon::insertProjectile(float angle) { - // Minus to account for positive y-axis going downwards in SFML. - sf::Vector2f offset(0, - mHolder.getRadius()); - thor::rotate(offset, thor::polarAngle(mHolder.getDirection())); + sf::Vector2f offset(mHolder.getDirection() * mHolder.getRadius()); float spread = (mHolder.getSpeed() == sf::Vector2f()) ? mSpread : mSpreadMoving; std::uniform_real_distribution distribution(- spread, spread); - angle += distribution(mGenerator); + angle += distribution(mGenerator) + 90.0f; - //float random = ((float) rand()) / (float) RAND_MAX; - //angle += random * 2 * mSpread - mSpread; sf::Vector2f direction(thor::rotatedVector(mHolder.getDirection(), angle)); std::shared_ptr projectile(new Bullet(mHolder.getPosition() + offset, diff --git a/source/sprites/RotatingShield.cpp b/source/sprites/RotatingShield.cpp new file mode 100644 index 0000000..6f27404 --- /dev/null +++ b/source/sprites/RotatingShield.cpp @@ -0,0 +1,15 @@ +/* + * RotatingShield.cpp + * + * Created on: 06.07.2013 + * Author: Felix + */ + +#include "RotatingShield.h" + +#include "../util/Yaml.h" + +RotatingShield::RotatingShield(const sf::Vector2f& position) : + Rectangle(position, CATEGORY_WORLD, MASK_ALL, + Yaml("rotating_shield.yaml")) { +} diff --git a/source/sprites/RotatingShield.h b/source/sprites/RotatingShield.h new file mode 100644 index 0000000..e978e33 --- /dev/null +++ b/source/sprites/RotatingShield.h @@ -0,0 +1,25 @@ +/* + * RotatingShield.h + * + * Created on: 06.07.2013 + * Author: Felix + */ + +#ifndef DG_ROTATINGSHIELD_H_ +#define DG_ROTATINGSHIELD_H_ + +#include "../abstract/Rectangle.h" + +class RotatingShield : public Rectangle { +public: + explicit RotatingShield(const sf::Vector2f& position); + +private: + using Sprite::setDelete; + using Sprite::setDirection; + using Sprite::getPosition; + + friend class Shield; +}; + +#endif /* DG_ROTATINGSHIELD_H_ */ diff --git a/source/sprites/Tile.cpp b/source/sprites/Tile.cpp index f5d30d7..d2c7f1a 100644 --- a/source/sprites/Tile.cpp +++ b/source/sprites/Tile.cpp @@ -50,7 +50,8 @@ Tile::isSolid(Type type) { switch (type) { case Type::FLOOR: return false; - case Type::WALL: // falltrough + case Type::WALL: + // falltrough default: return true; }