SpellCombatAction.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. //-----------------------------------------------------------------------------
  2. // SpellCombatAction.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 RolePlayingGameData;
  10. using Microsoft.Xna.Framework.Graphics;
  11. using Microsoft.Xna.Framework.Audio;
  12. namespace RolePlaying
  13. {
  14. /// <summary>
  15. /// A spell-casting combat action, including related data and calculations.
  16. /// </summary>
  17. class SpellCombatAction : CombatAction
  18. {
  19. /// <summary>
  20. /// Returns true if the action is offensive, targeting the opponents.
  21. /// </summary>
  22. public override bool IsOffensive
  23. {
  24. get { return Spell.IsOffensive; }
  25. }
  26. /// <summary>
  27. /// Returns true if the character can use this action.
  28. /// </summary>
  29. public override bool IsCharacterValidUser
  30. {
  31. get
  32. {
  33. return (Spell.MagicPointCost <= Combatant.Statistics.MagicPoints);
  34. }
  35. }
  36. /// <summary>
  37. /// Returns true if this action requires a target.
  38. /// </summary>
  39. public override bool IsTargetNeeded
  40. {
  41. get { return true; }
  42. }
  43. /// <summary>
  44. /// The spell used in this action.
  45. /// </summary>
  46. private Spell spell;
  47. /// <summary>
  48. /// The spell used in this action.
  49. /// </summary>
  50. public Spell Spell
  51. {
  52. get { return spell; }
  53. }
  54. /// <summary>
  55. /// The current position of the spell sprite.
  56. /// </summary>
  57. private Vector2 spellSpritePosition;
  58. /// <summary>
  59. /// Apply the action's spell to the given target.
  60. /// </summary>
  61. /// <returns>True if there was any effect on the target.</returns>
  62. private bool ApplySpell(Combatant spellTarget)
  63. {
  64. StatisticsValue effectStatistics = CalculateSpellDamage(combatant, spell);
  65. if (spell.IsOffensive)
  66. {
  67. // calculate the defense
  68. Int32Range defenseRange = spellTarget.Character.MagicDefenseRange +
  69. spellTarget.Statistics.MagicalDefense;
  70. Int32 defense = defenseRange.GenerateValue(Session.Random);
  71. // subtract the defense
  72. effectStatistics -= new StatisticsValue(defense,
  73. defense, defense, defense, defense, defense);
  74. // make sure that this only contains damage
  75. effectStatistics.ApplyMinimum(new StatisticsValue());
  76. // damage the target
  77. spellTarget.Damage(effectStatistics, spell.TargetDuration);
  78. }
  79. else
  80. {
  81. // make sure that this only contains healing
  82. effectStatistics.ApplyMinimum(new StatisticsValue());
  83. // heal the target
  84. spellTarget.Heal(effectStatistics, spell.TargetDuration);
  85. }
  86. return !effectStatistics.IsZero;
  87. }
  88. /// <summary>
  89. /// The speed at which the projectile moves, in units per second.
  90. /// </summary>
  91. private const float projectileSpeed = 600f;
  92. /// <summary>
  93. /// The direction of the projectile.
  94. /// </summary>
  95. private Vector2 projectileDirection;
  96. /// <summary>
  97. /// The distance covered so far by the projectile.
  98. /// </summary>
  99. private float projectileDistanceCovered = 0f;
  100. /// <summary>
  101. /// The total distance between the original combatant position and the target.
  102. /// </summary>
  103. private float totalProjectileDistance;
  104. /// <summary>
  105. /// The sprite effect on the projectile, if any.
  106. /// </summary>
  107. private SpriteEffects projectileSpriteEffect = SpriteEffects.None;
  108. /// <summary>
  109. /// The sound effect cue for the traveling projectile.
  110. /// </summary>
  111. private Cue projectileCue;
  112. /// <summary>
  113. /// Starts a new combat stage. Called right after the stage changes.
  114. /// </summary>
  115. /// <remarks>The stage never changes into NotStarted.</remarks>
  116. protected override void StartStage()
  117. {
  118. switch (stage)
  119. {
  120. case CombatActionStage.Preparing: // called from Start()
  121. {
  122. // play the animations
  123. combatant.CombatSprite.PlayAnimation("SpellCast");
  124. spellSpritePosition = Combatant.Position;
  125. spell.SpellSprite.PlayAnimation("Creation");
  126. // remove the magic points
  127. Combatant.PayCostForSpell(spell);
  128. }
  129. break;
  130. case CombatActionStage.Advancing:
  131. {
  132. // play the animations
  133. spell.SpellSprite.PlayAnimation("Traveling");
  134. // calculate the projectile destination
  135. projectileDirection = Target.Position -
  136. Combatant.OriginalPosition;
  137. totalProjectileDistance = projectileDirection.Length();
  138. projectileDirection.Normalize();
  139. projectileDistanceCovered = 0f;
  140. // determine if the projectile is flipped
  141. if (Target.Position.X > Combatant.Position.X)
  142. {
  143. projectileSpriteEffect = SpriteEffects.FlipHorizontally;
  144. }
  145. else
  146. {
  147. projectileSpriteEffect = SpriteEffects.None;
  148. }
  149. // get the projectile's cue and play it
  150. projectileCue = AudioManager.GetCue(spell.TravelingCueName);
  151. if (projectileCue != null)
  152. {
  153. projectileCue.Play();
  154. }
  155. }
  156. break;
  157. case CombatActionStage.Executing:
  158. {
  159. // play the animation
  160. spell.SpellSprite.PlayAnimation("Impact");
  161. // stop the projectile sound effect
  162. if (projectileCue != null)
  163. {
  164. projectileCue.Stop(AudioStopOptions.Immediate);
  165. }
  166. // apply the spell effect to the primary target
  167. bool damagedAnyone = ApplySpell(Target);
  168. // apply the spell to the secondary targets
  169. foreach (Combatant targetCombatant in
  170. CombatEngine.SecondaryTargetedCombatants)
  171. {
  172. // skip dead or dying targets
  173. if (targetCombatant.IsDeadOrDying)
  174. {
  175. continue;
  176. }
  177. // apply the spell
  178. damagedAnyone |= ApplySpell(targetCombatant);
  179. }
  180. // play the impact sound effect
  181. if (damagedAnyone)
  182. {
  183. AudioManager.PlayCue(spell.ImpactCueName);
  184. if (spell.Overlay != null)
  185. {
  186. spell.Overlay.PlayAnimation(0);
  187. spell.Overlay.ResetAnimation();
  188. }
  189. }
  190. }
  191. break;
  192. case CombatActionStage.Returning:
  193. // play the animation
  194. combatant.CombatSprite.PlayAnimation("Idle");
  195. break;
  196. case CombatActionStage.Finishing:
  197. // play the animation
  198. combatant.CombatSprite.PlayAnimation("Idle");
  199. break;
  200. case CombatActionStage.Complete:
  201. // play the animation
  202. combatant.CombatSprite.PlayAnimation("Idle");
  203. // make sure that the overlay has stopped
  204. spell.Overlay.StopAnimation();
  205. break;
  206. }
  207. }
  208. /// <summary>
  209. /// Update the action for the current stage.
  210. /// </summary>
  211. /// <remarks>
  212. /// This function is guaranteed to be called at least once per stage.
  213. /// </remarks>
  214. protected override void UpdateCurrentStage(GameTime gameTime)
  215. {
  216. switch (stage)
  217. {
  218. case CombatActionStage.Advancing:
  219. if (projectileDistanceCovered < totalProjectileDistance)
  220. {
  221. projectileDistanceCovered += projectileSpeed *
  222. (float)gameTime.ElapsedGameTime.TotalSeconds;
  223. }
  224. spellSpritePosition = combatant.OriginalPosition +
  225. projectileDirection * projectileDistanceCovered;
  226. break;
  227. }
  228. }
  229. /// <summary>
  230. /// Returns true if the combat action is ready to proceed to the next stage.
  231. /// </summary>
  232. protected override bool IsReadyForNextStage
  233. {
  234. get
  235. {
  236. switch (stage)
  237. {
  238. case CombatActionStage.Preparing: // ready to advance?
  239. return (combatant.CombatSprite.IsPlaybackComplete &&
  240. spell.SpellSprite.IsPlaybackComplete);
  241. case CombatActionStage.Advancing: // ready to execute?
  242. if (spell.SpellSprite.IsPlaybackComplete ||
  243. (projectileDistanceCovered >= totalProjectileDistance))
  244. {
  245. projectileDistanceCovered = totalProjectileDistance;
  246. return true;
  247. }
  248. return false;
  249. case CombatActionStage.Executing: // ready to return?
  250. return spell.SpellSprite.IsPlaybackComplete;
  251. }
  252. // fall through to the base behavior
  253. return base.IsReadyForNextStage;
  254. }
  255. }
  256. /// <summary>
  257. /// The heuristic used to compare actions of this type to similar ones.
  258. /// </summary>
  259. public override int Heuristic
  260. {
  261. get
  262. {
  263. return (Combatant.Statistics.MagicalOffense +
  264. Spell.TargetEffectRange.HealthPointsRange.Average);
  265. }
  266. }
  267. /// <summary>
  268. /// Constructs a new SpellCombatAction object.
  269. /// </summary>
  270. /// <param name="character">The combatant performing the action.</param>
  271. public SpellCombatAction(Combatant combatant, Spell spell)
  272. : base(combatant)
  273. {
  274. // check the parameter
  275. if (spell == null)
  276. {
  277. throw new ArgumentNullException("spell");
  278. }
  279. // assign the parameter
  280. this.spell = spell;
  281. this.adjacentTargets = this.spell.AdjacentTargets;
  282. }
  283. /// <summary>
  284. /// Start executing the combat action.
  285. /// </summary>
  286. public override void Start()
  287. {
  288. // play the creation sound effect
  289. AudioManager.PlayCue(spell.CreatingCueName);
  290. base.Start();
  291. }
  292. /// <summary>
  293. /// Updates the action over time.
  294. /// </summary>
  295. public override void Update(GameTime gameTime)
  296. {
  297. // update the animations
  298. float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
  299. spell.SpellSprite.UpdateAnimation(elapsedSeconds);
  300. if (spell.Overlay != null)
  301. {
  302. spell.Overlay.UpdateAnimation(elapsedSeconds);
  303. if (!spell.Overlay.IsPlaybackComplete &&
  304. Target.CombatSprite.IsPlaybackComplete)
  305. {
  306. spell.Overlay.StopAnimation();
  307. }
  308. }
  309. base.Update(gameTime);
  310. }
  311. /// <summary>
  312. /// Draw any elements of the action that are independent of the character.
  313. /// </summary>
  314. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  315. {
  316. // draw the spell projectile
  317. if (!spell.SpellSprite.IsPlaybackComplete)
  318. {
  319. if (stage == CombatActionStage.Advancing)
  320. {
  321. spell.SpellSprite.Draw(spriteBatch, spellSpritePosition, 0f,
  322. projectileSpriteEffect);
  323. }
  324. else
  325. {
  326. spell.SpellSprite.Draw(spriteBatch, spellSpritePosition, 0f);
  327. }
  328. }
  329. // draw the spell overlay
  330. if (!spell.Overlay.IsPlaybackComplete)
  331. {
  332. spell.Overlay.Draw(spriteBatch, Target.Position, 0f);
  333. }
  334. base.Draw(gameTime, spriteBatch);
  335. }
  336. /// <summary>
  337. /// Calculate the spell damage done by the given combatant and spell.
  338. /// </summary>
  339. public static StatisticsValue CalculateSpellDamage(Combatant combatant,
  340. Spell spell)
  341. {
  342. // check the parameters
  343. if (combatant == null)
  344. {
  345. throw new ArgumentNullException("combatant");
  346. }
  347. if (spell == null)
  348. {
  349. throw new ArgumentNullException("spell");
  350. }
  351. // get the magical offense from the character's class, gear, and bonuses
  352. // -- note that this includes stat buffs
  353. int magicalOffense = combatant.Statistics.MagicalOffense;
  354. // add the magical offense to the spell
  355. StatisticsValue damage =
  356. spell.TargetEffectRange.GenerateValue(Session.Random);
  357. damage.HealthPoints += (damage.HealthPoints != 0) ? magicalOffense : 0;
  358. damage.MagicPoints += (damage.MagicPoints != 0) ? magicalOffense : 0;
  359. damage.PhysicalOffense += (damage.PhysicalOffense != 0) ? magicalOffense : 0;
  360. damage.PhysicalDefense += (damage.PhysicalDefense != 0) ? magicalOffense : 0;
  361. damage.MagicalOffense += (damage.MagicalOffense != 0) ? magicalOffense : 0;
  362. damage.MagicalDefense += (damage.MagicalDefense != 0) ? magicalOffense : 0;
  363. // add in the spell damage
  364. return damage;
  365. }
  366. }
  367. }