MochiEngine
Loading...
Searching...
No Matches
Mochi::Graphics::AnimatedSprite Class Reference

AnimatedSprite does all the Spritesheet does and also provides an easy interface to animate and select animations for the sprite. More...

#include <AnimatedSprite.h>

Inheritance diagram for Mochi::Graphics::AnimatedSprite:
Mochi::Graphics::Spritesheet Mochi::Graphics::SpriteBase Mochi::Graphics::OneshotAnimation

Public Member Functions

 AnimatedSprite (IAnimationFactory *animationFactory, AbstractTextureFactory *textureFactory, const std::string &animationPath, const std::string &mainAnimation)
 Constructs the spritesheet with the default SpriteBase constructor, internally calling LoadTexture.
virtual void Update (const float &dt) override
 This method updates an internal timer and changes frames when needed, according to the rules defined in the AnimationsData metadata. This needs to be called manually to work.
virtual void PlayAnimation (const std::string &animationName)
 Given an existing animation name, it starts that animation. This means immediately changing the frame, resetting the timer and setting the current animation.
void StopAnimation ()
 Stops the current animation and the timer, freezing the sprite in the current frame.
std::string GetCurrentAnimation () const
Public Member Functions inherited from Mochi::Graphics::Spritesheet
 Spritesheet (IAnimationFactory *animationFactory, AbstractTextureFactory *textureFactory, const std::string &animationPath, const int &startingFrame)
 Constructs the spritesheet with the default SpriteBase constructor, internally calling LoadTexture.
void SetFrame (const int &frameIndex)
 Sets the current frame for the animation.
int GetFrame () const
 Gets the current frame index.
Public Member Functions inherited from Mochi::Graphics::SpriteBase
 SpriteBase (AbstractTextureFactory *factory, const std::string &path)
 Constructor that internally calls LoadTexture(AbstractTextureFactory *textureFactory, const std::string &filename).
 SpriteBase ()
 This constructor only initializes a minimal part of the sprite. LoadTexture(AbstractTextureFactory *textureFactory, const std::string &filename) needs to be called for the sprite to be valid.
void LoadTexture (AbstractTextureFactory *textureFactory, const std::string &path)
 Loads the texture and caches its size for future rendering.
virtual std::vector< RenderCommandGetRenderData () const
 Gets the list of render commands that represent this sprite in a format compatible with the renderer.
void SetZIndex (const uint16_t &zIndex)
uint16_t GetZIndex () const
bool IsVisible () const
void SetVisible (const bool &visible)
uint8_t GetAlpha () const
void SetAlpha (const uint8_t &alpha)
std::shared_ptr< TransformGetTransform () const
void SetTransform (std::shared_ptr< Transform > tr)
void SetSrcRect (const Rectf &src)

Protected Attributes

bool mPlaying
float mTimer
int mLoops
bool mForward
std::string mCurrentAnimation
Protected Attributes inherited from Mochi::Graphics::Spritesheet
std::shared_ptr< AnimationsDatamAnimationsData
Protected Attributes inherited from Mochi::Graphics::SpriteBase
std::shared_ptr< ITexturemTexture
Rectf mSrcRect
uint16_t mZindex
bool mVisible
uint8_t mAlpha
Vector2f mSize
std::shared_ptr< TransformmTransform

Detailed Description

AnimatedSprite does all the Spritesheet does and also provides an easy interface to animate and select animations for the sprite.

AnimatedSprite has the same data as Spritesheet, but also uses the AnimationsData to internally change the frame shown in the spritesheet.

Ownership:

Guarantees:

  • The sprite will change frames on the time defined by AnimationsData, given Update is called with a correct delta time.
  • Animations will change the next frame after calling PlayAnimation.
  • Animations will stop playing when calling StopAnimation.

Limitations:

  • For now, AnimatedSprite only works as part of a Spritesheet. So animations composed of several sprites are not currently supported.

Constructor & Destructor Documentation

◆ AnimatedSprite()

Mochi::Graphics::AnimatedSprite::AnimatedSprite ( IAnimationFactory * animationFactory,
AbstractTextureFactory * textureFactory,
const std::string & animationPath,
const std::string & mainAnimation )

Constructs the spritesheet with the default SpriteBase constructor, internally calling LoadTexture.

Parameters
animationFactoryThe animation factory.
textureFactoryThe texture factory.
animationPathThe path or unique identifier to the animation file in the file system.
mainAnimationThe first animation the sprite will show.

Member Function Documentation

◆ PlayAnimation()

void Mochi::Graphics::AnimatedSprite::PlayAnimation ( const std::string & animationName)
virtual

Given an existing animation name, it starts that animation. This means immediately changing the frame, resetting the timer and setting the current animation.

Parameters
animationNameThe unique name of the desired animation to play.

◆ Update()

void Mochi::Graphics::AnimatedSprite::Update ( const float & dt)
overridevirtual

This method updates an internal timer and changes frames when needed, according to the rules defined in the AnimationsData metadata. This needs to be called manually to work.

Parameters
dtThe delta time.

Reimplemented from Mochi::Graphics::SpriteBase.

Reimplemented in Mochi::Graphics::OneshotAnimation.


The documentation for this class was generated from the following files: