Combatant.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //-----------------------------------------------------------------------------
  2. // Combatant.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. /// Base class for all combatants.
  15. /// </summary>
  16. abstract class Combatant
  17. {
  18. /// <summary>
  19. /// The character encapsulated by this combatant.
  20. /// </summary>
  21. public abstract FightingCharacter Character
  22. {
  23. get;
  24. }
  25. /// <summary>
  26. /// The current state of this combatant.
  27. /// </summary>
  28. public abstract Character.CharacterState State
  29. {
  30. get;
  31. set;
  32. }
  33. /// <summary>
  34. /// Returns true if the character is dead or dying.
  35. /// </summary>
  36. public bool IsDeadOrDying
  37. {
  38. get
  39. {
  40. return ((State == RolePlayingGameData.Character.CharacterState.Dying) ||
  41. (State == RolePlayingGameData.Character.CharacterState.Dead));
  42. }
  43. }
  44. /// <summary>
  45. /// If true, the combatant has taken their turn this round.
  46. /// </summary>
  47. private bool isTurnTaken;
  48. /// <summary>
  49. /// If true, the combatant has taken their turn this round.
  50. /// </summary>
  51. public bool IsTurnTaken
  52. {
  53. get { return isTurnTaken; }
  54. set { isTurnTaken = value; }
  55. }
  56. /// <summary>
  57. /// Accessor for the combat sprite for this combatant.
  58. /// </summary>
  59. public abstract AnimatingSprite CombatSprite
  60. {
  61. get;
  62. }
  63. /// <summary>
  64. /// The current position on screen for this combatant.
  65. /// </summary>
  66. private Vector2 position;
  67. /// <summary>
  68. /// The current position on screen for this combatant.
  69. /// </summary>
  70. public Vector2 Position
  71. {
  72. get { return position; }
  73. set { position = value; }
  74. }
  75. /// <summary>
  76. /// The original position on screen for this combatant.
  77. /// </summary>
  78. private Vector2 originalPosition;
  79. /// <summary>
  80. /// The original position on screen for this combatant.
  81. /// </summary>
  82. public Vector2 OriginalPosition
  83. {
  84. get { return originalPosition; }
  85. set { originalPosition = value; }
  86. }
  87. /// <summary>
  88. /// The current statistics of this combatant.
  89. /// </summary>
  90. public abstract StatisticsValue Statistics
  91. {
  92. get;
  93. }
  94. /// <summary>
  95. /// Heals the combatant's health by the given amount.
  96. /// </summary>
  97. public void HealHealth(int healthHealingAmount, int duration)
  98. {
  99. Heal(new StatisticsValue(healthHealingAmount, 0, 0, 0, 0, 0), duration);
  100. }
  101. /// <summary>
  102. /// Heal the combatant by the given amount.
  103. /// </summary>
  104. public virtual void Heal(StatisticsValue healingStatistics, int duration)
  105. {
  106. CombatEngine.AddNewHealingEffects(OriginalPosition, healingStatistics);
  107. }
  108. /// <summary>
  109. /// Damages the combatant's health by the given amount.
  110. /// </summary>
  111. public void DamageHealth(int healthDamageAmount, int duration)
  112. {
  113. Damage(new StatisticsValue(healthDamageAmount, 0, 0, 0, 0, 0), duration);
  114. }
  115. /// <summary>
  116. /// Damages the combatant by the given amount.
  117. /// </summary>
  118. public virtual void Damage(StatisticsValue damageStatistics, int duration)
  119. {
  120. State = RolePlayingGameData.Character.CharacterState.Hit;
  121. CombatEngine.AddNewDamageEffects(OriginalPosition, damageStatistics);
  122. }
  123. /// <summary>
  124. /// Pay the cost for the given spell.
  125. /// </summary>
  126. /// <returns>True if the cost could be paid (and therefore was paid).</returns>
  127. public virtual bool PayCostForSpell(Spell spell) { return false; }
  128. /// <summary>
  129. /// The current combat action for this combatant.
  130. /// </summary>
  131. private CombatAction combatAction;
  132. /// <summary>
  133. /// The current combat action for this combatant.
  134. /// </summary>
  135. public CombatAction CombatAction
  136. {
  137. get { return combatAction; }
  138. set { combatAction = value; }
  139. }
  140. /// <summary>
  141. /// Statistics stack of the combat effects that are applied to this combatant.
  142. /// </summary>
  143. private StatisticsValueStack combatEffects = new StatisticsValueStack();
  144. /// <summary>
  145. /// Statistics stack of the combat effects that are applied to this combatant.
  146. /// </summary>
  147. public StatisticsValueStack CombatEffects
  148. {
  149. get { return combatEffects; }
  150. }
  151. /// <summary>
  152. /// Constructs a new Combatant object.
  153. /// </summary>
  154. protected Combatant() { }
  155. /// <summary>
  156. /// Update the combatant for this frame.
  157. /// </summary>
  158. public virtual void Update(GameTime gameTime)
  159. {
  160. float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
  161. // update the combat action
  162. if (combatAction != null)
  163. {
  164. // update the combat action
  165. combatAction.Update(gameTime);
  166. // remove the combat action if it is done and set the turn-taken flag
  167. if (combatAction.Stage == CombatAction.CombatActionStage.Complete)
  168. {
  169. combatAction = null;
  170. isTurnTaken = true;
  171. }
  172. }
  173. // update the combat sprite animation
  174. CombatSprite.UpdateAnimation(elapsedSeconds);
  175. // check for death
  176. if (!IsDeadOrDying && (Statistics.HealthPoints <= 0))
  177. {
  178. AudioManager.PlayCue("Death");
  179. State = RolePlayingGameData.Character.CharacterState.Dying;
  180. }
  181. // check for waking up
  182. else if (IsDeadOrDying && (Statistics.HealthPoints > 0))
  183. {
  184. State = RolePlayingGameData.Character.CharacterState.Idle;
  185. }
  186. else if (CombatSprite.IsPlaybackComplete)
  187. {
  188. if (State == RolePlayingGameData.Character.CharacterState.Hit)
  189. {
  190. State = RolePlayingGameData.Character.CharacterState.Idle;
  191. }
  192. else if (State == RolePlayingGameData.Character.CharacterState.Dying)
  193. {
  194. State = RolePlayingGameData.Character.CharacterState.Dead;
  195. }
  196. }
  197. }
  198. /// <summary>
  199. /// Advance the combatant state for one combat round.
  200. /// </summary>
  201. public virtual void AdvanceRound()
  202. {
  203. // advance the combat effects stack
  204. combatEffects.Advance();
  205. }
  206. /// <summary>
  207. /// Draw the combatant for this frame.
  208. /// </summary>
  209. public virtual void Draw(GameTime gameTime)
  210. {
  211. CombatSprite.Draw(Session.ScreenManager.SpriteBatch,
  212. Position, 1f - Position.Y / 720f);
  213. Session.ScreenManager.SpriteBatch.Draw(Character.ShadowTexture, Position,
  214. null, Color.White, 0f, new Vector2(Character.ShadowTexture.Width / 2,
  215. Character.ShadowTexture.Height / 2), 1f, SpriteEffects.None, 1f);
  216. // draw the combat action
  217. if (combatAction != null)
  218. {
  219. // update the combat action
  220. combatAction.Draw(gameTime, Session.ScreenManager.SpriteBatch);
  221. }
  222. }
  223. }
  224. }