CombatantPlayer.cs 5.8 KB

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