Drop primary weapon on death.

This commit is contained in:
Felix Ableitner 2013-07-14 21:56:44 +02:00
parent cb11a22bff
commit 9401ea53c1
13 changed files with 71 additions and 11 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 445 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

View file

@ -88,12 +88,15 @@ Character::getFaction() const {
} }
/** /**
* Called when health reaches zero. Default implementation drops a corpse at * Called when health reaches zero. Drops corpse and item.
* the current position.
*/ */
void void
Character::onDeath() { Character::onDeath() {
mWorld.insert(std::shared_ptr<Sprite>(new Corpse(getPosition()))); mWorld.insert(std::shared_ptr<Sprite>(new Corpse(getPosition())));
mWorld.insert(mActiveWeapon);
mActiveWeapon->drop(getPosition());
} }
/** /**

View file

@ -17,8 +17,7 @@ Sprite::Sprite(const sf::Vector2f& position, Category category,
unsigned short mask, const sf::Vector2f& size, unsigned short mask, const sf::Vector2f& size,
const std::string& texture, const sf::Vector2f& direction) : const std::string& texture, const sf::Vector2f& direction) :
mCategory(category), mCategory(category),
mMask(mask), mMask(mask) {
mDelete(false) {
mShape.setSize(size); mShape.setSize(size);
mShape.setOrigin(size / 2.0f); mShape.setOrigin(size / 2.0f);
mShape.setTextureRect(sf::IntRect(sf::Vector2i(), sf::Vector2i(size))); mShape.setTextureRect(sf::IntRect(sf::Vector2i(), sf::Vector2i(size)));
@ -102,6 +101,7 @@ bool
Sprite::isInside(const sf::FloatRect& rect) const { Sprite::isInside(const sf::FloatRect& rect) const {
return rect.intersects(mShape.getGlobalBounds()); return rect.intersects(mShape.getGlobalBounds());
} }
/** /**
* Called when a collision with another Sprite occured. Override this method * Called when a collision with another Sprite occured. Override this method
* to manage collision events. * to manage collision events.

View file

@ -72,7 +72,7 @@ private:
sf::Vector2f mSpeed; sf::Vector2f mSpeed;
Category mCategory; Category mCategory;
unsigned short mMask; unsigned short mMask;
bool mDelete; bool mDelete = false;
}; };
#endif /* DG_SPRITE_H_ */ #endif /* DG_SPRITE_H_ */

View file

@ -10,6 +10,7 @@
#include "../abstract/Character.h" #include "../abstract/Character.h"
Gadget::Gadget(std::string name) : Gadget::Gadget(std::string name) :
Item(sf::Vector2f(40, 40), "gadget.png"),
mName(name) { mName(name) {
} }

View file

@ -10,9 +10,11 @@
#include <Thor/Time.hpp> #include <Thor/Time.hpp>
#include "Item.h"
class Character; class Character;
class Gadget { class Gadget : public Item {
public: public:
Gadget(std::string name); Gadget(std::string name);
void use(Character& character); void use(Character& character);

26
source/items/Item.cpp Normal file
View file

@ -0,0 +1,26 @@
/*
* Item.cpp
*
* Created on: 13.07.2013
* Author: Felix
*/
#include "Item.h"
#include "../World.h"
Item::Item(const sf::Vector2f& size, const std::string& texture) :
Sprite(sf::Vector2f(), CATEGORY_NONSOLID, MASK_NONE, size, texture,
sf::Vector2f()) {
}
void
Item::drop(const sf::Vector2f& position) {
setPosition(position);
}
bool
Item::testCollision(std::shared_ptr<Sprite> other,
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) {
return false;
}

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

@ -0,0 +1,25 @@
/*
* Item.h
*
* Created on: 13.07.2013
* Author: Felix
*/
#ifndef DG_ITEM_H_
#define DG_ITEM_H_
#include "../abstract/Sprite.h"
class World;
class Item : public Sprite {
public:
Item(const sf::Vector2f& size, const std::string& texture);
virtual ~Item() {};
void drop(const sf::Vector2f& position);
bool testCollision(std::shared_ptr<Sprite> other,
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond);
};
#endif /* DG_ITEM_H_ */

View file

@ -14,6 +14,7 @@
#include "../util/Yaml.h" #include "../util/Yaml.h"
Weapon::Weapon(World& world, Character& holder, const Yaml& config) : Weapon::Weapon(World& world, Character& holder, const Yaml& config) :
Item(sf::Vector2f(80, 30), "weapon.png"),
mWorld(world), mWorld(world),
mHolder(holder), mHolder(holder),
mName(config.get("name", std::string())), mName(config.get("name", std::string())),

View file

@ -15,6 +15,7 @@
#include <Thor/Time.hpp> #include <Thor/Time.hpp>
#include "Item.h"
#include "../util/Yaml.h" #include "../util/Yaml.h"
class Character; class Character;
@ -22,7 +23,7 @@ class World;
class Particle; class Particle;
class Yaml; class Yaml;
class Weapon { class Weapon : public Item {
public: public:
explicit Weapon(World& world, Character& holder, const Yaml& config); explicit Weapon(World& world, Character& holder, const Yaml& config);

View file

@ -31,3 +31,4 @@ int main(int argc, char* argv[]) {
return 0; return 0;
} }