CombatantPlayer.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CombatantPlayer.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. #endregion
  14. namespace RolePlaying
  15. {
  16. /// <summary>
  17. /// Encapsulates all of the combat-runtime data for a particular player combatant.
  18. /// </summary>
  19. class CombatantPlayer : Combatant
  20. {
  21. /// <summary>
  22. /// The Player object encapsulated by this object.
  23. /// </summary>
  24. private Player player;
  25. /// <summary>
  26. /// The Player object encapsulated by this object.
  27. /// </summary>
  28. public Player Player
  29. {
  30. get { return player; }
  31. }
  32. /// <summary>
  33. /// The character encapsulated by this combatant.
  34. /// </summary>
  35. public override FightingCharacter Character
  36. {
  37. get { return player as FightingCharacter; }
  38. }
  39. #region State Data
  40. /// <summary>
  41. /// The current state of this combatant.
  42. /// </summary>
  43. public override Character.CharacterState State
  44. {
  45. get { return player.State; }
  46. set
  47. {
  48. if (value == player.State)
  49. {
  50. return;
  51. }
  52. player.State = value;
  53. switch (player.State)
  54. {
  55. case RolePlayingGameData.Character.CharacterState.Idle:
  56. CombatSprite.PlayAnimation("Idle");
  57. break;
  58. case RolePlayingGameData.Character.CharacterState.Hit:
  59. CombatSprite.PlayAnimation("Hit");
  60. break;
  61. case RolePlayingGameData.Character.CharacterState.Dying:
  62. player.StatisticsModifiers.HealthPoints =
  63. -1 * player.CharacterStatistics.HealthPoints;
  64. CombatSprite.PlayAnimation("Die");
  65. break;
  66. }
  67. }
  68. }
  69. #endregion
  70. #region Graphics Data
  71. /// <summary>
  72. /// Accessor for the combat sprite for this combatant.
  73. /// </summary>
  74. public override AnimatingSprite CombatSprite
  75. {
  76. get { return player.CombatSprite; }
  77. }
  78. #endregion
  79. #region Current Statistics
  80. /// <summary>
  81. /// The current statistics of this combatant.
  82. /// </summary>
  83. public override StatisticsValue Statistics
  84. {
  85. get { return player.CurrentStatistics + CombatEffects.TotalStatistics; }
  86. }
  87. /// <summary>
  88. /// Heals the combatant by the given amount.
  89. /// </summary>
  90. public override void Heal(StatisticsValue healingStatistics, int duration)
  91. {
  92. if (duration > 0)
  93. {
  94. CombatEffects.AddStatistics(healingStatistics, duration);
  95. }
  96. else
  97. {
  98. player.StatisticsModifiers += healingStatistics;
  99. player.StatisticsModifiers.ApplyMaximum(new StatisticsValue());
  100. }
  101. base.Heal(healingStatistics, duration);
  102. }
  103. /// <summary>
  104. /// Damages the combatant by the given amount.
  105. /// </summary>
  106. public override void Damage(StatisticsValue damageStatistics, int duration)
  107. {
  108. if (duration > 0)
  109. {
  110. CombatEffects.AddStatistics(new StatisticsValue() - damageStatistics,
  111. duration);
  112. }
  113. else
  114. {
  115. player.StatisticsModifiers -= damageStatistics;
  116. player.StatisticsModifiers.ApplyMaximum(new StatisticsValue());
  117. }
  118. base.Damage(damageStatistics, duration);
  119. }
  120. /// <summary>
  121. /// Pay the cost for the given spell.
  122. /// </summary>
  123. /// <returns>True if the cost could be paid (and therefore was paid).</returns>
  124. public override bool PayCostForSpell(Spell spell)
  125. {
  126. // check the parameter.
  127. if (spell == null)
  128. {
  129. throw new ArgumentNullException("spell");
  130. }
  131. // check the requirements
  132. if (Statistics.MagicPoints < spell.MagicPointCost)
  133. {
  134. return false;
  135. }
  136. // reduce the player's magic points by the spell's cost
  137. player.StatisticsModifiers.MagicPoints -= spell.MagicPointCost;
  138. return true;
  139. }
  140. #endregion
  141. #region Initialization
  142. /// <summary>
  143. /// Construct a new CombatantPlayer object containing the given player.
  144. /// </summary>
  145. public CombatantPlayer(Player player) : base()
  146. {
  147. // check the parameter
  148. if (player == null)
  149. {
  150. throw new ArgumentNullException("player");
  151. }
  152. // assign the parameters
  153. this.player = player;
  154. // if the player starts dead, make sure the sprite is already "dead"
  155. if (IsDeadOrDying)
  156. {
  157. if (Statistics.HealthPoints > 0)
  158. {
  159. State = RolePlayingGameData.Character.CharacterState.Idle;
  160. }
  161. else
  162. {
  163. CombatSprite.PlayAnimation("Die");
  164. CombatSprite.AdvanceToEnd();
  165. }
  166. }
  167. else
  168. {
  169. State = RolePlayingGameData.Character.CharacterState.Idle;
  170. CombatSprite.PlayAnimation("Idle");
  171. }
  172. }
  173. #endregion
  174. #region Updating
  175. /// <summary>
  176. /// Update the player for this frame.
  177. /// </summary>
  178. public override void Update(GameTime gameTime)
  179. {
  180. base.Update(gameTime);
  181. }
  182. #endregion
  183. }
  184. }