Migration of Animation Code to External Library

Created the AnimationLibrary library to hold both Animation and AnimatedSprite.
This commit is contained in:
quinnvoker
2018-11-17 11:24:20 -08:00
parent e14c1627cc
commit fee1757862
6 changed files with 219 additions and 0 deletions
+79
View File
@@ -0,0 +1,79 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.TextureAtlases;
namespace QURO.AnimationLibrary
{
public class AnimatedSprite
{
public TextureAtlas CurrentTextureAtlas { get; set; }
private Animation currentAnimation;
public Animation CurrentAnimation
{
get { return currentAnimation; }
set
{
currentAnimation = value;
CurrentFrameIndex = 0;
}
}
public int CurrentFrameIndex { get; set; }
public TextureRegion2D CurrentFrame => CurrentAnimation.Frames[CurrentFrameIndex];
private float timer;
public AnimatedSprite() { }
public AnimatedSprite(TextureAtlas textureAtlas, Animation startingAnimation)
{
CurrentTextureAtlas = textureAtlas;
CurrentAnimation = startingAnimation;
timer = CurrentAnimation.Delay;
}
public void Reset()
{
CurrentFrameIndex = 0;
timer = CurrentAnimation.Delay;
}
public void Update(GameTime gameTime)
{
if (!CurrentAnimation.IsStillImage)
{
if (timer <= 0)
{
if (CurrentFrameIndex < CurrentAnimation.Frames.Length - 1)
{
CurrentFrameIndex++;
}
else
{
if (CurrentAnimation.IsLooping)
{
CurrentFrameIndex = 0;
}
}
timer = CurrentAnimation.Delay;
}
else
{
timer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
}
}
}
public void Draw(SpriteBatch spriteBatch, Vector2 position, Color tint)
{
spriteBatch.Draw(CurrentTextureAtlas.Texture, position, CurrentFrame.Bounds, tint);
}
public void Draw(SpriteBatch spriteBatch, Vector2 position, Vector2 spriteOffset, Color tint)
{
Rectangle sourceRect = CurrentFrame.Bounds;
sourceRect.Offset(spriteOffset);
spriteBatch.Draw(CurrentTextureAtlas.Texture, position, sourceRect, tint);
}
}
}