mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-06-26 14:41:41 +02:00
48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Microsoft.Xna.Framework;
|
|
using MLEM.Textures;
|
|
|
|
namespace MLEM.Animations {
|
|
public class SpriteAnimationGroup {
|
|
|
|
private readonly List<ConditionedAnimation> animations = new List<ConditionedAnimation>();
|
|
private ConditionedAnimation currAnimation;
|
|
public SpriteAnimation CurrentAnimation => this.currAnimation?.Animation;
|
|
public AnimationFrame CurrentFrame => this.CurrentAnimation?.CurrentFrame;
|
|
public TextureRegion CurrentRegion => this.CurrentAnimation?.CurrentRegion;
|
|
|
|
public SpriteAnimationGroup Add(SpriteAnimation anim, Func<bool> condition) {
|
|
this.animations.Add(new ConditionedAnimation(anim, condition));
|
|
return this;
|
|
}
|
|
|
|
public void Update(GameTime time) {
|
|
if (this.currAnimation == null || !this.currAnimation.ShouldPlay()) {
|
|
this.currAnimation = null;
|
|
foreach (var anim in this.animations) {
|
|
if (anim.ShouldPlay()) {
|
|
this.currAnimation = anim;
|
|
anim.Animation.Restart();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (this.currAnimation != null)
|
|
this.currAnimation.Animation.Update(time);
|
|
}
|
|
|
|
}
|
|
|
|
internal class ConditionedAnimation {
|
|
|
|
public readonly SpriteAnimation Animation;
|
|
public readonly Func<bool> ShouldPlay;
|
|
|
|
public ConditionedAnimation(SpriteAnimation animation, Func<bool> shouldPlay) {
|
|
this.Animation = animation;
|
|
this.ShouldPlay = shouldPlay;
|
|
}
|
|
|
|
}
|
|
} |