GameAnimateModel.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameAnimateModel.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using RobotGameData.Render;
  17. using RobotGameData.GameObject;
  18. using RobotGameData.Resource;
  19. using RobotGameData.Collision;
  20. #endregion
  21. namespace RobotGameData.GameObject
  22. {
  23. /// <summary>
  24. /// It has the Model’s animation data and processes the animate function
  25. /// of the Model.
  26. /// </summary>
  27. public class GameAnimateModel : GameModel
  28. {
  29. #region Fields
  30. protected List<AnimationBlender> animationBlenderList =
  31. new List<AnimationBlender>();
  32. protected List<AnimationSequence> animationList =
  33. new List<AnimationSequence>();
  34. protected bool traceAnimation = false;
  35. #endregion
  36. #region Properties
  37. public int AnimationCount
  38. {
  39. get { return animationList.Count; }
  40. }
  41. #endregion
  42. /// <summary>
  43. /// Constructor.
  44. /// </summary>
  45. /// <param name="resource">model resource</param>
  46. public GameAnimateModel(GameResourceModel resource)
  47. : base(resource) {}
  48. /// <summary>
  49. /// Constructor.
  50. /// </summary>
  51. /// <param name="fileName">model file name</param>
  52. public GameAnimateModel(string fileName)
  53. : base( fileName) {}
  54. public override void Initialize()
  55. {
  56. base.Initialize();
  57. }
  58. protected override void UnloadContent()
  59. {
  60. base.UnloadContent();
  61. }
  62. /// <summary>
  63. /// animates the skeleton of bones using stored key frame
  64. /// in the AnimationBlender.
  65. /// </summary>
  66. protected override void OnUpdate(GameTime gameTime)
  67. {
  68. // Reset the bone's transform by source transform
  69. this.ModelData.model.CopyBoneTransformsFrom(this.ModelData.boneTransforms);
  70. if (animationList.Count > 0)
  71. {
  72. for(int i=0; i<this.ModelData.model.Bones.Count; i++)
  73. {
  74. ModelBone bone = this.ModelData.model.Bones[i];
  75. AnimationBlender Blender = animationBlenderList[i];
  76. if (Blender.IsEmpty == false)
  77. {
  78. // if exist KeyFrameSequence in the AnimationBinder,
  79. // calculates the animation key frame matrix
  80. if (Blender.AnimationBinder != null)
  81. {
  82. // gets calculated animation key frame on this time
  83. bone.Transform =
  84. Blender.GetKeyFrameMatrix(
  85. (float)gameTime.ElapsedGameTime.TotalSeconds);
  86. }
  87. }
  88. }
  89. }
  90. base.OnUpdate(gameTime);
  91. }
  92. protected override void OnReset()
  93. {
  94. base.OnReset();
  95. }
  96. protected override void OnDraw(RenderTracer renderTracer)
  97. {
  98. base.OnDraw(renderTracer);
  99. }
  100. /// <summary>
  101. /// processes the necessary steps for binding a model.
  102. /// It creates AnimationBlender to the number of the model's bone.
  103. /// </summary>
  104. public override void BindModel(ModelData modelData)
  105. {
  106. base.BindModel(modelData);
  107. if (animationBlenderList.Count == 0)
  108. {
  109. // Insert all bones in the AnimationBinder
  110. for (int i = 0; i < modelData.model.Bones.Count; i++)
  111. {
  112. AnimationBlender animationBlender = new AnimationBlender();
  113. animationBlender.Name = modelData.model.Bones[i].Name;
  114. animationBlenderList.Add(animationBlender);
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// adds an animation to the list.
  120. /// </summary>
  121. /// <param name="animation">animation structure</param>
  122. /// <returns>index of the added animation</returns>
  123. public int AddAnimation(AnimationSequence animation)
  124. {
  125. if (animation == null)
  126. {
  127. throw new ArgumentNullException("animation");
  128. }
  129. animationList.Add(animation);
  130. return animationList.IndexOf(animation);
  131. }
  132. /// <summary>
  133. /// adds an animation to the list.
  134. /// </summary>
  135. /// <param name="fileName">animation file (.Animation)</param>
  136. /// <returns>index of the added animation</returns>
  137. public int AddAnimation(string fileName)
  138. {
  139. // Load and find an animation resource
  140. GameResourceAnimation resource =
  141. FrameworkCore.ResourceManager.LoadAnimation(fileName);
  142. return AddAnimation(resource.Animation);
  143. }
  144. /// <summary>
  145. /// removes the animation by index.
  146. /// </summary>
  147. public bool RemoveAnimation(int index)
  148. {
  149. animationList.RemoveAt(index);
  150. return false;
  151. }
  152. /// <summary>
  153. /// remove all stored animations.
  154. /// </summary>
  155. public void ClearAnimationAll()
  156. {
  157. animationList.Clear();
  158. }
  159. /// <summary>
  160. /// gets the animation structure by index.
  161. /// </summary>
  162. /// <param name="index">stored animation index</param>
  163. /// <returns>animation structure</returns>
  164. public AnimationSequence GetAnimation(int index)
  165. {
  166. return animationList[index];
  167. }
  168. public AnimationBlender FindAnimationBlenderByBoneName(string boneName)
  169. {
  170. for (int i = 0; i < animationBlenderList.Count; i++)
  171. {
  172. if (animationBlenderList[i].Name == boneName)
  173. return animationBlenderList[i];
  174. }
  175. return null;
  176. }
  177. /// <summary>
  178. /// plays the bone animation by index.
  179. /// </summary>
  180. /// <param name="index">stored animation index</param>
  181. /// <param name="playMode">animation play mode</param>
  182. /// <returns></returns>
  183. public bool PlayAnimation(int index, AnimPlayMode playMode)
  184. {
  185. return PlayAnimation(index, 0.0f, 0.0f, 1.0f, playMode);
  186. }
  187. /// <summary>
  188. /// plays the bone animation by index.
  189. /// </summary>
  190. /// <param name="index">stored animation index</param>
  191. /// <param name="startTime">begin time of the animation</param>
  192. /// <param name="blendTime">blending time of the animation</param>
  193. /// <param name="timeScaleFactor">
  194. /// time scale of the animation (default is 1.0)
  195. /// </param>
  196. /// <param name="playMode">animation play mode</param>
  197. /// <returns></returns>
  198. public bool PlayAnimation(int index, float startTime, float blendTime,
  199. float timeScaleFactor, AnimPlayMode playMode)
  200. {
  201. AnimationSequence animation = GetAnimation(index);
  202. if (animation != null)
  203. {
  204. // Binding the playable AnimationSequence to AnimationBinder
  205. for( int i=0; i<animation.KeyFrameSequences.Count; i++)
  206. {
  207. KeyFrameSequence sequence = animation.KeyFrameSequences[i];
  208. AnimationBlender blender =
  209. FindAnimationBlenderByBoneName(sequence.BoneName);
  210. if (blender == null)
  211. {
  212. throw new InvalidOperationException(
  213. "The animation specified a bone (\"" + sequence.BoneName +
  214. "\") that the model (\"" + this.Name + "\") does not have.");
  215. }
  216. // Initialize KeyFrameSequence infomation
  217. blender.AddKeyFrameSequence(sequence, startTime, blendTime,
  218. timeScaleFactor, playMode);
  219. }
  220. if (traceAnimation)
  221. {
  222. System.Diagnostics.Debug.WriteLine(
  223. string.Format("Play Animtion : {0} ({1})", Name, index));
  224. }
  225. return true;
  226. }
  227. return false;
  228. }
  229. /// <summary>
  230. /// gets animation playing time of the bone
  231. /// </summary>
  232. /// <param name="boneName">bone's name</param>
  233. /// <returns>current playing time</returns>
  234. public float GetBonePlayTime(string boneName)
  235. {
  236. for (int i = 0; i < animationBlenderList.Count; i++)
  237. {
  238. if (animationBlenderList[i].Name == boneName)
  239. {
  240. return animationBlenderList[i].AnimationBinder.LocalTime;
  241. }
  242. }
  243. return 0.0f;
  244. }
  245. }
  246. }