PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Graphics/Animation.cs

https://bitbucket.org/ioachim/thefat
C# | 220 lines | 179 code | 33 blank | 8 comment | 17 complexity | f53fd07027f3f17a14dfc79b2b36a8fd MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Audio;
  9. namespace TheFat
  10. {
  11. public class SoundEffectArgs : EventArgs
  12. {
  13. public string SoundName
  14. {
  15. get;
  16. protected set;
  17. }
  18. public Cue Sound
  19. {
  20. get;
  21. set;
  22. }
  23. public SoundEffectArgs(string soundName) {
  24. SoundName = soundName;
  25. }
  26. }
  27. class Animation : ICloneable
  28. {
  29. public String animationName;
  30. //public string myName;
  31. List<string> assetNames;
  32. List<Texture2D> frames;
  33. double playTime;
  34. int currentFrameIndex;
  35. bool frameJustChanged;
  36. bool animationJustChanged;
  37. bool animationJustRestarted;
  38. public event EventHandler<SoundEffectArgs> SoundEffect;
  39. public string Sound
  40. {
  41. get;
  42. set;
  43. }
  44. public List<KeyValuePair<int, string>> FrameSounds
  45. {
  46. get;
  47. set;
  48. }
  49. public Boolean Finished
  50. {
  51. get {
  52. return (playTime / FrameDelay > frames.Count);
  53. }
  54. }
  55. public Boolean Repeat
  56. {
  57. get;
  58. set;
  59. }
  60. public float FrameDelay
  61. {
  62. get;
  63. set;
  64. }
  65. public Texture2D CurrentFrame
  66. {
  67. get;
  68. protected set;
  69. }
  70. public int CurrentFrameIndex
  71. {
  72. get { return currentFrameIndex;}
  73. protected set
  74. {
  75. currentFrameIndex = value;
  76. CurrentFrame = frames[value];
  77. }
  78. }
  79. public Animation(IEnumerable<string> assetNamesParam, float frameDelay)
  80. {
  81. this.assetNames = assetNamesParam.ToList();
  82. this.FrameDelay = frameDelay;
  83. this.frames = new List<Texture2D>(this.assetNames.Count);
  84. this.frameJustChanged = true;
  85. this.animationJustChanged = true;
  86. this.animationJustRestarted = true;
  87. FrameSounds = new List<KeyValuePair<int, string>>();
  88. Repeat = false;
  89. }
  90. public Animation(List<string> assetNamesParam, float frameDelay, List<KeyValuePair<int, string>> frameSounds)
  91. {
  92. this.assetNames = assetNamesParam;
  93. this.FrameDelay = frameDelay;
  94. this.frames = new List<Texture2D>(this.assetNames.Count);
  95. this.FrameSounds = frameSounds;
  96. this.frameJustChanged = true;
  97. this.animationJustChanged = true;
  98. this.animationJustRestarted = true;
  99. Repeat = false;
  100. }
  101. public Animation(List<string> assetNamesParam, float frameDelay, string sound)
  102. {
  103. this.assetNames = assetNamesParam;
  104. this.FrameDelay = frameDelay;
  105. this.frames = new List<Texture2D>(this.assetNames.Count);
  106. this.Sound = sound;
  107. this.frameJustChanged = true;
  108. this.animationJustChanged = true;
  109. this.animationJustRestarted = true;
  110. FrameSounds = new List<KeyValuePair<int, string>>();
  111. Repeat = false;
  112. }
  113. public void LoadContent(ContentManager content)
  114. {
  115. foreach (string asset in assetNames)
  116. {
  117. frames.Add(content.Load<Texture2D>(asset));
  118. }
  119. CurrentFrameIndex = 0;
  120. }
  121. public void Update(GameTime gameTime)
  122. {
  123. playTime += gameTime.ElapsedGameTime.TotalMilliseconds;
  124. int index = (int)(playTime / FrameDelay) % frames.Count;
  125. int frameIndex = 0;
  126. //condicion de repetir animacion
  127. //determina el frame actual correspondiente
  128. if (!this.Repeat && (playTime / FrameDelay) > frames.Count)
  129. {
  130. frameIndex = frames.Count - 1;
  131. }
  132. else frameIndex = index;
  133. if (CurrentFrameIndex != frameIndex)
  134. {
  135. frameJustChanged = true;
  136. if (CurrentFrameIndex > frameIndex)
  137. {
  138. this.animationJustRestarted = true;
  139. }
  140. }
  141. //frames
  142. if (frameJustChanged)
  143. {
  144. //sonido
  145. if (FrameSounds != null)
  146. {
  147. for (int i = CurrentFrameIndex; i <= frameIndex; i++)
  148. {
  149. foreach (var pair in FrameSounds.Where(pair => pair.Key == i))
  150. {
  151. SoundEffect(this, new SoundEffectArgs(pair.Value));
  152. }
  153. }
  154. }
  155. //grafica
  156. CurrentFrameIndex = frameIndex;
  157. frameJustChanged = false;
  158. }
  159. //animacion
  160. if (animationJustRestarted)
  161. {
  162. //sonido
  163. if (Sound != null)
  164. {
  165. if (SoundEffect != null)
  166. {
  167. SoundEffect(this, new SoundEffectArgs(Sound));
  168. }
  169. }
  170. animationJustRestarted = false;
  171. }
  172. if (animationJustChanged)
  173. {
  174. animationJustChanged = false;
  175. }
  176. }
  177. public Object Clone()
  178. {
  179. Animation toReturn = (Animation)this.MemberwiseClone();
  180. return toReturn;
  181. }
  182. public void Reset()
  183. {
  184. playTime = 0;
  185. }
  186. }
  187. }