What's up bros.
I need to figure out a way to switch between different animations in a simple way. I've got working code now, but it's really ugly, that's why I was wondering if there was another, better solution.
The deal is that every function I create needs to simulate one frame of behaviour at a time, I can't just stay in the function forever, because I need to render too. And this is really bothering me, I can't wrap my head around this, because it's not the normal programming I'm used to. For example, in order to switch from IDLE animation to ATTACK, I need to first check if IDLE animation is over, and exactly then change to ATTACK, and then wait for ATTACK animation to be over, and only then switch back to IDLE. I'm using timers, of course, but still, my code is messed up.
Can you propose something that will do the trick, and also will be simple to implement?
Thanks for reading. :rolleyes:
EDIT: My code for now is this: ( it works, but I don't see how someone else will understand that code )
void Character::updateAnimation() //Executed once every frame
{
animationOver = false; // animation is not over yet
// If I have requested a new animation from some other function and it is different that the current one.
if( currentAnimState != requestedAnim )
{
// If the current animation is aaalmost over, just a little bit before over, only then you can change to another animation.
if( animTimer.GetRunningTime() >= getCurrentModel()->getMaxAnimationTime() - animRecoil || currentAnimState == int( Anim::IDLE ) )
{
animationOver = true; //The previous animation is over
currentAnimState = requestedAnim; //Change the current animation state
setCurrentModel( models[ int( currentAnimState ) ] ); // set the new animation
animTimer.startNow(); //Start the new timer
}
}
if( currentAnimState != int( Anim::IDLE ) ) //If animation is different from Idle, switch back to Idle, when it's over.
{
requestedAnim = int( Anim::IDLE );
}
}