Animation.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //-----------------------------------------------------------------------------
  2. // Animation.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework.Content;
  8. using System;
  9. using System.Diagnostics;
  10. using System.Xml;
  11. using System.Xml.Linq;
  12. namespace RolePlaying.Data
  13. {
  14. /// <summary>
  15. /// An animation description for an AnimatingSprite object.
  16. /// </summary>
  17. #if !XBOX
  18. [DebuggerDisplay("Name = {Name}")]
  19. #endif
  20. public class Animation : ContentObject
  21. {
  22. /// <summary>
  23. /// The name of the animation.
  24. /// </summary>
  25. private string name;
  26. /// <summary>
  27. /// The name of the animation.
  28. /// </summary>
  29. [ContentSerializer(Optional = true)]
  30. public string Name
  31. {
  32. get { return name; }
  33. set { name = value; }
  34. }
  35. /// <summary>
  36. /// The first frame of the animation.
  37. /// </summary>
  38. private int startingFrame;
  39. /// <summary>
  40. /// The first frame of the animation.
  41. /// </summary>
  42. public int StartingFrame
  43. {
  44. get { return startingFrame; }
  45. set { startingFrame = value; }
  46. }
  47. /// <summary>
  48. /// The last frame of the animation.
  49. /// </summary>
  50. private int endingFrame;
  51. /// <summary>
  52. /// The last frame of the animation.
  53. /// </summary>
  54. public int EndingFrame
  55. {
  56. get { return endingFrame; }
  57. set { endingFrame = value; }
  58. }
  59. /// <summary>
  60. /// The interval between frames of the animation.
  61. /// </summary>
  62. private int interval;
  63. /// <summary>
  64. /// The interval between frames of the animation.
  65. /// </summary>
  66. public int Interval
  67. {
  68. get { return interval; }
  69. set { interval = value; }
  70. }
  71. /// <summary>
  72. /// If true, the animation loops.
  73. /// </summary>
  74. private bool isLoop;
  75. /// <summary>
  76. /// If true, the animation loops.
  77. /// </summary>
  78. public bool IsLoop
  79. {
  80. get { return isLoop; }
  81. set { isLoop = value; }
  82. }
  83. /// <summary>
  84. /// Creates a new Animation object.
  85. /// </summary>
  86. public Animation() { }
  87. /// <summary>
  88. /// Creates a new Animation object by full specification.
  89. /// </summary>
  90. public Animation(string name, int startingFrame, int endingFrame, int interval,
  91. bool isLoop)
  92. {
  93. this.Name = name;
  94. this.StartingFrame = startingFrame;
  95. this.EndingFrame = endingFrame;
  96. this.Interval = interval;
  97. this.IsLoop = isLoop;
  98. }
  99. /// <summary>
  100. /// Read an Animation object from the content pipeline.
  101. /// </summary>
  102. public class AnimationReader : ContentTypeReader<Animation>
  103. {
  104. /// <summary>
  105. /// Read an Animation object from the content pipeline.
  106. /// </summary>
  107. protected override Animation Read(ContentReader input,
  108. Animation existingInstance)
  109. {
  110. Animation animation = existingInstance;
  111. if (animation == null)
  112. {
  113. animation = new Animation();
  114. }
  115. animation.AssetName = input.AssetName;
  116. animation.Name = input.ReadString();
  117. animation.StartingFrame = input.ReadInt32();
  118. animation.EndingFrame = input.ReadInt32();
  119. animation.Interval = input.ReadInt32();
  120. animation.IsLoop = input.ReadBoolean();
  121. return animation;
  122. }
  123. }
  124. internal static Animation Load(XElement animationElement, ContentManager contentManager)
  125. {
  126. var animation = new Animation
  127. {
  128. AssetName = animationElement?.Value,
  129. Name = animationElement?.Element("Name")?.Value,
  130. StartingFrame = (int?)animationElement?.Element("StartingFrame") ?? 0,
  131. EndingFrame = (int?)animationElement?.Element("EndingFrame") ?? 0,
  132. Interval = (int?)animationElement?.Element("Interval") ?? 100,
  133. IsLoop = (bool?)animationElement?.Element("IsLoop") ?? false,
  134. };
  135. return animation;
  136. }
  137. }
  138. }