121 lines
2.7 KiB
C++
121 lines
2.7 KiB
C++
#include "classicsprite.h"
|
|
#include <SFML/Graphics/RenderTarget.hpp>
|
|
|
|
ClassicSprite::ClassicSprite(const sf::RectangleShape& shape, const sf::Font& font) :
|
|
_prototype(shape),
|
|
_shape(shape),
|
|
_trail(shape),
|
|
_font(font)
|
|
{
|
|
_grade_text.setFont(_font);
|
|
}
|
|
|
|
void ClassicSprite::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
|
{
|
|
target.draw(_shape, states);
|
|
target.draw(_trail, states);
|
|
target.draw(_grade_text, states);
|
|
}
|
|
|
|
void ClassicSprite::reset()
|
|
{
|
|
_shape.setPosition(0, 0);
|
|
_trail.setPosition(0, 0);
|
|
_grade_text.setPosition(0, 0);
|
|
_grade_text.setFillColor(sf::Color(255, 255, 255, 0));
|
|
|
|
_shape = _prototype;
|
|
_trail = _prototype;
|
|
_trail_fade = false;
|
|
}
|
|
|
|
void ClassicSprite::setCoordinates(const Coordinates& coordinates, float trail_x, float trail_y) noexcept
|
|
{
|
|
_shape.setPosition(coordinates.x, coordinates.y);
|
|
_trail.setPosition(trail_x, trail_y);
|
|
_grade_text.setPosition(coordinates.x + _shape.getSize().x/2, coordinates.y + 10);
|
|
}
|
|
|
|
Coordinates ClassicSprite::coordinates() const
|
|
{
|
|
return {_shape.getPosition().x, _shape.getPosition().y};
|
|
}
|
|
|
|
Coordinates ClassicSprite::trailCoordinates() const
|
|
{
|
|
return {_trail.getPosition().x, _trail.getPosition().y};
|
|
}
|
|
|
|
void ClassicSprite::update(float trail_x, float trail_y) noexcept
|
|
{
|
|
_trail.setPosition(trail_x, trail_y);
|
|
|
|
if (_trail_fade)
|
|
{
|
|
auto fill_color = _trail.getFillColor();
|
|
|
|
if (fill_color.a == 0)
|
|
return;
|
|
|
|
auto new_alpha = fill_color.a - 35;
|
|
fill_color.a = new_alpha < 0 ? 0 : new_alpha;
|
|
|
|
_trail.setFillColor(fill_color);
|
|
}
|
|
}
|
|
|
|
void ClassicSprite::update() noexcept
|
|
{
|
|
fade();
|
|
}
|
|
|
|
void ClassicSprite::pulse()
|
|
{
|
|
_grade_text.setFillColor(sf::Color(255, 255, 255, 255));
|
|
_shape.setFillColor(sf::Color(140, 140, 140));
|
|
_trail.setPosition(0, 0);
|
|
_trail.setFillColor(sf::Color(0, 0, 0, 0));
|
|
}
|
|
|
|
void ClassicSprite::fade()
|
|
{
|
|
auto fill_color = _grade_text.getFillColor();
|
|
|
|
if (fill_color.a <= 15)
|
|
{
|
|
fill_color.a = 0;
|
|
_grade_text.setFillColor(fill_color);
|
|
return;
|
|
}
|
|
|
|
auto new_alpha = fill_color.a - 15;
|
|
fill_color.a = new_alpha < 0 ? 0 : new_alpha;
|
|
|
|
_grade_text.setFillColor(fill_color);
|
|
|
|
fill_color = _shape.getFillColor();
|
|
|
|
if (fill_color.a == 0)
|
|
{
|
|
fill_color.a = 0;
|
|
_shape.setFillColor(fill_color);
|
|
return;
|
|
}
|
|
|
|
new_alpha = fill_color.a - 15;
|
|
fill_color.a = new_alpha < 0 ? 0 : new_alpha;
|
|
|
|
_shape.setFillColor(fill_color);
|
|
}
|
|
|
|
void ClassicSprite::trailFade()
|
|
{
|
|
_trail_fade = true;
|
|
}
|
|
|
|
bool ClassicSprite::isDead() const
|
|
{
|
|
return _grade_text.getFillColor().a == 0
|
|
|| _shape.getFillColor().a == 0;
|
|
}
|