The best way to manage sprite sheet animations?

Started by
21 comments, last by MagnusJ 8 years, 7 months ago

FYI, as well as for future readers, I finished the article, and it got released for reading yesterday, you can find it at

http://www.gamedev.net/page/resources/_/technical/game-programming/from-user-input-to-animations-using-state-machines-r4155

Advertisement

I've essentially ripped my solution to shreds and I'm rebuilding almost all of it again to work in a more

state-driven way. I'll be looking at various guides that you've all linked me too, but I have a general idea of

how I want to design it:

In the base class for all entities (Entity.cs) there's a method which checks against certain conditions and then changes

states. For example, if an entity's velocity != Vector2.Zero, then I can set "physicalState = PhysicalStates.Moving", so that

all entities will follow this flow. I decided to make all the enums private, so that everything is all contained within this lower

layer to avoid potential confusion later on!

Another interesting solution is using a stack for the animations.

The final animation for your character would be death. Let's say we push that at the bottom of the stack.

If I had as many states for my character as you, I would go as far as implementing each state as it's own class.

For example, the character starts in the idle state, and that state is also pushed onto the stack. Each animation state has at least two functions - start and update.

Start sets which animation to play.

Update controls when to exit or add a state. The idle state will only end in one condition - when the player life reaches zero.
From idle we can go to different other states, all launched from idle.


class IdleState : public AnimationState
{
    void Start() override
    {
       m_character.SetAnimation(idle); // idle could be a enum
    }

    bool Update() override
    {
       if (m_character.IsDead() == true) return false; // false will pop the state
       if (jump_button_pressed) m_character.AddState(new JumpState(m_character));
       if (IsFalling() == true) m_character.AddState(new FallState(m_character));          
       etc...
       return true;    
    }

    bool IsFalling()
    {
       return (m_character.OnGround() == false && m_character.GetVelocity().y > 0);
    }
}

In this way, all states will be encapsulated and it will be more easy to add or change existing states without interfering with player code or other states. Also, your entity class will be kept at a minimum size.

Each entity will need to have its own stack:


class Entity
{
     Stack<AnimationState> m_animationStateStack;

     void Update()
     {
           if (m_animationStateStat.top().Update() == false) m_animationStateStack.pop();
     }
}

Each state also need to know of the stateStack in order to add new states.

This topic is closed to new replies.

Advertisement