From 5b7076ac1c5135ffe1ab046388bb0bf8c405a549 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Tue, 22 Jun 2021 21:20:08 +0300 Subject: [PATCH] Implement states --- include/note.h | 1 + src/classicgame/classicnote.cpp | 101 ++---------------- src/classicgame/classicnote.h | 10 +- .../classicnoteactivestate.cpp | 45 ++++++++ .../classicnotestate/classicnoteactivestate.h | 17 +++ .../classicnotedyingstate.cpp | 26 +++++ .../classicnotestate/classicnotedyingstate.h | 14 +++ .../classicnoteflyingstate.cpp | 49 +++++++++ .../classicnotestate/classicnoteflyingstate.h | 17 +++ .../classicnotestate/classicnotenonestate.cpp | 20 ++++ .../classicnotestate/classicnotenonestate.h | 14 +++ .../classicnotestate/classicnotestate.h | 1 + 12 files changed, 221 insertions(+), 94 deletions(-) create mode 100644 src/classicgame/classicnotestate/classicnoteactivestate.cpp create mode 100644 src/classicgame/classicnotestate/classicnoteactivestate.h create mode 100644 src/classicgame/classicnotestate/classicnotedyingstate.cpp create mode 100644 src/classicgame/classicnotestate/classicnotedyingstate.h create mode 100644 src/classicgame/classicnotestate/classicnoteflyingstate.cpp create mode 100644 src/classicgame/classicnotestate/classicnoteflyingstate.h create mode 100644 src/classicgame/classicnotestate/classicnotenonestate.cpp create mode 100644 src/classicgame/classicnotestate/classicnotenonestate.h diff --git a/include/note.h b/include/note.h index b52298b..3d099fe 100644 --- a/include/note.h +++ b/include/note.h @@ -14,6 +14,7 @@ public: virtual ~Note() = default; virtual bool isActive() const = 0; + virtual bool isActive(const microsec& music_offset) const = 0; virtual void update(const microsec& music_offset) = 0; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0; diff --git a/src/classicgame/classicnote.cpp b/src/classicgame/classicnote.cpp index c9d7558..1edfc61 100644 --- a/src/classicgame/classicnote.cpp +++ b/src/classicgame/classicnote.cpp @@ -18,9 +18,14 @@ bool ClassicNote::isActive() const return _states.at(_current_state)->value() == ClassicNoteState::ACTIVE; } -void ClassicNote::putToGame(const microsec &offset) +bool ClassicNote::isActive(const microsec &music_offset) const { - _appearance_time = offset; // To animation manager + return _evaluator.isActive(music_offset); +} + +void ClassicNote::putToGame(const microsec &music_offset) +{ + _appearance_time = music_offset; // To animation manager _trail_path_percent = ((_perfect_offset - _appearance_time) * 0.01); _current_state = ClassicNoteState::FLYING; @@ -32,76 +37,15 @@ bool ClassicNote::isExpired() const return _states.at(_current_state)->value() == ClassicNoteState::NONE; } -static int getPt( float n1 , float n2 , float perc ) -{ - float diff = n2 - n1; - - return n1 + ( diff * perc ); -} - void ClassicNote::update(const microsec& music_offset) { - _states.at(_current_state)->update(this, music_offset); - /*switch (_state) // States will be objects - { - case State::DYING: - _sprite->update(); - if (_sprite->isDead()) - setState(State::NONE); - break; - - case State::FLYING: + auto next_state = _states.at(_current_state)->update(this, music_offset); + if (next_state != _current_state) { - float i; - auto update_time = music_offset - _appearance_time; // This all will be inside ::update - i = update_time / _trail_path_percent * 0.01; // of an animation object - - float xa = getPt( _coordinates.x + 20. , _coordinates.x + 90. , i ); - float ya = getPt( _coordinates.y - 600. , _coordinates.y - 150. , i ); - float xb = getPt( _coordinates.x + 90. , _coordinates.x , i ); - float yb = getPt( _coordinates.y - 150. , _coordinates.y , i ); - - _sprite->update(getPt( xa , xb , i ), getPt( ya , yb , i )); - if (i >= 1) - { - _sprite->trailFade(); - } - - if (_evaluator.isActive(music_offset)) - setState(State::ACTIVE); - break; - } + _current_state = next_state; + _states.at(_current_state)->onEntering(this); - case State::ACTIVE: - { - float i; - auto update_time = music_offset - _appearance_time; // This all will be inside ::update - i = update_time / _trail_path_percent * 0.01; // of an animation object - - float xa = getPt( _coordinates.x + 20. , _coordinates.x + 90. , i ); - float ya = getPt( _coordinates.y - 600. , _coordinates.y - 150. , i ); - float xb = getPt( _coordinates.x + 90. , _coordinates.x , i ); - float yb = getPt( _coordinates.y - 150. , _coordinates.y , i ); - - _sprite->update(getPt( xa , xb , i ), getPt( ya , yb , i )); - if (i >= 1) - { - _sprite->trailFade(); - } - - if (!_evaluator.isActive(music_offset)) - setState(State::DYING); - break; } - - default: - break; - } */ -} - -void ClassicNote::draw(sf::RenderTarget& target, sf::RenderStates states) const -{ - target.draw(*_sprite, states); } auto ClassicNote::input(ClassicInputType&& input_data) -> Grade @@ -124,29 +68,6 @@ Action ClassicNote::action() const return _action; } -/*void ClassicNote::setState(State next_state) -{ - switch (next_state) // States will be objects - { - case State::DYING: - _sprite->pulse(); - break; - - case State::FLYING: - _sprite->setCoordinates(_coordinates.x, _coordinates.y, _coordinates.x + 20, _coordinates.y - 600); - break; - - case State::NONE: - _sprite->reset(); - break; - - default: - break; - } - - _state = next_state; -} */ - std::shared_ptr ClassicNote::sprite() const noexcept { return _sprite; diff --git a/src/classicgame/classicnote.h b/src/classicgame/classicnote.h index 0670b5e..6bbe04c 100644 --- a/src/classicgame/classicnote.h +++ b/src/classicgame/classicnote.h @@ -37,20 +37,22 @@ public: virtual ~ClassicNote() = default; virtual bool isActive() const override; + virtual bool isActive(const microsec &music_offset) const override; virtual void update(const microsec &music_offset) override; - virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override; - - virtual void putToGame(const microsec &offset) override; + virtual void putToGame(const microsec &music_offset) override; virtual bool isExpired() const override; Grade input(ClassicInputType&& input_data); Action action() const; std::shared_ptr sprite() const noexcept; - void saveAppearanceTime(const microsec& offset); + void saveAppearanceTime(const microsec& music_offset); void setSprite(const std::shared_ptr& sprite) noexcept; const Coordinates& getCoordinates() const noexcept; + const microsec& getApearanceTime() const { return _appearance_time; } + const float& getOneTrailPercent() const { return _trail_path_percent; } + private: const Coordinates _coordinates; const PrecisionEvaluator _evaluator; diff --git a/src/classicgame/classicnotestate/classicnoteactivestate.cpp b/src/classicgame/classicnotestate/classicnoteactivestate.cpp new file mode 100644 index 0000000..fcfb8da --- /dev/null +++ b/src/classicgame/classicnotestate/classicnoteactivestate.cpp @@ -0,0 +1,45 @@ +#include "classicnoteactivestate.h" +#include "../classicnote.h" +#include "../classicsprite.h" + +auto ClassicNoteActiveState::value() const -> Value +{ + return Value::ACTIVE; +} + +auto ClassicNoteActiveState::update(const ClassicNote* note, const microsec& offset) -> Value +{ + float i; + auto update_time = offset - note->getApearanceTime(); // This all will be inside ::update + i = update_time / note->getOneTrailPercent() * 0.01; // of an animation object + + const auto& coordinates = note->getCoordinates(); + const auto& sprite = note->sprite(); + float xa = getPt( coordinates.x + 20. , coordinates.x + 90. , i ); + float ya = getPt( coordinates.y - 600. , coordinates.y - 150. , i ); + float xb = getPt( coordinates.x + 90. , coordinates.x , i ); + float yb = getPt( coordinates.y - 150. , coordinates.y , i ); + + sprite->update(getPt( xa , xb , i ), getPt( ya , yb , i )); + if (i >= 1) + { + sprite->trailFade(); + } + + if (!note->isActive(offset)) + return Value::DYING; + + return value(); +} + +constexpr int ClassicNoteActiveState::getPt(float n1, float n2, float perc) const +{ + float diff = n2 - n1; + return n1 + (diff * perc); +} + +void ClassicNoteActiveState::onEntering(const ClassicNote* note) +{ + (void)note; +} + diff --git a/src/classicgame/classicnotestate/classicnoteactivestate.h b/src/classicgame/classicnotestate/classicnoteactivestate.h new file mode 100644 index 0000000..5fc2024 --- /dev/null +++ b/src/classicgame/classicnotestate/classicnoteactivestate.h @@ -0,0 +1,17 @@ +#ifndef CLASSICNOTEACTIVESTATE_H +#define CLASSICNOTEACTIVESTATE_H + +#include "classicnotestate.h" + +class ClassicNoteActiveState : public ClassicNoteState +{ +public: + virtual Value value() const override; + virtual Value update(const ClassicNote* note, const microsec& offset) override; + virtual void onEntering(const ClassicNote* note) override; + +private: + inline constexpr int getPt(float n1 , float n2 , float perc) const; +}; + +#endif // CLASSICNOTEACTIVESTATE_H diff --git a/src/classicgame/classicnotestate/classicnotedyingstate.cpp b/src/classicgame/classicnotestate/classicnotedyingstate.cpp new file mode 100644 index 0000000..d7376c6 --- /dev/null +++ b/src/classicgame/classicnotestate/classicnotedyingstate.cpp @@ -0,0 +1,26 @@ +#include "classicnotedyingstate.h" +#include "../classicnote.h" +#include "../classicsprite.h" + +auto ClassicNoteDyingState::value() const -> Value +{ + return Value::DYING; +} + +auto ClassicNoteDyingState::update(const ClassicNote* note, const microsec& offset) -> Value +{ + (void) offset; + const auto& sprite = note->sprite(); + + sprite->update(); + if (sprite->isDead()) + return Value::NONE; + + return value(); +} + +void ClassicNoteDyingState::onEntering(const ClassicNote* note) +{ + note->sprite()->pulse(); +} + diff --git a/src/classicgame/classicnotestate/classicnotedyingstate.h b/src/classicgame/classicnotestate/classicnotedyingstate.h new file mode 100644 index 0000000..ac3640b --- /dev/null +++ b/src/classicgame/classicnotestate/classicnotedyingstate.h @@ -0,0 +1,14 @@ +#ifndef CLASSICNOTEDYINGSTATE_H +#define CLASSICNOTEDYINGSTATE_H + +#include "classicnotestate.h" + +class ClassicNoteDyingState : public ClassicNoteState +{ +public: + virtual Value value() const override; + virtual Value update(const ClassicNote* note, const microsec& offset) override; + virtual void onEntering(const ClassicNote* note) override; +}; + +#endif // CLASSICNOTEDYINGSTATE_H diff --git a/src/classicgame/classicnotestate/classicnoteflyingstate.cpp b/src/classicgame/classicnotestate/classicnoteflyingstate.cpp new file mode 100644 index 0000000..d043b01 --- /dev/null +++ b/src/classicgame/classicnotestate/classicnoteflyingstate.cpp @@ -0,0 +1,49 @@ +#include "classicnoteflyingstate.h" +#include "../classicnote.h" +#include "../classicsprite.h" + +auto ClassicNoteFlyingState::value() const -> Value +{ + return Value::FLYING; +} + +auto ClassicNoteFlyingState::update(const ClassicNote* note, const microsec& offset) -> Value +{ + float i; + auto update_time = offset - note->getApearanceTime(); // This all will be inside ::update + i = update_time / note->getOneTrailPercent() * 0.01; // of an animation object + + const auto& coordinates = note->getCoordinates(); + const auto& sprite = note->sprite(); + float xa = getPt( coordinates.x + 20. , coordinates.x + 90. , i ); + float ya = getPt( coordinates.y - 600. , coordinates.y - 150. , i ); + float xb = getPt( coordinates.x + 90. , coordinates.x , i ); + float yb = getPt( coordinates.y - 150. , coordinates.y , i ); + + sprite->update(getPt( xa , xb , i ), getPt( ya , yb , i )); + if (i >= 1) + { + sprite->trailFade(); + } + + if (note->isActive(offset)) + return Value::ACTIVE; + + return value(); +} + +constexpr int ClassicNoteFlyingState::getPt(float n1, float n2, float perc) const +{ + float diff = n2 - n1; + return n1 + (diff * perc); +} + +void ClassicNoteFlyingState::onEntering(const ClassicNote* note) +{ + const auto& coordinates = note->getCoordinates(); + const auto& sprite = note->sprite(); + + sprite->setCoordinates(coordinates.x, coordinates.y, + coordinates.x + 20, coordinates.y - 600); +} + diff --git a/src/classicgame/classicnotestate/classicnoteflyingstate.h b/src/classicgame/classicnotestate/classicnoteflyingstate.h new file mode 100644 index 0000000..4f287b4 --- /dev/null +++ b/src/classicgame/classicnotestate/classicnoteflyingstate.h @@ -0,0 +1,17 @@ +#ifndef CLASSICNOTEFLYINGSTATE_H +#define CLASSICNOTEFLYINGSTATE_H + +#include "classicnotestate.h" + +class ClassicNoteFlyingState : public ClassicNoteState +{ +public: + virtual Value value() const override; + virtual Value update(const ClassicNote* note, const microsec& offset) override; + virtual void onEntering(const ClassicNote* note) override; + +private: + inline constexpr int getPt(float n1 , float n2 , float perc) const; +}; + +#endif // CLASSICNOTEFLYINGSTATE_H diff --git a/src/classicgame/classicnotestate/classicnotenonestate.cpp b/src/classicgame/classicnotestate/classicnotenonestate.cpp new file mode 100644 index 0000000..dd3e10f --- /dev/null +++ b/src/classicgame/classicnotestate/classicnotenonestate.cpp @@ -0,0 +1,20 @@ +#include "classicnotenonestate.h" +#include "../classicnote.h" +#include "../classicsprite.h" + +auto ClassicNoteNoneState::value() const -> Value +{ + return Value::NONE; +} + +auto ClassicNoteNoneState::update(const ClassicNote* note, const microsec& offset) -> Value +{ + (void) offset; + (void) note; +} + +void ClassicNoteNoneState::onEntering(const ClassicNote* note) +{ + note->sprite()->reset(); +} + diff --git a/src/classicgame/classicnotestate/classicnotenonestate.h b/src/classicgame/classicnotestate/classicnotenonestate.h new file mode 100644 index 0000000..752bfb9 --- /dev/null +++ b/src/classicgame/classicnotestate/classicnotenonestate.h @@ -0,0 +1,14 @@ +#ifndef CLASSICNOTENONESTATE_H +#define CLASSICNOTENONESTATE_H + +#include "classicnotestate.h" + +class ClassicNoteNoneState : public ClassicNoteState +{ +public: + virtual Value value() const override; + virtual Value update(const ClassicNote* note, const microsec& offset) override; + virtual void onEntering(const ClassicNote* note) override; +}; + +#endif // CLASSICNOTENONESTATE_H diff --git a/src/classicgame/classicnotestate/classicnotestate.h b/src/classicgame/classicnotestate/classicnotestate.h index 3399faf..e3b0d68 100644 --- a/src/classicgame/classicnotestate/classicnotestate.h +++ b/src/classicgame/classicnotestate/classicnotestate.h @@ -2,6 +2,7 @@ #define CLASSICNOTESTATE_H #include +#include using microsec = sf::Int64;