Adapt graphics manager to GameContext

This commit is contained in:
NaiJi ✨ 2022-05-17 07:22:18 +03:00
parent c2b23a4c59
commit 0d97e61f4b
17 changed files with 258 additions and 283 deletions

View File

@ -38,7 +38,7 @@ void EditorState::display() const
void EditorState::enter()
{
_music = _core_factory->getMusic();
_music->open("sources/METEOR.flac");
_music->open("resources/METEOR.flac");
_music->setVolume(5.f);
auto& group = _group;

View File

@ -41,7 +41,7 @@ void GameState::enter()
{
_game->run();
_music->open("sources/METEOR.flac");
_music->open("resources/METEOR.flac");
_music->setVolume(10);
_music->play();
}

View File

@ -2,7 +2,7 @@
int main()
{
sf::RenderWindow window(sf::VideoMode{1280, 720}, "Project Kyoku", sf::Style::Default);
sf::RenderWindow window(sf::VideoMode{1280, 720}, "Project Kyoku", sf::Style::Fullscreen);
ApplicationSFML app(&window);
if (app.init())
app.run();

View File

@ -6,6 +6,7 @@
#include "core/timeline.h"
#include "game/classicgame.h"
#include "game/gamecontext.h"
#include "game/holdmanager.h"
#include "editor/classiceditor.h"
#include "editor/selectionmanager.h"
@ -17,7 +18,7 @@ std::unique_ptr<kku::Game> classic::getGame(const std::shared_ptr<kku::CoreFacto
const auto factory = std::make_shared<ClassicGraphicsFactory>(core_factory);
const auto timeline = std::make_shared<kku::Timeline<ClassicNote>>();
const auto graphics_manager = std::make_shared<ClassicSceneGraphicsManager<ClassicNote>>(timeline, factory, visibility_offset);
const auto graphics_manager = std::make_shared<ClassicSceneGraphicsManager>(timeline, factory, visibility_offset);
const auto hold_manager = std::make_shared<HoldManager>();
const auto context = std::make_shared<GameContext>(hold_manager, graphics_manager);
@ -32,7 +33,7 @@ std::unique_ptr<kku::Editor> classic::getEditor(const std::shared_ptr<kku::CoreF
const auto factory = std::make_shared<ClassicGraphicsFactory>(core_factory);
const auto timeline = std::make_shared<kku::Timeline<ClassicNote>>();
const auto graphics_manager = std::make_shared<ClassicSceneGraphicsManager<ClassicNote>>(timeline, factory, visibility_offset);
const auto graphics_manager = std::make_shared<ClassicSceneGraphicsManager>(timeline, factory, visibility_offset);
return std::make_unique<ClassicEditor>(timeline, graphics_manager);
return nullptr;//std::make_unique<ClassicEditor>(timeline, graphics_manager);
}

View File

@ -20,7 +20,7 @@ ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>&
_current_time(0),
_scroll_step(500000)
{
kku::microsec starting_beat_offset = 402162;
/*kku::microsec starting_beat_offset = 402162;
int amount_of_beats = 209;
kku::microsec interval = 1412162;
kku::microsec tempo_interval = interval / 4;
@ -78,7 +78,7 @@ ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>&
x = 90.;
}
_timeline->setNotes(notes);
_timeline->setNotes(notes);*/
}
kku::microsec ClassicEditor::adjustOffset(kku::microsec offset) const noexcept
@ -92,7 +92,7 @@ kku::microsec ClassicEditor::adjustOffset(kku::microsec offset) const noexcept
void ClassicEditor::input(kku::GameEvent&& input)
{
_current_time = input.timestamp;
const auto& event = input.event;
//const auto& event = input.event;
switch (input.event.type)
{
@ -122,7 +122,7 @@ void ClassicEditor::input(kku::GameEvent&& input)
case kku::SystemEvent::Type::MousePress:
{
const auto note = _timeline->getNoteBy(_current_time);
/*const auto note = _timeline->getNoteBy(_current_time);
if (_timeline->isExpired(note) && !_bpm_sections.empty() && _current_time >= (*_bpm_sections.begin()).offset_start)
{
ArrowNoteInitializer init;
@ -147,7 +147,7 @@ void ClassicEditor::input(kku::GameEvent&& input)
if (!_selection_manager->isMultiselectionEnabled())
_selection_manager->discard();
_graphics_manager->input(std::move(input));
_graphics_manager->input(std::move(input));*/
break;
}
@ -158,7 +158,6 @@ void ClassicEditor::input(kku::GameEvent&& input)
void ClassicEditor::update(kku::UpdateData&& updatedata)
{
_timeline->update(updatedata.timestamp);
_graphics_manager->update(updatedata.timestamp);
}
void ClassicEditor::display() const

View File

@ -4,7 +4,8 @@
ClassicArrowNote::ClassicArrowNote(Init&& init) :
ClassicNote(init.perfect_offset),
_evaluator(init.intervals, init.perfect_offset),
_context(init.context)
_context(init.context),
_is_holding(init.is_holding)
{
_elements.resize(init.elements.size());
@ -32,6 +33,11 @@ void ClassicArrowNote::input(kku::GameEvent&& input)
_context->input(this, std::move(input));
}
bool ClassicArrowNote::isHold() const
{
return _is_holding;
}
std::vector<ArrowElement>& ClassicArrowNote::getElements()
{
return _elements;
@ -41,3 +47,12 @@ auto ClassicArrowNote::calculatePrecision(const kku::microsec& offset) const ->
{
return _evaluator.calculatePrecision(offset);
}
bool ClassicArrowNote::isPressedAs(kku::SystemEvent::Key::Code key) const
{
return std::any_of(_elements.begin(), _elements.end(),
[key](const auto& element)
{
return key == element.pressed_as;
});
}

View File

@ -14,11 +14,13 @@ public:
struct Init
{
Context * const context;
const std::shared_ptr<const Context> context;
const kku::microsec perfect_offset = 0;
const std::vector<kku::microsec>& intervals = {};
const std::vector<ArrowElement>& elements = {};
const bool is_holding = false;
};
enum class Grade
@ -37,9 +39,12 @@ public:
bool isHold() const;
std::vector<ArrowElement>& getElements();
Grade calculatePrecision(const kku::microsec& offset) const;
bool isPressedAs(kku::SystemEvent::Key::Code key) const;
private:
const kku::PrecisionEvaluator<Grade> _evaluator;
std::vector<ArrowElement> _elements;
const Context * const _context;
const std::shared_ptr<const Context> _context;
bool _is_holding;
};

View File

@ -2,7 +2,6 @@
#include "classicmode/classicnote.h"
#include "classicmapcreator.h"
#include "gamecontext.h"
#include "graphics/classicscenegraphicsmanager.h"
#include "holdmanager.h"
ClassicGame::ClassicGame(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
@ -61,10 +60,9 @@ void ClassicGame::update(kku::UpdateData&& updatedata)
}*/
_timeline->update(updatedata.timestamp);
_context->getGraphicsManager()->update(updatedata.timestamp);
}
void ClassicGame::display() const
{
_context->getGraphicsManager()->display();
_context->display();
}

View File

@ -8,7 +8,6 @@
#include "classicmode/classicnote.h"
#include "classicmode/classicactions.h"
class ClassicGraphicsManager;
class HoldManager;
class GameContext;

View File

@ -1,12 +1,13 @@
#include "classicmapcreator.h"
#include "classicarrownote.h"
#include "gamecontext.h"
// Replace with interface by dependency injection
#include "graphics/animations/classicflyinganimationscenario.h"
#include "graphics/animations/classicdyinganimationscenario.h"
//
auto classic::createBeatmap(const std::string& filepath, Context * const context) -> Beatmap
auto classic::createBeatmap(const std::string& filepath, const std::shared_ptr<GameContext>& context) -> Beatmap
{
(void) filepath;
@ -31,39 +32,40 @@ auto classic::createBeatmap(const std::string& filepath, Context * const context
while (bpm_iterator < bpm_end)
{
ArrowElement element;
element.position = kku::Point(x, 390.f);
element.falling_curve_interpolation = {};
element.keys = {kku::SystemEvent::Key::Code::W,
kku::SystemEvent::Key::Code::Up};
element.type = Type::UP;
bool hold = false;
if (counter == 0)
{
hold = true;
element.keys = {kku::SystemEvent::Key::Code::D,
kku::SystemEvent::Key::Code::Right};
element.type = Type::RIGHT;
counter = 13;
}
--counter;
ClassicArrowNote::Init init
{
context,
bpm_iterator,
input_intervals,
{element},
hold
};
ArrowElementInitializer element;
init.initializer.intervals = input_intervals;
init.initializer.perfect_offset = bpm_iterator;
init.hold = false;
element.element.position = kku::Point(x, 390.f);
element.element.falling_curve_interpolation = {};
element.keys = {kku::SystemEvent::Key::Code::W,
kku::SystemEvent::Key::Code::Up};
element.element.type = Type::UP;
if (counter == 0)
{
init.hold = true;
element.keys = {kku::SystemEvent::Key::Code::D,
kku::SystemEvent::Key::Code::Right};
element.element.type = Type::RIGHT;
}
--counter;
init.elements = {element};
notes.insert(new ClassicArrowNote(std::move(init)));

View File

@ -7,7 +7,7 @@
#include "core/time.h"
#include "classicmode/classicnote.h"
class Context;
class GameContext;
namespace classic
{
@ -18,7 +18,6 @@ struct Beatmap
kku::microsec visibility_offset;
};
Beatmap createBeatmap(const std::string& filepath, Context * const context);
Beatmap createBeatmap(const std::string& filepath, const std::shared_ptr<GameContext>& context);
}

View File

@ -2,6 +2,7 @@
#include "game/classicarrownote.h"
#include "game/holdmanager.h"
#include "graphics/animations/classicanimationscenario.h"
#include "graphics/classicgraphicsmanager.h"
GameContext::GameContext(const std::shared_ptr<HoldManager>& hold_manager,
const std::shared_ptr<ClassicGraphicsManager>& graphics_manager) :
@ -90,7 +91,7 @@ std::shared_ptr<HoldManager> GameContext::getHoldManager() const
return _hold_manager;
}
std::shared_ptr<ClassicGraphicsManager> GameContext::getGraphicsManager() const
void GameContext::display() const
{
return _graphics_manager;
_graphics_manager->display();
}

View File

@ -14,10 +14,9 @@ public:
virtual void input(ClassicArrowNote *note, kku::GameEvent&& input) const override;
virtual void update(ClassicArrowNote *note, const kku::microsec &music_offset) const override;
virtual void display() const override;
std::shared_ptr<HoldManager> getHoldManager() const;
std::shared_ptr<ClassicGraphicsManager> getGraphicsManager() const;
private:
const std::shared_ptr<HoldManager> _hold_manager;

View File

@ -5,6 +5,9 @@
#include <memory>
class ClassicArrowNote;
// class ClassicSliderNote;
class ClassicGraphicsManager : public std::enable_shared_from_this<ClassicGraphicsManager>
{
public:
@ -15,7 +18,8 @@ public:
virtual void input(kku::GameEvent&& input) = 0;
virtual void display() const = 0;
virtual void update(const kku::microsec& offset) = 0;
virtual void update(const kku::microsec& offset, ClassicArrowNote* note) = 0;
// virtual void update(const kku::microsec& offset, ClassicSliderNote* note) = 0;
protected:
kku::microsec _visibility_offset;

View File

@ -0,0 +1,166 @@
#include "classicscenegraphicsmanager.h"
ClassicSceneGraphicsManager::ClassicSceneGraphicsManager(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
const std::shared_ptr<ClassicGraphicsFactory>& factory,
const kku::microsec& visibility_offset) :
ClassicGraphicsManager(visibility_offset),
_sprite_container({Type::UP, Type::DOWN,
Type::LEFT, Type::RIGHT},
factory),
_factory(factory),
_timeline(timeline)
{
_timeline->expire(_first);
_timeline->expire(_last);
}
void ClassicSceneGraphicsManager::input(kku::GameEvent&& input)
{
if (nothingToDraw())
return;
for (auto it = _first; it != _last; ++it)
{
(*it)->input(std::move(input));
}
}
void ClassicSceneGraphicsManager::display() const
{
if (nothingToDraw())
return;
for (auto it = _first; it != _last; ++it)
{
//display((*it)->getElements());
}
}
void ClassicSceneGraphicsManager::update(const kku::microsec& offset, ClassicArrowNote* note)
{
//TODO
(void)note;
fetchLastNote(offset);
fetchFirstNote(offset);
updateVisibleNotes(offset);
}
void ClassicSceneGraphicsManager::display(const std::vector<ArrowElement>& elements) const
{
for (std::size_t i = 0; i < elements.size(); ++i)
{
const auto& sprite = elements[i].sprite;
if (i >= 1)
{
//const auto& neighbor_sprite = elements[i - 1].sprite;
//const auto c1 = neighbor_sprite->trailPosition();
//const auto c2 = sprite->trailPosition();
//_render_target->draw(makeLine(c1, c2));
}
sprite->display();
}
}
void ClassicSceneGraphicsManager::setGraphics(std::vector<ArrowElement>& elements, kku::TimeRange&& range)
{
for (auto& element : elements)
{
element.sprite = _sprite_container.getSprite(element.type);
element.sprite->setPosition(element.position);
element.sprite->setTrailPosition(kku::Point( 0.f, 9.f ));
element.animations[ClassicNote::State::NONE] = nullptr;
element.animations[ClassicNote::State::FLYING] = std::make_shared<ClassicFlyingAnimationScenario>();
element.animations[ClassicNote::State::DYING] = std::make_shared<ClassicDyingAnimationScenario>();
element.animations[ClassicNote::State::DEAD] = nullptr;
element.animations[ClassicNote::State::FLYING]->launch(element.sprite, range.begin, range.end);
}
}
bool ClassicSceneGraphicsManager::nothingToDraw() const noexcept
{
return _timeline->isExpired(_first)
|| _timeline->isExpired(_last);
}
bool ClassicSceneGraphicsManager::isVisiblyClose(const Iterator& iterator, const kku::microsec& music_offset) const noexcept
{
return ((*iterator)->getPerfectOffset() - _visibility_offset) <= music_offset;
}
void ClassicSceneGraphicsManager::fetchFirstNote(const kku::microsec& offset)
{
if (nothingToDraw())
return;
if (offset < (*_first)->getPerfectOffset())
{
Iterator note_iterator = _first;
while (note_iterator != _timeline->begin() && isVisiblyClose(note_iterator, offset))
{
--note_iterator;
}
_first = note_iterator;
auto note = *_first;
if (note->getState() != ClassicNote::State::FLYING
&& note->getState() != ClassicNote::State::DYING
&& offset <= note->getPerfectOffset())
{
note->setState(ClassicNote::State::FLYING);
//setGraphics(note, kku::TimeRange{note->getPerfectOffset() - _visibility_offset, note->getPerfectOffset()});
}
}
else
{
Iterator note_iterator = _first;
while (note_iterator != _last)
{
auto note = *note_iterator;
if (note->getState() == ClassicNote::State::DEAD)
{
// note->removeGraphics(this);
++_first;
}
++note_iterator;
}
}
}
void ClassicSceneGraphicsManager::fetchLastNote(const kku::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->getState() != ClassicNote::State::FLYING
&& note->getState() != ClassicNote::State::DYING
&& offset <= note->getPerfectOffset())
{
note->setState(ClassicNote::State::FLYING);
//note->setGraphics(this, kku::TimeRange{note->getPerfectOffset() - _visibility_offset, note->getPerfectOffset()});
}
++note_iterator;
}
_last = note_iterator;
}
void ClassicSceneGraphicsManager::updateVisibleNotes(const kku::microsec& offset)
{
for (auto it = _first; it != _last; ++it)
(*it)->update(offset);
}

View File

@ -11,248 +11,34 @@
#include "graphics/animations/classicflyinganimationscenario.h"
#include "graphics/animations/classicdyinganimationscenario.h"
template <class TNote, class = std::enable_if_t<std::is_base_of<kku::Note, TNote>::value>>
class ClassicSceneGraphicsManager : public ClassicGraphicsManager
{
public:
explicit ClassicSceneGraphicsManager(const std::shared_ptr<kku::Timeline<TNote>>& timeline,
explicit ClassicSceneGraphicsManager(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
const std::shared_ptr<ClassicGraphicsFactory>& factory,
const kku::microsec& visibility_offset) :
ClassicGraphicsManager(visibility_offset),
_sprite_container({Type::UP, Type::DOWN,
Type::LEFT, Type::RIGHT},
factory),
_factory(factory),
_timeline(timeline)
{
_timeline->expire(_first);
_timeline->expire(_last);
}
const kku::microsec& visibility_offset);
virtual void input(kku::GameEvent&& input) override
{
if (nothingToDraw())
return;
virtual void input(kku::GameEvent&& input) override;
virtual void display() const override;
for (auto it = _first; it != _last; ++it)
{
(*it)->input(std::move(input));
}
}
virtual void display() const override
{
if (nothingToDraw())
return;
for (auto it = _first; it != _last; ++it)
{
//display((*it)->getElements());
}
}
virtual void update(const kku::microsec& offset) override
{
fetchLastNote(offset);
fetchFirstNote(offset);
updateVisibleNotes(offset);
}
virtual void display(const std::vector<ArrowElement>& elements) const
{
for (std::size_t i = 0; i < elements.size(); ++i)
{
const auto& sprite = elements[i].sprite;
if (i >= 1)
{
//const auto& neighbor_sprite = elements[i - 1].sprite;
//const auto c1 = neighbor_sprite->trailPosition();
//const auto c2 = sprite->trailPosition();
//_render_target->draw(makeLine(c1, c2));
}
sprite->display();
}
}
virtual void setGraphics(std::vector<ArrowElement>& elements, kku::TimeRange&& range)
{
for (auto& element : elements)
{
element.sprite = _sprite_container.getSprite(element.type);
element.sprite->setPosition(element.position);
element.sprite->setTrailPosition(kku::Point( 0.f, 9.f ));
element.animations[ClassicNote::State::NONE] = nullptr;
element.animations[ClassicNote::State::FLYING] = std::make_shared<ClassicFlyingAnimationScenario>();
element.animations[ClassicNote::State::DYING] = std::make_shared<ClassicDyingAnimationScenario>();
element.animations[ClassicNote::State::DEAD] = nullptr;
element.animations[ClassicNote::State::FLYING]->launch(element.sprite, range.begin, range.end);
}
}
virtual void removeGraphics(std::vector<ArrowElement>& elements)
{
for (auto& element : elements)
{
_sprite_container.resetSprite(element.sprite, element.type);
element.sprite = nullptr;
element.animations[ClassicNote::State::NONE] = nullptr;
element.animations[ClassicNote::State::FLYING] = nullptr;
element.animations[ClassicNote::State::DYING] = nullptr;
element.animations[ClassicNote::State::DEAD] = nullptr;
}
}
virtual void display(const std::vector<MockElement>& elements) const
{
for (std::size_t i = 0; i < elements.size(); ++i)
{
const auto& sprite = elements[i].sprite;
if (i >= 1)
{
//const auto& neighbor_sprite = elements[i - 1].sprite;
//const auto c1 = neighbor_sprite->trailPosition();
//const auto c2 = sprite->trailPosition();
//_render_target->draw(makeLine(c1, c2));
}
sprite->display();
}
}
virtual void setGraphics(std::vector<MockElement>& elements, kku::TimeRange&& range)
{
for (auto& element : elements)
{
element.sprite = _sprite_container.getSprite(element.type);
element.sprite->setPosition(element.position);
element.sprite->setTrailPosition(kku::Point( 0.f, 9.f ));
//element.selection = _factory->createSelection();
//element.selection->adjustTo(element.sprite);
element.animations[ClassicNote::State::NONE] = nullptr;
element.animations[ClassicNote::State::FLYING] = std::make_shared<ClassicFlyingAnimationScenario>();
element.animations[ClassicNote::State::DYING] = std::make_shared<ClassicDyingAnimationScenario>();
element.animations[ClassicNote::State::DEAD] = nullptr;
element.animations[ClassicNote::State::FLYING]->launch(element.sprite, range.begin, range.end);
}
}
virtual void removeGraphics(std::vector<MockElement>& elements)
{
for (auto& element : elements)
{
_sprite_container.resetSprite(element.sprite, element.type);
element.sprite = nullptr;
element.animations[ClassicNote::State::NONE] = nullptr;
element.animations[ClassicNote::State::FLYING] = nullptr;
element.animations[ClassicNote::State::DYING] = nullptr;
element.animations[ClassicNote::State::DEAD] = nullptr;
}
}
virtual void update(const kku::microsec& offset, ClassicArrowNote* note) override;
virtual void display(const std::vector<ArrowElement>& elements) const;
virtual void setGraphics(std::vector<ArrowElement>& elements, kku::TimeRange&& range);
protected:
kku::SpriteContainer<Type, ClassicGraphicsFactory, ClassicNoteGraphics> _sprite_container;
const std::shared_ptr<const ClassicGraphicsFactory> _factory;
typedef typename std::set<TNote*>::const_iterator Iterator;
typedef typename std::set<ClassicNote*>::const_iterator Iterator;
Iterator _first;
Iterator _last;
const std::shared_ptr<kku::Timeline<TNote>> _timeline;
const std::shared_ptr<kku::Timeline<ClassicNote>> _timeline;
inline bool nothingToDraw() const noexcept
{
return _timeline->isExpired(_first)
|| _timeline->isExpired(_last);
}
inline bool isVisiblyClose(const Iterator& iterator, const kku::microsec& music_offset) const noexcept
{
return ((*iterator)->getPerfectOffset() - _visibility_offset) <= music_offset;
}
void fetchFirstNote(const kku::microsec& offset)
{
if (nothingToDraw())
return;
if (offset < (*_first)->getPerfectOffset())
{
Iterator note_iterator = _first;
while (note_iterator != _timeline->begin() && isVisiblyClose(note_iterator, offset))
{
--note_iterator;
}
_first = note_iterator;
auto note = *_first;
if (note->getState() != ClassicNote::State::FLYING
&& note->getState() != ClassicNote::State::DYING
&& offset <= note->getPerfectOffset())
{
note->setState(ClassicNote::State::FLYING);
//setGraphics(note, kku::TimeRange{note->getPerfectOffset() - _visibility_offset, note->getPerfectOffset()});
}
}
else
{
Iterator note_iterator = _first;
while (note_iterator != _last)
{
auto note = *note_iterator;
if (note->getState() == ClassicNote::State::DEAD)
{
// note->removeGraphics(this);
++_first;
}
++note_iterator;
}
}
}
void fetchLastNote(const kku::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->getState() != ClassicNote::State::FLYING
&& note->getState() != ClassicNote::State::DYING
&& offset <= note->getPerfectOffset())
{
note->setState(ClassicNote::State::FLYING);
//note->setGraphics(this, kku::TimeRange{note->getPerfectOffset() - _visibility_offset, note->getPerfectOffset()});
}
++note_iterator;
}
_last = note_iterator;
}
void updateVisibleNotes(const kku::microsec& offset)
{
for (auto it = _first; it != _last; ++it)
(*it)->update(offset);
}
inline bool nothingToDraw() const noexcept;
inline bool isVisiblyClose(const Iterator& iterator, const kku::microsec& music_offset) const noexcept;
void fetchFirstNote(const kku::microsec& offset);
void fetchLastNote(const kku::microsec& offset);
void updateVisibleNotes(const kku::microsec& offset);
};

View File

@ -12,4 +12,5 @@ public:
virtual void input(ClassicArrowNote *note, kku::GameEvent&& input) const = 0;
virtual void update(ClassicArrowNote *note, const kku::microsec &music_offset) const = 0;
virtual void display() const = 0;
};