LinearBehavior.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // LinearBehavior.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. #if IPHONE
  12. using Microsoft.Xna.Framework;
  13. #else
  14. using Microsoft.Xna.Framework;
  15. #endif
  16. #endregion
  17. namespace Waypoint
  18. {
  19. /// <summary>
  20. /// This Behavior makes the tank turn instantly and follow a direct
  21. /// line to the current waypoint
  22. /// </summary>
  23. class LinearBehavior : Behavior
  24. {
  25. #region Initialization
  26. public LinearBehavior(Tank tank)
  27. : base(tank)
  28. {
  29. }
  30. #endregion
  31. #region Update
  32. /// <summary>
  33. /// This Update finds the direction vector that goes from a straight
  34. /// line directly to the current waypoint
  35. /// </summary>
  36. /// <param name="gameTime"></param>
  37. public override void Update(GameTime gameTime)
  38. {
  39. // This gives us a vector that points directly from the tank's
  40. // current location to the waypoint.
  41. Vector2 direction = -(tank.Location - tank.Waypoints.Peek());
  42. // This scales the vector to 1, we'll use move Speed and elapsed Time
  43. // in the Tank's Update function to find the how far the tank moves
  44. direction.Normalize();
  45. tank.Direction = direction;
  46. }
  47. #endregion
  48. }
  49. }