Restructured Solution Layout

Moved several enums and helper classes into the QURO library for flexibility and potential reuse.

Also solved desync in animations by making AnimatedSprites reset their timer every time a new animation is loaded.
This commit is contained in:
quinnvoker
2018-11-17 20:54:01 -08:00
parent fb85769677
commit 9a25f068fc
9 changed files with 40 additions and 13 deletions
+88
View File
@@ -0,0 +1,88 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using QURO;
namespace QURO.AnimationLibrary
{
public class AnimatedSprite
{
public Texture2D SpriteSheet { get; set; }
private Animation currentAnimation;
public Animation CurrentAnimation
{
get { return currentAnimation; }
set
{
currentAnimation = value;
finishedPlaying = false;
CurrentFrameIndex = 0;
timer = CurrentFrame.Delay;
}
}
public int CurrentFrameIndex { get; set; }
public Frame CurrentFrame => CurrentAnimation.Frames[CurrentFrameIndex];
private float timer;
private bool finishedPlaying;
public AnimatedSprite() { }
public AnimatedSprite(Texture2D spriteSheet, Animation startingAnimation)
{
SpriteSheet = spriteSheet;
CurrentAnimation = startingAnimation;
timer = CurrentFrame.Delay;
finishedPlaying = false;
}
public void Reset()
{
CurrentFrameIndex = 0;
finishedPlaying = false;
timer = CurrentFrame.Delay;
}
public void Update(GameTime gameTime)
{
if (!CurrentAnimation.IsStillImage && !finishedPlaying)
{
if (timer <= 0)
{
if (CurrentFrameIndex < CurrentAnimation.Frames.Length - 1)
{
CurrentFrameIndex++;
}
else
{
if (CurrentAnimation.IsLooping)
{
CurrentFrameIndex = 0;
}
else
{
finishedPlaying = true;
}
}
timer = CurrentFrame.Delay;
}
else
{
timer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
}
}
}
public void Draw(SpriteBatch spriteBatch, Vector2 position, Color tint)
{
spriteBatch.Draw(SpriteSheet, position, CurrentFrame.SourceRect, tint);
}
public void Draw(SpriteBatch spriteBatch, Vector2 position, Vector2 spriteOffset, Color tint)
{
Rectangle sourceRect = CurrentFrame.SourceRect;
sourceRect.Offset(spriteOffset);
spriteBatch.Draw(SpriteSheet, position, sourceRect, tint);
}
}
}
+36
View File
@@ -0,0 +1,36 @@
using Microsoft.Xna.Framework;
namespace QURO.AnimationLibrary
{
public class Animation
{
public string Name { get; set; }
public bool IsLooping { get; set; }
public Frame[] Frames { get; set; }
public bool IsStillImage => Frames.Length <= 1;
public Animation() { }
public Animation(string name, Frame[] frames, bool loop)
{
Name = name;
Frames = frames;
IsLooping = loop;
}
public Animation(string name, Rectangle[] sourceRects, float delay, bool loop)
{
Name = name;
Frames = new Frame[sourceRects.Length];
for(int index = 0; index < sourceRects.Length; index++)
{
Frames[index] = new Frame(sourceRects[index], delay);
}
IsLooping = loop;
}
}
}
+29
View File
@@ -0,0 +1,29 @@
using System.Collections.Generic;
namespace QURO.AnimationLibrary
{
public class AnimationSet
{
public Dictionary<string, Animation> Anims;
public AnimationSet()
{
Anims = new Dictionary<string, Animation>();
}
public void Add(Animation anim)
{
Anims.Add(anim.Name, anim);
}
public void Remove(Animation anim)
{
Anims.Remove(anim.Name);
}
public void Remove(string animName)
{
Anims.Remove(animName);
}
}
}
+18
View File
@@ -0,0 +1,18 @@
using Microsoft.Xna.Framework;
namespace QURO.AnimationLibrary
{
public class Frame
{
public Rectangle SourceRect { get; set; }
public float Delay { get; set; }
public Frame() { }
public Frame(Rectangle sourceRectangle, float delay)
{
SourceRect = sourceRectangle;
Delay = delay;
}
}
}
@@ -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,61 @@
<?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>QURO.AnimationLibrary</RootNamespace>
<AssemblyName>QURO.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.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="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="AnimationSet.cs" />
<Compile Include="Frame.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\QURO\QURO.csproj">
<Project>{4581e0c8-b14a-454d-9a7b-52d37535e7e9}</Project>
<Name>QURO</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>