AIContext.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // AIContext.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;
  15. #endregion
  16. namespace RobotGameData.AI
  17. {
  18. /// <summary>
  19. /// This manages A.I.
  20. /// If an A.I. class is added, it will get updated every time when it gets played.
  21. /// When an A.I. gets played, the A.I.’s former Finish event handler,
  22. /// which has not been replaced yet, will be called.
  23. /// And, then, when the context gets updated, the A.I.’s current
  24. /// Update event handler will be called.
  25. /// </summary>
  26. public class AIContext : GameNode
  27. {
  28. #region Fields
  29. /// <summary>
  30. /// If set to false, all of the related functions get turned off.
  31. /// </summary>
  32. bool activeOn = true;
  33. List<AIBase> aiList = new List<AIBase>();
  34. AIBase activeAI = null;
  35. AIBase nextAI = null;
  36. #endregion
  37. #region Properties
  38. public AIBase ActiveAI
  39. {
  40. get { return activeAI; }
  41. }
  42. public AIBase NextAI
  43. {
  44. get { return nextAI; }
  45. }
  46. #endregion
  47. #region Update
  48. /// <summary>
  49. /// It will process the A.I. which has been registered to the list.
  50. /// When the current A.I. is updated and then, when ActiveTime,
  51. /// which is specified in the current A.I.,
  52. /// becomes zero, it will be replaced with the next-to-be-executed A.I.
  53. /// </summary>
  54. protected override void OnUpdate(GameTime gameTime)
  55. {
  56. if ((ActiveAI == null && NextAI == null) || !activeOn) return;
  57. // Change to next AI
  58. if (ActiveAI == null)
  59. {
  60. PlayAI(NextAI, NextAI.ActiveTime);
  61. }
  62. else if (NextAI != null && ActiveAI.ActiveTime == 0.0f)
  63. {
  64. PlayAI(NextAI, NextAI.ActiveTime);
  65. nextAI = null;
  66. }
  67. else
  68. {
  69. // Process to current AI
  70. if (ActiveAI != null)
  71. {
  72. ActiveAI.Update(gameTime);
  73. if (ActiveAI.ActiveTime > 0.0f)
  74. ActiveAI.ActiveTime -=
  75. (float)gameTime.ElapsedGameTime.TotalSeconds;
  76. if( ActiveAI.ActiveTime < 0.0f)
  77. ActiveAI.ActiveTime = 0.0f;
  78. }
  79. }
  80. }
  81. #endregion
  82. #region A.I. Controls
  83. /// <summary>
  84. /// By using the index, it returns one from the A.I.
  85. /// classes which have been registered to the list.
  86. /// </summary>
  87. public AIBase FindAI(int index)
  88. {
  89. if (!activeOn) return null;
  90. return aiList[index];
  91. }
  92. /// <summary>
  93. /// By using the name, it returns one from the A.I.
  94. /// classes which have been registered to the list.
  95. /// </summary>
  96. public AIBase FindAI(string name)
  97. {
  98. if (!activeOn) return null;
  99. foreach (AIBase aiBase in aiList)
  100. {
  101. if (name == aiBase.Name)
  102. return aiBase;
  103. }
  104. return null;
  105. }
  106. /// <summary>
  107. /// it adds an A.I. with the specified name to the list.
  108. /// </summary>
  109. public int AddAI(string name, AIBase aiBase)
  110. {
  111. if (!activeOn) return -1;
  112. aiBase.Name = name;
  113. aiList.Add(aiBase);
  114. return aiList.IndexOf(aiBase);
  115. }
  116. /// <summary>
  117. /// by using the index of A.I., it removes an A.I. from the registered list.
  118. /// </summary>
  119. public void RemoveAI(int index)
  120. {
  121. aiList.RemoveAt(index);
  122. }
  123. /// <summary>
  124. /// it removes every registered A.I.
  125. /// </summary>
  126. public void RemoveAllAI()
  127. {
  128. aiList.Clear();
  129. }
  130. /// <summary>
  131. /// by using the index of the registered A.I.,
  132. /// it specifies the next-to-be-executed A.I.
  133. /// The active time of the next-to-be-executed A.I. is specified by activeTime.
  134. /// </summary>
  135. public void SetNextAI(int index, float activeTime)
  136. {
  137. if (!activeOn) return;
  138. nextAI = FindAI(index);
  139. nextAI.ActiveTime = activeTime;
  140. }
  141. /// <summary>
  142. /// A new A.I. will be played.
  143. /// The active time of the A.I. is specified by activeTime.
  144. /// The A.I., which has been active so far, will be ignored.
  145. /// </summary>
  146. public void StartAI(int index, float activeTime)
  147. {
  148. if (!activeOn) return;
  149. activeAI = null;
  150. nextAI = null;
  151. PlayAI(index, activeTime);
  152. }
  153. /// <summary>
  154. /// The specified A.I. will be played.
  155. /// The active time of the A.I. is specified by activeTime.
  156. /// The A.I., which has been active so far, will call the
  157. /// Finish event handler and will be replaced by the newly specified A.I.
  158. /// When the newly specified A.I. start activating,
  159. /// Start event handler is called.
  160. /// </summary>
  161. private void PlayAI(int index, float activeTime)
  162. {
  163. if (!activeOn) return;
  164. AIBase aiBase = FindAI(index);
  165. PlayAI(aiBase, activeTime);
  166. }
  167. /// <summary>
  168. /// The specified A.I. will be played.
  169. /// The active time of the A.I. is specified by activeTime.
  170. /// The A.I., which has been active so far, will call the
  171. /// Finish event handler and will be replaced by the newly specified A.I.
  172. /// When the newly specified A.I. start activating,
  173. /// Start event handler is called.
  174. /// </summary>
  175. private void PlayAI(AIBase aiBase, float activeTime)
  176. {
  177. if (!activeOn) return;
  178. // Call the finish event of the previous A.I.
  179. if (activeAI != null)
  180. activeAI.AIFinish();
  181. // Set to new active A.I.
  182. activeAI = aiBase;
  183. activeAI.ActiveTime = activeTime;
  184. // Call start event of new A.I.
  185. activeAI.AIStart();
  186. }
  187. #endregion
  188. }
  189. }