From fee17578627092935ba422ce8d06bc9c34753c93 Mon Sep 17 00:00:00 2001 From: quinnvoker Date: Sat, 17 Nov 2018 11:24:20 -0800 Subject: [PATCH] Migration of Animation Code to External Library Created the AnimationLibrary library to hold both Animation and AnimatedSprite. --- AnimationLibrary/AnimatedSprite.cs | 79 +++++++++++++++++++++ AnimationLibrary/Animation.cs | 31 ++++++++ AnimationLibrary/AnimationLibrary.csproj | 62 ++++++++++++++++ AnimationLibrary/Properties/AssemblyInfo.cs | 36 ++++++++++ AnimationLibrary/packages.config | 5 ++ PuzzSwap.sln | 6 ++ 6 files changed, 219 insertions(+) create mode 100644 AnimationLibrary/AnimatedSprite.cs create mode 100644 AnimationLibrary/Animation.cs create mode 100644 AnimationLibrary/AnimationLibrary.csproj create mode 100644 AnimationLibrary/Properties/AssemblyInfo.cs create mode 100644 AnimationLibrary/packages.config diff --git a/AnimationLibrary/AnimatedSprite.cs b/AnimationLibrary/AnimatedSprite.cs new file mode 100644 index 0000000..7d4afa7 --- /dev/null +++ b/AnimationLibrary/AnimatedSprite.cs @@ -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); + } + } +} diff --git a/AnimationLibrary/Animation.cs b/AnimationLibrary/Animation.cs new file mode 100644 index 0000000..2014dca --- /dev/null +++ b/AnimationLibrary/Animation.cs @@ -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; + } + } +} diff --git a/AnimationLibrary/AnimationLibrary.csproj b/AnimationLibrary/AnimationLibrary.csproj new file mode 100644 index 0000000..a1420f1 --- /dev/null +++ b/AnimationLibrary/AnimationLibrary.csproj @@ -0,0 +1,62 @@ + + + + + Debug + AnyCPU + {69DADC27-FBEB-4625-9212-6EB5BED5102A} + Library + Properties + AnimationLibrary + AnimationLibrary + v4.5 + 512 + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\MonoGame.Extended.1.1.0\lib\portable-net45+win8+wpa81\MonoGame.Extended.dll + + + False + C:\Program Files (x86)\MonoGame\v3.0\Assemblies\DesktopGL\MonoGame.Framework.dll + + + ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AnimationLibrary/Properties/AssemblyInfo.cs b/AnimationLibrary/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..83114ef --- /dev/null +++ b/AnimationLibrary/Properties/AssemblyInfo.cs @@ -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")] diff --git a/AnimationLibrary/packages.config b/AnimationLibrary/packages.config new file mode 100644 index 0000000..5207eb6 --- /dev/null +++ b/AnimationLibrary/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/PuzzSwap.sln b/PuzzSwap.sln index d1f4a58..5abe745 100644 --- a/PuzzSwap.sln +++ b/PuzzSwap.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27703.2047 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PuzzSwap", "PuzzSwap\PuzzSwap.csproj", "{CD753795-0CB4-4F05-A0E7-2D2EFC968D82}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnimationLibrary", "AnimationLibrary\AnimationLibrary.csproj", "{69DADC27-FBEB-4625-9212-6EB5BED5102A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {CD753795-0CB4-4F05-A0E7-2D2EFC968D82}.Debug|Any CPU.Build.0 = Debug|Any CPU {CD753795-0CB4-4F05-A0E7-2D2EFC968D82}.Release|Any CPU.ActiveCfg = Release|Any CPU {CD753795-0CB4-4F05-A0E7-2D2EFC968D82}.Release|Any CPU.Build.0 = Release|Any CPU + {69DADC27-FBEB-4625-9212-6EB5BED5102A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {69DADC27-FBEB-4625-9212-6EB5BED5102A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {69DADC27-FBEB-4625-9212-6EB5BED5102A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {69DADC27-FBEB-4625-9212-6EB5BED5102A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE