Separate Sprite animation logic into animation objects
This commit is contained in:
parent
841e042477
commit
24aadf8174
|
@ -34,7 +34,8 @@ void ClassicArrowNote::putToGame(const microsec &music_offset)
|
||||||
for (auto& element : _elements)
|
for (auto& element : _elements)
|
||||||
{
|
{
|
||||||
element.sprite = _context->graphics_manager->getSprite(element.type);
|
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());
|
element.animations[_state]->launch(element.sprite, music_offset, offset());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,16 +7,31 @@ void ClassicDyingAnimationScenario::launch(const std::shared_ptr<ClassicSprite>
|
||||||
_time_begin = time_begin;
|
_time_begin = time_begin;
|
||||||
_time_end = time_end;
|
_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 ClassicDyingAnimationScenario::update(const microsec& music_offset)
|
||||||
{
|
{
|
||||||
(void) 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
|
bool ClassicDyingAnimationScenario::isDone() const
|
||||||
{
|
{
|
||||||
return _sprite->isDead();
|
return _sprite->color().a == 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#ifndef CLASSICDYINGANIMATIONSCENARIO_H
|
#pragma once
|
||||||
#define CLASSICDYINGANIMATIONSCENARIO_H
|
|
||||||
|
|
||||||
#include "classicanimationscenario.h"
|
#include "classicanimationscenario.h"
|
||||||
|
|
||||||
|
@ -10,5 +9,3 @@ public:
|
||||||
virtual void update(const microsec& music_offset) override;
|
virtual void update(const microsec& music_offset) override;
|
||||||
virtual bool isDone() const override;
|
virtual bool isDone() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CLASSICDYINGANIMATIONSCENARIO_H
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ void ClassicFlyingAnimationScenario::launch(const std::shared_ptr<ClassicSprite>
|
||||||
_percentage = ((_time_end - _time_begin) * 0.01);
|
_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;
|
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)
|
void ClassicFlyingAnimationScenario::update(const microsec& music_offset)
|
||||||
{
|
{
|
||||||
float i;
|
|
||||||
const auto crd = _sprite->coordinates();
|
const auto crd = _sprite->coordinates();
|
||||||
auto update_time = music_offset - _time_begin;
|
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 xa = getPoint( crd.x + 20. , crd.x + 90. , i );
|
||||||
float ya = getPoint( crd.y - 600. , crd.y - 150. , i );
|
float ya = getPoint( crd.y - 600. , crd.y - 150. , i );
|
||||||
float xb = getPoint( crd.x + 90. , crd.x , i );
|
float xb = getPoint( crd.x + 90. , crd.x , i );
|
||||||
float yb = getPoint( crd.y - 150. , crd.y , i );
|
float yb = getPoint( crd.y - 150. , crd.y , i );
|
||||||
|
|
||||||
_sprite->update(getPoint( xa , xb , i ), getPoint( ya , yb , i ));
|
_sprite->setTrailCoordinates({getPoint( xa , xb , i ), getPoint( ya , yb , i )});
|
||||||
if (i >= 1)
|
|
||||||
{
|
bool pastPerfectScore = (i >= 1);
|
||||||
_sprite->trailFade();
|
|
||||||
}
|
if (pastPerfectScore)
|
||||||
|
fadeTrailSprite();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClassicFlyingAnimationScenario::isDone() const
|
bool ClassicFlyingAnimationScenario::isDone() const
|
||||||
{
|
{
|
||||||
return false;
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#ifndef CLASSICFLYINGANIMATIONSCENARIO_H
|
#pragma once
|
||||||
#define CLASSICFLYINGANIMATIONSCENARIO_H
|
|
||||||
|
|
||||||
#include "classicanimationscenario.h"
|
#include "classicanimationscenario.h"
|
||||||
|
|
||||||
|
@ -11,9 +10,8 @@ public:
|
||||||
virtual bool isDone() const override;
|
virtual bool isDone() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int getPoint(float n1, float n2, float perc) const;
|
float getPoint(float n1, float n2, float perc) const;
|
||||||
|
void fadeTrailSprite() const;
|
||||||
|
|
||||||
float _percentage;
|
float _percentage;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CLASSICFLYINGANIMATIONSCENARIO_H
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
ClassicGraphicsManager::ClassicGraphicsManager(sf::RenderTarget& target) :
|
ClassicGraphicsManager::ClassicGraphicsManager(sf::RenderTarget& target) :
|
||||||
_sprite_container({Type::UP, Type::DOWN,
|
_sprite_container({Type::UP, Type::DOWN,
|
||||||
Type::LEFT, Type::RIGHT},
|
Type::LEFT, Type::RIGHT},
|
||||||
std::make_unique<ClassicSpriteFactory>("VeraMono.ttf")),
|
std::make_unique<ClassicSpriteFactory>()),
|
||||||
_render_target(target)
|
_render_target(target)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
|
@ -1,39 +1,35 @@
|
||||||
#include "classicsprite.h"
|
#include "classicsprite.h"
|
||||||
#include <SFML/Graphics/RenderTarget.hpp>
|
#include <SFML/Graphics/RenderTarget.hpp>
|
||||||
|
|
||||||
ClassicSprite::ClassicSprite(const sf::RectangleShape& shape, const sf::Font& font) :
|
ClassicSprite::ClassicSprite(const sf::RectangleShape& shape) :
|
||||||
_prototype(shape),
|
_prototype(shape),
|
||||||
_shape(shape),
|
_shape(shape),
|
||||||
_trail(shape),
|
_trail(shape)
|
||||||
_font(font)
|
{}
|
||||||
{
|
|
||||||
_grade_text.setFont(_font);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClassicSprite::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
void ClassicSprite::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||||
{
|
{
|
||||||
target.draw(_shape, states);
|
target.draw(_shape, states);
|
||||||
target.draw(_trail, states);
|
target.draw(_trail, states);
|
||||||
target.draw(_grade_text, states);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClassicSprite::reset()
|
void ClassicSprite::reset()
|
||||||
{
|
{
|
||||||
_shape.setPosition(0, 0);
|
_shape.setPosition(0, 0);
|
||||||
_trail.setPosition(0, 0);
|
_trail.setPosition(0, 0);
|
||||||
_grade_text.setPosition(0, 0);
|
|
||||||
_grade_text.setFillColor(sf::Color(255, 255, 255, 0));
|
|
||||||
|
|
||||||
_shape = _prototype;
|
_shape = _prototype;
|
||||||
_trail = _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);
|
_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
|
Coordinates ClassicSprite::coordinates() const
|
||||||
|
@ -46,75 +42,22 @@ Coordinates ClassicSprite::trailCoordinates() const
|
||||||
return {_trail.getPosition().x, _trail.getPosition().y};
|
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);
|
_shape.setFillColor(color);
|
||||||
|
|
||||||
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
|
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));
|
return _shape.getFillColor();
|
||||||
_shape.setFillColor(sf::Color(140, 140, 140));
|
|
||||||
_trail.setPosition(0, 0);
|
|
||||||
_trail.setFillColor(sf::Color(0, 0, 0, 0));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClassicSprite::fade()
|
sf::Color ClassicSprite::trailColor() const
|
||||||
{
|
{
|
||||||
auto fill_color = _grade_text.getFillColor();
|
return _trail.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;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,33 +3,27 @@
|
||||||
#include "tools/mathutils.h"
|
#include "tools/mathutils.h"
|
||||||
#include "sprite.h"
|
#include "sprite.h"
|
||||||
#include <SFML/Graphics/RectangleShape.hpp>
|
#include <SFML/Graphics/RectangleShape.hpp>
|
||||||
#include <SFML/Graphics/Text.hpp>
|
|
||||||
|
|
||||||
class ClassicSprite : public Sprite, public sf::Drawable
|
class ClassicSprite : public Sprite, public sf::Drawable
|
||||||
{
|
{
|
||||||
public:
|
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 draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||||
virtual void reset() 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 coordinates() const;
|
||||||
Coordinates trailCoordinates() const;
|
Coordinates trailCoordinates() const;
|
||||||
void update(float trail_x, float trail_y) noexcept;
|
|
||||||
void update() noexcept;
|
|
||||||
|
|
||||||
void pulse();
|
void setColor(const sf::Color& color);
|
||||||
void fade();
|
void setTrailColor(const sf::Color& color);
|
||||||
void trailFade();
|
sf::Color color() const;
|
||||||
bool isDead() const;
|
sf::Color trailColor() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sf::RectangleShape _prototype;
|
sf::RectangleShape _prototype;
|
||||||
|
|
||||||
sf::RectangleShape _shape;
|
sf::RectangleShape _shape;
|
||||||
sf::RectangleShape _trail;
|
sf::RectangleShape _trail;
|
||||||
sf::Text _grade_text;
|
|
||||||
sf::Font _font;
|
|
||||||
|
|
||||||
bool _trail_fade = false;
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,11 +7,6 @@
|
||||||
class ClassicSpriteFactory
|
class ClassicSpriteFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
inline ClassicSpriteFactory(const std::string& font_filename)
|
|
||||||
{
|
|
||||||
_font.loadFromFile(font_filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::shared_ptr<ClassicSprite> create(Type type)
|
inline std::shared_ptr<ClassicSprite> create(Type type)
|
||||||
{
|
{
|
||||||
sf::RectangleShape sprite;
|
sf::RectangleShape sprite;
|
||||||
|
@ -38,10 +33,7 @@ public:
|
||||||
sprite.setFillColor(sf::Color(255, 239, 0));
|
sprite.setFillColor(sf::Color(255, 239, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::make_shared<ClassicSprite>(sprite, _font);
|
return std::make_shared<ClassicSprite>(sprite);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
sf::Font _font;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue