WaypointList.cs 3.5 KB

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