LinearBehavior.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //-----------------------------------------------------------------------------
  2. // LinearBehavior.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. #if IPHONE
  9. using Microsoft.Xna.Framework;
  10. #else
  11. using Microsoft.Xna.Framework;
  12. #endif
  13. namespace Waypoint
  14. {
  15. /// <summary>
  16. /// This Behavior makes the tank turn instantly and follow a direct
  17. /// line to the current waypoint
  18. /// </summary>
  19. class LinearBehavior : Behavior
  20. {
  21. public LinearBehavior(Tank tank)
  22. : base(tank)
  23. {
  24. }
  25. /// <summary>
  26. /// This Update finds the direction vector that goes from a straight
  27. /// line directly to the current waypoint
  28. /// </summary>
  29. /// <param name="gameTime"></param>
  30. public override void Update(GameTime gameTime)
  31. {
  32. // This gives us a vector that points directly from the tank's
  33. // current location to the waypoint.
  34. Vector2 direction = -(tank.Location - tank.Waypoints.Peek());
  35. // This scales the vector to 1, we'll use move Speed and elapsed Time
  36. // in the Tank's Update function to find the how far the tank moves
  37. direction.Normalize();
  38. tank.Direction = direction;
  39. }
  40. }
  41. }