PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Solution-RCD6/ASPItalia.ModelVirtualCasting.NinjaBraa/NinjBraa/NinjBraa/Elements/General/StraightLineMovementComponent.cs

#
C# | 196 lines | 94 code | 46 blank | 56 comment | 6 complexity | 5c6efb9c0d8552cd6704a60b2a480eb7 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // StraightLineMovementComponent.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 System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. #endregion
  16. namespace NinjaBra
  17. {
  18. /// <summary>
  19. /// A component that moves between two points in a straight line at a specified speed.
  20. /// </summary>
  21. class StraightLineMovementComponent : AnimatedComponent
  22. {
  23. #region Fields/Properties/Events
  24. /// <summary>
  25. /// Velocity in pixels per second.
  26. /// </summary>
  27. Vector2 velocity;
  28. // Distance remaining until the destination is reached.
  29. float distance;
  30. bool isEventFired = true;
  31. /// <summary>
  32. /// An event fired once the component has reached its destination. This event will only fire once
  33. /// per call of <see cref="Move"/>.
  34. /// </summary>
  35. public event EventHandler FinishedMoving;
  36. /// <summary>
  37. /// Component's center, which is also it's position.
  38. /// </summary>
  39. public override Vector2 Center
  40. {
  41. get
  42. {
  43. return Position;
  44. }
  45. }
  46. #endregion
  47. #region Initialization
  48. /// <summary>
  49. /// Creates a new instance of the straight line motion game component.
  50. /// </summary>
  51. /// <param name="game">Associated game object.</param>
  52. /// <param name="gameScreen">Game screen where the component will be presented.</param>
  53. /// <param name="texture">Texture asset which represents the component.</param>
  54. public StraightLineMovementComponent(Game game, GameplayScreen gameScreen, Texture2D texture)
  55. : base(game, gameScreen, texture)
  56. {
  57. }
  58. /// <summary>
  59. /// Creates a new instance of the straight line motion game component.
  60. /// </summary>
  61. /// <param name="game">Associated game object.</param>
  62. /// <param name="gameScreen">Game screen where the component will be presented.</param>
  63. /// <param name="animation">Animation object which represents the component.</param>
  64. public StraightLineMovementComponent(Game game, GameplayScreen gameScreen, Animation animation)
  65. : base(game, gameScreen, animation)
  66. {
  67. }
  68. #endregion
  69. #region Update
  70. /// <summary>
  71. /// Updates the component's position.
  72. /// </summary>
  73. /// <param name="gameTime">Game time information.</param>
  74. public override void Update(GameTime gameTime)
  75. {
  76. base.Update(gameTime);
  77. float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
  78. Vector2 movement = velocity * elapsedSeconds;
  79. Position += movement;
  80. distance -= movement.Length();
  81. // Check whether the event generated by reaching the destination needs to be fired.
  82. if (!isEventFired && distance <= 0)
  83. {
  84. // Move back in case we moved too much
  85. velocity.Normalize();
  86. Position += velocity * distance;
  87. // Fire the event and stop moving
  88. if (FinishedMoving != null)
  89. {
  90. FinishedMoving(this, EventArgs.Empty);
  91. }
  92. isEventFired = true;
  93. velocity = Vector2.Zero;
  94. }
  95. }
  96. #endregion
  97. #region Rendering
  98. /// <summary>
  99. /// Renders the component.
  100. /// </summary>
  101. /// <param name="gameTime">Game time information.</param>
  102. public override void Draw(GameTime gameTime)
  103. {
  104. spriteBatch.Begin();
  105. animation.Draw(spriteBatch, Position, 0, VisualCenter, 1, SpriteEffects.None, 0);
  106. spriteBatch.End();
  107. }
  108. #endregion
  109. #region Public Methods
  110. /// <summary>
  111. /// Causes the component to move from a specified location, in a straight, to another location.
  112. /// </summary>
  113. /// <param name="velocity">Movement velocity in pixels per second. Must be a positive number.</param>
  114. /// <param name="initialPosition">Component's starting point.</param>
  115. /// <param name="destination">The component's movement destination.</param>
  116. public void Move(float velocity, Vector2 initialPosition, Vector2 destination)
  117. {
  118. if (velocity <= 0)
  119. {
  120. throw new ArgumentOutOfRangeException("velocity", "Argument must be greater than 0.");
  121. }
  122. Vector2 toDestinationVector = (destination - initialPosition);
  123. Vector2 velocityUnitVector = toDestinationVector;
  124. velocityUnitVector.Normalize();
  125. this.velocity = velocityUnitVector * velocity;
  126. this.distance = toDestinationVector.Length();
  127. Position = initialPosition;
  128. isEventFired = false;
  129. }
  130. /// <summary>
  131. /// Causes the component to move from a specified location, in a straight, to another location.
  132. /// </summary>
  133. /// <param name="time">The time it should take the component to reach its destination. Must be a positive
  134. /// time span.</param>
  135. /// <param name="initialPosition">Component's starting point.</param>
  136. /// <param name="destination">The component's movement destination.</param>
  137. public void Move(TimeSpan time, Vector2 initialPosition, Vector2 destination)
  138. {
  139. if (time <= TimeSpan.Zero)
  140. {
  141. throw new ArgumentOutOfRangeException("time", "Argument must be a positive time span.");
  142. }
  143. Vector2 toDestinationVector = (destination - initialPosition);
  144. float distance = toDestinationVector.Length();
  145. float requiredVelocity = distance / (float)time.TotalSeconds;
  146. Move(requiredVelocity, initialPosition, destination);
  147. }
  148. #endregion
  149. }
  150. }