You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
project-kyoku/src/classicgame/classicnote.cpp

115 lines
2.9 KiB
C++

#include "classicnote.h"
#include "classicsprite.h"
#include "classicgraphicsmanager.h"
#include "classicflyinganimationscenario.h"
#include "classicdyinganimationscenario.h"
ClassicNote::ClassicNote(const std::vector<microsec>& intervals, microsec perfect_offset,
Type type, const Coordinates& coord, const std::unique_ptr<ClassicGraphicsManager> &manager) :
Note(perfect_offset),
_coordinates(coord),
_evaluator(intervals, _perfect_offset),
_keys({sf::Keyboard::W, sf::Keyboard::Up}),
_graphics_manager(manager),
_type(type),
_state(State::NONE)
{
_animations[State::NONE] = nullptr;
_animations[State::FLYING] = std::make_shared<ClassicFlyingAnimationScenario>();
_animations[State::ACTIVE] = _animations[State::FLYING];
_animations[State::DYING] = std::make_shared<ClassicDyingAnimationScenario>();
_animations[State::DEAD] = nullptr;
}
bool ClassicNote::isActive() const
{
return _state == State::ACTIVE;
}
void ClassicNote::putToGame(const microsec &music_offset)
{
_graphics_manager->initGraphics(this);
_state = State::FLYING;
_animations[_state]->launch(_sprite, music_offset, offset());
}
bool ClassicNote::isInGame() const
{
return _state == State::FLYING
|| _state == State::ACTIVE
|| _state == State::DYING;
}
bool ClassicNote::isExpired() const
{
return _state == State::DEAD;
}
void ClassicNote::update(const microsec& music_offset)
{
switch (_state)
{
default: return;
break;
case State::FLYING:
if (_evaluator.isActive(music_offset))
_state = State::ACTIVE;
break;
case State::DYING:
if (_animations[_state]->isDone())
_state = State::DEAD;
break;
case State::ACTIVE:
if (!_evaluator.isActive(music_offset))
{
_state = State::DYING;
_animations[_state]->launch(_sprite, music_offset, offset());
}
break;
}
if (_animations[_state])
_animations[_state]->update(music_offset);
}
void ClassicNote::input(PlayerInput&& inputdata)
{
auto grade = ClassicNote::Grade::BAD;
if (std::find(_keys.begin(), _keys.end(), inputdata.event.key.code) != _keys.end())
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::shared_ptr<ClassicSprite> ClassicNote::sprite() const noexcept
{
return _sprite;
}
void ClassicNote::setSprite(const std::shared_ptr<ClassicSprite>& sprite) noexcept
{
_sprite = sprite;
}
const Coordinates& ClassicNote::getCoordinates() const noexcept
{
return _coordinates;
}
Type ClassicNote::type() const noexcept
{
return _type;
}
void ClassicNote::draw() const
{
_graphics_manager->draw(this);
}