Projectile.cs 5.5 KB

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