forked from NaiJi/project-kyoku
Encapsulate animation into objects
This commit is contained in:
parent
a028773fe8
commit
06d099c11f
|
@ -15,18 +15,19 @@ file(GLOB SOURCES "src/*.cpp" "src/classicgame/*.*" "src/classicgame/classicnote
|
||||||
|
|
||||||
# STATIC #
|
# STATIC #
|
||||||
# You need to build SFML from sources with cmake
|
# You need to build SFML from sources with cmake
|
||||||
set(SFML_LIB_DIR
|
#set(SFML_LIB_DIR
|
||||||
${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-graphics.so.2.5
|
# ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-graphics.so.2.5
|
||||||
${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-system.so.2.5
|
# ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-system.so.2.5
|
||||||
${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-window.so.2.5
|
# ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-window.so.2.5
|
||||||
${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-audio.so.2.5)
|
# ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-audio.so.2.5)
|
||||||
set(SFML_INCL_DIR ${CMAKE_SOURCE_DIR}/SFML-2.5.1/include)
|
#set(SFML_INCL_DIR ${CMAKE_SOURCE_DIR}/SFML-2.5.1/include)
|
||||||
include_directories(${SFML_INCL_DIR} ${CMAKE_SOURCE_DIR}/include)
|
#include_directories(${SFML_INCL_DIR} ${CMAKE_SOURCE_DIR}/include)
|
||||||
add_executable(project-kyoku ${SOURCES})
|
#add_executable(project-kyoku ${SOURCES})
|
||||||
target_link_libraries(project-kyoku ${SFML_LIB_DIR})
|
#target_link_libraries(project-kyoku ${SFML_LIB_DIR})
|
||||||
|
|
||||||
# DYNAMIC #
|
# DYNAMIC #
|
||||||
# You only need to install SFML from your package manager
|
# You only need to install SFML from your package manager
|
||||||
#find_package(SFML REQUIRED graphics window system)
|
find_package(SFML REQUIRED graphics window system)
|
||||||
#add_executable(project-kyoku ${SOURCES})
|
add_executable(project-kyoku ${SOURCES})
|
||||||
#target_link_libraries(project-kyoku sfml-system sfml-audio sfml-graphics sfml-network)
|
include_directories(${SFML_INCL_DIR} ${CMAKE_SOURCE_DIR}/include)
|
||||||
|
target_link_libraries(project-kyoku sfml-system sfml-audio sfml-graphics sfml-network)
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SFML/Config.hpp>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
using microsec = sf::Int64;
|
||||||
|
|
||||||
|
class ClassicSprite;
|
||||||
|
|
||||||
|
class ClassicAnimationScenario
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~ClassicAnimationScenario() = default;
|
||||||
|
|
||||||
|
virtual void launch(const std::shared_ptr<ClassicSprite> sprite, const microsec& time_begin, const microsec &time_end) = 0;
|
||||||
|
virtual void update(const microsec& music_offset) = 0;
|
||||||
|
virtual bool isDone() const = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::shared_ptr<ClassicSprite> _sprite;
|
||||||
|
microsec _time_begin;
|
||||||
|
microsec _time_end;
|
||||||
|
};
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include "classicdyinganimationscenario.h"
|
||||||
|
#include "classicsprite.h"
|
||||||
|
|
||||||
|
void ClassicDyingAnimationScenario::launch(const std::shared_ptr<ClassicSprite> sprite, const microsec& time_begin, const microsec &time_end)
|
||||||
|
{
|
||||||
|
_sprite = sprite;
|
||||||
|
_time_begin = time_begin;
|
||||||
|
_time_end = time_end;
|
||||||
|
|
||||||
|
_sprite->pulse();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClassicDyingAnimationScenario::update(const microsec& music_offset)
|
||||||
|
{
|
||||||
|
_sprite->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ClassicDyingAnimationScenario::isDone() const
|
||||||
|
{
|
||||||
|
return _sprite->isDead();
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
#ifndef CLASSICDYINGANIMATIONSCENARIO_H
|
||||||
|
#define CLASSICDYINGANIMATIONSCENARIO_H
|
||||||
|
|
||||||
|
#include "classicanimationscenario.h"
|
||||||
|
|
||||||
|
class ClassicDyingAnimationScenario : public ClassicAnimationScenario
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void launch(const std::shared_ptr<ClassicSprite> sprite, const microsec& time_begin, const microsec& time_end) override;
|
||||||
|
virtual void update(const microsec& music_offset) override;
|
||||||
|
virtual bool isDone() const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CLASSICDYINGANIMATIONSCENARIO_H
|
|
@ -0,0 +1,42 @@
|
||||||
|
#include "classicflyinganimationscenario.h"
|
||||||
|
#include "classicsprite.h"
|
||||||
|
|
||||||
|
void ClassicFlyingAnimationScenario::launch(const std::shared_ptr<ClassicSprite> sprite, const microsec& time_begin, const microsec &time_end)
|
||||||
|
{
|
||||||
|
_sprite = sprite;
|
||||||
|
_time_begin = time_begin;
|
||||||
|
_time_end = time_end;
|
||||||
|
|
||||||
|
_percentage = ((_time_end - _time_begin) * 0.01);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ClassicFlyingAnimationScenario::getPoint(float n1, float n2, float perc) const
|
||||||
|
{
|
||||||
|
float diff = n2 - n1;
|
||||||
|
|
||||||
|
return n1 + ( diff * perc );
|
||||||
|
}
|
||||||
|
|
||||||
|
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 xa = getPoint( crd.first + 20. , crd.first + 90. , i );
|
||||||
|
float ya = getPoint( crd.second - 600. , crd.second - 150. , i );
|
||||||
|
float xb = getPoint( crd.first + 90. , crd.first , i );
|
||||||
|
float yb = getPoint( crd.second - 150. , crd.second , i );
|
||||||
|
|
||||||
|
_sprite->update(getPoint( xa , xb , i ), getPoint( ya , yb , i ));
|
||||||
|
if (i >= 1)
|
||||||
|
{
|
||||||
|
_sprite->trailFade();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ClassicFlyingAnimationScenario::isDone() const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
#ifndef CLASSICFLYINGANIMATIONSCENARIO_H
|
||||||
|
#define CLASSICFLYINGANIMATIONSCENARIO_H
|
||||||
|
|
||||||
|
#include "classicanimationscenario.h"
|
||||||
|
|
||||||
|
class ClassicFlyingAnimationScenario : public ClassicAnimationScenario
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void launch(const std::shared_ptr<ClassicSprite> sprite, const microsec& time_begin, const microsec& time_end) override;
|
||||||
|
virtual void update(const microsec& music_offset) override;
|
||||||
|
virtual bool isDone() const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
int getPoint(float n1, float n2, float perc) const;
|
||||||
|
|
||||||
|
float _percentage;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CLASSICFLYINGANIMATIONSCENARIO_H
|
|
@ -1,7 +1,6 @@
|
||||||
#include "classicgame.h"
|
#include "classicgame.h"
|
||||||
#include "classictimeline.h"
|
#include "classictimeline.h"
|
||||||
#include "classicmapcreator.h"
|
#include "classicmapcreator.h"
|
||||||
#include "classicnote.h"
|
|
||||||
|
|
||||||
ClassicGame::ClassicGame(std::unique_ptr<ClassicGraphicsManager>&& manager) :
|
ClassicGame::ClassicGame(std::unique_ptr<ClassicGraphicsManager>&& manager) :
|
||||||
_timeline(std::make_unique<ClassicTimeline>()),
|
_timeline(std::make_unique<ClassicTimeline>()),
|
||||||
|
|
|
@ -8,12 +8,13 @@ ClassicGraphicsManager::ClassicGraphicsManager(sf::RenderTarget& target) :
|
||||||
_render_target(target)
|
_render_target(target)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void ClassicGraphicsManager::initSprite(ClassicNote* note)
|
void ClassicGraphicsManager::initGraphics(ClassicNote* note)
|
||||||
{
|
{
|
||||||
note->setSprite(_sprite_container.getSprite(note->type()));
|
note->setSprite(_sprite_container.getSprite(note->type()));
|
||||||
|
note->sprite()->setCoordinates(note->getCoordinates().x, note->getCoordinates().y, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClassicGraphicsManager::resetSprite(ClassicNote* note)
|
void ClassicGraphicsManager::resetGraphics(ClassicNote* note)
|
||||||
{
|
{
|
||||||
_sprite_container.resetSprite(note->sprite(), note->type());
|
_sprite_container.resetSprite(note->sprite(), note->type());
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,8 @@ class ClassicGraphicsManager
|
||||||
public:
|
public:
|
||||||
explicit ClassicGraphicsManager(sf::RenderTarget& target);
|
explicit ClassicGraphicsManager(sf::RenderTarget& target);
|
||||||
|
|
||||||
void initSprite(ClassicNote* note);
|
void initGraphics(ClassicNote* note);
|
||||||
void resetSprite(ClassicNote* note);
|
void resetGraphics(ClassicNote* note);
|
||||||
void draw(const ClassicNote *note);
|
void draw(const ClassicNote *note);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#include "classicnote.h"
|
#include "classicnote.h"
|
||||||
#include "classicsprite.h"
|
#include "classicsprite.h"
|
||||||
#include <SFML/Graphics/RenderTarget.hpp>
|
#include "classicgraphicsmanager.h"
|
||||||
|
#include "classicflyinganimationscenario.h"
|
||||||
|
#include "classicdyinganimationscenario.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
ClassicNote::ClassicNote(const std::vector<microsec>& intervals, microsec perfect_offset,
|
ClassicNote::ClassicNote(const std::vector<microsec>& intervals, microsec perfect_offset,
|
||||||
|
@ -8,10 +10,16 @@ ClassicNote::ClassicNote(const std::vector<microsec>& intervals, microsec perfec
|
||||||
Note(perfect_offset),
|
Note(perfect_offset),
|
||||||
_coordinates(coord),
|
_coordinates(coord),
|
||||||
_evaluator(intervals, _perfect_offset),
|
_evaluator(intervals, _perfect_offset),
|
||||||
|
_keys({sf::Keyboard::W, sf::Keyboard::Up}),
|
||||||
_graphics_manager(manager),
|
_graphics_manager(manager),
|
||||||
_is_expired(true),
|
_is_expired(true),
|
||||||
_type(type)
|
_type(type),
|
||||||
{}
|
_state(State::NONE)
|
||||||
|
{
|
||||||
|
_animations[State::NONE] = nullptr;
|
||||||
|
_animations[State::FLYING] = std::make_shared<ClassicFlyingAnimationScenario>();
|
||||||
|
_animations[State::DYING] = std::make_shared<ClassicDyingAnimationScenario>();
|
||||||
|
}
|
||||||
|
|
||||||
bool ClassicNote::isActive(const microsec &music_offset) const
|
bool ClassicNote::isActive(const microsec &music_offset) const
|
||||||
{
|
{
|
||||||
|
@ -21,9 +29,9 @@ bool ClassicNote::isActive(const microsec &music_offset) const
|
||||||
void ClassicNote::putToGame(const microsec &music_offset)
|
void ClassicNote::putToGame(const microsec &music_offset)
|
||||||
{
|
{
|
||||||
_is_expired = false;
|
_is_expired = false;
|
||||||
_graphics_manager->initSprite(this); (void) music_offset;
|
_graphics_manager->initGraphics(this);
|
||||||
//_appearance_time = music_offset; // To animation manager
|
_state = State::FLYING;
|
||||||
//_trail_path_percent = ((_perfect_offset - _appearance_time) * 0.01);
|
_animations[_state]->launch(_sprite, music_offset, offset());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClassicNote::isExpired() const
|
bool ClassicNote::isExpired() const
|
||||||
|
@ -33,13 +41,19 @@ bool ClassicNote::isExpired() const
|
||||||
|
|
||||||
void ClassicNote::update(const microsec& music_offset)
|
void ClassicNote::update(const microsec& music_offset)
|
||||||
{
|
{
|
||||||
|
if (!_animations[_state])
|
||||||
|
return;
|
||||||
|
|
||||||
bool is_not_active_anymore = (!_is_expired && !isActive(music_offset));
|
bool is_not_active_anymore = (!_is_expired && !isActive(music_offset));
|
||||||
|
|
||||||
if (is_not_active_anymore)
|
if (is_not_active_anymore)
|
||||||
{
|
{
|
||||||
_graphics_manager->resetSprite(this);
|
_state = State::DYING;
|
||||||
_is_expired = true;
|
_animations[_state]->launch(_sprite, music_offset, offset());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_animations[_state]->update(music_offset);
|
||||||
|
_is_expired = _animations[_state]->isDone();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClassicNote::input(PlayerInput&& inputdata)
|
void ClassicNote::input(PlayerInput&& inputdata)
|
||||||
|
@ -49,6 +63,9 @@ void ClassicNote::input(PlayerInput&& inputdata)
|
||||||
if (std::find(_keys.begin(), _keys.end(), inputdata.event.key.code) != _keys.end())
|
if (std::find(_keys.begin(), _keys.end(), inputdata.event.key.code) != _keys.end())
|
||||||
grade = _evaluator.calculatePrecision(inputdata.timestamp);
|
grade = _evaluator.calculatePrecision(inputdata.timestamp);
|
||||||
|
|
||||||
|
_state = State::DYING;
|
||||||
|
_animations[_state]->launch(_sprite, inputdata.timestamp, offset());
|
||||||
|
|
||||||
std::cout << "User input: " << static_cast<int>(grade) << "\n";
|
std::cout << "User input: " << static_cast<int>(grade) << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
#include "note.h"
|
#include "note.h"
|
||||||
#include "precisionevaluator.h"
|
#include "precisionevaluator.h"
|
||||||
#include "classicgraphicsmanager.h"
|
#include "classicactions.h"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
@ -19,6 +19,8 @@ struct Coordinates
|
||||||
}; // MOVE TO OWN HEADER ^
|
}; // MOVE TO OWN HEADER ^
|
||||||
|
|
||||||
class ClassicSprite;
|
class ClassicSprite;
|
||||||
|
class ClassicGraphicsManager;
|
||||||
|
class ClassicAnimationScenario;
|
||||||
|
|
||||||
class ClassicNote : public Note
|
class ClassicNote : public Note
|
||||||
{
|
{
|
||||||
|
@ -31,6 +33,14 @@ public:
|
||||||
BAD
|
BAD
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum State
|
||||||
|
{
|
||||||
|
NONE,
|
||||||
|
|
||||||
|
FLYING,
|
||||||
|
DYING
|
||||||
|
};
|
||||||
|
|
||||||
explicit ClassicNote(const std::vector<microsec>& intervals, microsec perfect_offset,
|
explicit ClassicNote(const std::vector<microsec>& intervals, microsec perfect_offset,
|
||||||
Type type, const Coordinates& coord,
|
Type type, const Coordinates& coord,
|
||||||
const std::unique_ptr<ClassicGraphicsManager>& manager);
|
const std::unique_ptr<ClassicGraphicsManager>& manager);
|
||||||
|
@ -52,11 +62,14 @@ public:
|
||||||
private:
|
private:
|
||||||
const Coordinates _coordinates;
|
const Coordinates _coordinates;
|
||||||
const PrecisionEvaluator<Grade> _evaluator;
|
const PrecisionEvaluator<Grade> _evaluator;
|
||||||
|
const std::array<const sf::Keyboard::Key, 2> _keys;
|
||||||
|
|
||||||
const std::unique_ptr<ClassicGraphicsManager>& _graphics_manager;
|
const std::unique_ptr<ClassicGraphicsManager>& _graphics_manager;
|
||||||
std::shared_ptr<ClassicSprite> _sprite;
|
std::shared_ptr<ClassicSprite> _sprite;
|
||||||
|
|
||||||
bool _is_expired;
|
bool _is_expired;
|
||||||
std::array<sf::Keyboard::Key, 2> _keys;
|
|
||||||
const Type _type;
|
const Type _type;
|
||||||
|
|
||||||
|
State _state;
|
||||||
|
std::array<std::shared_ptr<ClassicAnimationScenario>, 3> _animations;
|
||||||
};
|
};
|
||||||
|
|
|
@ -36,6 +36,11 @@ void ClassicSprite::setCoordinates(float x, float y, float trail_x, float trail_
|
||||||
_grade_text.setPosition(x + _shape.getSize().x/2, y + 10);
|
_grade_text.setPosition(x + _shape.getSize().x/2, y + 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::pair<float, float> ClassicSprite::coordinates() const
|
||||||
|
{
|
||||||
|
return std::make_pair(_shape.getPosition().x, _shape.getPosition().y);
|
||||||
|
}
|
||||||
|
|
||||||
void ClassicSprite::update(float trail_x, float trail_y) noexcept
|
void ClassicSprite::update(float trail_x, float trail_y) noexcept
|
||||||
{
|
{
|
||||||
_trail.setPosition(trail_x, trail_y);
|
_trail.setPosition(trail_x, trail_y);
|
||||||
|
|
|
@ -12,6 +12,7 @@ public:
|
||||||
virtual void reset() override;
|
virtual void reset() override;
|
||||||
|
|
||||||
void setCoordinates(float x, float y, float trail_x, float trail_y) noexcept;
|
void setCoordinates(float x, float y, float trail_x, float trail_y) noexcept;
|
||||||
|
std::pair<float, float> coordinates() const;
|
||||||
void update(float trail_x, float trail_y) noexcept;
|
void update(float trail_x, float trail_y) noexcept;
|
||||||
void update() noexcept;
|
void update() noexcept;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue