Migration of Animation Code to External Library
Created the AnimationLibrary library to hold both Animation and AnimatedSprite.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MonoGame.Extended.TextureAtlases;
|
||||
|
||||
namespace QURO.AnimationLibrary
|
||||
{
|
||||
public class AnimatedSprite
|
||||
{
|
||||
public TextureAtlas CurrentTextureAtlas { get; set; }
|
||||
private Animation currentAnimation;
|
||||
public Animation CurrentAnimation
|
||||
{
|
||||
get { return currentAnimation; }
|
||||
set
|
||||
{
|
||||
currentAnimation = value;
|
||||
CurrentFrameIndex = 0;
|
||||
}
|
||||
}
|
||||
public int CurrentFrameIndex { get; set; }
|
||||
public TextureRegion2D CurrentFrame => CurrentAnimation.Frames[CurrentFrameIndex];
|
||||
|
||||
private float timer;
|
||||
|
||||
public AnimatedSprite() { }
|
||||
|
||||
public AnimatedSprite(TextureAtlas textureAtlas, Animation startingAnimation)
|
||||
{
|
||||
CurrentTextureAtlas = textureAtlas;
|
||||
CurrentAnimation = startingAnimation;
|
||||
timer = CurrentAnimation.Delay;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
CurrentFrameIndex = 0;
|
||||
timer = CurrentAnimation.Delay;
|
||||
}
|
||||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
if (!CurrentAnimation.IsStillImage)
|
||||
{
|
||||
if (timer <= 0)
|
||||
{
|
||||
if (CurrentFrameIndex < CurrentAnimation.Frames.Length - 1)
|
||||
{
|
||||
CurrentFrameIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (CurrentAnimation.IsLooping)
|
||||
{
|
||||
CurrentFrameIndex = 0;
|
||||
}
|
||||
}
|
||||
timer = CurrentAnimation.Delay;
|
||||
}
|
||||
else
|
||||
{
|
||||
timer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Draw(SpriteBatch spriteBatch, Vector2 position, Color tint)
|
||||
{
|
||||
spriteBatch.Draw(CurrentTextureAtlas.Texture, position, CurrentFrame.Bounds, tint);
|
||||
}
|
||||
|
||||
public void Draw(SpriteBatch spriteBatch, Vector2 position, Vector2 spriteOffset, Color tint)
|
||||
{
|
||||
Rectangle sourceRect = CurrentFrame.Bounds;
|
||||
sourceRect.Offset(spriteOffset);
|
||||
|
||||
spriteBatch.Draw(CurrentTextureAtlas.Texture, position, sourceRect, tint);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using MonoGame.Extended.TextureAtlases;
|
||||
|
||||
namespace QURO.AnimationLibrary
|
||||
{
|
||||
public class Animation
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public bool IsLooping { get; set; }
|
||||
public float Delay { get; set; }
|
||||
|
||||
public TextureRegion2D[] Frames { get; set; }
|
||||
|
||||
public bool IsStillImage => Frames.Length <= 1;
|
||||
|
||||
public Animation() { }
|
||||
|
||||
public Animation(string name, TextureAtlas textureAtlas, string[] frameNames, float delaySetting = 0.2f, bool willLoop = false)
|
||||
{
|
||||
Name = name;
|
||||
|
||||
Frames = new TextureRegion2D[frameNames.Length];
|
||||
|
||||
for (int index = 0; index < frameNames.Length; index++)
|
||||
{
|
||||
Frames[index] = textureAtlas.GetRegion(frameNames[index]);
|
||||
}
|
||||
IsLooping = willLoop;
|
||||
Delay = delaySetting;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{69DADC27-FBEB-4625-9212-6EB5BED5102A}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>AnimationLibrary</RootNamespace>
|
||||
<AssemblyName>AnimationLibrary</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="MonoGame.Extended, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MonoGame.Extended.1.1.0\lib\portable-net45+win8+wpa81\MonoGame.Extended.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MonoGame.Framework, Version=3.8.0.76, Culture=neutral, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>C:\Program Files (x86)\MonoGame\v3.0\Assemblies\DesktopGL\MonoGame.Framework.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AnimatedSprite.cs" />
|
||||
<Compile Include="Animation.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("AnimationLibrary")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("AnimationLibrary")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2018")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("69dadc27-fbeb-4625-9212-6eb5bed5102a")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="MonoGame.Extended" version="1.1.0" targetFramework="net461" />
|
||||
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user