diff --git a/include/block.hpp b/include/block.hpp index 4081da6..5ceaec2 100644 --- a/include/block.hpp +++ b/include/block.hpp @@ -6,9 +6,11 @@ class Block : public Object { private: - mutable sf::Sprite sprite; mutable sf::Sprite select_sprite; +protected: + mutable sf::Sprite sprite; + public: /** * Identifiant unique du type "bloc" @@ -28,6 +30,11 @@ public: */ virtual std::unique_ptr getAABB() const; + /** + * Opérations de préparation de la texture du bloc + */ + virtual void beforeDraw(Level& level); + /** * Dessin du bloc dans la fenêtre donnée */ diff --git a/include/gravity_block.hpp b/include/gravity_block.hpp index 199f341..fdbed7a 100644 --- a/include/gravity_block.hpp +++ b/include/gravity_block.hpp @@ -22,6 +22,11 @@ public: */ virtual ObjectPtr clone() const; + /** + * Opérations de préparation de la texture du bloc + */ + virtual void beforeDraw(Level& level); + /** * Appelé lorsque le bloc de gravité est activé par un objet */ diff --git a/src/block.cpp b/src/block.cpp index acd7c2a..6d176d7 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -32,15 +32,12 @@ ObjectPtr Block::load(std::ifstream& file) { return object; } -void Block::draw(Level& level) { - // utilisation de la texture - ResourceManager& resources = level.getResourceManager(); - sf::RenderWindow& window = level.getWindow(); +void Block::beforeDraw(Level& level) { + // texturage et coloration du bloc selon ses propriétés + sprite.setTexture( + level.getResourceManager().getTexture("block.tga") + ); - sprite.setTexture(resources.getTexture("block.tga")); - select_sprite.setTexture(resources.getTexture("block_select.png")); - - // coloration du bloc en fonction de sa charge if (getCharge() > 0) { sprite.setColor(sf::Color(180, 180, 255)); } else if (getCharge() < 0) { @@ -48,13 +45,22 @@ void Block::draw(Level& level) { } else { sprite.setColor(sf::Color::White); } +} + +void Block::draw(Level& level) { + // utilisation de la texture + sf::RenderWindow& window = level.getWindow(); + beforeDraw(level); sprite.setPosition(getPosition()); - select_sprite.setPosition(getPosition()); - window.draw(sprite); if (isSelected()) { + select_sprite.setPosition(getPosition()); + select_sprite.setTexture( + level.getResourceManager().getTexture("block_select.png") + ); + window.draw(select_sprite); } } diff --git a/src/gravity_block.cpp b/src/gravity_block.cpp index 3b19adc..e29b020 100644 --- a/src/gravity_block.cpp +++ b/src/gravity_block.cpp @@ -10,6 +10,35 @@ ObjectPtr GravityBlock::clone() const { return ObjectPtr(new GravityBlock(*this)); } +void GravityBlock::beforeDraw(Level& level) { + Block::beforeDraw(level); + + // texturage et coloration du bloc selon ses propriétés + std::string texture_name = "gravity_block_"; + + switch (gravity_direction) { + case GravityDirection::NORTH: + texture_name += "north"; + break; + + case GravityDirection::EAST: + texture_name += "east"; + break; + + case GravityDirection::SOUTH: + texture_name += "south"; + break; + + case GravityDirection::WEST: + texture_name += "west"; + break; + } + + sprite.setTexture( + level.getResourceManager().getTexture(texture_name + ".tga") + ); +} + void GravityBlock::activated(Level& level, Object& object) { Block::activated(level, object);