CombatAction.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CombatAction.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 RolePlayingGameData;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. #endregion
  15. namespace RolePlaying
  16. {
  17. /// <summary>
  18. /// An action taken by an individual character in combat.
  19. /// </summary>
  20. /// <remarks>
  21. /// Note that party actions, like Flee, are not represented here. These are only
  22. /// the actions that individual characters, on either side, can perform in combat.
  23. /// </remarks>
  24. abstract class CombatAction
  25. {
  26. #region State
  27. /// <summary>
  28. /// Returns true if the action is offensive, targeting the opponents.
  29. /// </summary>
  30. public abstract bool IsOffensive
  31. {
  32. get;
  33. }
  34. /// <summary>
  35. /// Returns true if this action requires a target.
  36. /// </summary>
  37. public abstract bool IsTargetNeeded
  38. {
  39. get;
  40. }
  41. #endregion
  42. #region Combat Stage
  43. /// <summary>
  44. /// Stages of the action as it is executed.
  45. /// </summary>
  46. public enum CombatActionStage
  47. {
  48. /// <summary>
  49. /// The initial state, with no action taken yet.
  50. /// </summary>
  51. NotStarted,
  52. /// <summary>
  53. /// The action is getting ready to start.
  54. /// </summary>
  55. /// <example>
  56. /// Spell actions stay in this stage while the casting animation plays.
  57. /// </example>
  58. Preparing,
  59. /// <summary>
  60. /// The effect is traveling to the target, if needed.
  61. /// </summary>
  62. /// <example>
  63. /// The character walks to melee targets while in this stage. Spell effects
  64. /// also travel to the target while in this state.
  65. /// </example>
  66. Advancing,
  67. /// <summary>
  68. /// The action is being applied to the target(s).
  69. /// </summary>
  70. Executing,
  71. /// <summary>
  72. /// The effect is returning from the target, if needed.
  73. /// </summary>
  74. /// <example>
  75. /// The character walks back from the melee target while in this stage.
  76. /// </example>
  77. Returning,
  78. /// <summary>
  79. /// The action is performing any final actions.
  80. /// </summary>
  81. Finishing,
  82. /// <summary>
  83. /// The action is complete.
  84. /// </summary>
  85. Complete,
  86. };
  87. /// <summary>
  88. /// The current state of the action.
  89. /// </summary>
  90. protected CombatActionStage stage = CombatActionStage.NotStarted;
  91. /// <summary>
  92. /// The current state of the action.
  93. /// </summary>
  94. public CombatActionStage Stage
  95. {
  96. get { return stage; }
  97. }
  98. /// <summary>
  99. /// Starts a new combat stage. Called right after the stage changes.
  100. /// </summary>
  101. /// <remarks>The stage never changes into NotStarted.</remarks>
  102. protected virtual void StartStage()
  103. {
  104. switch (stage)
  105. {
  106. case CombatActionStage.Preparing: // called from Start()
  107. break;
  108. case CombatActionStage.Advancing:
  109. break;
  110. case CombatActionStage.Executing:
  111. break;
  112. case CombatActionStage.Returning:
  113. break;
  114. case CombatActionStage.Finishing:
  115. break;
  116. case CombatActionStage.Complete:
  117. break;
  118. }
  119. }
  120. /// <summary>
  121. /// Update the action for the current stage.
  122. /// </summary>
  123. /// <remarks>
  124. /// This function is guaranteed to be called at least once per stage.
  125. /// </remarks>
  126. protected virtual void UpdateCurrentStage(GameTime gameTime)
  127. {
  128. switch (stage)
  129. {
  130. case CombatActionStage.NotStarted:
  131. break;
  132. case CombatActionStage.Preparing:
  133. break;
  134. case CombatActionStage.Advancing:
  135. break;
  136. case CombatActionStage.Executing:
  137. break;
  138. case CombatActionStage.Returning:
  139. break;
  140. case CombatActionStage.Finishing:
  141. break;
  142. case CombatActionStage.Complete:
  143. break;
  144. }
  145. }
  146. /// <summary>
  147. /// Returns true if the combat action is ready to proceed to the next stage.
  148. /// </summary>
  149. protected virtual bool IsReadyForNextStage
  150. {
  151. get
  152. {
  153. switch (stage)
  154. {
  155. case CombatActionStage.Preparing: // ready to advance?
  156. break;
  157. case CombatActionStage.Advancing: // ready to execute?
  158. break;
  159. case CombatActionStage.Executing: // ready to return?
  160. break;
  161. case CombatActionStage.Returning: // ready to finish?
  162. break;
  163. case CombatActionStage.Finishing: // ready to complete?
  164. break;
  165. }
  166. // fall through - the action doesn't care about the state, so move on
  167. return true;
  168. }
  169. }
  170. #endregion
  171. #region Combatant
  172. /// <summary>
  173. /// The character performing this action.
  174. /// </summary>
  175. protected Combatant combatant;
  176. /// <summary>
  177. /// The character performing this action.
  178. /// </summary>
  179. public Combatant Combatant
  180. {
  181. get { return combatant; }
  182. }
  183. /// <summary>
  184. /// Returns true if the character can use this action.
  185. /// </summary>
  186. public virtual bool IsCharacterValidUser
  187. {
  188. get { return true; }
  189. }
  190. #endregion
  191. #region Target
  192. /// <summary>
  193. /// The target of the action.
  194. /// </summary>
  195. public Combatant Target = null;
  196. /// <summary>
  197. /// The number of adjacent targets in each direction that are affected.
  198. /// </summary>
  199. protected int adjacentTargets = 0;
  200. /// <summary>
  201. /// The number of adjacent targets in each direction that are affected.
  202. /// </summary>
  203. public int AdjacentTargets
  204. {
  205. get { return adjacentTargets; }
  206. }
  207. #endregion
  208. #region Heuristic
  209. /// <summary>
  210. /// The heuristic used to compare actions of this type to similar ones.
  211. /// </summary>
  212. public abstract int Heuristic
  213. {
  214. get;
  215. }
  216. /// <summary>
  217. /// Compares the combat actions by their heuristic, in descending order.
  218. /// </summary>
  219. public static int CompareCombatActionsByHeuristic(
  220. CombatAction a, CombatAction b)
  221. {
  222. return b.Heuristic.CompareTo(a.Heuristic);
  223. }
  224. #endregion
  225. #region Initialization
  226. /// <summary>
  227. /// Constructs a new CombatAction object.
  228. /// </summary>
  229. /// <param name="combatant">The combatant performing the action.</param>
  230. public CombatAction(Combatant combatant)
  231. {
  232. // check the parameter
  233. if (combatant == null)
  234. {
  235. throw new ArgumentNullException("combatant");
  236. }
  237. // assign the parameter
  238. this.combatant = combatant;
  239. Reset();
  240. }
  241. /// <summary>
  242. /// Reset the action so that it may be started again.
  243. /// </summary>
  244. public virtual void Reset()
  245. {
  246. // set the state to not-started
  247. stage = CombatActionStage.NotStarted;
  248. }
  249. /// <summary>
  250. /// Start executing the combat action.
  251. /// </summary>
  252. public virtual void Start()
  253. {
  254. // set the state to the first step
  255. stage = CombatActionStage.Preparing;
  256. StartStage();
  257. }
  258. #endregion
  259. #region Updating
  260. /// <summary>
  261. /// Updates the action over time.
  262. /// </summary>
  263. public virtual void Update(GameTime gameTime)
  264. {
  265. // update the current stage
  266. UpdateCurrentStage(gameTime);
  267. // if the action is ready for the next stage, then advance
  268. if ((stage != CombatActionStage.NotStarted) &&
  269. (stage != CombatActionStage.Complete) && IsReadyForNextStage)
  270. {
  271. switch (stage)
  272. {
  273. case CombatActionStage.Preparing:
  274. stage = CombatActionStage.Advancing;
  275. break;
  276. case CombatActionStage.Advancing:
  277. stage = CombatActionStage.Executing;
  278. break;
  279. case CombatActionStage.Executing:
  280. stage = CombatActionStage.Returning;
  281. break;
  282. case CombatActionStage.Returning:
  283. stage = CombatActionStage.Finishing;
  284. break;
  285. case CombatActionStage.Finishing:
  286. stage = CombatActionStage.Complete;
  287. break;
  288. }
  289. StartStage();
  290. }
  291. }
  292. #endregion
  293. #region Drawing
  294. /// <summary>
  295. /// Draw any elements of the action that are independent of the character.
  296. /// </summary>
  297. public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch) { }
  298. #endregion
  299. }
  300. }