CombatantMonster.cs 7.4 KB

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