Separate ClassicNoteManager logic from ClassicNote, make it run

selection
NaiJi ✨ 3 years ago
parent a223b7253c
commit 8c3f2a112f

@ -7,9 +7,9 @@
#include <iostream> #include <iostream>
ClassicGame::ClassicGame(std::shared_ptr<ClassicGraphicsManager>&& manager, std::unique_ptr<Music>&& music) : ClassicGame::ClassicGame(std::shared_ptr<ClassicGraphicsManager>&& manager, std::unique_ptr<Music>&& music) :
_timeline(std::make_unique<ClassicTimeline>()),
_graphics_manager(std::move(manager)), _graphics_manager(std::move(manager)),
_note_manager(std::make_unique<ClassicNoteManager>(_graphics_manager)), _note_manager(std::make_unique<ClassicNoteManager>(_graphics_manager)),
_timeline(std::make_unique<ClassicTimeline>(_note_manager)),
_music(std::move(music)), _music(std::move(music)),
_is_paused(false) _is_paused(false)
{ {
@ -66,7 +66,7 @@ void ClassicGame::run()
_music->openFromFile("METEOR.flac"); _music->openFromFile("METEOR.flac");
_music->setVolume(10); _music->setVolume(10);
_music->play(); _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) void ClassicGame::input(PlayerInput&& inputdata)
@ -116,10 +116,11 @@ void ClassicGame::input(PlayerInput&& inputdata)
case sf::Event::KeyReleased: case sf::Event::KeyReleased:
{ {
const auto& note_manager = _note_manager;
bool key_match = std::any_of(_notes_on_hold.begin(), _notes_on_hold.end(), 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) if (key_match)

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

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

@ -7,6 +7,7 @@
#include "classicdyinganimationscenario.h" #include "classicdyinganimationscenario.h"
// //
ClassicNoteManager::ClassicNoteManager(const std::shared_ptr<ClassicGraphicsManager>& manager) : ClassicNoteManager::ClassicNoteManager(const std::shared_ptr<ClassicGraphicsManager>& manager) :
_graphics_manager(manager) _graphics_manager(manager)
{} {}
@ -85,8 +86,8 @@ void ClassicNoteManager::input(ClassicNote* note, PlayerInput&& inputdata)
if (element.pressed) if (element.pressed)
return false; return false;
auto key_iterator = std::find(element.keys.begin(), element.keys.end(), inputdata.event.key.code); auto key_iterator = std::find(element.available_keys.begin(), element.available_keys.end(), inputdata.event.key.code);
bool found_key = key_iterator != element.keys.end(); bool found_key = key_iterator != element.available_keys.end();
if (found_key) if (found_key)
{ {
element.pressed = true; element.pressed = true;
@ -138,12 +139,3 @@ bool ClassicNoteManager::isPressedAs(const ClassicNote* note, sf::Keyboard::Key
return key == element.pressed_as; 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;
}

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

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

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

Loading…
Cancel
Save