experiments-monogame/Lighthouse/Lighthouse/Components/PlayerAnimator.cs
2024-12-16 21:27:11 -06:00

43 lines
No EOL
1.4 KiB
C#

using Nez.Sprites;
using Nez.Textures;
namespace Lighthouse
{
public class PlayerAnimator : SpriteAnimator
{
public override void OnAddedToEntity()
{
var texture = Entity.Scene.Content.LoadTexture("Textures/FarmerCyan");
var sprites = Sprite.SpritesFromAtlas(texture, 16, 16);
SetSprite(sprites[1]);
LayerDepth = 0.5f;
AddAnimation("walk down", new Sprite[] {sprites[1], sprites[2], sprites[3], sprites[4]});
AddAnimation("walk up", new Sprite[] {sprites[6], sprites[7], sprites[8], sprites[9]});
AddAnimation("walk right", new Sprite[] {sprites[11], sprites[12], sprites[13], sprites[14]});
AddAnimation("walk left", new Sprite[] {sprites[16], sprites[17], sprites[18], sprites[19]});
}
public void StepAnimation(string name)
{
if (name != CurrentAnimationName)
{
Play(name);
Pause();
}
else
{
AdvanceFrame();
}
}
private void AdvanceFrame()
{
CurrentFrame += 1;
if (CurrentFrame >= CurrentAnimation.Sprites.Length)
CurrentFrame = 0;
// Update won't be called because of Paused state, so need to do this ourselves
Sprite = CurrentAnimation.Sprites[CurrentFrame];
}
}
}