MeleeCombatAction.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MeleeCombatAction.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 Microsoft.Xna.Framework;
  12. using RolePlayingGameData;
  13. using Microsoft.Xna.Framework.Graphics;
  14. #endregion
  15. namespace RolePlaying
  16. {
  17. /// <summary>
  18. /// A melee-attack combat action, including related data and calculations.
  19. /// </summary>
  20. class MeleeCombatAction : CombatAction
  21. {
  22. #region State
  23. /// <summary>
  24. /// Returns true if the action is offensive, targeting the opponents.
  25. /// </summary>
  26. public override bool IsOffensive
  27. {
  28. get { return true; }
  29. }
  30. /// <summary>
  31. /// Returns true if this action requires a target.
  32. /// </summary>
  33. public override bool IsTargetNeeded
  34. {
  35. get { return true; }
  36. }
  37. #endregion
  38. #region Advancing/Returning Data
  39. /// <summary>
  40. /// The speed at which the advancing character moves, in units per second.
  41. /// </summary>
  42. private const float advanceSpeed = 300f;
  43. /// <summary>
  44. /// The offset from the advance destination to the target position
  45. /// </summary>
  46. private static readonly Vector2 advanceOffset = new Vector2(85f, 0f);
  47. /// <summary>
  48. /// The direction of the advancement.
  49. /// </summary>
  50. private Vector2 advanceDirection;
  51. /// <summary>
  52. /// The distance covered so far by the advance/return action
  53. /// </summary>
  54. private float advanceDistanceCovered = 0f;
  55. /// <summary>
  56. /// The total distance between the original combatant position and the target.
  57. /// </summary>
  58. private float totalAdvanceDistance;
  59. #endregion
  60. #region Combat Stage
  61. /// <summary>
  62. /// Starts a new combat stage. Called right after the stage changes.
  63. /// </summary>
  64. /// <remarks>The stage never changes into NotStarted.</remarks>
  65. protected override void StartStage()
  66. {
  67. switch (stage)
  68. {
  69. case CombatActionStage.Preparing: // called from Start()
  70. {
  71. // play the animation
  72. combatant.CombatSprite.PlayAnimation("Idle");
  73. }
  74. break;
  75. case CombatActionStage.Advancing:
  76. {
  77. // play the animation
  78. combatant.CombatSprite.PlayAnimation("Walk");
  79. // calculate the advancing destination
  80. if (Target.Position.X > Combatant.Position.X)
  81. {
  82. advanceDirection = Target.Position -
  83. Combatant.OriginalPosition - advanceOffset;
  84. }
  85. else
  86. {
  87. advanceDirection = Target.Position -
  88. Combatant.OriginalPosition + advanceOffset;
  89. }
  90. totalAdvanceDistance = advanceDirection.Length();
  91. advanceDirection.Normalize();
  92. advanceDistanceCovered = 0f;
  93. }
  94. break;
  95. case CombatActionStage.Executing:
  96. {
  97. // play the animation
  98. combatant.CombatSprite.PlayAnimation("Attack");
  99. // play the audio
  100. Weapon weapon = combatant.Character.GetEquippedWeapon();
  101. if (weapon != null)
  102. {
  103. AudioManager.PlayCue(weapon.SwingCueName);
  104. }
  105. else
  106. {
  107. AudioManager.PlayCue("StaffSwing");
  108. }
  109. }
  110. break;
  111. case CombatActionStage.Returning:
  112. {
  113. // play the animation
  114. combatant.CombatSprite.PlayAnimation("Walk");
  115. // calculate the damage
  116. Int32Range damageRange = combatant.Character.TargetDamageRange +
  117. combatant.Statistics.PhysicalOffense;
  118. Int32Range defenseRange = Target.Character.HealthDefenseRange +
  119. Target.Statistics.PhysicalDefense;
  120. int damage = Math.Max(0,
  121. damageRange.GenerateValue(Session.Random) -
  122. defenseRange.GenerateValue(Session.Random));
  123. // apply the damage
  124. if (damage > 0)
  125. {
  126. // play the audio
  127. Weapon weapon = combatant.Character.GetEquippedWeapon();
  128. if (weapon != null)
  129. {
  130. AudioManager.PlayCue(weapon.HitCueName);
  131. }
  132. else
  133. {
  134. AudioManager.PlayCue("StaffHit");
  135. }
  136. // damage the target
  137. Target.DamageHealth(damage, 0);
  138. if ((weapon != null) && (weapon.Overlay != null))
  139. {
  140. weapon.Overlay.PlayAnimation(0);
  141. weapon.Overlay.ResetAnimation();
  142. }
  143. }
  144. }
  145. break;
  146. case CombatActionStage.Finishing:
  147. {
  148. // play the animation
  149. combatant.CombatSprite.PlayAnimation("Idle");
  150. }
  151. break;
  152. case CombatActionStage.Complete:
  153. {
  154. // play the animation
  155. combatant.CombatSprite.PlayAnimation("Idle");
  156. }
  157. break;
  158. }
  159. }
  160. /// <summary>
  161. /// Update the action for the current stage.
  162. /// </summary>
  163. /// <remarks>
  164. /// This function is guaranteed to be called at least once per stage.
  165. /// </remarks>
  166. protected override void UpdateCurrentStage(GameTime gameTime)
  167. {
  168. float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
  169. switch (stage)
  170. {
  171. case CombatActionStage.Advancing:
  172. {
  173. // move to the destination
  174. if (advanceDistanceCovered < totalAdvanceDistance)
  175. {
  176. advanceDistanceCovered = Math.Min(advanceDistanceCovered +
  177. advanceSpeed * elapsedSeconds, totalAdvanceDistance);
  178. }
  179. // update the combatant's position
  180. combatant.Position = combatant.OriginalPosition +
  181. advanceDirection * advanceDistanceCovered;
  182. }
  183. break;
  184. case CombatActionStage.Returning:
  185. {
  186. // move to the destination
  187. if (advanceDistanceCovered > 0f)
  188. {
  189. advanceDistanceCovered -= advanceSpeed * elapsedSeconds;
  190. }
  191. combatant.Position = combatant.OriginalPosition +
  192. advanceDirection * advanceDistanceCovered;
  193. }
  194. break;
  195. }
  196. }
  197. /// <summary>
  198. /// Returns true if the combat action is ready to proceed to the next stage.
  199. /// </summary>
  200. protected override bool IsReadyForNextStage
  201. {
  202. get
  203. {
  204. switch (stage)
  205. {
  206. case CombatActionStage.Preparing: // ready to advance?
  207. return true;
  208. case CombatActionStage.Advancing: // ready to execute?
  209. if (advanceDistanceCovered >= totalAdvanceDistance)
  210. {
  211. advanceDistanceCovered = totalAdvanceDistance;
  212. combatant.Position = combatant.OriginalPosition +
  213. advanceDirection * totalAdvanceDistance;
  214. return true;
  215. }
  216. else
  217. {
  218. return false;
  219. }
  220. case CombatActionStage.Executing: // ready to return?
  221. return combatant.CombatSprite.IsPlaybackComplete;
  222. case CombatActionStage.Returning: // ready to finish?
  223. if (advanceDistanceCovered <= 0f)
  224. {
  225. advanceDistanceCovered = 0f;
  226. combatant.Position = combatant.OriginalPosition;
  227. return true;
  228. }
  229. else
  230. {
  231. return false;
  232. }
  233. case CombatActionStage.Finishing: // ready to complete?
  234. return true;
  235. }
  236. // fall through to the base behavior
  237. return base.IsReadyForNextStage;
  238. }
  239. }
  240. #endregion
  241. #region Heuristic
  242. /// <summary>
  243. /// The heuristic used to compare actions of this type to similar ones.
  244. /// </summary>
  245. public override int Heuristic
  246. {
  247. get
  248. {
  249. return combatant.Character.TargetDamageRange.Average;
  250. }
  251. }
  252. #endregion
  253. #region Initialization
  254. /// <summary>
  255. /// Constructs a new MeleeCombatAction object.
  256. /// </summary>
  257. /// <param name="character">The character performing the action.</param>
  258. public MeleeCombatAction(Combatant combatant)
  259. : base(combatant) { }
  260. #endregion
  261. #region Updating
  262. /// <summary>
  263. /// Updates the action over time.
  264. /// </summary>
  265. public override void Update(GameTime gameTime)
  266. {
  267. // update the weapon animation
  268. float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
  269. Weapon weapon = Combatant.Character.GetEquippedWeapon();
  270. if ((weapon != null) && (weapon.Overlay != null))
  271. {
  272. weapon.Overlay.UpdateAnimation(elapsedSeconds);
  273. }
  274. // update the action
  275. base.Update(gameTime);
  276. }
  277. #endregion
  278. #region Drawing
  279. /// <summary>
  280. /// Draw any elements of the action that are independent of the character.
  281. /// </summary>
  282. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  283. {
  284. // draw the weapon overlay (typically blood)
  285. Weapon weapon = Combatant.Character.GetEquippedWeapon();
  286. if ((weapon != null) && (weapon.Overlay != null) &&
  287. !weapon.Overlay.IsPlaybackComplete)
  288. {
  289. weapon.Overlay.Draw(spriteBatch, Target.Position, 0f);
  290. }
  291. base.Draw(gameTime, spriteBatch);
  292. }
  293. #endregion
  294. }
  295. }