Hey ,
ich versuche derzeit in einen kleinen Spiel mehrere Animationen abzuspielen und zu warten bis alle vollständig abgespielt wurden (s.
Ich wäre froh, wenn ich mir helfen könntet
Spoiler anzeigen
Und falls das notwendig sein sollte:
Spoiler anzeigen
ich versuche derzeit in einen kleinen Spiel mehrere Animationen abzuspielen und zu warten bis alle vollständig abgespielt wurden (s.
DrawAndWait
). Dazu hab ich 2 Klassen erstellt (MultiAnimation
& Animation
) in welchen AnimatedSpriteSheet
's verwendet werden. Allerdings hab ich dort wahrscheinlich ein Denkfehler drinne. Denn entweder die Animation spielt nur für paar Millisekunden oder sie spielt gar nicht. Ich wäre froh, wenn ich mir helfen könntet
C#-Quellcode
- public class MultiAnimation : IUpdateable, IDrawable
- {
- public bool Enabled { get; set; }
- public Animation[] Animations { get; }
- public MultiAnimation(IList<AnimatedSpriteSheet> spriteSheets, IList<Rectangle> positions, bool enabled)
- {
- if (spriteSheets.Count != positions.Count && positions.Count != 1)
- throw new NotSupportedException("positions must have the same quantity as spriteSheets or 1");
- var anis = new List<Animation>();
- for (var i = 0; i < spriteSheets.Count; i++)
- {
- var sheet = spriteSheets[i];
- var rect = positions.Count == 1 ? positions[0] : positions[i];
- sheet.ActivateKeyframe(0);
- var images = ReflectionHelper<List<Keyframe>,
- AnimatedSpriteSheet>.GetPrivateField(sheet, "_keyframes",
- BindingFlags.NonPublic | BindingFlags.Instance);
- var ani = new Animation(sheet, images.Count, rect);
- anis.Add(ani);
- }
- Animations = anis.ToArray();
- Enabled = enabled;
- }
- public void Update(GameTime gameTime)
- {
- if (!Enabled) return;
- foreach (var ani in Animations)
- {
- ani.Sheet.Update(gameTime);
- }
- }
- public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
- {
- if (!Enabled) return;
- spriteBatch.Begin();
- foreach (var ani in Animations)
- {
- spriteBatch.DrawTexture(ani.Sheet, ani.Position, Color.White);
- }
- spriteBatch.End();
- }
- public void DrawAndWait(SpriteBatch batch, GameTime time, List<int> loops)
- {
- if (!Enabled) return;
- var aniList = Animations.ToList();
- var aniLoops = new Dictionary<Animation, int>();
- for (var i = 0; i < aniList.Count; i++)
- {
- var ani = aniList[i];
- var aniLoop = loops[i];
- aniLoops.Add(ani, aniLoop);
- }
- var aniTuple =
- aniList.Select(
- ani =>
- new Quartet<Animation, int, int, Keyframe> { Item1 = ani, Item2 = 0, Item3 = 0, Item4 = null })
- .ToList();
- while (!aniTuple.All(a => a.Item3 >= aniLoops[a.Item1]))
- {
- while (!aniTuple.All(a => a.Item2 >= a.Item1.Images))
- {
- foreach (var tuple in aniTuple.Where(a => a.Item2 < a.Item1.Images))
- {
- var ani = tuple.Item1;
- ani.Sheet.Update(time);
- if (tuple.Item4 != null && tuple.Item4 == ani.Sheet.CurrentKeyframe)
- {
- continue;
- }
- tuple.Item4 = ani.Sheet.CurrentKeyframe;
- if (tuple.Item4 == null)
- continue;
- tuple.Item2++;
- if (tuple.Item2 >= ani.Images)
- {
- tuple.Item3++;
- if (tuple.Item3 >= aniLoops[ani])
- continue;
- }
- batch.Begin();
- if (ani.Sheet.CurrentKeyframe != null)
- batch.DrawTexture(ani.Sheet, ani.Position, Color.White);
- batch.End();
- }
- }
- foreach (var tuple in aniTuple)
- {
- tuple.Item2 = 0;
- }
- }
- }
- }
- public class Animation
- {
- public AnimatedSpriteSheet Sheet { get; }
- public int Images { get; set; }
- public Rectangle Position { get; }
- public Animation(AnimatedSpriteSheet sheet, int images, Rectangle position)
- {
- Sheet = sheet;
- Images = images;
- Position = position;
- }
- }
Und falls das notwendig sein sollte:
C#-Quellcode
- public class Quartet<T1, T2, T3, T4>
- {
- public T1 Item1 { get; set; }
- public T2 Item2 { get; set; }
- public T3 Item3 { get; set; }
- public T4 Item4 { get; set; }
- }
- public static class ReflectionHelper<T, TI>
- {
- public static T GetPrivateField(TI parentClass, string name, BindingFlags flags)
- {
- var fieldInfo = parentClass.GetType().GetField(name, flags);
- if (fieldInfo != null)
- {
- var fi = fieldInfo.GetValue(parentClass);
- return (T)fi;
- }
- return default(T);
- }
- }
KaskadekingDE on GitHub
Bitte keine Fragen über Programmierung per PN! Dafür ist das Forum hier.
Who cares? ¯\_(ツ)_/¯
Bitte keine Fragen über Programmierung per PN! Dafür ist das Forum hier.
Who cares? ¯\_(ツ)_/¯