project-kyoku/modes/classicmode/game/classicgraphicsmanager.cpp

61 lines
1.7 KiB
C++
Raw Normal View History

2021-12-21 12:07:19 -05:00
#include "classicgraphicsmanager.h"
#include "graphics/classicsprite.h"
ClassicGraphicsManager::ClassicGraphicsManager(Timeline<ClassicNote> &timeline, const microsec& visibility_offset) :
_sprite_container({Type::UP, Type::DOWN,
Type::LEFT, Type::RIGHT},
std::make_unique<ClassicSpriteFactory>()),
_timeline(&timeline),
_visibility_offset(visibility_offset)
{
_timeline->expire(_first);
_timeline->expire(_last);
}
void ClassicGraphicsManager::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
if (nothingToDraw())
return;
std::for_each(_first, _last,
[&target, &states](const auto& note)
{
target.draw(*note->sprite(), states);
});
}
std::shared_ptr<ClassicSprite> ClassicGraphicsManager::getSprite(Type type)
{
return _sprite_container.getSprite(type);
}
void ClassicGraphicsManager::update(const microsec &offset)
{
Iterator note_iterator = _timeline->getTopNote();
while (!_timeline->isExpired(note_iterator) && isVisiblyClose(note_iterator, offset))
{
if (nothingToDraw())
_first = note_iterator;
auto note = *note_iterator;
if (!note->isInGame())
note->putToGame(offset);
++note_iterator;
}
_last = note_iterator;
}
bool ClassicGraphicsManager::nothingToDraw() const noexcept
{
return _timeline->isExpired(_first)
|| _timeline->isExpired(_last);
}
bool ClassicGraphicsManager::isVisiblyClose(const Iterator& iterator, const microsec& music_offset) const noexcept
{
return ((iterator)->offset() - _visibility_offset) <= music_offset;
}