From 8c3f2a112f5dc8aac15107d9fd3e922d041f38e4 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Tue, 10 Aug 2021 22:08:45 +0300 Subject: [PATCH] Separate ClassicNoteManager logic from ClassicNote, make it run --- src/classicgame/classicgame.cpp | 9 ++--- src/classicgame/classicgame.h | 2 +- src/classicgame/classicmapcreator.cpp | 49 ++++++++++++++++++-------- src/classicgame/classicnotemanager.cpp | 14 ++------ src/classicgame/classicnotemanager.h | 2 -- src/classicgame/classictimeline.cpp | 27 +++++++------- src/classicgame/classictimeline.h | 6 ++-- 7 files changed, 61 insertions(+), 48 deletions(-) diff --git a/src/classicgame/classicgame.cpp b/src/classicgame/classicgame.cpp index be6dbdd..c68fc3b 100644 --- a/src/classicgame/classicgame.cpp +++ b/src/classicgame/classicgame.cpp @@ -7,9 +7,9 @@ #include ClassicGame::ClassicGame(std::shared_ptr&& manager, std::unique_ptr&& music) : - _timeline(std::make_unique()), _graphics_manager(std::move(manager)), _note_manager(std::make_unique(_graphics_manager)), + _timeline(std::make_unique(_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) + [¬e_manager, key=inputdata.event.key.code](const auto& note) { - return note->isPressedAs(key); + return note_manager->isPressedAs(note, key); }); if (key_match) diff --git a/src/classicgame/classicgame.h b/src/classicgame/classicgame.h index 7dd687e..0604eb1 100644 --- a/src/classicgame/classicgame.h +++ b/src/classicgame/classicgame.h @@ -33,9 +33,9 @@ private: std::vector _notes_on_hold; - std::unique_ptr _timeline; std::shared_ptr _graphics_manager; std::shared_ptr _note_manager; + std::unique_ptr _timeline; sf::SoundBuffer _slap_buffer; sf::Sound _slap; diff --git a/src/classicgame/classicmapcreator.cpp b/src/classicgame/classicmapcreator.cpp index 4cf5155..41ee3c6 100644 --- a/src/classicgame/classicmapcreator.cpp +++ b/src/classicgame/classicmapcreator.cpp @@ -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& 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(); + note->elements[0].animations[ClassicNoteState::ACTIVE] = note->elements[0].animations[ClassicNoteState::FLYING]; + note->elements[0].animations[ClassicNoteState::DYING] = std::make_shared(); + 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; diff --git a/src/classicgame/classicnotemanager.cpp b/src/classicgame/classicnotemanager.cpp index 028dd8a..9d33536 100644 --- a/src/classicgame/classicnotemanager.cpp +++ b/src/classicgame/classicnotemanager.cpp @@ -7,6 +7,7 @@ #include "classicdyinganimationscenario.h" // + ClassicNoteManager::ClassicNoteManager(const std::shared_ptr& 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, 5>& animations) const -{ - animations[ClassicNoteState::NONE] = nullptr; - animations[ClassicNoteState::FLYING] = std::make_shared(); - animations[ClassicNoteState::ACTIVE] = animations[ClassicNoteState::FLYING]; - animations[ClassicNoteState::DYING] = std::make_shared(); - animations[ClassicNoteState::DEAD] = nullptr; -} diff --git a/src/classicgame/classicnotemanager.h b/src/classicgame/classicnotemanager.h index f7353bc..572c7fd 100644 --- a/src/classicgame/classicnotemanager.h +++ b/src/classicgame/classicnotemanager.h @@ -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, 5>& animations) const; - private: const std::shared_ptr _graphics_manager; }; diff --git a/src/classicgame/classictimeline.cpp b/src/classicgame/classictimeline.cpp index f250733..eae3e32 100644 --- a/src/classicgame/classictimeline.cpp +++ b/src/classicgame/classictimeline.cpp @@ -1,12 +1,13 @@ -#include "classicnote.h" #include "classictimeline.h" +#include "classicnotemanager.h" #include -ClassicTimeline::ClassicTimeline() : +ClassicTimeline::ClassicTimeline(const std::shared_ptr& manager) : + _note_manager(manager), _current_offset(0) {} -void ClassicTimeline::run(std::vector&& notes, const microsec& visibility) +void ClassicTimeline::setNotes(std::vector &¬es, 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) + [¬e_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) + [¬e_manager](const auto& note) { - note->draw(); + note_manager->draw(note); }); } diff --git a/src/classicgame/classictimeline.h b/src/classicgame/classictimeline.h index 4cc8d1c..1cedfce 100644 --- a/src/classicgame/classictimeline.h +++ b/src/classicgame/classictimeline.h @@ -7,19 +7,18 @@ class ClassicNote; class ClassicNoteManager; -class ClassicGraphicsManager; class ClassicTimeline : public Timeline { public: - explicit ClassicTimeline(); + explicit ClassicTimeline(const std::shared_ptr& manager); virtual ~ClassicTimeline(); virtual void update(const microsec& offset) override; virtual void clear() override; virtual void drawVisibleNotes() const override; - void run(std::vector&& notes, const microsec& visibility); + void setNotes(std::vector&& 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 _note_manager; std::vector _input_intervals; std::vector _timeline; microsec _visibility_offset;