skizzle/src/objects/block.cpp

95 lines
2.2 KiB
C++
Raw Normal View History

#include "manager.hpp"
2016-04-20 15:31:13 +00:00
#include "resource_manager.hpp"
#include "states/game.hpp"
#include "objects/player.hpp"
#include "objects/block.hpp"
2016-03-08 16:46:33 +00:00
2016-04-02 13:18:29 +00:00
const unsigned int Block::TYPE_ID = 2;
2016-04-11 10:34:43 +00:00
Block::Block() : Object() {
sprite.setOrigin(sf::Vector2f(23, 23));
2016-04-11 10:34:43 +00:00
aabb = sf::FloatRect(
-Manager::GRID / 2,
-Manager::GRID / 2,
Manager::GRID,
Manager::GRID
2016-04-11 10:34:43 +00:00
);
}
Block::~Block() {}
2016-03-28 18:02:23 +00:00
2016-04-10 05:37:09 +00:00
Object::Ptr Block::clone() const {
return Object::Ptr(new Block(*this));
}
2016-04-10 05:37:09 +00:00
void Block::init(std::ifstream& file, Object::Ptr object) {
2016-04-02 13:18:29 +00:00
// lecture des propriétés communes des objets
2016-04-10 05:37:09 +00:00
Object::init(file, object);
}
2016-04-02 13:18:29 +00:00
2016-04-10 05:37:09 +00:00
Object::Ptr Block::load(std::ifstream& file) {
Object::Ptr object = Object::Ptr(new Block);
Block::init(file, object);
2016-04-02 13:18:29 +00:00
return object;
}
2016-04-10 04:53:45 +00:00
void Block::save(std::ofstream& file) const {
// écriture des propriétés communes
Object::save(file);
}
void Block::draw(Level& level) {
// récupération de la texture correspondant au type de bloc
std::string texture_name = "block.tga";
if (getMass() > 0) {
texture_name = "movable_" + texture_name;
2016-04-10 20:15:42 +00:00
}
if (isSelected()) {
texture_name = "selected_" + texture_name;
}
2016-04-07 09:22:20 +00:00
sprite.setTexture(*ResourceManager::get().getTexture(
"objects/" + texture_name
));
2016-04-10 20:15:42 +00:00
// coloration du bloc selon sa charge
if (getCharge() > 0) {
sprite.setColor(sf::Color(180, 180, 255));
} else if (getCharge() < 0) {
sprite.setColor(sf::Color(255, 180, 180));
} else {
sprite.setColor(sf::Color::White);
}
2016-04-07 09:22:20 +00:00
sprite.setPosition(getPosition());
level.getManager().getWindow().draw(sprite);
}
void Block::activate(Game& game, Object::Ptr object) {
2016-04-09 01:53:17 +00:00
// ne rien faire si le bloc est activé.
// Ceci est un bloc de base qui n'a pas a réagir
// aux activations
}
sf::FloatRect Block::getAABB() const {
2016-04-11 10:34:43 +00:00
sf::FloatRect transl_aabb = aabb;
transl_aabb.left += getPosition().x;
transl_aabb.top += getPosition().y;
return transl_aabb;
}
2016-04-09 13:32:42 +00:00
float Block::getRadius() const {
return Manager::GRID / 2;
2016-04-09 13:32:42 +00:00
}
unsigned int Block::getTypeId() const {
return TYPE_ID;
}
2016-04-09 13:32:42 +00:00
CollisionType Block::getCollisionType() const {
return CollisionType::AABB;
}