WaypointList.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // WaypointList.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 System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Content;
  15. #endregion
  16. namespace Waypoint
  17. {
  18. /// <summary>
  19. /// WaypointList is a drawable List of screen locations
  20. /// </summary>
  21. public class WaypointList : Queue<Vector2>
  22. {
  23. #region Fields
  24. // Draw data
  25. Texture2D waypointTexture;
  26. Vector2 waypointCenter;
  27. #endregion
  28. #region Initialization
  29. /// <summary>
  30. /// Load the WaypointList's texture resources
  31. /// </summary>
  32. /// <param name="content"></param>
  33. public void LoadContent(ContentManager content)
  34. {
  35. waypointTexture = content.Load<Texture2D>("dot");
  36. waypointCenter =
  37. new Vector2(waypointTexture.Width / 2, waypointTexture.Height / 2);
  38. }
  39. #endregion
  40. #region Draw
  41. /// <summary>
  42. /// Draw the waypoint list, fading from red for the next waypoint to blue
  43. /// for the last
  44. /// </summary>
  45. /// <param name="spriteBatch"></param>
  46. public void Draw(SpriteBatch spriteBatch)
  47. {
  48. if (Count == 1)
  49. {
  50. // If we only have a single waypoint in the list we don’t have to
  51. // worry about creating a nice color gradient, so we just draw it
  52. // as completely red here
  53. spriteBatch.Begin();
  54. spriteBatch.Draw(waypointTexture, Peek(), null, Color.Red,
  55. 0f, waypointCenter, 1f, SpriteEffects.None, 0f);
  56. spriteBatch.End();
  57. }
  58. else if (Count > 0)
  59. {
  60. // In this case we have more than one waypoint on the list, so we
  61. // want to fade smoothly from red for the first and blue for the
  62. // last so we can tell visually what order they’re in
  63. float numberPoints = this.Count - 1;
  64. float i = 0;
  65. spriteBatch.Begin();
  66. foreach (Vector2 location in this)
  67. {
  68. // This creates a gradient between 0 for the first waypoint on
  69. // the list and 1 for the last, 0 creates a color that's
  70. // completely red and 1 creates a color that's completely blue
  71. spriteBatch.Draw(waypointTexture, location, null,
  72. new Color(Vector4.Lerp(Color.Red.ToVector4(),
  73. Color.Blue.ToVector4(), i / numberPoints)),
  74. 0f, waypointCenter, 1f, SpriteEffects.None, 0f);
  75. i++;
  76. }
  77. spriteBatch.End();
  78. }
  79. }
  80. #endregion
  81. }
  82. }