From 8579dc55070f4e205d5dc113f388b2ebffcdb90c Mon Sep 17 00:00:00 2001 From: NaiJi Date: Mon, 4 Oct 2021 18:30:21 +0300 Subject: [PATCH] Implement logic for HoldManager --- modes/classicmode/game/classicarrownote.cpp | 10 ++++++ modes/classicmode/game/classicarrownote.h | 1 + modes/classicmode/game/classicgame.cpp | 27 +++++---------- modes/classicmode/game/classicgame.h | 7 ++-- modes/classicmode/game/classicmapcreator.cpp | 8 ++--- modes/classicmode/game/classicmapcreator.h | 13 +++---- modes/classicmode/game/context.h | 2 ++ modes/classicmode/game/holdmanager.cpp | 36 ++++++++++++++++++-- modes/classicmode/game/holdmanager.h | 10 ++++-- 9 files changed, 72 insertions(+), 42 deletions(-) diff --git a/modes/classicmode/game/classicarrownote.cpp b/modes/classicmode/game/classicarrownote.cpp index e884960..1efc94b 100644 --- a/modes/classicmode/game/classicarrownote.cpp +++ b/modes/classicmode/game/classicarrownote.cpp @@ -1,5 +1,6 @@ #include "classicarrownote.h" #include "classicgraphicsmanager.h" +#include "holdmanager.h" // Replace with interface by dependency injection #include "classicflyinganimationscenario.h" @@ -64,7 +65,11 @@ void ClassicArrowNote::input(PlayerInput&& inputdata) bool all_pressed = allElementsPressed(); if (all_pressed) + { grade = _evaluator.calculatePrecision(inputdata.timestamp); + if (isHold()) + _context->hold_manager->emplace(this); + } if (all_pressed || !input_valid) { @@ -138,3 +143,8 @@ bool ClassicArrowNote::isPressedAs(sf::Keyboard::Key key) const return key == element.pressed_as; }); } + +bool ClassicArrowNote::isHold() const +{ + return _is_hold; +} diff --git a/modes/classicmode/game/classicarrownote.h b/modes/classicmode/game/classicarrownote.h index 2ac8bc1..066e54d 100644 --- a/modes/classicmode/game/classicarrownote.h +++ b/modes/classicmode/game/classicarrownote.h @@ -16,6 +16,7 @@ public: bool allElementsPressed() const; bool isPressedAs(sf::Keyboard::Key key) const; + inline bool isHold() const; private: diff --git a/modes/classicmode/game/classicgame.cpp b/modes/classicmode/game/classicgame.cpp index d988d47..bf50681 100644 --- a/modes/classicmode/game/classicgame.cpp +++ b/modes/classicmode/game/classicgame.cpp @@ -1,14 +1,17 @@ #include "classicgame.h" #include "classicnote.h" #include "classicmapcreator.h" +#include "holdmanager.h" +#include "context.h" #include "tools/music.h" ClassicGame::ClassicGame(std::shared_ptr&& manager, std::unique_ptr&& music) : _graphics_manager(std::move(manager)), + _hold_manager(std::make_shared(_graphics_manager)), _music(std::move(music)), _is_paused(false) { - _slap_buffer.loadFromFile("very-final-slap.wav"); + _slap_buffer.loadFromFile("Tick.ogg"); _slap.setBuffer(_slap_buffer); _slap.setVolume(50); @@ -56,7 +59,9 @@ ClassicGame::~ClassicGame() void ClassicGame::run() { - ClassicMapCreator creator(_graphics_manager); + const auto context = std::make_shared(Context{_graphics_manager, _hold_manager}); + + ClassicMapCreator creator(context); auto beatmap = creator.createBeatmap("aa"); _music->openFromFile("METEOR.flac"); _music->setVolume(10); @@ -99,29 +104,13 @@ void ClassicGame::input(PlayerInput&& inputdata) auto note = (*note_it); note->input(std::move(inputdata)); _slap.play(); - - /*if (note->isHold() && note->allElementsPressed()) // also check for Type - { - _notes_on_hold.emplace_back(note); - std::cout << "HOLD initited by " << inputdata.event.key.code << '\n'; - }*/ } } break; case sf::Event::KeyReleased: { - /*bool key_match = std::any_of(_notes_on_hold.begin(), _notes_on_hold.end(), - [key=inputdata.event.key.code](const auto& note) - { - return note->isPressedAs(key); - }); - - if (key_match) - { - _notes_on_hold.clear(); - std::cout << "HOLD released by " << inputdata.event.key.code << '\n'; - }*/ + _hold_manager->checkRelease(inputdata.event.key.code); } break; diff --git a/modes/classicmode/game/classicgame.h b/modes/classicmode/game/classicgame.h index 885e4a3..81297d2 100644 --- a/modes/classicmode/game/classicgame.h +++ b/modes/classicmode/game/classicgame.h @@ -1,5 +1,4 @@ -#ifndef CLASSICGAME_H -#define CLASSICGAME_H +#pragma once #include #include @@ -15,6 +14,7 @@ class Music; class ClassicGraphicsManager; +class HoldManager; class ClassicGame final : public Game { @@ -34,6 +34,7 @@ private: std::map _buttons_to_released_actions; std::shared_ptr _graphics_manager; + std::shared_ptr _hold_manager; Timeline _timeline; sf::SoundBuffer _slap_buffer; @@ -43,5 +44,3 @@ private: bool _is_paused; }; - -#endif // CLASSICGAME_H diff --git a/modes/classicmode/game/classicmapcreator.cpp b/modes/classicmode/game/classicmapcreator.cpp index 65d1c3e..a2027b2 100644 --- a/modes/classicmode/game/classicmapcreator.cpp +++ b/modes/classicmode/game/classicmapcreator.cpp @@ -6,8 +6,8 @@ #include "classicdyinganimationscenario.h" // -ClassicMapCreator::ClassicMapCreator(const std::shared_ptr& manager) : - _graphics_manager(manager) +ClassicMapCreator::ClassicMapCreator(const std::shared_ptr &context) : + _context(context) {} Beatmap ClassicMapCreator::createBeatmap(const std::string& filepath) const @@ -33,8 +33,6 @@ Beatmap ClassicMapCreator::createBeatmap(const std::string& filepath) const int counter = 3; - const auto context = std::make_shared(Context{_graphics_manager}); - while (bpm_iterator < bpm_end) { ArrowNoteInitializer init; @@ -42,7 +40,7 @@ Beatmap ClassicMapCreator::createBeatmap(const std::string& filepath) const init.initializer.intervals = input_intervals; init.initializer.perfect_offset = bpm_iterator; init.hold = false; - init.initializer.context = context; + init.initializer.context = _context; element.element.coordinates = {x, 390.}; element.element.falling_curve_interpolation = {}; diff --git a/modes/classicmode/game/classicmapcreator.h b/modes/classicmode/game/classicmapcreator.h index d96f591..7ede8bb 100644 --- a/modes/classicmode/game/classicmapcreator.h +++ b/modes/classicmode/game/classicmapcreator.h @@ -1,12 +1,11 @@ -#ifndef CLASSICMAPCREATOR_H -#define CLASSICMAPCREATOR_H +#pragma once #include #include "tools/mathutils.h" -#include "core/note.h" +#include "classicnote.h" -#include "classicgraphicsmanager.h" +struct Context; struct Beatmap { @@ -17,12 +16,10 @@ struct Beatmap class ClassicMapCreator { public: - explicit ClassicMapCreator(const std::shared_ptr& manager); + explicit ClassicMapCreator(const std::shared_ptr& context); Beatmap createBeatmap(const std::string& filepath) const; private: - const std::shared_ptr _graphics_manager; + const std::shared_ptr _context; }; - -#endif // CLASSICMAPCREATOR_H diff --git a/modes/classicmode/game/context.h b/modes/classicmode/game/context.h index 5d53823..8d977a9 100644 --- a/modes/classicmode/game/context.h +++ b/modes/classicmode/game/context.h @@ -3,8 +3,10 @@ #include class ClassicGraphicsManager; +class HoldManager; struct Context { std::shared_ptr graphics_manager; + std::shared_ptr hold_manager; }; diff --git a/modes/classicmode/game/holdmanager.cpp b/modes/classicmode/game/holdmanager.cpp index 4e80bc1..161a12e 100644 --- a/modes/classicmode/game/holdmanager.cpp +++ b/modes/classicmode/game/holdmanager.cpp @@ -1,6 +1,36 @@ #include "holdmanager.h" +#include "classicarrownote.h" +#include -/* THIS IS SIDEQUEST!!!! Right now I am working on the Editor >:C - * - * */ +HoldManager::HoldManager(const std::shared_ptr& graphics_manager) : + _graphics_manager(graphics_manager) +{} + +void HoldManager::emplace(ClassicArrowNote* note) +{ + _notes_on_hold.emplace_back(note); +} + +void HoldManager::checkRelease(sf::Keyboard::Key released_key) +{ + bool key_match = std::any_of(_notes_on_hold.begin(), _notes_on_hold.end(), + [released_key](const auto& note) + { + return note->isPressedAs(released_key); + }); + + if (key_match) + _notes_on_hold.clear(); +} + +void HoldManager::drawHoldBar() +{ + if (_notes_on_hold.empty()) + return; + + /* taking proxy sprites for notes on hold + * and drawing on centered bar */ + + // _graphics_manager-> . . . +} diff --git a/modes/classicmode/game/holdmanager.h b/modes/classicmode/game/holdmanager.h index 29ffbe3..577ded8 100644 --- a/modes/classicmode/game/holdmanager.h +++ b/modes/classicmode/game/holdmanager.h @@ -2,17 +2,21 @@ #include "core/inputtype.h" #include +#include class ClassicArrowNote; +class ClassicGraphicsManager; -class HoldManager // Not important now +class HoldManager { public: - explicit HoldManager() = default; - + explicit HoldManager(const std::shared_ptr& graphics_manager); void emplace(ClassicArrowNote* note); void checkRelease(sf::Keyboard::Key released_key); + void drawHoldBar(); + private: std::vector _notes_on_hold; + std::shared_ptr _graphics_manager; };