ItemCombatAction.cs 15 KB

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