Drop primary weapon on death.
This commit is contained in:
parent
cb11a22bff
commit
9401ea53c1
13 changed files with 71 additions and 11 deletions
BIN
resources/textures/gadget.png
Normal file
BIN
resources/textures/gadget.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 445 B |
BIN
resources/textures/weapon.png
Normal file
BIN
resources/textures/weapon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1 KiB |
|
@ -24,7 +24,7 @@ World::insert(std::shared_ptr<Sprite> drawable) {
|
|||
auto item = std::find(mDrawables[cat].begin(), mDrawables[cat].end(), drawable);
|
||||
assert(item == mDrawables[cat].end());
|
||||
#endif
|
||||
mDrawables[drawable->getCategory()].push_back(drawable);
|
||||
mDrawables[drawable->getCategory()].push_back(drawable);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -37,8 +37,8 @@ World::insertCharacter(std::shared_ptr<Character> character) {
|
|||
auto item = std::find(mCharacters.begin(), mCharacters.end(), character);
|
||||
assert(item == mCharacters.end());
|
||||
#endif
|
||||
mCharacters.push_back(character);
|
||||
insert(character);
|
||||
mCharacters.push_back(character);
|
||||
insert(character);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -88,12 +88,15 @@ Character::getFaction() const {
|
|||
}
|
||||
|
||||
/**
|
||||
* Called when health reaches zero. Default implementation drops a corpse at
|
||||
* the current position.
|
||||
* Called when health reaches zero. Drops corpse and item.
|
||||
*/
|
||||
void
|
||||
Character::onDeath() {
|
||||
mWorld.insert(std::shared_ptr<Sprite>(new Corpse(getPosition())));
|
||||
|
||||
mWorld.insert(mActiveWeapon);
|
||||
mActiveWeapon->drop(getPosition());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,8 +17,7 @@ Sprite::Sprite(const sf::Vector2f& position, Category category,
|
|||
unsigned short mask, const sf::Vector2f& size,
|
||||
const std::string& texture, const sf::Vector2f& direction) :
|
||||
mCategory(category),
|
||||
mMask(mask),
|
||||
mDelete(false) {
|
||||
mMask(mask) {
|
||||
mShape.setSize(size);
|
||||
mShape.setOrigin(size / 2.0f);
|
||||
mShape.setTextureRect(sf::IntRect(sf::Vector2i(), sf::Vector2i(size)));
|
||||
|
@ -102,6 +101,7 @@ bool
|
|||
Sprite::isInside(const sf::FloatRect& rect) const {
|
||||
return rect.intersects(mShape.getGlobalBounds());
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a collision with another Sprite occured. Override this method
|
||||
* to manage collision events.
|
||||
|
|
|
@ -72,7 +72,7 @@ private:
|
|||
sf::Vector2f mSpeed;
|
||||
Category mCategory;
|
||||
unsigned short mMask;
|
||||
bool mDelete;
|
||||
bool mDelete = false;
|
||||
};
|
||||
|
||||
#endif /* DG_SPRITE_H_ */
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "../abstract/Character.h"
|
||||
|
||||
Gadget::Gadget(std::string name) :
|
||||
Item(sf::Vector2f(40, 40), "gadget.png"),
|
||||
mName(name) {
|
||||
}
|
||||
|
||||
|
|
|
@ -10,9 +10,11 @@
|
|||
|
||||
#include <Thor/Time.hpp>
|
||||
|
||||
#include "Item.h"
|
||||
|
||||
class Character;
|
||||
|
||||
class Gadget {
|
||||
class Gadget : public Item {
|
||||
public:
|
||||
Gadget(std::string name);
|
||||
void use(Character& character);
|
||||
|
|
26
source/items/Item.cpp
Normal file
26
source/items/Item.cpp
Normal 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
25
source/items/Item.h
Normal 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_ */
|
|
@ -14,6 +14,7 @@
|
|||
#include "../util/Yaml.h"
|
||||
|
||||
Weapon::Weapon(World& world, Character& holder, const Yaml& config) :
|
||||
Item(sf::Vector2f(80, 30), "weapon.png"),
|
||||
mWorld(world),
|
||||
mHolder(holder),
|
||||
mName(config.get("name", std::string())),
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
#include <Thor/Time.hpp>
|
||||
|
||||
#include "Item.h"
|
||||
#include "../util/Yaml.h"
|
||||
|
||||
class Character;
|
||||
|
@ -22,7 +23,7 @@ class World;
|
|||
class Particle;
|
||||
class Yaml;
|
||||
|
||||
class Weapon {
|
||||
class Weapon : public Item {
|
||||
public:
|
||||
explicit Weapon(World& world, Character& holder, const Yaml& config);
|
||||
|
||||
|
|
|
@ -31,3 +31,4 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue