Somes weeks ago, i've started pouring more hours on the animation part of my game, which was pretty bare up to this point. After sucessfully creating a tool to easily create and modify my 2d animations from a sprite sheet (setting things like sprite rect, crop, hitboxes, weapon mounting point and angle, etc...), i now have to actually do the logic of switching all of the animation the actors have in the game.
The code below is my function to change the actor's animation based on his state.
While, i've not actually come to a dead end, i feel what my code is starting to feel tangled and harder to debug.
As you will notice a little bit further down, it'll only get more complicated, as the number of possible animation is very wide.
So my question is this: Is there a better way of doing this? Or should i just continue doing all thoses else-if?
I really do not want all the hours i'm doing to go to waste if i have to redo everything, especially all the time spent doing my own animation tool (which is very specific to my game).
//////////////////////////////////////////////////////////////////////////
//
void IActor::ManageAnimation()
{
Animations id = ANIMATION_IDLE_OOC;
//If out of combat
if(!IsState(STATE_INCOMBAT) )
{
if(IsState(STATE_JUMPING))
id = ANIMATION_JUMP_OOC;
else if(IsState(STATE_FALLING))
id = ANIMATION_FALL_OOC;
else if(IsIdle(MAX_IDLE_TIME))
id = ANIMATION_SLEEP_OOC;
//Sliding show be shown over moving
else if(IsState(STATE_SLIDING))
id = ANIMATION_SLIDE_OOC;
else if(IsState(STATE_MOVING))
id = ANIMATION_MOVE_OOC;
}
else
{
//...
}
SetAnimation(id);
}
enum Animations
{
//////////////////////////////////////////////////////////////////////////
//Generic animation. Everyone should have theses. It's the job of this class to set them.
//Out of combat
ANIMATION_IDLE_OOC, //Basic idle animation out of combat.
ANIMATION_SLEEP_OOC, //Animation for idling too long... Player yawn, wolf lay down and sleep, etc...
ANIMATION_MOVE_OOC, //Moving
ANIMATION_LOOK_UP_OOC, //Look up
ANIMATION_LOOK_DOWN_OOC, //Look down
ANIMATION_TREATHENED_OOC, //Transition between out of combat and in combat.
ANIMATION_JUMP_OOC, //Jump
ANIMATION_FALL_OOC, //Falling
ANIMATION_SLIDE_OOC, //When the actor is changing direction or sliding down a slope.
//In combat
ANIMATION_IDLE_IC, //Basic idle animation in combat.
ANIMATION_MOVE_IC, //Moving
ANIMATION_LOOK_UP_IC, //Look up
ANIMATION_LOOK_DOWN_IC, //Look down
ANIMATION_TREATHENED_IC, //Transition between in combat and out of combat.
ANIMATION_JUMP_IC, //Jump
ANIMATION_FALL_IC, //Falling
ANIMATION_SLIDE_IC, //When the actor is changing direction or sliding down a slope.
//generic
ANIMATION_HURT,
ANIMATION_CRITITAL_HURT,
ANIMATION_DIE,
ANIMATION_DEAD,
//End of generic animation
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//Specific animation. Not everyone have them. It's the job of the Actor's interface to set them. thoses animation can all be canceled by us.
//Reserve for physical attack with a weapon or unarmed.
ANIMATION_ATTACK_1,
ANIMATION_ATTACK_2,
ANIMATION_ATTACK_3,
ANIMATION_ATTACK_4,
ANIMATION_ATTACK_5,
//Reserved for Spell animations
ANIMATION_SPECIAL_1,
ANIMATION_SPECIAL_2,
ANIMATION_SPECIAL_3,
ANIMATION_SPECIAL_4,
ANIAMTION_SPECIAL_5,
//Reserved for social animations. could be laugh, cry, angry, yell, etc...
ANIMATION_SOCIAL_1,
ANIMATION_SOCIAL_2,
ANIMATION_SOCIAL_3,
ANIMATION_SOCIAL_4,
ANIMATION_SOCIAL_5,
//End specific animation
//////////////////////////////////////////////////////////////////////////
};