Separate Sprite animation logic into animation objects

This commit is contained in:
NaiJi ✨ 2021-10-04 17:20:24 +03:00
parent 841e042477
commit 24aadf8174
9 changed files with 71 additions and 118 deletions

View File

@ -34,7 +34,8 @@ void ClassicArrowNote::putToGame(const microsec &music_offset)
for (auto& element : _elements)
{
element.sprite = _context->graphics_manager->getSprite(element.type);
element.sprite->setCoordinates(element.coordinates, 0., 9.);
element.sprite->setCoordinates(element.coordinates);
element.sprite->setTrailCoordinates({0., 9.});
element.animations[_state]->launch(element.sprite, music_offset, offset());
}
}

View File

@ -7,16 +7,31 @@ void ClassicDyingAnimationScenario::launch(const std::shared_ptr<ClassicSprite>
_time_begin = time_begin;
_time_end = time_end;
_sprite->pulse();
_sprite->setColor(sf::Color(140, 140, 140));
_sprite->setTrailColor(sf::Color(0, 0, 0, 0));
_sprite->setTrailCoordinates({0, 0});
}
void ClassicDyingAnimationScenario::update(const microsec& music_offset)
{
(void) music_offset;
_sprite->update();
auto fill_color = _sprite->color();
if (fill_color.a == 0)
{
fill_color.a = 0;
_sprite->setColor(fill_color);
return;
}
auto new_alpha = fill_color.a - 15;
fill_color.a = new_alpha < 0 ? 0 : new_alpha;
_sprite->setColor(fill_color);
}
bool ClassicDyingAnimationScenario::isDone() const
{
return _sprite->isDead();
return _sprite->color().a == 0;
}

View File

@ -1,5 +1,4 @@
#ifndef CLASSICDYINGANIMATIONSCENARIO_H
#define CLASSICDYINGANIMATIONSCENARIO_H
#pragma once
#include "classicanimationscenario.h"
@ -10,5 +9,3 @@ public:
virtual void update(const microsec& music_offset) override;
virtual bool isDone() const override;
};
#endif // CLASSICDYINGANIMATIONSCENARIO_H

View File

@ -10,7 +10,7 @@ void ClassicFlyingAnimationScenario::launch(const std::shared_ptr<ClassicSprite>
_percentage = ((_time_end - _time_begin) * 0.01);
}
int ClassicFlyingAnimationScenario::getPoint(float n1, float n2, float perc) const
float ClassicFlyingAnimationScenario::getPoint(float n1, float n2, float perc) const
{
float diff = n2 - n1;
@ -19,24 +19,37 @@ int ClassicFlyingAnimationScenario::getPoint(float n1, float n2, float perc) con
void ClassicFlyingAnimationScenario::update(const microsec& music_offset)
{
float i;
const auto crd = _sprite->coordinates();
auto update_time = music_offset - _time_begin;
i = update_time / _percentage * 0.01;
float i = update_time / _percentage * 0.01;
float xa = getPoint( crd.x + 20. , crd.x + 90. , i );
float ya = getPoint( crd.y - 600. , crd.y - 150. , i );
float xb = getPoint( crd.x + 90. , crd.x , i );
float yb = getPoint( crd.y - 150. , crd.y , i );
_sprite->update(getPoint( xa , xb , i ), getPoint( ya , yb , i ));
if (i >= 1)
{
_sprite->trailFade();
}
_sprite->setTrailCoordinates({getPoint( xa , xb , i ), getPoint( ya , yb , i )});
bool pastPerfectScore = (i >= 1);
if (pastPerfectScore)
fadeTrailSprite();
}
bool ClassicFlyingAnimationScenario::isDone() const
{
return false;
}
void ClassicFlyingAnimationScenario::fadeTrailSprite() const
{
auto fill_color = _sprite->trailColor();
if (fill_color.a == 0)
return;
auto new_alpha = fill_color.a - 35;
fill_color.a = new_alpha < 0 ? 0 : new_alpha;
_sprite->setTrailColor(fill_color);
}

View File

@ -1,5 +1,4 @@
#ifndef CLASSICFLYINGANIMATIONSCENARIO_H
#define CLASSICFLYINGANIMATIONSCENARIO_H
#pragma once
#include "classicanimationscenario.h"
@ -11,9 +10,8 @@ public:
virtual bool isDone() const override;
private:
int getPoint(float n1, float n2, float perc) const;
float getPoint(float n1, float n2, float perc) const;
void fadeTrailSprite() const;
float _percentage;
};
#endif // CLASSICFLYINGANIMATIONSCENARIO_H

