2021-04-08 15:51:59 -04:00
|
|
|
#include <iostream>
|
2021-06-07 14:19:58 -04:00
|
|
|
#include "classicactions.h"
|
|
|
|
#include "classictimeline.h"
|
2021-06-08 14:32:36 -04:00
|
|
|
#include "classicnote.h"
|
2021-06-16 11:16:18 -04:00
|
|
|
#include "spritecontainer.h"
|
2021-06-16 13:11:00 -04:00
|
|
|
#include "classicgraphicsmanager.h"
|
2021-06-16 11:16:18 -04:00
|
|
|
|
2021-06-09 14:08:58 -04:00
|
|
|
#include <SFML/Graphics/RenderTarget.hpp>
|
2021-04-08 15:51:59 -04:00
|
|
|
|
2021-06-07 14:19:58 -04:00
|
|
|
ClassicTimeline::ClassicTimeline()
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
|
|
|
// BPM of METEOR is 170.
|
|
|
|
// Length is 1:14
|
|
|
|
// I calculated that the time between beats is about 1412162 microseconds
|
|
|
|
|
2021-06-11 12:58:44 -04:00
|
|
|
std::string song_filename = "METEOR.flac";
|
2021-06-08 14:32:36 -04:00
|
|
|
|
|
|
|
_music.openFromFile(song_filename);
|
2021-06-09 14:08:58 -04:00
|
|
|
_music.setVolume(10);
|
2021-06-08 14:32:36 -04:00
|
|
|
|
2021-04-09 09:28:45 -04:00
|
|
|
_timeline.reserve(1000);
|
|
|
|
|
2021-06-11 12:58:44 -04:00
|
|
|
microsec starting_beat_offset = 352162;
|
2021-04-08 09:34:03 -04:00
|
|
|
int amount_of_beats = 209;
|
2021-04-09 09:28:45 -04:00
|
|
|
microsec interval = 1412162;
|
2021-04-08 09:34:03 -04:00
|
|
|
microsec note_input_offset = 412162;
|
2021-04-09 09:28:45 -04:00
|
|
|
microsec bpm_iterator = starting_beat_offset;
|
|
|
|
microsec bpm_end = starting_beat_offset + (interval * amount_of_beats);
|
2021-06-11 12:58:44 -04:00
|
|
|
_visibility_offset = note_input_offset * 8;
|
2021-04-08 09:34:03 -04:00
|
|
|
|
2021-06-09 14:08:58 -04:00
|
|
|
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_DOWN, {90, 90}));
|
2021-04-17 12:14:36 -04:00
|
|
|
bpm_iterator += interval;
|
|
|
|
|
2021-06-09 14:08:58 -04:00
|
|
|
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_LEFT, {190, 90}));
|
2021-04-17 12:14:36 -04:00
|
|
|
bpm_iterator += interval;
|
|
|
|
|
2021-06-09 14:08:58 -04:00
|
|
|
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_LEFT, {290, 90}));
|
2021-04-17 12:14:36 -04:00
|
|
|
bpm_iterator += interval;
|
|
|
|
|
2021-06-11 12:58:44 -04:00
|
|
|
float x = 90.;
|
|
|
|
|
2021-04-09 09:28:45 -04:00
|
|
|
while (bpm_iterator < bpm_end)
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-06-11 12:58:44 -04:00
|
|
|
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_UP, {x, 390.}));
|
2021-04-09 09:28:45 -04:00
|
|
|
bpm_iterator += interval;
|
2021-06-11 12:58:44 -04:00
|
|
|
x += 70;
|
2021-04-08 09:34:03 -04:00
|
|
|
}
|
|
|
|
|
2021-06-16 11:16:18 -04:00
|
|
|
expire(_first_visible_note);
|
2021-06-09 18:47:18 -04:00
|
|
|
expire(_last_visible_note);
|
2021-06-08 14:32:36 -04:00
|
|
|
expire(_active_note);
|
2021-04-08 09:34:03 -04:00
|
|
|
_top_note = _timeline.begin();
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:58:44 -04:00
|
|
|
void ClassicTimeline::run()
|
2021-06-09 14:08:58 -04:00
|
|
|
{
|
2021-06-09 18:47:18 -04:00
|
|
|
_music.play();
|
2021-06-09 14:08:58 -04:00
|
|
|
}
|
|
|
|
|
2021-06-08 14:32:36 -04:00
|
|
|
ClassicTimeline::~ClassicTimeline()
|
2021-04-09 09:28:45 -04:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2021-06-08 14:32:36 -04:00
|
|
|
void ClassicTimeline::clear()
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-06-16 13:11:00 -04:00
|
|
|
for (auto& note : _timeline)
|
2021-04-08 09:34:03 -04:00
|
|
|
delete note;
|
|
|
|
|
|
|
|
_timeline.clear();
|
|
|
|
}
|
|
|
|
|
2021-06-08 14:32:36 -04:00
|
|
|
void ClassicTimeline::update()
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-06-09 18:47:18 -04:00
|
|
|
const microsec& offset = currentMusicOffset();
|
2021-06-08 14:32:36 -04:00
|
|
|
checkCurrentActiveNote(offset);
|
|
|
|
checkForNextActiveNote(offset);
|
2021-06-16 13:11:00 -04:00
|
|
|
updateVisibleSprites(offset);
|
2021-04-08 09:34:03 -04:00
|
|
|
}
|
|
|
|
|
2021-06-16 13:11:00 -04:00
|
|
|
void ClassicTimeline::checkCurrentActiveNote(const microsec& music_offset)
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-06-16 11:16:18 -04:00
|
|
|
if (isExpired(_active_note))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto note = *_active_note;
|
|
|
|
|
2021-06-16 13:11:00 -04:00
|
|
|
if (note->state() != ClassicNote::State::FLYING || !note->isActive(music_offset))
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-06-16 13:11:00 -04:00
|
|
|
note->setState(ClassicNote::State::DYING);
|
2021-06-08 14:32:36 -04:00
|
|
|
expire(_active_note);
|
2021-04-08 09:34:03 -04:00
|
|
|
++_top_note;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 13:11:00 -04:00
|
|
|
void ClassicTimeline::checkForNextActiveNote(const microsec& music_offset)
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-06-16 11:16:18 -04:00
|
|
|
if (!isExpired(_active_note))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto top_note = *_top_note;
|
|
|
|
if (top_note->isActive(music_offset))
|
2021-06-08 14:32:36 -04:00
|
|
|
_active_note = _top_note;
|
2021-04-08 09:34:03 -04:00
|
|
|
}
|
|
|
|
|
2021-06-16 13:11:00 -04:00
|
|
|
void ClassicTimeline::updateVisibleSprites(const microsec& music_offset)
|
|
|
|
{
|
|
|
|
if (nothingToDraw())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::for_each(_first_visible_note, _last_visible_note,
|
|
|
|
[&music_offset](const auto& note)
|
|
|
|
{
|
|
|
|
note->update(music_offset);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-08 14:32:36 -04:00
|
|
|
ClassicTimeline::Iterator ClassicTimeline::getActiveNote() noexcept
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
|
|
|
return _active_note;
|
|
|
|
}
|
2021-06-08 14:32:36 -04:00
|
|
|
|
2021-06-16 13:11:00 -04:00
|
|
|
bool ClassicTimeline::isExpired(const Iterator& iterator) const
|
2021-06-08 14:32:36 -04:00
|
|
|
{
|
|
|
|
return iterator == _timeline.end();
|
|
|
|
}
|
|
|
|
|
2021-06-16 13:11:00 -04:00
|
|
|
void ClassicTimeline::expire(Iterator& iterator)
|
2021-06-08 14:32:36 -04:00
|
|
|
{
|
|
|
|
iterator = _timeline.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
microsec ClassicTimeline::currentMusicOffset() const
|
|
|
|
{
|
|
|
|
return _music.getPlayingOffset().asMicroseconds();
|
|
|
|
}
|
|
|
|
|
2021-06-16 13:11:00 -04:00
|
|
|
bool ClassicTimeline::isVisiblyClose(const Iterator& iterator, const microsec& music_offset) const
|
2021-06-09 18:47:18 -04:00
|
|
|
{
|
|
|
|
return ((*iterator)->offset() - _visibility_offset) <= music_offset;
|
|
|
|
}
|
|
|
|
|
2021-06-16 13:11:00 -04:00
|
|
|
void ClassicTimeline::fetchVisibleNotes(const std::unique_ptr<ClassicGraphicsManager>& graphics_manager)
|
2021-06-08 14:32:36 -04:00
|
|
|
{
|
2021-06-11 12:58:44 -04:00
|
|
|
const microsec music_offset = currentMusicOffset();
|
2021-06-16 13:11:00 -04:00
|
|
|
initGraphicsForNewNotes(graphics_manager, music_offset);
|
|
|
|
discardGraphicsForDeadNotes(graphics_manager);
|
2021-06-16 11:16:18 -04:00
|
|
|
}
|
2021-06-08 14:32:36 -04:00
|
|
|
|
2021-06-16 13:11:00 -04:00
|
|
|
void ClassicTimeline::initGraphicsForNewNotes(const std::unique_ptr<ClassicGraphicsManager>& graphics_manager, const microsec &music_offset)
|
2021-06-16 11:16:18 -04:00
|
|
|
{
|
2021-06-09 18:47:18 -04:00
|
|
|
Iterator note_iterator = _top_note;
|
|
|
|
while (isVisiblyClose(note_iterator, music_offset))
|
2021-06-08 14:32:36 -04:00
|
|
|
{
|
2021-06-16 11:16:18 -04:00
|
|
|
if (nothingToDraw())
|
|
|
|
_first_visible_note = note_iterator;
|
2021-06-11 12:58:44 -04:00
|
|
|
|
2021-06-16 11:16:18 -04:00
|
|
|
auto note = *note_iterator;
|
|
|
|
|
2021-06-16 13:11:00 -04:00
|
|
|
if (!note->sprite())
|
|
|
|
{
|
|
|
|
note->saveAppearanceTime(music_offset);
|
|
|
|
graphics_manager->initSprite(note);
|
|
|
|
}
|
2021-06-09 18:47:18 -04:00
|
|
|
|
2021-06-16 13:11:00 -04:00
|
|
|
++note_iterator;
|
2021-06-08 14:32:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_last_visible_note = note_iterator;
|
|
|
|
}
|
2021-06-09 14:08:58 -04:00
|
|
|
|
2021-06-16 13:11:00 -04:00
|
|
|
void ClassicTimeline::discardGraphicsForDeadNotes(const std::unique_ptr<ClassicGraphicsManager>& graphics_manager)
|
2021-06-09 14:08:58 -04:00
|
|
|
{
|
2021-06-16 11:16:18 -04:00
|
|
|
if (nothingToDraw())
|
2021-06-09 14:08:58 -04:00
|
|
|
return;
|
|
|
|
|
2021-06-16 11:16:18 -04:00
|
|
|
auto note_iterator = _first_visible_note;
|
|
|
|
while (note_iterator != _last_visible_note)
|
2021-06-09 14:08:58 -04:00
|
|
|
{
|
2021-06-16 11:16:18 -04:00
|
|
|
auto note = *note_iterator;
|
|
|
|
if (note->state() == ClassicNote::State::DEAD)
|
|
|
|
{
|
|
|
|
note->setState(ClassicNote::State::NONE);
|
2021-06-16 13:11:00 -04:00
|
|
|
graphics_manager->resetSprite(note);
|
|
|
|
|
2021-06-16 11:16:18 -04:00
|
|
|
++_first_visible_note;
|
|
|
|
}
|
2021-06-16 13:11:00 -04:00
|
|
|
|
|
|
|
++note_iterator;
|
2021-06-09 14:08:58 -04:00
|
|
|
}
|
|
|
|
}
|
2021-06-16 11:16:18 -04:00
|
|
|
|
|
|
|
bool ClassicTimeline::nothingToDraw() const noexcept
|
|
|
|
{
|
|
|
|
return isExpired(_first_visible_note);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClassicTimeline::drawVisibleNotes(sf::RenderWindow &window) const
|
|
|
|
{
|
|
|
|
if (nothingToDraw())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::for_each(_first_visible_note, _last_visible_note,
|
|
|
|
[&window](const auto& note)
|
|
|
|
{
|
|
|
|
window.draw(*note);
|
|
|
|
});
|
|
|
|
}
|
2021-06-16 13:11:00 -04:00
|
|
|
|