Separate ClassicNoteManager logic from ClassicNote, make it run

This commit is contained in:
NaiJi ✨ 2021-08-10 22:08:45 +03:00
parent a223b7253c
commit 8c3f2a112f
7 changed files with 61 additions and 48 deletions

View File

@ -7,9 +7,9 @@
#include <iostream>
ClassicGame::ClassicGame(std::shared_ptr<ClassicGraphicsManager>&& manager, std::unique_ptr<Music>&& music) :
_timeline(std::make_unique<ClassicTimeline>()),
_graphics_manager(std::move(manager)),
_note_manager(std::make_unique<ClassicNoteManager>(_graphics_manager)),
_timeline(std::make_unique<ClassicTimeline>(_note_manager)),
_music(std::move(music)),
_is_paused(false)
{
@ -66,7 +66,7 @@ void ClassicGame::run()
_music->openFromFile("METEOR.flac");
_music->setVolume(10);
_music->play();
_timeline->run(std::move(beatmap.notes), beatmap.visibility_offset);
_timeline->setNotes(std::move(beatmap.notes), beatmap.visibility_offset);
}
void ClassicGame::input(PlayerInput&& inputdata)
@ -116,10 +116,11 @@ void ClassicGame::input(PlayerInput&& inputdata)
case sf::Event::KeyReleased:
{
const auto& note_manager = _note_manager;
bool key_match = std::any_of(_notes_on_hold.begin(), _notes_on_hold.end(),
[key=inputdata.event.key.code](const auto& note)
[&note_manager, key=inputdata.event.key.code](const auto& note)
{
return note->isPressedAs(key);
return note_manager->isPressedAs(note, key);
});
if (key_match)

View File

@ -33,9 +33,9 @@ private:
std::vector<ClassicNote*> _notes_on_hold;
std::unique_ptr<ClassicTimeline> _timeline;
std::shared_ptr<ClassicGraphicsManager> _graphics_manager;
std::shared_ptr<ClassicNoteManager> _note_manager;
std::unique_ptr<ClassicTimeline> _timeline;
sf::SoundBuffer _slap_buffer;
sf::Sound _slap;

View File

@ -1,6 +1,11 @@
#include "classicmapcreator.h"
#include "classicnote.h"
// Replace with interface by dependency injection
#include "classicflyinganimationscenario.h"
#include "classicdyinganimationscenario.h"
//
ClassicMapCreator::ClassicMapCreator(const std::shared_ptr<ClassicGraphicsManager>& manager) :
_graphics_manager(manager)
{}
@ -30,29 +35,43 @@ Beatmap ClassicMapCreator::createBeatmap(const std::string& filepath) const
while (bpm_iterator < bpm_end)
{
ClassicNote::ClassicNoteInitializer init;
ClassicNote::ClassicNoteInitializer::Element element;
init.intervals = input_intervals;
init.perfect_offset = bpm_iterator;
init.hold = false;
ClassicNote *note = new ClassicNote(
{
{input_intervals, bpm_iterator},
{input_intervals},
false,
ClassicNoteState::NONE,
{
ClassicNote::Element
{
{x, 390.},
false,
sf::Keyboard::Unknown,
Type::UP,
{},
{sf::Keyboard::W, sf::Keyboard::Up},
{nullptr},
{}
}
}
});
element.coordinates = {x, 390.};
element.falling_curve_interpolation = {};
element.keys = {sf::Keyboard::W, sf::Keyboard::Up};
element.type = Type::UP;
note->elements[0].animations[ClassicNoteState::NONE] = nullptr;
note->elements[0].animations[ClassicNoteState::FLYING] = std::make_shared<ClassicFlyingAnimationScenario>();
note->elements[0].animations[ClassicNoteState::ACTIVE] = note->elements[0].animations[ClassicNoteState::FLYING];
note->elements[0].animations[ClassicNoteState::DYING] = std::make_shared<ClassicDyingAnimationScenario>();
note->elements[0].animations[ClassicNoteState::DEAD] = nullptr;
if (counter == 0)
{
init.hold = true;
element.keys = {sf::Keyboard::D, sf::Keyboard::Right};
element.type = Type::RIGHT;
note->hold = true;
note->elements[0].available_keys = {sf::Keyboard::D, sf::Keyboard::Right};
note->elements[0].type = Type::RIGHT;
}
--counter;
init.elements = {element};
notes.emplace_back(new ClassicNote(std::move(init), _graphics_manager));
notes.emplace_back(note);
bpm_iterator += tempo_interval;
x += 70;

View File

@ -7,6 +7,7 @@
#include "classicdyinganimationscenario.h"
//
ClassicNoteManager::ClassicNoteManager(const std::shared_ptr<ClassicGraphicsManager>& manager) :
_graphics_manager(manager)
{}
@ -85,8 +86,8 @@ void ClassicNoteManager::input(ClassicNote* note, PlayerInput&& inputdata)
if (element.pressed)
return false;
auto key_iterator = std::find(element.keys.begin(), element.keys.end(), inputdata.event.key.code);
bool found_key = key_iterator != element.keys.end();
auto key_iterator = std::find(element.available_keys.begin(), element.available_keys.end(), inputdata.event.key.code);
bool found_key = key_iterator != element.available_keys.end();
if (found_key)
{
element.pressed = true;
@ -138,12 +139,3 @@ bool ClassicNoteManager::isPressedAs(const ClassicNote* note, sf::Keyboard::Key
return key == element.pressed_as;
});
}
void ClassicNoteManager::setDefaultAnimations(std::array<std::shared_ptr<ClassicAnimationScenario>, 5>& animations) const
{
animations[ClassicNoteState::NONE] = nullptr;
animations[ClassicNoteState::FLYING] = std::make_shared<ClassicFlyingAnimationScenario>();
animations[ClassicNoteState::ACTIVE] = animations[ClassicNoteState::FLYING];
animations[ClassicNoteState::DYING] = std::make_shared<ClassicDyingAnimationScenario>();
animations[ClassicNoteState::DEAD] = nullptr;
}

View File

@ -23,8 +23,6 @@ public:
bool allElementsPressed(const ClassicNote* note) const;
bool isPressedAs(const ClassicNote* note, sf::Keyboard::Key key) const;
void setDefaultAnimations(std::array<std::shared_ptr<ClassicAnimationScenario>, 5>& animations) const;
private:
const std::shared_ptr<ClassicGraphicsManager> _graphics_manager;
};

