Restruct: QURO << QURO.Animation; SpriteMapRegion -> Sprite: Now can draw itself

This commit is contained in:
quinnvoker
2018-12-19 09:16:17 -08:00
parent 06486e72fc
commit b8173d688d
19 changed files with 63 additions and 275 deletions
-112
View File
@@ -1,112 +0,0 @@
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.Origin, CurrentFrame.Bounds, tint);
if (CurrentFrame.SubSprites != null)
DrawSubSprites(spriteBatch, position, tint);
}
public void Draw(SpriteBatch spriteBatch, Vector2 position, Vector2 spriteMapOffset, Color tint, bool offsetSubSprites = false)
{
Rectangle sourceRect = CurrentFrame.Bounds;
sourceRect.Offset(spriteMapOffset);
spriteBatch.Draw(SpriteSheet, position, sourceRect, tint);
if (CurrentFrame.SubSprites != null)
DrawSubSprites(spriteBatch, position, tint, offsetSubSprites? (Vector2?)spriteMapOffset : null);
}
public void DrawSubSprites(SpriteBatch spriteBatch, Vector2 position, Color tint, Vector2? spriteMapOffset = null)
{
if (CurrentFrame.SubSprites.Count != CurrentFrame.SubSpritePositions.Count)
{
System.Console.WriteLine("SubSpriteCount and SubSpritePositionCount are not equal! Skipping subsprites...");
return;
}
for (int index = 0; index < CurrentFrame.SubSprites.Count; index++)
{
var subSprite = CurrentFrame.SubSprites[index];
Rectangle sourceRect = subSprite.Bounds;
if (spriteMapOffset != null)
sourceRect.Offset((Vector2)spriteMapOffset);
spriteBatch.Draw(SpriteSheet, position - subSprite.Origin + CurrentFrame.SubSpritePositions[index], sourceRect, tint);
}
}
}
}
-25
View File
@@ -1,25 +0,0 @@
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() : this("unnamed", new Frame[] { new Frame() }, false)
{
}
public Animation(string name, Frame[] frames, bool loop)
{
Name = name;
Frames = frames;
IsLooping = loop;
}
}
}
-29
View File
@@ -1,29 +0,0 @@
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);
}
}
}
-36
View File
@@ -1,36 +0,0 @@
using QURO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
namespace QURO.AnimationLibrary
{
public class Frame
{
public SpriteMapRegion Sprite { get; set; }
public List<SpriteMapRegion> SubSprites { get; set; }
public List<Vector2> SubSpritePositions { get; set; }
public float Delay { get; set; }
public string Name { get { return Sprite.Name; } }
public Rectangle Bounds { get { return Sprite.Bounds; } }
public Vector2 Origin { get { return Sprite.Origin; } }
public Frame() : this(new SpriteMapRegion(), 0, null, null)
{
}
public Frame(SpriteMapRegion sprite, float delay) : this(sprite, delay, null, null)
{
}
public Frame(SpriteMapRegion sprite, float delay, List<SpriteMapRegion> subSprites, List<Vector2> subSpritePositons)
{
Sprite = sprite;
Delay = delay;
SubSprites = subSprites;
SubSpritePositions = subSpritePositons;
}
}
}