AnimationBinder.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // AnimationBinder.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 RobotGameData.Helper;
  15. #endregion
  16. namespace RobotGameData.GameObject
  17. {
  18. /// <summary>
  19. /// This class calculates the animation key frame for the bones of character
  20. /// with time flow.
  21. /// Each character bone corresponds to each AnimtionBinder and each has
  22. /// key frame for an animation.
  23. /// Therefore, in order to do the animation blend,
  24. /// two key frame values of AnimationBinder need to be combined.
  25. /// </summary>
  26. public class AnimationBinder
  27. {
  28. #region Fields
  29. AnimPlayMode playMode = AnimPlayMode.Repeat;
  30. float localTime = 0.0f;
  31. float timeScaleFactor = 1.0f;
  32. KeyFrameSequence keyFrameSequence = null;
  33. #endregion
  34. #region Properties
  35. public KeyFrameSequence KeyFrameSequence { get { return keyFrameSequence; } }
  36. public AnimPlayMode PlayMode { get { return playMode; } }
  37. public float LocalTime { get { return localTime; } }
  38. public float Duration { get { return KeyFrameSequence.Duration; } }
  39. public float ScaleFactor { get { return timeScaleFactor; } }
  40. public bool IsPlayDone { get { return (LocalTime >= Duration); } }
  41. #endregion
  42. /// <summary>
  43. /// sets the mode of the animation play.
  44. /// </summary>
  45. /// <param name="mode">animation mode</param>
  46. public void SetPlayMode(AnimPlayMode mode)
  47. {
  48. playMode = mode;
  49. }
  50. /// <summary>
  51. /// sets the animation’s local time.
  52. /// </summary>
  53. public void SetTime(float time)
  54. {
  55. localTime = time;
  56. }
  57. /// <summary>
  58. /// sets the time flow of animation.
  59. /// If the value is bigger than 1.0, the animate speed becomes faster.
  60. /// If lower than 1.0, the animate speed becomes lower.
  61. /// </summary>
  62. /// <param name="scaleFactor">time scale factor</param>
  63. public void SetTimeScaleFactor(float scaleFactor)
  64. {
  65. timeScaleFactor = scaleFactor;
  66. }
  67. /// <summary>
  68. /// sets the animation key frame structure.
  69. /// </summary>
  70. public void SetKeyFrameSequence(KeyFrameSequence keyFrame)
  71. {
  72. keyFrameSequence = keyFrame;
  73. }
  74. /// <summary>
  75. /// binds the animation key frame.
  76. /// </summary>
  77. /// <param name="keyFrame">Animation key frame sequence</param>
  78. /// <param name="startTime">Start time of the animaton</param>
  79. /// <param name="timeScaleFactor">Time scale of the animaton</param>
  80. /// <param name="mode">Play mode of the animaton</param>
  81. public void BindKeyFrameSequence(KeyFrameSequence keyFrame,
  82. float startTime,
  83. float timeScaleFactor,
  84. AnimPlayMode mode)
  85. {
  86. SetKeyFrameSequence(keyFrame);
  87. SetTime(startTime);
  88. SetTimeScaleFactor(timeScaleFactor);
  89. SetPlayMode(mode);
  90. }
  91. /// <summary>
  92. /// Initialize members
  93. /// </summary>
  94. public void Initialize()
  95. {
  96. SetKeyFrameSequence(null);
  97. SetTime(0.0f);
  98. SetTimeScaleFactor(1.0f);
  99. SetPlayMode(AnimPlayMode.Repeat);
  100. }
  101. /// <summary>
  102. /// Copies to target AnimationBinder
  103. /// </summary>
  104. /// <param name="target">target AnimationBinder</param>
  105. public void CopyTo(AnimationBinder target)
  106. {
  107. target.SetKeyFrameSequence(KeyFrameSequence);
  108. target.SetTime(LocalTime);
  109. target.SetTimeScaleFactor(ScaleFactor);
  110. target.SetPlayMode(PlayMode);
  111. }
  112. /// <summary>
  113. /// calculates the key frame of the animation of the specified time.
  114. /// </summary>
  115. /// <param name="time">animation time</param>
  116. /// <returns>animation key frame</returns>
  117. public KeyFrame GetKeyFrame(float time)
  118. {
  119. if (KeyFrameSequence == null)
  120. {
  121. throw new InvalidOperationException("Sequence is not set.");
  122. }
  123. // Don't do anything if the timeScaleFactor is 0. (default scale is 1.0)
  124. if (ScaleFactor != 0.0f)
  125. {
  126. // Accumulate animation local time
  127. localTime += (float)(ScaleFactor * time);
  128. switch (PlayMode)
  129. {
  130. case AnimPlayMode.Once: // Just play once
  131. {
  132. if (localTime > KeyFrameSequence.Duration)
  133. localTime = KeyFrameSequence.Duration;
  134. }
  135. break;
  136. case AnimPlayMode.Repeat: // Play looping
  137. {
  138. // Calculate time remainder after local time looping
  139. if (localTime > Duration)
  140. localTime = HelperMath.CalculateModulo(localTime,
  141. KeyFrameSequence.Duration);
  142. }
  143. break;
  144. default:
  145. throw new NotSupportedException("Not supported play mode");
  146. }
  147. return KeyFrameSequence.GetInterpolateKeyFrame(localTime, PlayMode);
  148. }
  149. return new KeyFrame();
  150. }
  151. /// <summary>
  152. /// calculates the key frame of the animation of the specified time.
  153. /// </summary>
  154. /// <param name="time">animation time</param>
  155. /// <returns>Matrix of the animation key frame</returns>
  156. public Matrix GetKeyFrameMatrix(float time)
  157. {
  158. if (KeyFrameSequence == null)
  159. {
  160. throw new InvalidOperationException("Sequence is not set.");
  161. }
  162. // Don't do anything if the timeScaleFactor is 0. (default scale is 1.0)
  163. if (ScaleFactor != 0.0f)
  164. {
  165. // Accumulate animation local time
  166. localTime += (float)(ScaleFactor * time);
  167. switch (PlayMode)
  168. {
  169. case AnimPlayMode.Once: // Just play once
  170. {
  171. if (localTime > KeyFrameSequence.Duration)
  172. localTime = KeyFrameSequence.Duration;
  173. }
  174. break;
  175. case AnimPlayMode.Repeat: // Play looping
  176. {
  177. // Calculate time remainder after local time looping
  178. if (localTime > Duration)
  179. localTime = HelperMath.CalculateModulo(localTime,
  180. KeyFrameSequence.Duration);
  181. }
  182. break;
  183. default:
  184. throw new NotSupportedException("Not supported play mode");
  185. }
  186. return KeyFrameSequence.GetInterpolateMatrix(localTime, PlayMode);
  187. }
  188. return Matrix.Identity;
  189. }
  190. }
  191. }