Skip to content

Commit

Permalink
feat: Animation End Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
BlurOne-GIT committed Mar 29, 2024
1 parent d31e504 commit 5ac3ddd
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion MmgEngine/Animation.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

Expand All @@ -7,11 +8,24 @@ public class Animation<T>
{
#region Fields
public T[] Frames { get; }
public int Position { get; set; }
public int Length => Frames.Length;
private int _position;
public int Position
{
get => _position;
set
{
_eventFired = false;
_position = value;
}
}
private int _frameDelay;
public bool IsLooped { get; set; }
public int FrameDuration { get; set; }
public bool Paused { get; set; } = false;
public bool IsAtEnd => Position >= Length;
private bool _eventFired;
public event EventHandler AnimationReachedEnd;
#endregion

public Animation(T[] frames, bool looped, int frameDuration = 1)
Expand All @@ -31,10 +45,18 @@ public T NextFrame()
return CurrentFrame();

if (Position >= Frames.Length)
{
if (!_eventFired)
{
AnimationReachedEnd?.Invoke(this, EventArgs.Empty);
_eventFired = true;
}

if (IsLooped)
Position = 0;
else
return Frames[Position-1];
}

if (--_frameDelay > 0)
return Frames[Position];
Expand Down

0 comments on commit 5ac3ddd

Please sign in to comment.