Projectile.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Projectile.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region File Information
  10. //-----------------------------------------------------------------------------
  11. // Projectile.cs
  12. //
  13. // Microsoft XNA Community Game Platform
  14. // Copyright (C) Microsoft Corporation. All rights reserved.
  15. //-----------------------------------------------------------------------------
  16. #endregion
  17. #region Using Statements
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Text;
  22. using Microsoft.Xna.Framework;
  23. using Microsoft.Xna.Framework.Graphics;
  24. #endregion
  25. namespace CatapultGame
  26. {
  27. class Projectile : DrawableGameComponent
  28. {
  29. #region Fields/Properties
  30. SpriteBatch spriteBatch;
  31. Game curGame;
  32. Random random;
  33. // Textures for projectile
  34. string textureName;
  35. // Position and speed of projectile
  36. Vector2 projectileVelocity = Vector2.Zero;
  37. float projectileInitialVelocityY;
  38. Vector2 projectileRotationPosition = Vector2.Zero;
  39. float projectileRotation;
  40. float flightTime;
  41. bool isAI;
  42. float hitOffset;
  43. float gravity;
  44. Vector2 projectileStartPosition;
  45. public Vector2 ProjectileStartPosition
  46. {
  47. get
  48. {
  49. return projectileStartPosition;
  50. }
  51. set
  52. {
  53. projectileStartPosition = value;
  54. }
  55. }
  56. Vector2 projectilePosition = Vector2.Zero;
  57. public Vector2 ProjectilePosition
  58. {
  59. get
  60. {
  61. return projectilePosition;
  62. }
  63. set
  64. {
  65. projectilePosition = value;
  66. }
  67. }
  68. /// <summary>
  69. /// Gets the position where the projectile hit the ground.
  70. /// Only valid after a hit occurs.
  71. /// </summary>
  72. public Vector2 ProjectileHitPosition { get; private set; }
  73. Texture2D projectileTexture;
  74. public Texture2D ProjectileTexture
  75. {
  76. get
  77. {
  78. return projectileTexture;
  79. }
  80. set
  81. {
  82. projectileTexture = value;
  83. }
  84. }
  85. #endregion
  86. #region Initialization
  87. public Projectile(Game game)
  88. : base(game)
  89. {
  90. curGame = game;
  91. random = new Random();
  92. }
  93. public Projectile(Game game, SpriteBatch screenSpriteBatch,
  94. string TextureName, Vector2 startPosition, float groundHitOffset,
  95. bool isAi, float Gravity)
  96. : this(game)
  97. {
  98. spriteBatch = screenSpriteBatch;
  99. projectileStartPosition = startPosition;
  100. textureName = TextureName;
  101. isAI = isAi;
  102. hitOffset = groundHitOffset;
  103. gravity = Gravity;
  104. }
  105. public override void Initialize()
  106. {
  107. // Load a projectile texture
  108. projectileTexture = curGame.Content.Load<Texture2D>(textureName);
  109. }
  110. #endregion
  111. #region Render
  112. public override void Draw(GameTime gameTime)
  113. {
  114. spriteBatch.Draw(projectileTexture, projectilePosition, null,
  115. Color.White, projectileRotation,
  116. new Vector2(projectileTexture.Width / 2,
  117. projectileTexture.Height / 2),
  118. 1.0f, SpriteEffects.None, 0);
  119. base.Draw(gameTime);
  120. }
  121. #endregion
  122. #region Public functionality
  123. /// <summary>
  124. /// Helper function - calculates the projectile position and velocity based on time.
  125. /// </summary>
  126. /// <param name="gameTime">The time since last calculation</param>
  127. public void UpdateProjectileFlightData(GameTime gameTime, float wind, float gravity, out bool groundHit)
  128. {
  129. flightTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
  130. // Calculate new projectile position using standard
  131. // formulas, taking the wind as a force.
  132. int direction = isAI ? -1 : 1;
  133. float previousXPosition = projectilePosition.X;
  134. float previousYPosition = projectilePosition.Y;
  135. projectilePosition.X = projectileStartPosition.X +
  136. (direction * projectileVelocity.X * flightTime) +
  137. 0.5f * (8 * wind * (float)Math.Pow(flightTime, 2));
  138. projectilePosition.Y = projectileStartPosition.Y -
  139. (projectileVelocity.Y * flightTime) +
  140. 0.5f * (gravity * (float)Math.Pow(flightTime, 2));
  141. // Calculate the projectile rotation
  142. projectileRotation += MathHelper.ToRadians(projectileVelocity.X * 0.5f);
  143. // Check if projectile hit the ground or even passed it
  144. // (could happen during normal calculation)
  145. if (projectilePosition.Y >= 332 + hitOffset)
  146. {
  147. projectilePosition.X = previousXPosition;
  148. projectilePosition.Y = previousYPosition;
  149. ProjectileHitPosition = new Vector2(previousXPosition, 332);
  150. groundHit = true;
  151. }
  152. else
  153. {
  154. groundHit = false;
  155. }
  156. }
  157. public void Fire(float velocityX, float velocityY)
  158. {
  159. // Set initial projectile velocity
  160. projectileVelocity.X = velocityX;
  161. projectileVelocity.Y = velocityY;
  162. projectileInitialVelocityY = projectileVelocity.Y;
  163. // Reset calculation variables
  164. flightTime = 0;
  165. }
  166. #endregion
  167. }
  168. }