CombatAction.cs 9.7 KB

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