2
0

TimeSequence.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // TimeSequence.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.Render;
  15. #endregion
  16. namespace RobotGameData.ParticleSystem
  17. {
  18. #region Enum
  19. public enum SequenceStyle
  20. {
  21. /// <summary>
  22. /// N/A
  23. /// </summary>
  24. None = 0,
  25. /// <summary>
  26. /// has been disabled and does not process any info.
  27. /// </summary>
  28. Disable,
  29. }
  30. public enum SequenceState
  31. {
  32. /// <summary>
  33. /// N/A
  34. /// </summary>
  35. None = 0,
  36. /// <summary>
  37. /// currently in active.
  38. /// </summary>
  39. Active,
  40. }
  41. #endregion
  42. #region TimeSequenceInfo
  43. /// <summary>
  44. /// time information contains particle information.
  45. /// Contains information on the start time and the life time.
  46. /// </summary>
  47. [Serializable]
  48. public class TimeSequenceInfo
  49. {
  50. public float StartTime = 0.0f;
  51. public float Duration = 0.0f;
  52. public SequenceStyle Style = SequenceStyle.None;
  53. public ParticleInfo ParticleInfo = null;
  54. public TextureSequence TextureSequence = null;
  55. }
  56. #endregion
  57. #region TimeSequenceData
  58. /// <summary>
  59. /// a data class that updates the current state by using time information.
  60. /// </summary>
  61. public class TimeSequenceData
  62. {
  63. #region Fields
  64. TimeSequenceInfo sequenceInfo;
  65. GameSceneNode owner = null;
  66. SequenceStyle style = SequenceStyle.None;
  67. SequenceState state = SequenceState.None;
  68. #endregion
  69. #region Properties
  70. public SequenceStyle Style
  71. {
  72. get { return style; }
  73. set { style = value; }
  74. }
  75. public SequenceState State
  76. {
  77. get { return state; }
  78. set { state = value; }
  79. }
  80. public GameSceneNode Owner
  81. {
  82. get { return owner; }
  83. set { owner = value; }
  84. }
  85. public TimeSequenceInfo SequenceInfo
  86. {
  87. get { return sequenceInfo; }
  88. }
  89. #endregion
  90. #region Constructors
  91. public TimeSequenceData(TimeSequenceInfo info)
  92. {
  93. sequenceInfo = info;
  94. if (sequenceInfo.ParticleInfo != null)
  95. sequenceInfo.ParticleInfo.Initialize();
  96. Style = info.Style;
  97. }
  98. #endregion
  99. }
  100. #endregion
  101. #region TimeSequence
  102. /// <summary>
  103. /// It has all particles. By using TimeSequenceData,
  104. /// it calculates the lifetime of each Particle class.
  105. /// It also has Reset(), Stop() function which control all particle simultaneously.
  106. /// </summary>
  107. public class TimeSequence : GameSceneNode
  108. {
  109. #region Fields
  110. List<TimeSequenceData> timeSequenceDataList = new List<TimeSequenceData>();
  111. float localTIme = 0.0f;
  112. float duration = 0.0f;
  113. bool active = false; // default is false
  114. bool infinite = false;
  115. #endregion
  116. #region Properties
  117. public bool IsActive
  118. {
  119. get { return active; }
  120. }
  121. public bool IsInfinite
  122. {
  123. get { return infinite; }
  124. }
  125. public float LocalTime
  126. {
  127. get { return localTIme; }
  128. set { localTIme = value; }
  129. }
  130. public int Count
  131. {
  132. get { return timeSequenceDataList.Count; }
  133. }
  134. #endregion
  135. /// <summary>
  136. /// enables/disables the scene owner
  137. /// </summary>
  138. /// <param name="data">time data</param>
  139. /// <param name="enable">enable flag</param>
  140. public static void SetOwnerEnable(TimeSequenceData data, bool enable)
  141. {
  142. if (data.Owner != null)
  143. {
  144. data.Owner.Enabled = enable;
  145. data.Owner.Visible = enable;
  146. }
  147. }
  148. /// <summary>
  149. /// adds a new time data.
  150. /// </summary>
  151. /// <param name="data">new time data</param>
  152. public void AddSequence(TimeSequenceData data)
  153. {
  154. timeSequenceDataList.Add(data);
  155. SetOwnerEnable(data, false);
  156. UpdateDuration();
  157. }
  158. /// <summary>
  159. /// removes the time data.
  160. /// </summary>
  161. /// <param name="data">the time data</param>
  162. public void RemoveSequence(TimeSequenceData data)
  163. {
  164. timeSequenceDataList.Remove(data);
  165. }
  166. /// <summary>
  167. /// removes time data by the index.
  168. /// </summary>
  169. /// <param name="index">an index of time data</param>
  170. public void RemoveSequene(int index)
  171. {
  172. timeSequenceDataList.RemoveAt(index);
  173. UpdateDuration();
  174. }
  175. /// <summary>
  176. /// removes all time data
  177. /// </summary>
  178. public void RemoveAllSequence()
  179. {
  180. timeSequenceDataList.Clear();
  181. duration = 0.0f;
  182. }
  183. /// <summary>
  184. /// gets the time data by index.
  185. /// </summary>
  186. /// <param name="index">an index of time data</param>
  187. /// <returns>the time data</returns>
  188. public TimeSequenceData GetSequence(int index)
  189. {
  190. return timeSequenceDataList[index];
  191. }
  192. /// <summary>
  193. /// calcurates the duration time.
  194. /// </summary>
  195. public void UpdateDuration()
  196. {
  197. duration = 0.0f;
  198. infinite = false;
  199. for (int i = 0; i < timeSequenceDataList.Count; i++)
  200. {
  201. TimeSequenceInfo info = timeSequenceDataList[i].SequenceInfo;
  202. if (info.Duration <= 0.0f)
  203. infinite = true;
  204. if (info.StartTime + info.Duration > duration)
  205. duration = info.StartTime + info.Duration;
  206. }
  207. }
  208. /// <summary>
  209. /// resets local time.
  210. /// stops when the owner is particle and plays.
  211. /// </summary>
  212. public void Reset()
  213. {
  214. localTIme = 0.0f;
  215. active = true;
  216. for (int i = 0; i < timeSequenceDataList.Count; i++)
  217. {
  218. TimeSequenceData data = timeSequenceDataList[i];
  219. data.State = SequenceState.None;
  220. SetOwnerEnable(data, false);
  221. if (data.Owner is Particle)
  222. {
  223. Particle particle = data.Owner as Particle;
  224. particle.Stop();
  225. particle.Start();
  226. }
  227. }
  228. }
  229. /// <summary>
  230. /// disables the owner.
  231. /// Stops when the owner is particle.
  232. /// </summary>
  233. public void Stop()
  234. {
  235. localTIme = 0.0f;
  236. active = false;
  237. for (int i = 0; i < timeSequenceDataList.Count; i++)
  238. {
  239. TimeSequenceData data = timeSequenceDataList[i];
  240. data.State = SequenceState.None;
  241. SetOwnerEnable(data, false);
  242. if (data.Owner is Particle)
  243. {
  244. Particle particle = data.Owner as Particle;
  245. particle.Stop();
  246. }
  247. }
  248. }
  249. /// <summary>
  250. /// configures the reference transform matrix.
  251. /// </summary>
  252. /// <param name="enabled"></param>
  253. /// <param name="matrix"></param>
  254. public void SetRefMatrix(bool enabled, Matrix? matrix)
  255. {
  256. for (int i = 0; i < timeSequenceDataList.Count; i++)
  257. {
  258. TimeSequenceData data = timeSequenceDataList[i];
  259. if (data.Owner is Particle)
  260. {
  261. Particle particle = data.Owner as Particle;
  262. particle.SetRefMatrixEnable(enabled);
  263. particle.SetRefMatrix(matrix);
  264. }
  265. }
  266. }
  267. /// <summary>
  268. /// updates every time data that has been registered to the list.
  269. /// Time data whose start time has passed gets enabled.
  270. /// Time data whose duration time has passed gets disabled.
  271. /// </summary>
  272. /// <param name="gameTime"></param>
  273. protected override void OnUpdate(GameTime gameTime)
  274. {
  275. if (!infinite && !active)
  276. return;
  277. for (int i = 0; i < timeSequenceDataList.Count; i++)
  278. {
  279. TimeSequenceData data = timeSequenceDataList[i];
  280. if (data.Style == SequenceStyle.Disable)
  281. continue;
  282. if (data.State == SequenceState.Active)
  283. {
  284. // If activate
  285. if (data.SequenceInfo.Duration > 0.0f)
  286. {
  287. // If the Duration is 0, time is infinite
  288. if (localTIme >= (data.SequenceInfo.StartTime +
  289. data.SequenceInfo.Duration))
  290. {
  291. data.State = SequenceState.None;
  292. SetOwnerEnable(data, false);
  293. }
  294. }
  295. }
  296. else
  297. {
  298. // Starting time..
  299. if (localTIme >= data.SequenceInfo.StartTime)
  300. {
  301. data.State = SequenceState.Active;
  302. SetOwnerEnable(data, true);
  303. }
  304. }
  305. }
  306. localTIme += (float)gameTime.ElapsedGameTime.TotalSeconds;
  307. // Finished
  308. if (localTIme > duration)
  309. {
  310. active = false;
  311. }
  312. }
  313. }
  314. #endregion
  315. }