Implement states
This commit is contained in:
		
							parent
							
								
									d9788b31b8
								
							
						
					
					
						commit
						5b7076ac1c
					
				| @ -14,6 +14,7 @@ public: | |||||||
|     virtual ~Note() = default; |     virtual ~Note() = default; | ||||||
| 
 | 
 | ||||||
|     virtual bool isActive() const = 0; |     virtual bool isActive() const = 0; | ||||||
|  |     virtual bool isActive(const microsec& music_offset) const = 0; | ||||||
|     virtual void update(const microsec& music_offset) = 0; |     virtual void update(const microsec& music_offset) = 0; | ||||||
|     virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0; |     virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -18,9 +18,14 @@ bool ClassicNote::isActive() const | |||||||
|     return _states.at(_current_state)->value() == ClassicNoteState::ACTIVE; |     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); |     _trail_path_percent = ((_perfect_offset - _appearance_time) * 0.01); | ||||||
| 
 | 
 | ||||||
|     _current_state = ClassicNoteState::FLYING; |     _current_state = ClassicNoteState::FLYING; | ||||||
| @ -32,76 +37,15 @@ bool ClassicNote::isExpired() const | |||||||
|     return _states.at(_current_state)->value() == ClassicNoteState::NONE; |     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) | void ClassicNote::update(const microsec& music_offset) | ||||||
| { | { | ||||||
|     _states.at(_current_state)->update(this, music_offset); |     auto next_state = _states.at(_current_state)->update(this, music_offset); | ||||||
|     /*switch (_state)   // States will be objects
 |     if (next_state != _current_state) | ||||||
|     { |     { | ||||||
|     case State::DYING: |         _current_state = next_state; | ||||||
|         _sprite->update(); |         _states.at(_current_state)->onEntering(this); | ||||||
|         if (_sprite->isDead()) |  | ||||||
|             setState(State::NONE); |  | ||||||
|         break; |  | ||||||
| 
 | 
 | ||||||
|     case State::FLYING: |  | ||||||
|     { |  | ||||||
|         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; |  | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
|     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 | auto ClassicNote::input(ClassicInputType&& input_data) -> Grade | ||||||
| @ -124,29 +68,6 @@ Action ClassicNote::action() const | |||||||
|     return _action; |     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<ClassicSprite> ClassicNote::sprite() const noexcept | std::shared_ptr<ClassicSprite> ClassicNote::sprite() const noexcept | ||||||
| { | { | ||||||
|     return _sprite; |     return _sprite; | ||||||
|  | |||||||
| @ -37,20 +37,22 @@ public: | |||||||
|     virtual ~ClassicNote() = default; |     virtual ~ClassicNote() = default; | ||||||
| 
 | 
 | ||||||
|     virtual bool isActive() const override; |     virtual bool isActive() const override; | ||||||
|  |     virtual bool isActive(const microsec &music_offset) const override; | ||||||
|     virtual void update(const microsec &music_offset) 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 &music_offset) override; | ||||||
| 
 |  | ||||||
|     virtual void putToGame(const microsec &offset) override; |  | ||||||
|     virtual bool isExpired() const override; |     virtual bool isExpired() const override; | ||||||
| 
 | 
 | ||||||
|     Grade input(ClassicInputType&& input_data); |     Grade input(ClassicInputType&& input_data); | ||||||
|     Action action() const; |     Action action() const; | ||||||
| 
 | 
 | ||||||
|     std::shared_ptr<ClassicSprite> sprite() const noexcept; |     std::shared_ptr<ClassicSprite> sprite() const noexcept; | ||||||
|     void saveAppearanceTime(const microsec& offset); |     void saveAppearanceTime(const microsec& music_offset); | ||||||
|     void setSprite(const std::shared_ptr<ClassicSprite>& sprite) noexcept; |     void setSprite(const std::shared_ptr<ClassicSprite>& sprite) noexcept; | ||||||
|     const Coordinates& getCoordinates() const noexcept; |     const Coordinates& getCoordinates() const noexcept; | ||||||
| 
 | 
 | ||||||
|  |     const microsec& getApearanceTime() const { return _appearance_time; } | ||||||
|  |     const float& getOneTrailPercent() const { return _trail_path_percent; } | ||||||
|  | 
 | ||||||
| private: | private: | ||||||
|     const Coordinates _coordinates; |     const Coordinates _coordinates; | ||||||
|     const PrecisionEvaluator<Grade> _evaluator; |     const PrecisionEvaluator<Grade> _evaluator; | ||||||
|  | |||||||
							
								
								
									
										45
									
								
								src/classicgame/classicnotestate/classicnoteactivestate.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/classicgame/classicnotestate/classicnoteactivestate.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -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; | ||||||
|  | } | ||||||
|  | 
 | ||||||
							
								
								
									
										17
									
								
								src/classicgame/classicnotestate/classicnoteactivestate.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/classicgame/classicnotestate/classicnoteactivestate.h
									
									
									
									
									
										Normal file
									
								
							| @ -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
 | ||||||
							
								
								
									
										26
									
								
								src/classicgame/classicnotestate/classicnotedyingstate.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/classicgame/classicnotestate/classicnotedyingstate.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -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(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
							
								
								
									
										14
									
								
								src/classicgame/classicnotestate/classicnotedyingstate.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/classicgame/classicnotestate/classicnotedyingstate.h
									
									
									
									
									
										Normal file
									
								
							| @ -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
 | ||||||
							
								
								
									
										49
									
								
								src/classicgame/classicnotestate/classicnoteflyingstate.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/classicgame/classicnotestate/classicnoteflyingstate.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -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); | ||||||
|  | } | ||||||
|  | 
 | ||||||
							
								
								
									
										17
									
								
								src/classicgame/classicnotestate/classicnoteflyingstate.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/classicgame/classicnotestate/classicnoteflyingstate.h
									
									
									
									
									
										Normal file
									
								
							| @ -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
 | ||||||
							
								
								
									
										20
									
								
								src/classicgame/classicnotestate/classicnotenonestate.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/classicgame/classicnotestate/classicnotenonestate.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -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(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
							
								
								
									
										14
									
								
								src/classicgame/classicnotestate/classicnotenonestate.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/classicgame/classicnotestate/classicnotenonestate.h
									
									
									
									
									
										Normal file
									
								
							| @ -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
 | ||||||
| @ -2,6 +2,7 @@ | |||||||
| #define CLASSICNOTESTATE_H | #define CLASSICNOTESTATE_H | ||||||
| 
 | 
 | ||||||
| #include <SFML/System/Clock.hpp> | #include <SFML/System/Clock.hpp> | ||||||
|  | #include <memory> | ||||||
| 
 | 
 | ||||||
| using microsec = sf::Int64; | using microsec = sf::Int64; | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user