Run: The player is actively moving either to the left or to the right.
Jump: The player is currently moving upwards after initiating a jump.
Fall: The player is moving downwards, typically due to gravity, after a jump or falling from a platform.
Hit: The player has been struck by an enemy or has fallen outside the boundaries of the map.
The current state of the player is determined by the context, which includes information such as the player's velocity, whether they are currently on the ground (is_on_ground), and their life status (is_dead).
game/player.h
// -----// --- Add: Includes. ---#include "engine/fsm.h"#include "engine/sprite_sheet_animation.h"#include "engine/state.h"// --- End ---// -----classPlayer:publicng::Node{// -----private:// --- Add: Context and Player states. ---structContext{sf::Vector2f velocity;bool is_on_ground =false;bool is_dead =false;};classIdleState:publicng::State<Context>{public:IdleState(ng::State<Context>::IDid,ng::SpriteSheetAnimationanimation);protected:voidOnEnter()override;voidUpdate()override;private:ng::SpriteSheetAnimation animation_;};classRunState:publicng::State<Context>{public:RunState(ng::State<Context>::IDid,ng::SpriteSheetAnimationanimation);protected:voidOnEnter()override;voidUpdate()override;private:ng::SpriteSheetAnimation animation_;};classJumpState:publicng::State<Context>{public:JumpState(ng::State<Context>::IDid,ng::SpriteSheetAnimationanimation,constsf::SoundBuffer*sound_buffer);protected:voidOnEnter()override;voidUpdate()override;private:ng::SpriteSheetAnimation animation_;sf::Sound sound_;};classFallState:publicng::State<Context>{public:FallState(ng::State<Context>::IDid,ng::SpriteSheetAnimationanimation);protected:voidOnEnter()override;voidUpdate()override;private:ng::SpriteSheetAnimation animation_;};classHitState:publicng::State<Context>{public:HitState(ng::State<Context>::IDid,ng::SpriteSheetAnimationanimation,ng::Node*node,GameManager*game_manager);protected:voidOnEnter()override;voidUpdate()override;private:voidDie();ng::SpriteSheetAnimation animation_;ng::Node* node_ =nullptr; GameManager* game_manager_ =nullptr;};// --- End ---// -----// --- Remove: The context has these already. ---sf::Vector2f velocity_;bool is_on_ground_ =false;bool is_dead_ =false;// --- End ---// --- Add: Context and FSM. --- Context context_;ng::FSM<Context> animator_;// --- End ---};