Tank.cs 8.6 KB

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