Gem.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Gem.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Audio;
  13. namespace Platformer
  14. {
  15. /// <summary>
  16. /// A valuable item the player can collect.
  17. /// </summary>
  18. class Gem
  19. {
  20. private Texture2D texture;
  21. private Vector2 origin;
  22. private SoundEffect collectedSound;
  23. public const int PointValue = 30;
  24. public readonly Color Color = Color.Yellow;
  25. // The gem is animated from a base position along the Y axis.
  26. private Vector2 basePosition;
  27. private float bounce;
  28. public Level Level
  29. {
  30. get { return level; }
  31. }
  32. Level level;
  33. /// <summary>
  34. /// Gets the current position of this gem in world space.
  35. /// </summary>
  36. public Vector2 Position
  37. {
  38. get
  39. {
  40. return basePosition + new Vector2(0.0f, bounce);
  41. }
  42. }
  43. /// <summary>
  44. /// Gets a circle which bounds this gem in world space.
  45. /// </summary>
  46. public Circle BoundingCircle
  47. {
  48. get
  49. {
  50. return new Circle(Position, Tile.Width / 3.0f);
  51. }
  52. }
  53. /// <summary>
  54. /// Constructs a new gem.
  55. /// </summary>
  56. public Gem(Level level, Vector2 position)
  57. {
  58. this.level = level;
  59. this.basePosition = position;
  60. LoadContent();
  61. }
  62. /// <summary>
  63. /// Loads the gem texture and collected sound.
  64. /// </summary>
  65. public void LoadContent()
  66. {
  67. texture = Level.Content.Load<Texture2D>("Sprites/Gem");
  68. origin = new Vector2(texture.Width / 2.0f, texture.Height / 2.0f);
  69. collectedSound = Level.Content.Load<SoundEffect>("Sounds/GemCollected");
  70. }
  71. /// <summary>
  72. /// Bounces up and down in the air to entice players to collect them.
  73. /// </summary>
  74. public void Update(GameTime gameTime)
  75. {
  76. // Bounce control constants
  77. const float BounceHeight = 0.18f;
  78. const float BounceRate = 3.0f;
  79. const float BounceSync = -0.75f;
  80. // Bounce along a sine curve over time.
  81. // Include the X coordinate so that neighboring gems bounce in a nice wave pattern.
  82. double t = gameTime.TotalGameTime.TotalSeconds * BounceRate + Position.X * BounceSync;
  83. bounce = (float)Math.Sin(t) * BounceHeight * texture.Height;
  84. }
  85. /// <summary>
  86. /// Called when this gem has been collected by a player and removed from the level.
  87. /// </summary>
  88. /// <param name="collectedBy">
  89. /// The player who collected this gem. Although currently not used, this parameter would be
  90. /// useful for creating special powerup gems. For example, a gem could make the player invincible.
  91. /// </param>
  92. public void OnCollected(Player collectedBy)
  93. {
  94. collectedSound.Play();
  95. }
  96. /// <summary>
  97. /// Draws a gem in the appropriate color.
  98. /// </summary>
  99. public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  100. {
  101. spriteBatch.Draw(texture, Position, null, Color, 0.0f, origin, 1.0f, SpriteEffects.None, 0.0f);
  102. }
  103. }
  104. }