mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-24 12:06:37 +00:00
49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using System;
|
|
using Microsoft.Xna.Framework;
|
|
using MonoGame.Extended.Animations.SpriteSheets;
|
|
using MonoGame.Extended.Entities;
|
|
using MonoGame.Extended.Sprites;
|
|
|
|
namespace MonoGame.Extended.Animations
|
|
{
|
|
[EntityComponent]
|
|
public class AnimatedSprite : Sprite
|
|
{
|
|
private readonly SpriteSheetAnimationFactory _animationFactory;
|
|
private SpriteSheetAnimation _currentAnimation;
|
|
|
|
public AnimatedSprite(SpriteSheetAnimationFactory animationFactory, string playAnimation = null)
|
|
: base(animationFactory.Frames[0])
|
|
{
|
|
_animationFactory = animationFactory;
|
|
|
|
if (playAnimation != null)
|
|
Play(playAnimation);
|
|
}
|
|
|
|
public SpriteSheetAnimation Play(string name, Action onCompleted = null)
|
|
{
|
|
if (_currentAnimation == null || _currentAnimation.IsComplete || _currentAnimation.Name != name)
|
|
{
|
|
_currentAnimation = _animationFactory.Create(name);
|
|
_currentAnimation.OnCompleted = onCompleted;
|
|
}
|
|
|
|
return _currentAnimation;
|
|
}
|
|
|
|
public void Update(float deltaTime)
|
|
{
|
|
if (_currentAnimation != null && !_currentAnimation.IsComplete)
|
|
{
|
|
_currentAnimation.Update(deltaTime);
|
|
TextureRegion = _currentAnimation.CurrentFrame;
|
|
}
|
|
}
|
|
|
|
public void Update(GameTime gameTime)
|
|
{
|
|
Update(gameTime.GetElapsedSeconds());
|
|
}
|
|
}
|
|
} |