Advertisement
Guest User

XNA SpriteAnimationManager

a guest
Nov 16th, 2012
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7. using System.IO;
  8.  
  9. namespace spritesheetanimation
  10. {
  11.     // Animation frame class
  12.     public class Frame
  13.     {
  14.         // Frame Number
  15.         [XmlAttribute("Num")]
  16.         public int Num;
  17.  
  18.         // Sub Image X positon in the Sprite Sheet
  19.         [XmlAttribute("X")]
  20.         public int X;
  21.        
  22.         // Sub Image Y positon in the Sprite Sheet
  23.         [XmlAttribute("Y")]
  24.         public int Y;
  25.  
  26.         // Sub Image Width
  27.         [XmlAttribute("Width")]
  28.         public int Width;
  29.  
  30.         // Sub Image Height
  31.         [XmlAttribute("Height")]
  32.         public int Height;
  33.  
  34.         // The X offset of sub image
  35.         [XmlAttribute("OffSetX")]
  36.         public int OffsetX;
  37.  
  38.         // The Y offset fo sub image
  39.         [XmlAttribute("OffsetY")]
  40.         public int OffsetY;
  41.  
  42.         // The duration between two frames
  43.         [XmlAttribute("Duration")]
  44.         public float Duration;
  45.     }
  46.  
  47.     // Animaiton class to hold the name and frames
  48.     public class Animation
  49.     {
  50.         // Animation Name
  51.         [XmlAttribute("Name")]
  52.         public string Name;
  53.  
  54.         // Animation Frame Rate
  55.         [XmlAttribute("FrameRate")]
  56.         public int FrameRate;
  57.  
  58.         public bool Loop;
  59.  
  60.         public bool Pingpong;
  61.  
  62.         // The Frames array in an animation
  63.         [XmlArray("Frames"), XmlArrayItem("Frame", typeof(Frame))]
  64.         public Frame[] Frames;
  65.     }
  66.  
  67.     // The Sprite Texture stores the Sprite Sheet path.fr
  68.     public class SpriteTexture
  69.     {
  70.         // The Sprite Sheet texture file path
  71.         [XmlAttribute("Path")]
  72.         public string Path;
  73.     }
  74.  
  75.     // Aniamtion Set contains the Sprite Texture and Animaitons.
  76.     [XmlRoot("Animations")]
  77.     public class AnimationSet
  78.     {
  79.         // The sprite texture object
  80.         [XmlElement("Texture", typeof(SpriteTexture))]
  81.         public SpriteTexture SpriteTexture;
  82.  
  83.         // The animation array in the Animation Set
  84.         [XmlElement("Animation", typeof(Animation))]
  85.         public Animation[] Animations;
  86.     }
  87.  
  88.     // Sprite Animation Manager class
  89.     public static class SpriteAnimationManager
  90.     {
  91.         public static int AnimationCount;
  92.  
  93.         // Read the Sprite Sheet Description information from the description xml file
  94.         public static AnimationSet Read(string Filename)
  95.         {
  96.             AnimationSet animationSet = new AnimationSet();
  97.  
  98.             // Create a XML reader for the sprite sheet animaiton description file
  99.             using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(Filename))
  100.             {
  101.                 // Create a XMLSerializer for the AnimationSet
  102.                 XmlSerializer serializer = new XmlSerializer(typeof(AnimationSet));
  103.  
  104.                 // Deserialize the Animation Set from the XmlReader to the animation set object
  105.                 animationSet = (AnimationSet)serializer.Deserialize(reader);
  106.             }
  107.  
  108.             // Count the animations to Animation Count
  109.             AnimationCount = animationSet.Animations.Length;
  110.  
  111.             return animationSet;
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement