skizzle/src/gravity_block.cpp

108 lines
2.8 KiB
C++
Raw Normal View History

#include "manager.hpp"
#include "utility.hpp"
2016-04-09 03:43:40 +00:00
#include "gravity_block.hpp"
2016-04-11 12:16:20 +00:00
#include "game.hpp"
2016-04-09 03:43:40 +00:00
const unsigned int GravityBlock::TYPE_ID = 3;
GravityBlock::GravityBlock() : Block(), opacity(255), used(false) {
icon_sprite.setOrigin(sf::Vector2f(23, 23));
}
2016-04-09 03:43:40 +00:00
GravityBlock::~GravityBlock() {}
2016-04-10 05:37:09 +00:00
Object::Ptr GravityBlock::clone() const {
return Object::Ptr(new GravityBlock(*this));
2016-04-09 03:43:40 +00:00
}
void GravityBlock::draw(Level& level) {
// sélectionne le sprite d'icône
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;
}
// on dessine le bloc normal
Block::draw(level);
// on anime l'opacité de l'icône
opacity = Utility::animateValue(opacity, 5, used ? 0 : 255);
icon_sprite.setColor(sf::Color(255, 255, 255, opacity));
// on dessine l'icône
icon_sprite.setTexture(level.getResourceManager().getTexture(
texture_name + ".tga"
));
icon_sprite.setPosition(getPosition());
level.getWindow().draw(icon_sprite);
}
void GravityBlock::activate(Game& game, Object::Ptr object) {
// on ne peut utiliser le bloc qu'une seule fois
if (used) {
return;
}
2016-04-11 12:16:20 +00:00
Block::activate(game, object);
2016-04-09 03:43:40 +00:00
// lorsque le bloc est activé, il transmet son
// sens de gravité au niveau
2016-04-11 12:16:20 +00:00
game.setGravityDirection(gravity_direction);
used = true;
2016-04-09 03:43:40 +00:00
}
unsigned int GravityBlock::getTypeId() const {
return TYPE_ID;
}
2016-04-10 05:37:09 +00:00
void GravityBlock::init(std::ifstream& file, Object::Ptr object) {
GravityBlock::Ptr gravity_block = std::dynamic_pointer_cast<GravityBlock>(object);
2016-04-09 03:43:40 +00:00
// lecture de la direction de la gravité
char gravity_direction;
file.read(&gravity_direction, 1);
2016-04-10 05:37:09 +00:00
gravity_block->setGravityDirection((GravityDirection) gravity_direction);
2016-04-09 03:43:40 +00:00
2016-04-10 04:53:45 +00:00
// lecture des propriétés d'un bloc
2016-04-10 05:37:09 +00:00
Block::init(file, object);
}
2016-04-09 03:43:40 +00:00
2016-04-10 05:37:09 +00:00
Object::Ptr GravityBlock::load(std::ifstream& file) {
Object::Ptr object = Object::Ptr(new GravityBlock);
GravityBlock::init(file, object);
2016-04-09 03:43:40 +00:00
return object;
}
2016-04-10 04:53:45 +00:00
void GravityBlock::save(std::ofstream& file) const {
// écriture de la direction de la gravité
char write_gravity_direction = (char) gravity_direction;
file.write(&write_gravity_direction, 1);
// écriture des propriétés d'un bloc
Block::save(file);
}
2016-04-09 03:43:40 +00:00
GravityDirection GravityBlock::getGravityDirection() const {
return gravity_direction;
}
void GravityBlock::setGravityDirection(GravityDirection set_gravity_direction) {
gravity_direction = set_gravity_direction;
}