Mushroom Animations

Animating the Mushroom

The Mushroom has the following states:

  • Run: The mushroom is actively moving either to the left or to the right.

  • Hit: The mushroom has been struck by the player or has fallen outside the boundaries of the map.

The current state of the player is determined by the context, which includes information about their life status (is_dead).

game/mushroom.h
// -----
// --- Add: Includes. ---
#include <SFML/Audio/Sound.hpp>

#include "engine/fsm.h"
#include "engine/sprite_sheet_animation.h"
#include "engine/state.h"
// --- End ---
// -----

class Mushroom : public ng::Node {
 // -----
 private:
  // --- Add: Context and Mushroom states.  ---
  struct Context {
    bool is_dead = false;
  };

  class RunState : public ng::State<Context> {
   public:
    RunState(ng::State<Context>::ID id, ng::SpriteSheetAnimation animation);

   protected:
    void OnEnter() override;

    void Update() override;

   private:
    ng::SpriteSheetAnimation animation_;
  };

  class HitState : public ng::State<Context> {
   public:
    HitState(ng::State<Context>::ID id, ng::SpriteSheetAnimation animation,
             const sf::SoundBuffer* sound_buffer, ng::Node* node);

   protected:
    void OnEnter() override;

    void Update() override;

   private:
    void Die();

    ng::SpriteSheetAnimation animation_;
    sf::Sound sound_;
    ng::Node* node_ = nullptr;
  };
  // --- End ---
  
  // -----
  bool is_dead_ = false; // Remove.
  // --- Add: Context and FSM. ---
  Context context_;
  ng::FSM<Context> animator_;
  // --- End ---
};

Last updated