CombatantMonster.cs 6.8 KB

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