ItemCombatAction.cs 14 KB

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