View File

@ -1,12 +1,13 @@
#include "classicnote.h"
#include "classictimeline.h"
#include "classicnotemanager.h"
#include <iostream>
ClassicTimeline::ClassicTimeline() :
ClassicTimeline::ClassicTimeline(const std::shared_ptr<ClassicNoteManager>& manager) :
_note_manager(manager),
_current_offset(0)
{}
void ClassicTimeline::run(std::vector<ClassicNote*>&& notes, const microsec& visibility)
void ClassicTimeline::setNotes(std::vector<ClassicNote *> &&notes, const microsec &visibility)
{
_visibility_offset = visibility;
_timeline = std::move(notes);
@ -48,7 +49,7 @@ void ClassicTimeline::checkCurrentActiveNote()
auto note = *_active_note;
if (!note->isActive())
if (!_note_manager->isActive(note))
{
expire(_active_note);
++_top_note;
@ -61,7 +62,7 @@ void ClassicTimeline::checkForNextActiveNote()
return;
auto top_note = *_top_note;
if (top_note->isActive())
if (_note_manager->isActive(top_note))
_active_note = _top_note;
}
@ -70,10 +71,11 @@ void ClassicTimeline::updateVisibleSprites(const microsec& music_offset)
if (nothingToDraw())
return;
const auto& note_manager = _note_manager;
std::for_each(_first_visible_note, _last_visible_note,
[&music_offset](const auto& note)
[&note_manager, &music_offset](const auto& note)
{
note->update(music_offset);
note_manager->update(note, music_offset);
});
}
@ -113,8 +115,8 @@ void ClassicTimeline::findLastVisibleNote(const microsec &music_offset)
auto note = *note_iterator;
if (!note->isInGame())
note->putToGame(music_offset);
if (!_note_manager->isInGame(note))
_note_manager->putToGame(note, music_offset);
++note_iterator;
}
@ -131,7 +133,7 @@ void ClassicTimeline::findFirstVisibleNote()
while (note_iterator != _last_visible_note)
{
auto note = *note_iterator;
if (note->shouldRemove())
if (_note_manager->shouldRemove(note))
++_first_visible_note;
++note_iterator;
@ -148,10 +150,11 @@ void ClassicTimeline::drawVisibleNotes() const
if (nothingToDraw())
return;
const auto& note_manager = _note_manager;
std::for_each(_first_visible_note, _last_visible_note,
[](const auto& note)
[&note_manager](const auto& note)
{
note->draw();
note_manager->draw(note);
});
}

View File

@ -7,19 +7,18 @@
class ClassicNote;
class ClassicNoteManager;
class ClassicGraphicsManager;
class ClassicTimeline : public Timeline
{
public:
explicit ClassicTimeline();
explicit ClassicTimeline(const std::shared_ptr<ClassicNoteManager>& manager);
virtual ~ClassicTimeline();
virtual void update(const microsec& offset) override;
virtual void clear() override;
virtual void drawVisibleNotes() const override;
void run(std::vector<ClassicNote*>&& notes, const microsec& visibility);
void setNotes(std::vector<ClassicNote*>&& notes, const microsec& visibility);
void fetchVisibleNotes();
void findLastVisibleNote(const microsec& music_offset);
@ -33,6 +32,7 @@ public:
inline void expire(Iterator& iterator);
private:
std::shared_ptr<ClassicNoteManager> _note_manager;
std::vector<microsec> _input_intervals;
std::vector<ClassicNote*> _timeline;
microsec _visibility_offset;