2
0

Sprite.cs 4.6 KB

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