Tank.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //-----------------------------------------------------------------------------
  2. // Tank.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 Microsoft.Xna.Framework.Audio;
  10. //using Microsoft.Xna.Framework.GamerServices;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Input;
  13. //using Microsoft.Xna.Framework.Storage;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.Media;
  16. namespace Waypoint
  17. {
  18. public enum BehaviorType
  19. {
  20. Linear,
  21. Steering,
  22. }
  23. /// <summary>
  24. /// A simple object that moves towards it's set destination
  25. /// </summary>
  26. public class Tank : DrawableGameComponent
  27. {
  28. /// <summary>
  29. /// The "close enough" limit, if the tank is inside this many pixel
  30. /// to it's destination it's considered at it's destination
  31. /// </summary>
  32. const float atDestinationLimit = 1f;
  33. /// <summary>
  34. /// This is how much the Tank can turn in one second in radians, since Pi
  35. /// radians makes half a circle the tank can all the way around in one second
  36. /// </summary>
  37. public static float MaxAngularVelocity
  38. {
  39. get { return maxAngularVelocity; }
  40. }
  41. const float maxAngularVelocity = MathHelper.Pi;
  42. /// <summary>
  43. /// This is the Tanks� best possible movement speed
  44. /// </summary>
  45. public static float MaxMoveSpeed
  46. {
  47. get { return maxMoveSpeed; }
  48. }
  49. const float maxMoveSpeed = 100f;
  50. /// <summary>
  51. /// This is most the tank can speed up or slow down in one second
  52. /// </summary>
  53. public static float MaxMoveSpeedDelta
  54. {
  55. get { return maxMoveSpeedDelta; }
  56. }
  57. const float maxMoveSpeedDelta = maxMoveSpeed / 2;
  58. // Graphics data
  59. SpriteBatch spriteBatch;
  60. Texture2D tankTexture;
  61. Vector2 tankTextureCenter;
  62. /// <summary>
  63. /// The tanks� current movement behavior, it�s responsible for updating the
  64. /// tanks� movement speed and direction
  65. /// </summary>
  66. Behavior currentBehavior;
  67. /// <summary>
  68. /// Gets the current movement behavior
  69. /// </summary>
  70. public BehaviorType BehaviorType
  71. {
  72. get { return behaviorType; }
  73. set
  74. {
  75. if (behaviorType != value || currentBehavior == null)
  76. {
  77. behaviorType = value;
  78. switch (behaviorType)
  79. {
  80. case BehaviorType.Linear:
  81. currentBehavior = new LinearBehavior(this);
  82. break;
  83. case BehaviorType.Steering:
  84. currentBehavior = new SteeringBehavior(this);
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. BehaviorType behaviorType;
  93. /// <summary>
  94. /// Length 1 vector that represents the tanks� movement and facing direction
  95. /// </summary>
  96. public Vector2 Direction
  97. {
  98. get { return direction; }
  99. set { direction = value; }
  100. }
  101. protected Vector2 direction;
  102. /// <summary>
  103. /// The tank's current movement speed
  104. /// </summary>
  105. public float MoveSpeed
  106. {
  107. get { return moveSpeed; }
  108. set { moveSpeed = value; }
  109. }
  110. protected float moveSpeed;
  111. /// <summary>
  112. /// The tank's location on the map
  113. /// </summary>
  114. public Vector2 Location
  115. {
  116. get { return location; }
  117. }
  118. private Vector2 location;
  119. /// <summary>
  120. /// The list of points the tanks will move to in order from first to last
  121. /// </summary>
  122. public WaypointList Waypoints
  123. {
  124. get { return waypoints; }
  125. }
  126. private WaypointList waypoints;
  127. /// <summary>
  128. /// Linear distance to the Tank's current destination
  129. /// </summary>
  130. public float DistanceToDestination
  131. {
  132. get { return Vector2.Distance(location, waypoints.Peek()); }
  133. }
  134. /// <summary>
  135. /// True when the tank is "close enough" to it's destination
  136. /// </summary>
  137. public bool AtDestination
  138. {
  139. get { return DistanceToDestination < atDestinationLimit; }
  140. }
  141. /// <summary>
  142. /// Tank constructor
  143. /// </summary>
  144. public Tank(Game game) : base(game)
  145. {
  146. location = Vector2.Zero;
  147. waypoints = new WaypointList();
  148. BehaviorType = BehaviorType.Linear;
  149. }
  150. /// <summary>
  151. /// Load the tank's texture resources
  152. /// </summary>
  153. /// <param name="content"></param>
  154. protected override void LoadContent()
  155. {
  156. spriteBatch = new SpriteBatch(GraphicsDevice);
  157. tankTexture = Game.Content.Load<Texture2D>("tank");
  158. tankTextureCenter =
  159. new Vector2(tankTexture.Width / 2, tankTexture.Height / 2);
  160. waypoints.LoadContent(Game.Content);
  161. }
  162. /// <summary>
  163. /// Reset the Tank's location on the map
  164. /// </summary>
  165. /// <param name="newLocation">new location on the map</param>
  166. public void Reset(Vector2 newLocation)
  167. {
  168. location = newLocation;
  169. waypoints.Clear();
  170. }
  171. /// <summary>
  172. /// Update the Tank's position if it's not "close enough" to
  173. /// it's destination
  174. /// </summary>
  175. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  176. public override void Update(GameTime gameTime)
  177. {
  178. float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
  179. // If we have any waypoints, the first one on the list is where
  180. // we want to go
  181. if (waypoints.Count > 0)
  182. {
  183. if (AtDestination)
  184. {
  185. // If we�re at the destination and there is at least one
  186. // waypoint in the list, get rid of the first one since we�re
  187. // there now
  188. waypoints.Dequeue();
  189. }
  190. else
  191. {
  192. // If we�re not at the destination, call Update on our
  193. // behavior and then move
  194. if (currentBehavior != null)
  195. {
  196. currentBehavior.Update(gameTime);
  197. }
  198. location = location + (Direction *
  199. MoveSpeed * elapsedTime);
  200. }
  201. }
  202. }
  203. /// <summary>
  204. /// Draw the Tank
  205. /// </summary>
  206. /// <param name="gameTime"></param>
  207. /// <param name="spriteBatch"></param>
  208. public override void Draw(GameTime gameTime)
  209. {
  210. waypoints.Draw(spriteBatch);
  211. float facingDirection = (float)Math.Atan2(
  212. Direction.Y, Direction.X);
  213. spriteBatch.Begin();
  214. spriteBatch.Draw(tankTexture, location, null, Color.White, facingDirection,
  215. tankTextureCenter, 1f, SpriteEffects.None, 0f);
  216. spriteBatch.End();
  217. }
  218. /// <summary>
  219. /// Change the tank movement Behavior
  220. /// </summary>
  221. public void CycleBehaviorType()
  222. {
  223. switch (behaviorType)
  224. {
  225. case BehaviorType.Linear:
  226. BehaviorType = BehaviorType.Steering;
  227. break;
  228. case BehaviorType.Steering:
  229. default:
  230. BehaviorType = BehaviorType.Linear;
  231. break;
  232. }
  233. }
  234. }
  235. }