Added rotating shield gadget.

This commit is contained in:
Felix Ableitner 2013-07-10 23:25:53 +02:00
parent 16ad937c57
commit f160cfb2f3
12 changed files with 117 additions and 11 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 677 B

View file

@ -0,0 +1,3 @@
name: Shield
texture: shield.png
size: [75, 5]

View file

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

View file

@ -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<Tile*>(it.get())->getType() != Tile::Type::WALL)
if (!it->collisionEnabled(Sprite::CATEGORY_ACTOR))
continue;
sf::Vector2f axis = it->getPosition() - lineCenter;
if (axis == sf::Vector2f())

View file

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

View file

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

36
source/items/Shield.cpp Normal file
View file

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

27
source/items/Shield.h Normal file
View file

@ -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<RotatingShield> mRotatingShield;
};
#endif /* DG_SHIELD_H_ */

View file

@ -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<float> 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<Sprite> projectile(new Bullet(mHolder.getPosition() + offset,

View file

@ -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")) {
}

View file

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

View file

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