Combatant.cs 8.8 KB

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