Skip to content

MonoGame Module

Gabriel Carollo edited this page Aug 13, 2024 · 4 revisions

MonoGame Module

This section explains the installation process and use of the MonoGame module of the Library.

1. Installation Process

There are many ways to install the package, here are some:

1.1 .NET CLI

Open your terminal of choice and past the following command:

dotnet add package AsepriteImporter.MG

1.2 Package Manager

On Visual Studio, open the Package Manager Console.

Tools > NuGet Package Manager > Package Manager Console

Package Manager Console

And paste the following command:

NuGet\Install-Package AsepriteImporter.MG

1.3 Package Reference

Edit your project file (.csproj) and paste this line:

<PackageReference Include="AsepriteImporter.MG"/>

2. Animating with the AsepriteAnimator

The AsepriteAnimator class can be used to handle animations of a sprite sheet.

To create an instance, it requires loaded AsepriteSheet (more about it on the "Core" page), and a counter object.

Documentation Page.

2.1 The Counter Object

To allow a more flexible implementation of counting up the frames, and not tie it to MonoGame's IGameComponent system, the animator uses an instance of a object that implements the IAnimationCounter interface.

2.2 Built-in Counters

You can skip implementing the IAnimationCounter interface by using one of two(2) built-in counters. Both work with MonoGame's IGameComponent system, and implement the IDrawable interface, therefore, you must provide either the main Game instance, or its GameComponentCollection reference.

Both set their DrawOrder to int.MinValue, so they run before any other drawable component, but if needed, you can provide a custom drawOrder, as an optional parameter.

  • DrawTimeCounter: counts the amount of milliseconds passed since last frame.
  • DrawFrameCounter: counts up (+1) for every time its Draw(GameTime) method is called.

ⓘ QUICK INFO Generally, you want to use the DrawTimeCounter, since Aseprite stores frame times as milliseconds.

EXAMPLE:

private IAnimationCounter GetCounter(Game game)
{
    return new DrawTimeCounter(game);
}

2.3 Implementing a Custom Counter

The only required method in the IAnimationCounter interface, is the GetCount() method, whose purpose is to provide the current count of the counter. The way your counter actually increases, is up to you.

EXAMPLE:

public class MyCounter : IAnimationCounter
{
    private double _count;

    public double GetCount()
    {
        // Counts up every time GetCount() is requested
        _count += 1;        
        return _count;
    }
}

Documentation Page.

3. Drawing the Frame

The animator itself doesn't Draw the texture, rather, you call the GetFrame(string) method, passing in the name of the animation, which returns a Rectangle representing the area on the texture corresponding to the current frame.

Since the animator uses a single counter, it doesn't matter how many times you call GetFrame(string) repeatedly, the frame will wrap based on the total duration of the animation.

EXAMPLE:

private void Draw(GameTime gameTime)
{
    Rectangle playerFrame = animator.GetFrame("player.idle");
    Rectangle monsterFrame = animator.GetFrame("monster.idle");

    spriteBatch.Begin();

    spriteBatch.Draw(texture, position, playerFrame, Color.White);
    spriteBatch.Draw(texture, position, monsterFrame, Color.White);

    spriteBatch.End();
}

Documentation Page.

4. Full Documentation

To go more in depth about all the MonoGame library's methods and classes, you may find the full documentation here.


By Varollo.