Skip to content

Commit

Permalink
Update animated sprite so the constructor api is similar to original …
Browse files Browse the repository at this point in the history
…MGE (#906)

* Update animated sprite so the constructor api is similar to original MGE

* Fix tests from changes
  • Loading branch information
AristurtleDev authored Jul 1, 2024
1 parent 98ee05f commit 2ad7a9d
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 273 deletions.
4 changes: 2 additions & 2 deletions source/MonoGame.Extended/AnimationComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public class AnimationComponent : GameComponent
public AnimationComponent(Game game)
: base(game)
{
Animations = new List<Animation>();
Animations = new List<AnimationController>();
}

public List<Animation> Animations { get; }
public List<AnimationController> Animations { get; }

public override void Update(GameTime gameTime)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
namespace MonoGame.Extended.Animations;

/// <summary>
/// Represents an animation with various control features such as play, pause, stop, looping, reversing, and
/// ping-pong effects.
/// Represents an animation controller with features to play, pause, stop, reset, and set the state of
/// animation playback such as looping, reversing, and ping-pong effects.
/// </summary>
public class Animation : IAnimation
public class AnimationController : IAnimationController
{
private readonly IAnimationDefinition _definition;
private readonly IAnimation _definition;
private int _direction;

/// <summary>
Expand Down Expand Up @@ -44,7 +44,7 @@ public bool IsReversed
public double Speed { get; set; }

/// <inheritdoc />
public event Action<IAnimation, AnimationEventTrigger> OnAnimationEvent;
public event Action<IAnimationController, AnimationEventTrigger> OnAnimationEvent;

/// <inheritdoc />
public TimeSpan CurrentFrameTimeRemaining { get; private set; }
Expand All @@ -56,10 +56,10 @@ public bool IsReversed
public int FrameCount => _definition.FrameCount;

/// <summary>
/// Initializes a new instance of the <see cref="Animation"/> class with the specified definition.
/// Initializes a new instance of the <see cref="AnimationController"/> class with the specified definition.
/// </summary>
/// <param name="definition">The definition of the animation.</param>
public Animation(IAnimationDefinition definition)
public AnimationController(IAnimation definition)
{
_definition = definition;

Expand Down Expand Up @@ -101,7 +101,7 @@ public bool Play(int startingFrame)
{
if (startingFrame < 0 || startingFrame >= _definition.FrameCount)
{
throw new ArgumentOutOfRangeException(nameof(startingFrame), $"{nameof(startingFrame)} cannot be less than zero or greater than or equal to the total number of frames in this {nameof(Animation)}");
throw new ArgumentOutOfRangeException(nameof(startingFrame), $"{nameof(startingFrame)} cannot be less than zero or greater than or equal to the total number of frames in this {nameof(AnimationController)}");
}

// Cannot play something that is already playing
Expand Down Expand Up @@ -134,7 +134,7 @@ public void SetFrame(int index)
{
if (index < 0 || index >= _definition.FrameCount)
{
throw new ArgumentOutOfRangeException(nameof(index), $"{nameof(index)} cannot be less than zero or greater than or equal to the total number of frames in this {nameof(Animation)}");
throw new ArgumentOutOfRangeException(nameof(index), $"{nameof(index)} cannot be less than zero or greater than or equal to the total number of frames in this {nameof(AnimationController)}");
}

CurrentFrame = index;
Expand Down
6 changes: 3 additions & 3 deletions source/MonoGame.Extended/Animations/AnimationEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace MonoGame.Extended.Animations;
public class AnimationEvent : EventArgs
{
/// <summary>
/// Gets the animation associated with the event.
/// Gets the animation controller associated with the event.
/// </summary>
public IAnimation Animation { get; }
public IAnimationController Animation { get; }

/// <summary>
/// Gets the trigger that caused the event.
/// </summary>
public AnimationEventTrigger Trigger { get; }

internal AnimationEvent(IAnimation animation, AnimationEventTrigger trigger) => (Animation, Trigger) = (animation, trigger);
internal AnimationEvent(IAnimationController animation, AnimationEventTrigger trigger) => (Animation, Trigger) = (animation, trigger);
}
132 changes: 14 additions & 118 deletions source/MonoGame.Extended/Animations/IAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,147 +3,43 @@
// See LICENSE file in the project root for full license information.

using System;
using Microsoft.Xna.Framework;
using MonoGame.Extended.Graphics;

namespace MonoGame.Extended.Animations;

/// <summary>
/// Defines the interface for an animation with various control features such as play, pause, stop, looping, reversing, and ping-pong effects.
/// Defines the interface for an animation, specifying properties of the animation such as frames, looping, reversing,
/// and ping-pong effects.
/// </summary>
public interface IAnimation : IDisposable
public interface IAnimation
{
/// <summary>
/// Gets a value indicating whether the animation is paused.
/// Gets the name of the animation.
/// </summary>
bool IsPaused { get; }
string Name { get; }

/// <summary>
/// Gets a value indicating whether the animation is currently animating.
/// Gets the read-only collection of frames in the animation.
/// </summary>
bool IsAnimating { get; }

/// <summary>
/// Gets or sets a value indicating whether the animation should loop.
/// </summary>
bool IsLooping { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the animation is reversed.
/// </summary>
bool IsReversed { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the animation should ping-pong (reverse direction at the ends).
/// </summary>
bool IsPingPong { get; set; }

/// <summary>
/// Gets or sets the speed of the animation.
/// </summary>
/// <value>The speed cannot be less than zero.</value>
double Speed { get; set; }

/// <summary>
/// Gets or sets the action to perform when an animation event is triggered.
/// </summary>
event Action<IAnimation, AnimationEventTrigger> OnAnimationEvent;

/// <summary>
/// Gets the time remaining for the current frame.
/// </summary>
TimeSpan CurrentFrameTimeRemaining { get; }

/// <summary>
/// Gets the index of the current frame of the animation.
/// </summary>
int CurrentFrame { get; }
ReadOnlySpan<IAnimationFrame> Frames { get; }

/// <summary>
/// Gets the total number of frames in the animation.
/// </summary>
int FrameCount { get; }

/// <summary>
/// Sets the animation to a specified frame.
/// </summary>
/// <param name="index">The index of the frame to set.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when the <paramref name="index"/> parameter is less than zero or greater than or equal to the total
/// number of frames.
/// </exception>
void SetFrame(int index);

/// <summary>
/// Plays the animation from the beginning.
/// </summary>
/// <returns>
/// <see langword="true"/> if the animation was successfully started; otherwise, <see langword="false"/>.
/// </returns>
bool Play();

/// <summary>
/// Plays the animation from a specified starting frame.
/// </summary>
/// <param name="startingFrame">The frame to start the animation from.</param>
/// <returns>
/// <see langword="true"/> if the animation was successfully started; otherwise, <see langword="false"/>.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when the <paramref name="startingFrame"/> parameter is less than zero or greater than or equal to the
/// total number of frames.
/// </exception>
bool Play(int startingFrame);

/// <summary>
/// Pauses the animation.
/// </summary>
/// <returns>
/// <see langword="true"/> if the animation was successfully paused; otherwise, <see langword="false"/>.
/// </returns>
bool Pause();

/// <summary>
/// Pauses the animation.
/// </summary>
/// <param name="resetFrameDuration">If set to <see langword="true"/>, resets the frame duration.</param>
/// <returns>
/// <see langword="true"/> if the animation was successfully paused; otherwise, <see langword="false"/>.
/// </returns>
bool Pause(bool resetFrameDuration);

/// <summary>
/// Unpauses the animation.
/// </summary>
/// <returns>
/// <see langword="true"/> if the animation was successfully unpaused; otherwise, <see langword="false"/>.
/// </returns>
bool Unpause();

/// <summary>
/// Unpauses the animation.
/// </summary>
/// <param name="advanceToNextFrame">If set to <see langword="true"/>, advances to the next frame.</param>
/// <returns>
/// <see langword="true"/> if the animation was successfully unpaused; otherwise, <see langword="false"/>.
/// </returns>
bool Unpause(bool advanceToNextFrame);

/// <summary>
/// Updates the animation.
/// Gets a value indicating whether the animation should loop.
/// </summary>
/// <param name="gameTime">A snapshot of the timing values for the current update cycle.</param>
void Update(GameTime gameTime);
bool IsLooping { get; }

/// <summary>
/// Stops the animation.
/// Gets a value indicating whether the animation is reversed.
/// </summary>
/// <returns>
/// <see langword="true"/> if the animation was successfully stopped; otherwise, <see langword="false"/>.
/// </returns>
bool Stop();
bool IsReversed { get; }

/// <summary>
/// Resets the animation to its initial state.
/// Gets a value indicating whether the animation should ping-pong (reverse direction at the ends).
/// </summary>
void Reset();
bool IsPingPong { get; }
}
Loading

0 comments on commit 2ad7a9d

Please sign in to comment.