project-kyoku/note.cpp

81 lines
1.7 KiB
C++

#include "note.h"
#include <iostream>
#include <cmath>
Note::Note(microsec offset, microsec life_span_offset, Note::Arrow type) :
_offset(offset),
_start_handling_offset(_offset - life_span_offset),
_end_handling_offset(_offset + life_span_offset),
_type(type)
{}
void Note::setPosition(coordinates position)
{
_position = position;
}
coordinates Note::position() const noexcept
{
return _position;
}
microsec Note::offset() const noexcept
{
return _offset;
}
NoteGrade Note::onTap(Arrow arrow_type, microsec tap_time_stamp)
{
if (arrow_type != _type)
return {0, NoteGrade::Rating::WRONG};
microsec odds = std::abs(tap_time_stamp - _offset);
return calculatePrecision(odds);
}
NoteGrade Note::calculatePrecision(microsec odds) const
{
NoteGrade ret(0, NoteGrade::Rating::BAD);
if (odds < _precision_qualifier)
{
ret = {50, NoteGrade::Rating::GREAT};
}
return ret;
}
bool Note::isActive(microsec music_play_offset) const noexcept
{
return music_play_offset > _start_handling_offset
&& music_play_offset < _end_handling_offset;
}
void Note::resetPrecisionQualifier(microsec qualifier)
{
_precision_qualifier = qualifier;
}
void Note::resetSprite(const std::shared_ptr<Sprite> &sprite) noexcept
{
if (_sprite)
_sprite->setAttachment(false);
_sprite = sprite;
if (_sprite)
_sprite->setAttachment(true);
}
Note::Arrow Note::type() const noexcept
{
return _type;
}
void Note::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
target.draw(*_sprite, states);
}
microsec Note::_precision_qualifier = 500000; // Default initialization as 0.5 second.