View File

@ -4,7 +4,7 @@
ClassicGraphicsManager::ClassicGraphicsManager(sf::RenderTarget& target) :
_sprite_container({Type::UP, Type::DOWN,
Type::LEFT, Type::RIGHT},
std::make_unique<ClassicSpriteFactory>("VeraMono.ttf")),
std::make_unique<ClassicSpriteFactory>()),
_render_target(target)
{}

View File

@ -1,39 +1,35 @@
#include "classicsprite.h"
#include <SFML/Graphics/RenderTarget.hpp>
ClassicSprite::ClassicSprite(const sf::RectangleShape& shape, const sf::Font& font) :
ClassicSprite::ClassicSprite(const sf::RectangleShape& shape) :
_prototype(shape),
_shape(shape),
_trail(shape),
_font(font)
{
_grade_text.setFont(_font);
}
_trail(shape)
{}
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
void ClassicSprite::setCoordinates(const Coordinates& coordinates)
{
_shape.setPosition(coordinates.x, coordinates.y);
_trail.setPosition(trail_x, trail_y);
_grade_text.setPosition(coordinates.x + _shape.getSize().x/2, coordinates.y + 10);
}
void ClassicSprite::setTrailCoordinates(const Coordinates &coordinates)
{
_trail.setPosition(coordinates.x, coordinates.y);
}
Coordinates ClassicSprite::coordinates() const
@ -46,75 +42,22 @@ Coordinates ClassicSprite::trailCoordinates() const
return {_trail.getPosition().x, _trail.getPosition().y};
}
void ClassicSprite::update(float trail_x, float trail_y) noexcept
void ClassicSprite::setColor(const sf::Color& color)
{
_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);
}
_shape.setFillColor(color);
}
void ClassicSprite::update() noexcept
void ClassicSprite::setTrailColor(const sf::Color& color)
{
fade();
_trail.setFillColor(color);
}
void ClassicSprite::pulse()
sf::Color ClassicSprite::color() const
{
_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));
return _shape.getFillColor();
}
void ClassicSprite::fade()
sf::Color ClassicSprite::trailColor() const
{
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;
return _trail.getFillColor();
}

View File

@ -3,33 +3,27 @@
#include "tools/mathutils.h"
#include "sprite.h"
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Text.hpp>
class ClassicSprite : public Sprite, public sf::Drawable
{
public:
ClassicSprite(const sf::RectangleShape& shape, const sf::Font &font);
explicit ClassicSprite(const sf::RectangleShape& shape);
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
virtual void reset() override;
void setCoordinates(const Coordinates &coordinates, float trail_x, float trail_y) noexcept;
void setCoordinates(const Coordinates &coordinates);
void setTrailCoordinates(const Coordinates &coordinates);
Coordinates coordinates() const;
Coordinates trailCoordinates() const;
void update(float trail_x, float trail_y) noexcept;
void update() noexcept;
void pulse();
void fade();
void trailFade();
bool isDead() const;
void setColor(const sf::Color& color);
void setTrailColor(const sf::Color& color);
sf::Color color() const;
sf::Color trailColor() const;
private:
sf::RectangleShape _prototype;
sf::RectangleShape _shape;
sf::RectangleShape _trail;
sf::Text _grade_text;
sf::Font _font;
bool _trail_fade = false;
};

View File

@ -7,11 +7,6 @@
class ClassicSpriteFactory
{
public:
inline ClassicSpriteFactory(const std::string& font_filename)
{
_font.loadFromFile(font_filename);
}
inline std::shared_ptr<ClassicSprite> create(Type type)
{
sf::RectangleShape sprite;
@ -38,10 +33,7 @@ public:
sprite.setFillColor(sf::Color(255, 239, 0));
}
return std::make_shared<ClassicSprite>(sprite, _font);
return std::make_shared<ClassicSprite>(sprite);
}
private:
sf::Font _font;
};