SpriteLibrary/SpriteLibrary/SpriteInfo.cs

109 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Resources;
namespace SpriteLibrary
{
public enum AnimationType { SpriteDefinition=0, Rotation=1, Mirror=2 }
public class AnimationInfo
{
public AnimationType FieldsToUse = AnimationType.SpriteDefinition;
public int AnimationToUse = 0;
public int RotationDegrees=0;
public bool MirrorHorizontally = false;
public bool MirrorVertically = false;
public Point StartPoint = new Point(-1, -1);
public string ImageName = "";
public int Width = -1;
public int Height = -1;
public int NumFrames = 1;
public int AnimSpeed = 200;
/// <summary>
/// A generic cloning method that works when everything is public
/// </summary>
/// <returns></returns>
public AnimationInfo Clone()
{
return SpriteDatabase.CloneByXMLSerializing<AnimationInfo>(this);
}
}
public class SpriteInfo
{
public string SpriteName = "";
public int ViewPercent = 100; //The percent size of the sprite. 100 is full. 50 is half-size
public List<AnimationInfo> Animations = new List<AnimationInfo>();
/// <summary>
/// A generic cloning method that works when everything is public
/// </summary>
/// <returns></returns>
public SpriteInfo Clone()
{
return SpriteDatabase.CloneByXMLSerializing<SpriteInfo>(this);
}
/// <summary>
/// Update the current SpriteInfo class such that it is identical to the class you are copying from.
/// </summary>
/// <param name="toCopyFrom">A spriteInfo class</param>
public void CopyFrom(SpriteInfo toCopyFrom)
{
if (toCopyFrom == null) return;
SpriteName = toCopyFrom.SpriteName;
ViewPercent = toCopyFrom.ViewPercent;
Animations.Clear();
foreach(AnimationInfo AI in toCopyFrom.Animations)
{
Animations.Add(AI.Clone());
}
}
public Sprite CreateSprite(SpriteController ControllerToUse, SpriteDatabase TheDatabaseToUse)
{
Sprite DestSprite = null;
for (int index = 0; index < Animations.Count; index++)
{
AnimationInfo CurrentAnimation = Animations[index];
Image myImage = TheDatabaseToUse.GetImageFromName(CurrentAnimation.ImageName, true);
if (myImage == null) return null; //break out if we do not have the image defined for this
AnimationType AT = CurrentAnimation.FieldsToUse;
if (index == 0) AT = AnimationType.SpriteDefinition; //the first one MUST be this.
switch(AT)
{
case AnimationType.SpriteDefinition:
if(DestSprite == null)//Creating the sprite from scratch
{
DestSprite = new Sprite(CurrentAnimation.StartPoint, ControllerToUse, myImage, CurrentAnimation.Width, CurrentAnimation.Height, CurrentAnimation.AnimSpeed, CurrentAnimation.NumFrames);
}
else
{
DestSprite.AddAnimation(CurrentAnimation.StartPoint, myImage, CurrentAnimation.Width, CurrentAnimation.Height, CurrentAnimation.AnimSpeed, CurrentAnimation.NumFrames);
}
break;
case AnimationType.Rotation:
DestSprite.AddAnimation(CurrentAnimation.AnimationToUse, CurrentAnimation.RotationDegrees);
break;
case AnimationType.Mirror:
DestSprite.AddAnimation(CurrentAnimation.AnimationToUse, CurrentAnimation.MirrorHorizontally,CurrentAnimation.MirrorVertically);
break;
}
}
int sizepercent = ViewPercent;
if (sizepercent < 5) sizepercent = 100;
if (sizepercent > 300) sizepercent = 100;
double delta = (double)sizepercent / 100.0; //turn it into a double, and into something we can multiply.
DestSprite.SetSize(new Size((int)(DestSprite.GetSize.Width * delta), (int)(DestSprite.GetSize.Height * delta)));
DestSprite.SetName(SpriteName);
//We have created a new sprite. Now, return a duplicate of that sprite.
return DestSprite;
}
}
}