yourYour GetNextFrame shouldshould have less iffewer if statements, in. In other words, you should merge some of them together, the. The ones that return early especially, those should especially be in an if ..then .. else if.. then structure, like this. it It works either way, but this looks cleaner, and is more to the point about what the code does.
public Rectangle GetNextFrame(GameActions gameAction)
{
var sequence = GetSequence(gameAction);
if (gameAction == GameActions.Idle) {
frameTimer = 0;
prevFrame.X = 0;
prevGameAction = gameAction;
return prevFrame;
} else if(frameTimer != 0) {
UpdateTimer();
return prevFrame;
} else if (sequence == null) {
return prevFrame;
}
UpdateTimer();
Rectangle nextFrame;
if (gameAction != prevGameAction)
{
nextFrame = sequence.First();
}
else
{
nextFrame = sequence.Next(prevFrame);
}
prevFrame = nextFrame;
prevGameAction = gameAction;
return nextFrame;
}
yourYour Animate Methodmethod has some extra lines in it as well where the ifif statements can be merged together.
public void Animate(Actions action)
{
Animation animation;
if (!animations.TryGetValue(action, out animation) || !animation.IsReady())
{
return;
}
frame = animation.GetNextFrame();
if (animation == prevAnimation)
{
return;
}
if (prevAnimation != null)
{
prevAnimation.Reset();
}
prevAnimation = animation;
}
thatThat one ifif statement with the orOR clause || does the exact same thing that your code did, but with lessfewer lines and in a cleaner manner.