diff --git a/include/block.hpp b/include/block.hpp index 4a5f644..ed71f85 100644 --- a/include/block.hpp +++ b/include/block.hpp @@ -46,9 +46,9 @@ public: virtual float getRadius() const; /** - * Calcule la texture à afficher pour ce bloc + * Prépare les textures avant le dessin du bloc */ - virtual std::string getTexture(); + virtual void prepareDraw(ResourceManager& resources); /** * Dessin du bloc dans la fenêtre donnée diff --git a/include/finish_block.hpp b/include/finish_block.hpp new file mode 100644 index 0000000..8365813 --- /dev/null +++ b/include/finish_block.hpp @@ -0,0 +1,60 @@ +#ifndef __PTF_FINISH_BLOCK_HPP__ +#define __PTF_FINISH_BLOCK_HPP__ + +#include +#include +#include "block.hpp" +#include "level.hpp" + +class FinishBlock : public Block { +public: + typedef std::shared_ptr Ptr; + +protected: + /** + * Initialisation des propriétés du bloc d'arrivée donné + * depuis le fichier donné + */ + static void init(std::ifstream& file, Object::Ptr object); + +public: + /** + * Identifiant unique du type "bloc d'arrivée" + */ + static const unsigned int TYPE_ID; + + FinishBlock(); + virtual ~FinishBlock(); + + /** + * Clone ce bloc d'arrivée en un bloc d'arrivée avec les mêmes propriétés + */ + virtual Object::Ptr clone() const; + + /** + * Prépare les textures avant le dessin du bloc + */ + virtual void prepareDraw(ResourceManager& resources); + + /** + * Appelé lorsque le bloc d'arrivée est activé par un objet + */ + virtual void activate(Level& level, Object* object); + + /** + * Récupère l'identifiant de type des blocs d'arrivée + */ + virtual unsigned int getTypeId() const; + + /** + * Chargement d'un bloc d'arrivée depuis le fichier donné + */ + static Object::Ptr load(std::ifstream& file); + + /** + * Sauvegarde le bloc d'arrivée dans le fichier donné + */ + virtual void save(std::ofstream& file) const; +}; + +#endif diff --git a/include/gravity_block.hpp b/include/gravity_block.hpp index d765e54..f27d131 100644 --- a/include/gravity_block.hpp +++ b/include/gravity_block.hpp @@ -35,9 +35,9 @@ public: virtual Object::Ptr clone() const; /** - * Calcule la texture à afficher pour ce bloc + * Prépare les textures avant le dessin du bloc */ - virtual std::string getTexture(); + virtual void prepareDraw(ResourceManager& resources); /** * Appelé lorsque le bloc de gravité est activé par un objet diff --git a/include/kill_block.hpp b/include/kill_block.hpp new file mode 100644 index 0000000..eb5f6c9 --- /dev/null +++ b/include/kill_block.hpp @@ -0,0 +1,60 @@ +#ifndef __PTF_KILL_BLOCK_HPP__ +#define __PTF_KILL_BLOCK_HPP__ + +#include +#include +#include "block.hpp" +#include "level.hpp" + +class KillBlock : public Block { +public: + typedef std::shared_ptr Ptr; + +protected: + /** + * Initialisation des propriétés du bloc tueur donné + * depuis le fichier donné + */ + static void init(std::ifstream& file, Object::Ptr object); + +public: + /** + * Identifiant unique du type "bloc tueur" + */ + static const unsigned int TYPE_ID; + + KillBlock(); + virtual ~KillBlock(); + + /** + * Clone ce bloc tueur en un bloc tueur avec les mêmes propriétés + */ + virtual Object::Ptr clone() const; + + /** + * Prépare les textures avant le dessin du bloc + */ + virtual void prepareDraw(ResourceManager& resources); + + /** + * Appelé lorsque le bloc tueur est activé par un objet + */ + virtual void activate(Level& level, Object* object); + + /** + * Récupère l'identifiant de type des blocs tueurs + */ + virtual unsigned int getTypeId() const; + + /** + * Chargement d'un bloc tueur depuis le fichier donné + */ + static Object::Ptr load(std::ifstream& file); + + /** + * Sauvegarde le bloc tueur dans le fichier donné + */ + virtual void save(std::ofstream& file) const; +}; + +#endif diff --git a/include/widget_toolbar.hpp b/include/widget_toolbar.hpp index 3c3481b..768dc5d 100644 --- a/include/widget_toolbar.hpp +++ b/include/widget_toolbar.hpp @@ -67,6 +67,16 @@ private: */ Object::Ptr createPlayer(); + /** + * Crée un objet de type FinishBlock + */ + Object::Ptr createFinishBlock(); + + /** + * Crée un objet de type KillBlock + */ + Object::Ptr createKillBlock(); + /** * Crée un objet de type GravityBlock */ diff --git a/res/textures/finish_block.tga b/res/textures/finish_block.tga new file mode 100644 index 0000000..18a4c65 Binary files /dev/null and b/res/textures/finish_block.tga differ diff --git a/res/textures/kill_block.tga b/res/textures/kill_block.tga new file mode 100644 index 0000000..010385d Binary files /dev/null and b/res/textures/kill_block.tga differ diff --git a/res/textures/toolbar_finish_block.tga b/res/textures/toolbar_finish_block.tga new file mode 100644 index 0000000..5b5882b Binary files /dev/null and b/res/textures/toolbar_finish_block.tga differ diff --git a/res/textures/toolbar_kill_block.tga b/res/textures/toolbar_kill_block.tga new file mode 100644 index 0000000..318f6bb Binary files /dev/null and b/res/textures/toolbar_kill_block.tga differ diff --git a/src/block.cpp b/src/block.cpp index 98d6af8..3159af9 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -6,12 +6,7 @@ const unsigned int Block::TYPE_ID = 2; -Block::Block() : Object() { - // déplacement de l'origine au centre du bloc - sprite.setOrigin(sf::Vector2f(23, 23)); - select_sprite.setOrigin(sf::Vector2f(23, 23)); -} - +Block::Block() : Object() {} Block::~Block() {} Object::Ptr Block::clone() const { @@ -34,18 +29,24 @@ void Block::save(std::ofstream& file) const { Object::save(file); } -std::string Block::getTexture() { +void Block::prepareDraw(ResourceManager& resources) { + std::string texture_name = "movable_block.tga"; + if (getMass() == 0) { - return "block.tga"; + texture_name = "block.tga"; } - return "movable_block.tga"; + sprite.setTexture(resources.getTexture(texture_name)); + sprite.setOrigin(sf::Vector2f(23, 23)); + + select_sprite.setTexture(resources.getTexture("block_select.tga")); + select_sprite.setOrigin(sf::Vector2f(23, 23)); } void Block::draw(Level& level) { // utilisation de la texture sf::RenderWindow& window = level.getWindow(); - sprite.setTexture(level.getResourceManager().getTexture(getTexture())); + prepareDraw(level.getResourceManager()); // coloration du bloc selon sa charge if (getCharge() > 0) { @@ -61,10 +62,6 @@ void Block::draw(Level& level) { if (isSelected()) { select_sprite.setPosition(getPosition()); - select_sprite.setTexture( - level.getResourceManager().getTexture("block_select.tga") - ); - window.draw(select_sprite); } } diff --git a/src/finish_block.cpp b/src/finish_block.cpp new file mode 100644 index 0000000..c50e0f6 --- /dev/null +++ b/src/finish_block.cpp @@ -0,0 +1,42 @@ +#include "finish_block.hpp" +#include "level.hpp" + +const unsigned int FinishBlock::TYPE_ID = 4; + +FinishBlock::FinishBlock() : Block() {} +FinishBlock::~FinishBlock() {} + +Object::Ptr FinishBlock::clone() const { + return Object::Ptr(new FinishBlock(*this)); +} + +void FinishBlock::prepareDraw(ResourceManager& resources) { + Block::prepareDraw(resources); + sprite.setOrigin(sf::Vector2f(23, 41)); + sprite.setTexture(resources.getTexture("finish_block.tga"), true); +} + +void FinishBlock::activate(Level& level, Object* object) { + Block::activate(level, object); + // TODO: implémenter le comportmeent des blocs d'arrivée +} + +unsigned int FinishBlock::getTypeId() const { + return TYPE_ID; +} + +void FinishBlock::init(std::ifstream& file, Object::Ptr object) { + // lecture des propriétés d'un bloc + Block::init(file, object); +} + +Object::Ptr FinishBlock::load(std::ifstream& file) { + Object::Ptr object = Object::Ptr(new FinishBlock); + FinishBlock::init(file, object); + return object; +} + +void FinishBlock::save(std::ofstream& file) const { + // écriture des propriétés d'un bloc + Block::save(file); +} diff --git a/src/gravity_block.cpp b/src/gravity_block.cpp index c279914..8d717c3 100644 --- a/src/gravity_block.cpp +++ b/src/gravity_block.cpp @@ -10,8 +10,7 @@ Object::Ptr GravityBlock::clone() const { return Object::Ptr(new GravityBlock(*this)); } -std::string GravityBlock::getTexture() { - // texturage et coloration du bloc selon ses propriétés +void GravityBlock::prepareDraw(ResourceManager& resources) { std::string texture_name = "gravity_block_"; switch (gravity_direction) { @@ -32,7 +31,7 @@ std::string GravityBlock::getTexture() { break; } - return texture_name + ".tga"; + sprite.setTexture(resources.getTexture(texture_name + ".tga")); } void GravityBlock::activate(Level& level, Object* object) { diff --git a/src/kill_block.cpp b/src/kill_block.cpp new file mode 100644 index 0000000..56a527f --- /dev/null +++ b/src/kill_block.cpp @@ -0,0 +1,41 @@ +#include "kill_block.hpp" +#include "level.hpp" + +const unsigned int KillBlock::TYPE_ID = 5; + +KillBlock::KillBlock() : Block() {} +KillBlock::~KillBlock() {} + +Object::Ptr KillBlock::clone() const { + return Object::Ptr(new KillBlock(*this)); +} + +void KillBlock::prepareDraw(ResourceManager& resources) { + Block::prepareDraw(resources); + sprite.setTexture(resources.getTexture("kill_block.tga")); +} + +void KillBlock::activate(Level& level, Object* object) { + Block::activate(level, object); + // TODO: implémenter le comportmeent des blocs tueurs +} + +unsigned int KillBlock::getTypeId() const { + return TYPE_ID; +} + +void KillBlock::init(std::ifstream& file, Object::Ptr object) { + // lecture des propriétés d'un bloc + Block::init(file, object); +} + +Object::Ptr KillBlock::load(std::ifstream& file) { + Object::Ptr object = Object::Ptr(new KillBlock); + KillBlock::init(file, object); + return object; +} + +void KillBlock::save(std::ofstream& file) const { + // écriture des propriétés d'un bloc + Block::save(file); +} diff --git a/src/widget_toolbar.cpp b/src/widget_toolbar.cpp index 7e69c91..bdbf577 100644 --- a/src/widget_toolbar.cpp +++ b/src/widget_toolbar.cpp @@ -2,6 +2,8 @@ #include "block.hpp" #include "player.hpp" #include "gravity_block.hpp" +#include "finish_block.hpp" +#include "kill_block.hpp" #include const int PADDING = 8; @@ -42,6 +44,14 @@ Object::Ptr WidgetToolbar::createPlayer() { return player; } +Object::Ptr WidgetToolbar::createFinishBlock() { + return Object::Ptr(new FinishBlock); +} + +Object::Ptr WidgetToolbar::createKillBlock() { + return Object::Ptr(new KillBlock); +} + Object::Ptr WidgetToolbar::createGravityBlock(GravityDirection direction) { GravityBlock::Ptr gravity_block = GravityBlock::Ptr(new GravityBlock); gravity_block->setGravityDirection(direction); @@ -59,23 +69,36 @@ Object::Ptr WidgetToolbar::createObject() { WidgetToolbar::WidgetToolbar(Manager& manager) : manager(manager), selected(nullptr) { // catégorie des objets plaçables de base ResourceManager& resources = manager.getResourceManager(); - ToolbarCategory::Ptr basic_cat = addCategory("BASE"); + ToolbarCategory::Ptr block_cat = addCategory("BLOCS"); - selected = basic_cat->addObject( + selected = block_cat->addObject( resources.getTexture("toolbar_block.tga"), std::bind(&WidgetToolbar::createBlock, this) ); - basic_cat->addObject( + block_cat->addObject( resources.getTexture("toolbar_movable_block.tga"), std::bind(&WidgetToolbar::createMovableBlock, this) ); - basic_cat->addObject( + // catégorie des blocs contrôlant les joueurs + ToolbarCategory::Ptr player_cat = addCategory("JOUEURS"); + + player_cat->addObject( resources.getTexture("toolbar_player.tga"), std::bind(&WidgetToolbar::createPlayer, this) ); + player_cat->addObject( + resources.getTexture("toolbar_kill_block.tga"), + std::bind(&WidgetToolbar::createKillBlock, this) + ); + + player_cat->addObject( + resources.getTexture("toolbar_finish_block.tga"), + std::bind(&WidgetToolbar::createFinishBlock, this) + ); + // catégorie des blocs changeant la gravité ToolbarCategory::Ptr gravity_cat = addCategory(L"GRAVITÉ");