Bonjour à tous ! je code actuellement un petit jeu type rpg. Je l'ai fini mais j'ai voulu ajouter une barre de vie qui apparaît sous les monstres. Sauf que ma barre n'est pas bien centré et je ne vois pas pourquoi ^^" voici un code minimaliste pour faire fonctionner la barre :
#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
#include <windows.h>
using std::string;
using std::cout;
using std::endl;
using std::vector;
sf::Vector2f operator-( sf::Vector2f v_f, float const &i ){ v_f.x -= i;v_f.y -= i;return v_f;}
sf::Vector2f operator/( sf::Vector2f v_f, float const &i ){ v_f.x /= i;v_f.y /= i;return v_f;}
sf::Vector2f operator+( sf::Vector2f v_f, float const &i ){ v_f.x += i;v_f.y += i;return v_f;}
class LifeBar : public sf::Drawable, public sf::Transformable
{
public:
LifeBar( sf::Vector2f position, sf::Vector2f size, float life ) : _max_status_size{ size.y - 2.0f }, _max_life{life}, _life{life}
{
_rect.setSize(size);
_rect.setFillColor( sf::Color( 0, 0, 0, 0 ) );
_rect.setOutlineThickness(1);
_rect.setOutlineColor(sf::Color::Black);
_rect.setPosition(position);
_status.setSize( size - 2.0f );
_status.setFillColor( sf::Color( 0, 255, 0, 200 ) );
_status.setPosition( position + 1 );
}
void lifeDamage( float const damage )
{
if( damage < _life )
_life -= damage;
else
_life = 0;
if( _life > _max_life )
_life = _max_life;
_status.setSize( sf::Vector2f( _status.getSize().x, _life * _max_status_size / _max_life ) );
_status.setFillColor( sf::Color( (_life * 255 / _max_life ) * -1, _life * 255 / _max_life, 0, 200 ) );
}
sf::Vector2f getSize() const
{
return _rect.getSize();
}
private:
float const _max_status_size;
sf::RectangleShape _rect;
sf::RectangleShape _status;
float const _max_life;
float _life;
virtual void draw( sf::RenderTarget& target, sf::RenderStates states ) const
{
states.transform *= getTransform();
target.draw( _rect, states );
target.draw( _status, states );
}
};
int main(void)
{
bool inApp{true};
unsigned int countOfClick{0};
sf::RenderWindow window;
sf::ContextSettings conSettings;
conSettings.antialiasingLevel = 4;
window.create( sf::VideoMode( 800, 600 ), "Test", sf::Style::Default, conSettings );
LifeBar lb( sf::Vector2f(50,50), sf::Vector2f(50,500), 100 );
lb.setOrigin( lb.getSize() / 2);
lb.rotate(270);
lb.setPosition(sf::Vector2f( static_cast<float>( window.getSize().x / 2 ), static_cast<float>( window.getSize().y / 2 ) ) );
sf::Clock clk;
while( inApp )
{
while( window.isOpen() )
{
sf::Event event;
while( window.pollEvent( event ) )
{
if( event.type == event.Closed )
window.close();
if( event.type == event.MouseButtonPressed )
{
if( sf::Mouse::isButtonPressed( sf::Mouse::Left ) )
{
system("cls");
cout << "Nombre de cliques : " << countOfClick++;
}
}
}
window.clear(sf::Color::White);
window.draw(lb);
window.display();
while( clk.getElapsedTime().asMicroseconds() < 1000 )
{
}
clk.restart();
lb.lifeDamage(0.01);
}
}
}
Merci de bien vouloir m'aider