Sprite.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //-----------------------------------------------------------------------------
  2. // Sprite.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. namespace TouchGesture
  10. {
  11. public class Sprite
  12. {
  13. // the possible colors for the sprite
  14. public static readonly Color[] Colors = new[]
  15. {
  16. Color.White,
  17. Color.Red,
  18. Color.Blue,
  19. Color.Green
  20. };
  21. // this is the amount of velocity that is maintained after
  22. // the sprite bounces off of the wall
  23. public const float BounceMagnitude = .5f;
  24. // this is the percentage of velocity lost each second as
  25. // the sprite moves around.
  26. public const float Friction = .9f;
  27. // the minimum and maximum scale values for the sprite
  28. public const float MinScale = .5f;
  29. public const float MaxScale = 2f;
  30. private Texture2D texture;
  31. private int colorIndex = 0;
  32. private float scale = 1f;
  33. public Vector2 Center;
  34. public Color Color = Colors[0];
  35. public Vector2 Velocity;
  36. public float Scale
  37. {
  38. get { return scale; }
  39. set { scale = MathHelper.Clamp(value, MinScale, MaxScale); }
  40. }
  41. public Rectangle HitBounds
  42. {
  43. get
  44. {
  45. // create a rectangle based on the texture
  46. Rectangle r = new Rectangle(
  47. (int)(Center.X - texture.Width / 2 * Scale),
  48. (int)(Center.Y - texture.Height / 2 * Scale),
  49. (int)(texture.Width * Scale),
  50. (int)(texture.Height * Scale));
  51. // inflate the texture a little to give us some additional pad room
  52. r.Inflate(10, 10);
  53. return r;
  54. }
  55. }
  56. public Sprite(Texture2D texture)
  57. {
  58. this.texture = texture;
  59. }
  60. public void ChangeColor()
  61. {
  62. // increment the color index and clamp to the array size
  63. colorIndex = (colorIndex + 1) % Colors.Length;
  64. // update to the new color
  65. Color = Colors[colorIndex];
  66. }
  67. /// <summary>
  68. /// Updates the sprite.
  69. /// </summary>
  70. /// <param name="gameTime">The current game timestamp.</param>
  71. /// <param name="bounds">The bounds in which the sprite should bounce around.</param>
  72. public void Update(GameTime gameTime, Rectangle bounds)
  73. {
  74. // move the sprite based on the velocity
  75. Center += Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
  76. // apply friction to the velocity to slow the sprite down
  77. Velocity *= 1f - (Friction * (float)gameTime.ElapsedGameTime.TotalSeconds);
  78. // calculate the scaled width and height for the method
  79. float halfWidth = (texture.Width * Scale) / 2f;
  80. float halfHeight = (texture.Height * Scale) / 2f;
  81. // check each side to make sure the sprite is in the bounds. if
  82. // the sprite is outside the bounds, we move the sprite and reverse
  83. // the velocity on that axis.
  84. if (Center.X < bounds.Left + halfWidth)
  85. {
  86. Center.X = bounds.Left + halfWidth;
  87. Velocity.X *= -BounceMagnitude;
  88. }
  89. if (Center.X > bounds.Right - halfWidth)
  90. {
  91. Center.X = bounds.Right - halfWidth;
  92. Velocity.X *= -BounceMagnitude;
  93. }
  94. if (Center.Y < bounds.Top + halfHeight)
  95. {
  96. Center.Y = bounds.Top + halfHeight;
  97. Velocity.Y *= -BounceMagnitude;
  98. }
  99. if (Center.Y > bounds.Bottom - halfHeight)
  100. {
  101. Center.Y = bounds.Bottom - halfHeight;
  102. Velocity.Y *= -BounceMagnitude;
  103. }
  104. }
  105. public void Draw(SpriteBatch spriteBatch)
  106. {
  107. spriteBatch.Draw(
  108. texture,
  109. Center,
  110. null,
  111. Color,
  112. 0,
  113. new Vector2(texture.Width / 2, texture.Height / 2),
  114. Scale,
  115. SpriteEffects.None,
  116. 0);
  117. }
  118. }
  119. }